-
Notifications
You must be signed in to change notification settings - Fork 13
/
FrogCrossingTest.java
63 lines (56 loc) · 2.26 KB
/
FrogCrossingTest.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
61
62
63
import org.junit.Test;
import java.util.Random;
import java.util.zip.CRC32;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
public class FrogCrossingTest {
@Test public void testMaximumFrogsExplicit() {
int[] strength0 = {8, 7, 5};
int[] boxes0 = {0, 2, 4, 7, 9, 10, 13, 14, 16};
assertEquals(3, FrogCrossing.maximumFrogs(strength0, boxes0));
int[] strength1 = {7, 5, 4};
int[] boxes1 = {0, 3, 4, 6, 8, 10, 11};
assertEquals(2, FrogCrossing.maximumFrogs(strength1, boxes1));
int[] strength2 = {20, 17, 15, 13, 11};
int[] boxes2 = {7, 8, 11, 17, 20, 21, 24, 26, 31, 33, 38};
assertEquals(4, FrogCrossing.maximumFrogs(strength2, boxes2));
}
@Test public void testMaximumFrogsTwenty() {
massTest(20, 2316408012L);
}
@Test public void testMaximumFrogsHundred() {
massTest(100, 595606895L);
}
private void massTest(int n, long expected) {
CRC32 check = new CRC32();
Random rng = new Random(12345 + n);
int frogs = 3, count = 0, goal = 10;
for(int i = 0; i < n; i++) {
int[] strength = new int[frogs];
for(int j = 0; j < frogs; j++) {
strength[j] = 3 + rng.nextInt(4 * frogs);
}
Arrays.sort(strength);
// Seriously, Java, still no method to sort the array in descending order?
int j1 = 0, j2 = strength.length-1;
while(j1 < j2) {
int tmp = strength[j1]; strength[j1] = strength[j2]; strength[j2] = tmp;
j1++; j2--;
}
int m = frogs + rng.nextInt(2 * frogs);
int[] boxes = new int[m];
for(int j = 1; j < boxes.length; j++) {
boxes[j] = boxes[j-1] + 1 + rng.nextInt(1 + frogs);
}
int result = FrogCrossing.maximumFrogs(strength, boxes);
check.update(result);
// System.out.println("Boxes at " + Arrays.toString(boxes));
// System.out.println("Strengths " + Arrays.toString(strength));
// System.out.println(result);
if(++count == goal) {
count = 0; goal++; frogs++;
}
}
assertEquals(expected, check.getValue());
}
}