# int Array Tests

• 04-14-2009, 05:10 AM
Suzanne1187
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
azzaiel
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};
• 04-14-2009, 06:27 AM
Mr.Beans
In java Arrays are actually objects.

You could just use

Code:

`n1.equals(n2);`
to check if their equal.

Mr. Beans

Note: Next time please use the code tags.
• 04-14-2009, 07:55 AM
Suzanne1187
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
Eranga
Quote:

Originally Posted by Suzanne1187
I've tried both and have gotten errors. Sorry i dont know what code tags are.

Wrap your code segment with the closing tag [/CODE] and the opening tag [CODE]. Formatted codes are easy to read.

Quote:

Originally Posted by Suzanne1187
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};

You asking how to write a method body?
• 04-15-2009, 04:38 AM
Suzanne1187
repeat- I need help writing the tests for a method involving arrays.....

such as
@Test
public void testingArrayMethod(){
.....
• 04-15-2009, 04:42 AM
azzaiel
are you using junit test?
• 04-15-2009, 04:46 AM
Suzanne1187
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
azzaiel
so you want to test a method that doesn't return a value..... hmmmm that's interesting can post your whole code?
• 04-15-2009, 05:03 AM
Suzanne1187
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
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);         } }```
for example i want {2,1,3,4,2,0} to become {0,2,1,3,2,4}
• 04-15-2009, 05:15 AM
azzaiel
hei try running this

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;         } }```
• 04-15-2009, 05:27 AM
Suzanne1187
thanks for the method... do you have any idea how to write the test for the method?
• 04-15-2009, 05:32 AM
azzaiel
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?
• 04-15-2009, 08:08 AM
JohnnyR
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
Suzanne1187
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
azzaiel
uhmm ok so Junit 3 or Junit 4
• 04-16-2009, 01:49 AM
JohnnyR
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!