-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayAlgorithmsTest.java
More file actions
executable file
·76 lines (67 loc) · 2.36 KB
/
Copy pathArrayAlgorithmsTest.java
File metadata and controls
executable file
·76 lines (67 loc) · 2.36 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Random;
import java.io.*;
import java.util.*;
import java.util.zip.CRC32;
public class ArrayAlgorithmsTest {
private static final int SEED = 12345;
private static final int TRIALS = 10000;
@Test public void testFallingPower() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int b = -10; b < 10; b++) {
for(int e = 0; e < 10; e++) {
long p = ArrayAlgorithms.fallingPower(b, e);
check.update((int)(p & 0xFFFF));
check.update((int)((p >> 31) & 0xFFFF));
}
}
assertEquals(4140005098L, check.getValue());
}
@Test public void testEveryOther() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int i = 0; i < TRIALS; i++) {
int len = rng.nextInt(1000);
int[] a = new int[len];
for(int j = 0; j < len; j++) {
a[j] = rng.nextInt(100000);
}
int[] b = ArrayAlgorithms.everyOther(a);
check.update(b.length);
for(int e: b) { check.update(e); }
}
assertEquals(3861208241L, check.getValue());
}
@Test public void testCreateZigZag() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int i = 0; i < TRIALS; i++) {
int rows = rng.nextInt(20) + 1;
int cols = rng.nextInt(20) + 1;
int start = rng.nextInt(100);
int[][] zig = ArrayAlgorithms.createZigZag(rows, cols, start);
assertEquals(rows, zig.length);
for(int j = 0; j < rows; j++) {
assertEquals(cols, zig[j].length);
for(int e: zig[j]) { check.update(e); }
}
}
assertEquals(3465650385L, check.getValue());
}
@Test public void testCountInversions() {
Random rng = new Random(SEED);
CRC32 check = new CRC32();
for(int i = 0; i < 1000; i++) {
int[] a = new int[i];
for(int j = 0; j < i; j++) {
a[j] = rng.nextInt(100000);
}
check.update(ArrayAlgorithms.countInversions(a));
}
assertEquals(1579619806L, check.getValue());
}
}