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 = 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**  Reply With Quote

2. ## assertEquals(n2,n1);
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};  Reply With Quote

3. ## In java Arrays are actually objects.

You could just use

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

Mr. Beans

Note: Next time please use the code tags.  Reply With Quote

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};  Reply With Quote

5. ##  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. 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?  Reply With Quote

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(){
.....  Reply With Quote

7. ## are you using junit test?  Reply With Quote

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  Reply With Quote

9. ## so you want to test a method that doesn't return a value..... hmmmm that's interesting can post your whole code?  Reply With Quote

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
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 = 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}  Reply With Quote

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 = smallest;
numbers[numbers.length-1] = largest;
}

}```  Reply With Quote

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?  Reply With Quote

13. ## no if you havent notice even if your method returns void the array numbers value still changed to numbers = 0 and numbers =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?  Reply With Quote

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?  Reply With Quote

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.  Reply With Quote

16. ## uhmm ok so Junit 3 or Junit 4  Reply With Quote

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!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•