You are on page 1of 2

The one where we learn about lists and testing.

1) Describe some jUnit tests youd write for Song.getStatistics() from the first project. Just as a reminder heres the specifications for it:

getStatistics
public double[] getStatistics()

Calculates basic statistics about this current song and returns it in an array of integers. The order of the calculations should be: 1. Average number of plays on each station that carries it, 0 if song is never played 2. Total number of plays across all stations 3. Station that plays this specific song most often, -1 if the song is played the same number of times across all stations 4. Maximum number of plays on one station 5. Station that plays this specific song the least -1 if the song is played the same number of times across all stations 6. Minimum number of plays on any one station If the song is never played, the average in (0) should be set to 0. For all calculations except for the average, we can count stations that do not carry this song. That is the maximum and minimum number of plays can be 0. If multiple stations pl ay this song the same number of times, use the station with the lowest station id for (2) and (4). If no stations play this song, (2) and (4) should be set to -1 since our station ids are zero-based. Returns: An array of basic song statistics
Junit tests verify a feature. Strive to make them as small as possible so we can test each feature by itself.

Test input: If we had provided bad input and constructed a Song object, what would running getStatistics do() Test Average: Song that is never played Test Max/Min played Station: Songs that are played the same across all station, never played (across all stations) Test Max/Min Plays : Songs that are played the same across all stations, never played, Also throw in tests that you know will pass so as you modify your code if these previously passing tests fail, you know youve done something wrong. Remember, your junit tests shouldnt be modified. In test driven development, you change your code around until the tests pass.

2) Write a method that deletes the smallest element of a Linked List of integers
public static int findMin(Node n){ if(n.next == null) return n.val; else return Math.min(n.val, findMax(n.next)); } public void deleteMin(Node head){ if(head == null){ return null; } if(head.next == null){ Node h = new Node(head.val); h.next = null; return h; } int min = findMin(n); for(Node s = head; s.next != null); s = s.next){ if(s.next.val == min){ s.next = s.next.next; } } }

3) What do I do?
public static int mystery(Node start){ int c = 0; int j = 1; Node i = start; while(i.next != null){ if(( j+2) >= 3 && (j + 2) % 3 == 0){ c += i.val; i.next = i.next.next; } j++; i = i.next; } return c; }

Add the value of every 3 nodes in our linkedlist. Write junit tests for me that show where I break.
@Test (expected = NullPointerException.class) public void testNullStart(){ Node start =null; assertEquals(mystery(start), 0); }

You might also like