Results 1 to 17 of 17
Thread: int Array Tests
- 04-14-2009, 05:10 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
int Array Tests
So im writing a method that will take an int array and move the lowest int to the beginning and the largest to the end; i have a test written below....
However, I'm having a problem on how to write the tests for this method. I'm new to arrays so not quite sure how to write tests for different situations.
TEST: I want to take n1 and modify it to become n2.
@Test
public void testSortOfSort(){
int[] n1 = {4,3,2,0,1,2};
int[] n2 = {0,3,2,1,2,4};
af.sortOfSort(n1);
assertEquals(n2,af.sortOfSort(n1));
METHOD: please take note that my method is not returning anything just modifying the given array
public void sortOfSort(int[] array){
int largest = 0;
int smallest = 0;
for(int n=0; n<array.length;n++){
if(array[n] >= largest)
largest = array[n];
if(array[n] <= smallest)
smallest = array[n];
}
int i = 1;
for(int n = 1; n<array.length-1; n++){
if(array[n] != smallest && array[n] != largest)
array[i] = array[n];
}
array[0] = smallest;
array[array.length-1] = largest;
System.out.println(array);
}
**My method may not be perfect yet, but thats okay I just need help with the tests**
- 04-14-2009, 05:31 AM #2
assertEquals(n2[0],n1[0]);
assertEquals(n2[n2.lenght - 1],n1[n1.lenght - 1],);
try using that assertions
assertEquals(n2,af.sortOfSort(n1)); < -- this wont really work hmm i guess becuase its try to compare the reference instead of the value the out of n2 wil be
n1{0,3,2,0,4} w/ is not equal to n2{0,3,2,1,2,4};It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-14-2009, 06:27 AM #3
In java Arrays are actually objects.
You could just use
to check if their equal.Java Code:n1.equals(n2);
Mr. Beans
Note: Next time please use the code tags.
- 04-14-2009, 07:55 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
I've tried both and have gotten errors. Sorry i dont know what code tags are.
Does anybody know how to write a test for method "public void sortOfSort(int[] array)" that will take in "array" and change it around according to a formula to n2??
**Changing n1 into n2**
int[] array = {4,3,2,0,1,2};
int[] n2 = {0,3,2,1,2,4};
- 04-14-2009, 07:58 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-15-2009, 04:38 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
repeat- I need help writing the tests for a method involving arrays.....
such as
@Test
public void testingArrayMethod(){
.....
- 04-15-2009, 04:42 AM #7
are you using junit test?
It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-15-2009, 04:46 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
yes, and the format ive been using for my other tests isnt working for this one because the method doesnt return anything
- 04-15-2009, 04:53 AM #9
so you want to test a method that doesn't return a value..... hmmmm that's interesting can post your whole code?
It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-15-2009, 05:03 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
I know... you will have to ask my professor why but its necessary for the project.
I dont know if the codes perfect yet cause i can't test it but here it is so far
for example i want {2,1,3,4,2,0} to become {0,2,1,3,2,4}Java Code:public void sortOfSort(int[] array){ int largest = 0; int smallest = 0; for(int n=0; n<array.length;n++){ if(array[n] >= largest) largest = array[n]; if(array[n] <= smallest) smallest = array[n]; } int i = 1; for(int n = 0; n<array.length-2; n++){ if(array[n] != smallest && array[n] != largest) array[i] = array[n]; i++; } array[0] = smallest; array[array.length-1] = largest; System.out.println(array); } }
- 04-15-2009, 05:15 AM #11
hei try running this
Java Code:public class Runner { public static void main(String[] args) { int[] numbers = {2,1,3,4,2,0}; sortOfSort(numbers); for (int index = 0; index < numbers.length; index++) { System.out.print(numbers[index]); } } public static void sortOfSort(int[] numbers) { int largest = 0; int smallest = 0; for(int n=0; n<numbers.length;n++){ if(numbers[n] >= largest) largest = numbers[n]; if(numbers[n] <= smallest) smallest = numbers[n]; } int i = 1; for(int n = 0; n<numbers.length-2; n++){ if(numbers[n] != smallest && numbers[n] != largest) numbers[i] = numbers[n]; i++; } numbers[0] = smallest; numbers[numbers.length-1] = largest; } }It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-15-2009, 05:27 AM #12
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
thanks for the method... do you have any idea how to write the test for the method?
- 04-15-2009, 05:32 AM #13
no if you havent notice even if your method returns void the array numbers value still changed to numbers[0] = 0 and numbers[5] =4 witch what your method is really trying to do so now you can do your assertions all you nid to test is if numbers 1st index is the lowest and the last index is equal to the hight right?
It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-15-2009, 08:08 AM #14
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
Also hate to put a burst in it but your method will also fail if the array you pass it doesn't have a number of 0 in the array.
IE: You pass it an array of {3,2,7,4,7,8,4}
Technically the way your code works from what I can see would return an array of {0,3,7,4,7,4,8} ?
I will play with it, but your setting the values of smallest/highest to 0 - which if there isn't a zero will cause it to break.
Question? Why don't you get your methods running and do a standard sysout print of the values to make sure it is correct first, then make your junit test?
- 04-15-2009, 08:12 AM #15
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
I have done that and my method now works perfectly.
Maybe I'm not communicating clearly, but my problem is that I dont know how to write junit tests for this situation, and was looking for help on how to write the junit tests not writing the method.
- 04-15-2009, 08:29 AM #16
uhmm ok so Junit 3 or Junit 4
It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-16-2009, 01:49 AM #17
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
int[] n1 = {4,3,2,0,1,2};
int[] n2 = {0,3,2,1,2,4};
assertArrayEquals(int[] n1, int[] n2)
//Asserts that two int arrays are equal. If they are not, an AssertionError is thrown.
You need Junit 4 however, otherwise if you have Junit 3 you will likely need to do a comparable iterator e.g.
for (int i = 0; i < n1.length; i++) {
assertEquals(n1[i], n2[i]);
}
So it loops through each one and as long as each one is true it will pass the test, if any fails then well it fails :-) Don't forget to throw an Assertion Error on the method!
Similar Threads
-
Remote Junit Tests
By lord.ec in forum EclipseReplies: 0Last Post: 12-09-2008, 08:15 AM -
Using ant to run JUnit tests
By racerxadam in forum Advanced JavaReplies: 0Last Post: 10-21-2008, 04:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks