-
Notifications
You must be signed in to change notification settings - Fork 13
/
BitwiseStuffTest.java
76 lines (69 loc) · 2.63 KB
/
BitwiseStuffTest.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
64
65
66
67
68
69
70
71
72
73
74
75
76
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.zip.CRC32;
import static org.junit.Assert.assertEquals;
public class BitwiseStuffTest {
@Test public void testCountClusters() {
Random rng = new Random(12345);
for(int i = 0; i < 10000; i++) {
long n = 0;
int prev = 0, curr, count = 0;
for(int j = 0; j < 64; j++) {
curr = (rng.nextInt(100) < 25) ? 1 - prev : prev;
if(curr == 1 && prev == 0) { count++; }
n = (n << 1) + curr;
prev = curr;
}
int result = BitwiseStuff.countClusters(n);
//System.out.println(n + " " + Long.toBinaryString(n) + " " + result + " " + count);
assertEquals(count, result);
}
}
@Test public void testBestRotateFill() {
Random rng = new Random(12345);
CRC32 check = new CRC32();
for(int i = 0; i < 10000; i++) {
long n = 0;
for(int j = 0; j < 64; j++) {
n = (n << 1) + ((rng.nextInt(100) < 75) ? 0 : 1);
}
int result = BitwiseStuff.bestRotateFill(n);
check.update(result);
}
assertEquals(505332074L, check.getValue());
}
@Test public void testReverseNybbles() {
Random rng = new Random(12345);
CRC32 check = new CRC32();
for(int i = 0; i < 10000; i++) {
long n = 0;
for(int j = 0; j < 64; j++) {
n = (n << 1) + (rng.nextBoolean() ? 0 : 1);
}
long result = BitwiseStuff.reverseNybbles(n);
long back = BitwiseStuff.reverseNybbles(result);
try {
check.update(Long.toHexString(result).getBytes("UTF-8"));
} catch(UnsupportedEncodingException ignored) {}
assertEquals(n, back);
}
assertEquals(232648447L, check.getValue());
}
@Test public void testDosido() {
Random rng = new Random(12345);
CRC32 check = new CRC32();
for(int i = 0; i < 10000; i++) {
long n = 0;
for(int j = 0; j < 64; j++) {
n = (n << 1) + (rng.nextBoolean() ? 0 : 1);
}
long result = BitwiseStuff.dosido(n);
long back = BitwiseStuff.dosido(result);
//System.out.println(Long.toHexString(n) + " " + Long.toHexString(result) + " " + Long.toHexString(back));
check.update(Long.toHexString(result).getBytes());
assertEquals(n, back);
}
assertEquals(4127798722L, check.getValue());
}
}