Results 1 to 17 of 17

Thread: int Array Tests

  1. #1
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default 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**

  2. #2
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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.

  4. #4
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    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};

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Suzanne1187 View Post
    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 View Post
    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?

  6. #6
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    repeat- I need help writing the tests for a method involving arrays.....

    such as
    @Test
    public void testingArrayMethod(){
    .....

  7. #7
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  8. #8
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    yes, and the format ive been using for my other tests isnt working for this one because the method doesnt return anything

  9. #9
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  10. #10
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    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[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}

  11. #11
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  12. #12
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    thanks for the method... do you have any idea how to write the test for the method?

  13. #13
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  14. #14
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    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?

  15. #15
    Suzanne1187 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default

    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.

  16. #16
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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

  17. #17
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    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

  1. Remote Junit Tests
    By lord.ec in forum Eclipse
    Replies: 0
    Last Post: 12-09-2008, 08:15 AM
  2. Using ant to run JUnit tests
    By racerxadam in forum Advanced Java
    Replies: 0
    Last Post: 10-21-2008, 04:48 PM

Posting Permissions

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