Repository navigation
Expand file tree
/
Copy pathByteBasicTest.java
More file actions
138 lines (120 loc) · 4.3 KB
/
Copy pathByteBasicTest.java
File metadata and controls
138 lines (120 loc) · 4.3 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
package me.lemire.integercompression;
import java.util.Arrays;
import me.lemire.integercompression.differential.IntegratedVariableByte;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Just some basic sanity tests.
*
* @author Daniel Lemire
*/
@SuppressWarnings({ "static-method" })
public class ByteBasicTest {
ByteIntegerCODEC[] codecs = {
new VariableByte(),
new IntegratedVariableByte(),
};
/**
*
*/
@Test
public void saulTest() {
for (ByteIntegerCODEC C : codecs) {
for (int x = 0; x < 50 * 4; ++x) {
int[] a = { 2, 3, 4, 5 };
byte[] b = new byte[90*4];
int[] c = new int[a.length];
IntWrapper aOffset = new IntWrapper(0);
IntWrapper bOffset = new IntWrapper(x);
C.compress(a, aOffset, a.length, b, bOffset);
int len = bOffset.get() - x;
bOffset.set(x);
IntWrapper cOffset = new IntWrapper(0);
C.uncompress(b, bOffset, len, c, cOffset);
if(!Arrays.equals(a, c)) {
System.out.println("Problem with "+C);
}
assertArrayEquals(a, c);
assertEquals(C.toString(), x + len, bOffset.get());
assertEquals(C.toString(), a.length, cOffset.get());
}
}
}
@Test
public void emptyInputPreservesOffsets() {
for (ByteIntegerCODEC codec : codecs) {
IntWrapper inpos = new IntWrapper(5);
IntWrapper outpos = new IntWrapper(2);
int[] output = { 10, 20, 30 };
codec.uncompress(new byte[5], inpos, 0, output, outpos);
assertEquals(codec.toString(), 5, inpos.get());
assertEquals(codec.toString(), 2, outpos.get());
assertArrayEquals(new int[] { 10, 20, 30 }, output);
}
}
/**
*
*/
@Test
public void varyingLengthTest() {
int N = 4096;
int[] data = new int[N];
for (int k = 0; k < N; ++k)
data[k] = k;
for (ByteIntegerCODEC c : codecs) {
for (int L = 1; L <= 128; L++) {
byte[] comp = TestUtils.compress(c, Arrays.copyOf(data, L));
int[] answer = TestUtils.uncompress(c, comp, L);
for (int k = 0; k < L; ++k)
if (answer[k] != data[k])
throw new RuntimeException("bug "+c.toString()+" "+k+" "+answer[k]+" "+data[k]);
}
for (int L = 128; L <= N; L *= 2) {
byte[] comp = TestUtils.compress(c, Arrays.copyOf(data, L));
int[] answer = TestUtils.uncompress(c, comp, L);
for (int k = 0; k < L; ++k)
if (answer[k] != data[k])
throw new RuntimeException("bug");
}
}
}
/**
*
*/
@Test
public void varyingLengthTest2() {
int N = 128;
int[] data = new int[N];
data[127] = -1;
for (ByteIntegerCODEC c : codecs) {
try {
// CODEC Simple9 is limited to "small" integers.
if (c.getClass().equals(
Class.forName("me.lemire.integercompression.Simple9")))
continue;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
for (int L = 1; L <= 128; L++) {
byte[] comp = TestUtils.compress(c, Arrays.copyOf(data, L));
int[] answer = TestUtils.uncompress(c, comp, L);
for (int k = 0; k < L; ++k)
if (answer[k] != data[k])
throw new RuntimeException("bug at k = "+k+" "+answer[k]+" "+data[k]);
}
for (int L = 128; L <= N; L *= 2) {
byte[] comp = TestUtils.compress(c, Arrays.copyOf(data, L));
int[] answer = TestUtils.uncompress(c, comp, L);
for (int k = 0; k < L; ++k)
if (answer[k] != data[k])
throw new RuntimeException("bug");
}
}
}
}