-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCountingSortTester.java
60 lines (50 loc) · 1.87 KB
/
CountingSortTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import static org.junit.Assert.*;
import org.junit.Test;
public class CountingSortTester {
/**
* Array that will cause CountingSort.naiveCountingSort to fail, but
* CountingSort.betterCountingSort can handle.
**/
private static int[] someNegative = {9, 5, -4, 2, 1, -2, 5, 3, 0, -2, 3, 1, 1};
/**
* Array that both sorts should sort successfully.
**/
private static int[] nonNegative = {9, 5, 2, 1, 5, 3, 0, 3, 1, 1};
public static void assertIsSorted(int[] a) {
int previous = Integer.MIN_VALUE;
for (int x : a) {
assertTrue(x >= previous);
previous = x;
}
}
@Test
public void testNaiveWithNonNegative() {
int[] sortedNonNegative = CountingSort.naiveCountingSort(nonNegative);
assertIsSorted(sortedNonNegative);
}
// This test should PASS to indicate that the naive solution is in fact WRONG
@Test
public void testNaiveWithSomeNegative() {
try {
int[] sortedSomeNegative = CountingSort.naiveCountingSort(someNegative);
assertTrue("Naive counting sort should not sort arrays with negative numbers.",
false);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Great! Exception happened as we expected,"
+ "since this sort is broken for negative numbers.");
}
}
@Test
public void testBetterWithNonNegative() {
int[] sortedNonNegative = CountingSort.betterCountingSort(nonNegative);
assertIsSorted(sortedNonNegative);
}
@Test
public void testBetterWithSomeNegative() {
int[] sortedSomeNegative = CountingSort.betterCountingSort(someNegative);
assertIsSorted(sortedSomeNegative);
}
public static void main(String[] args) {
jh61b.junit.TestRunner.runTests(CountingSortTester.class);
}
}