forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicTest.java
More file actions
375 lines (341 loc) · 13.2 KB
/
Copy pathBasicTest.java
File metadata and controls
375 lines (341 loc) · 13.2 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package me.lemire.integercompression;
import java.util.Arrays;
import java.util.Random;
import me.lemire.integercompression.synth.ClusteredDataGenerator;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Just some basic sanity tests.
*
* @author Daniel Lemire
*/
public class BasicTest
{
@Test
public void checkDeltaZigzagVB() {
DeltaZigzagVariableByte codec = new DeltaZigzagVariableByte();
testZeroInZeroOut(codec);
test(codec, 5, 10);
test(codec, 5, 14);
test(codec, 2, 18);
}
@Test
public void checkDeltaZigzagPacking() {
DeltaZigzagBinaryPacking codec = new DeltaZigzagBinaryPacking();
testZeroInZeroOut(codec);
testSpurious(codec);
IntegerCODEC compo = new Composition(
new DeltaZigzagBinaryPacking(),
new VariableByte());
testZeroInZeroOut(compo);
testUnsorted(compo);
test(compo, 5, 10);
test(compo, 5, 14);
test(compo, 2, 18);
}
@Test
public void checkXorBinaryPacking() {
testZeroInZeroOut(new XorBinaryPacking());
testSpurious(new XorBinaryPacking());
}
@Test
public void checkXorBinaryPacking1() {
IntegerCODEC c = new IntegratedComposition(new XorBinaryPacking(),
new IntegratedVariableByte());
testZeroInZeroOut(c);
}
@Test
public void checkXorBinaryPacking2() {
IntegerCODEC c = new IntegratedComposition(new XorBinaryPacking(),
new IntegratedVariableByte());
testUnsorted(c);
}
@Test
public void checkXorBinaryPacking3() {
IntegerCODEC c = new IntegratedComposition(new XorBinaryPacking(),
new IntegratedVariableByte());
test(c, 5, 10);
test(c, 5, 14);
test(c, 2, 18);
}
/**
* Verify bitpacking.
*/
@SuppressWarnings("static-method")
@Test
public void verifyBitPacking() {
final int N = 32;
final int TIMES = 1000;
Random r = new Random();
int[] data = new int[N];
int[] compressed = new int[N];
int[] uncompressed = new int[N];
for (int bit = 0; bit < 31; ++bit) {
for (int t = 0; t < TIMES; ++t) {
for (int k = 0; k < N; ++k) {
data[k] = r.nextInt(1 << bit);
}
BitPacking.fastpack(data, 0, compressed, 0, bit);
BitPacking.fastunpack(compressed, 0, uncompressed, 0, bit);
assertArrayEquals(uncompressed, data);
}
}
}
/**
* Verify bitpacking without mask.
*/
@SuppressWarnings("static-method")
@Test
public void verifyWithoutMask() {
final int N = 32;
final int TIMES = 1000;
Random r = new Random();
int[] data = new int[N];
int[] compressed = new int[N];
int[] uncompressed = new int[N];
for (int bit = 0; bit < 31; ++bit) {
for (int t = 0; t < TIMES; ++t) {
for (int k = 0; k < N; ++k) {
data[k] = r.nextInt(1 << bit);
}
BitPacking.fastpackwithoutmask(data, 0, compressed, 0, bit);
BitPacking.fastunpack(compressed, 0, uncompressed, 0, bit);
assertArrayEquals(uncompressed, data);
}
}
}
public static void maskArray(int[] array, int mask) {
for (int i = 0, end = array.length; i < end; ++i) {
array[i] &= mask;
}
}
/**
* Verify bitpacking with exception.
*/
@SuppressWarnings("static-method")
@Test
public void verifyWithExceptions() {
final int N = 32;
final int TIMES = 1000;
Random r = new Random();
int[] data = new int[N];
int[] compressed = new int[N];
int[] uncompressed = new int[N];
for (int bit = 0; bit < 31; ++bit) {
for (int t = 0; t < TIMES; ++t) {
for (int k = 0; k < N; ++k) {
data[k] = r.nextInt();
}
BitPacking.fastpack(data, 0, compressed, 0, bit);
BitPacking.fastunpack(compressed, 0, uncompressed, 0, bit);
// Check assertions.
maskArray(data, ((1 << bit) - 1));
assertArrayEquals(data, uncompressed);
}
}
}
/**
* check that the codecs can be inverted.
*/
@SuppressWarnings("static-method")
@Test
public void basictest() {
test(5, 10);
test(5, 14);
test(2, 18);
}
/**
* check that there is no spurious output.
*/
@SuppressWarnings("static-method")
@Test
public void spuriousouttest() {
testSpurious(new IntegratedBinaryPacking());
testSpurious(new BinaryPacking());
testSpurious(new NewPFD());
testSpurious(new NewPFDS9());
testSpurious(new NewPFDS16());
testSpurious(new OptPFD());
testSpurious(new OptPFDS9());
testSpurious(new OptPFDS16());
testSpurious(new FastPFOR());
}
/**
* check that an empty array generates an empty array
* after compression.
*/
@SuppressWarnings("static-method")
@Test
public void zeroinzerouttest() {
testZeroInZeroOut(new IntegratedBinaryPacking());
testZeroInZeroOut(new IntegratedVariableByte());
testZeroInZeroOut(new BinaryPacking());
testZeroInZeroOut(new NewPFD());
testZeroInZeroOut(new NewPFDS9());
testZeroInZeroOut(new NewPFDS16());
testZeroInZeroOut(new OptPFD());
testZeroInZeroOut(new OptPFDS9());
testZeroInZeroOut(new OptPFDS16());
testZeroInZeroOut(new FastPFOR());
testZeroInZeroOut(new VariableByte());
testZeroInZeroOut(new Composition(new IntegratedBinaryPacking(), new VariableByte()));
testZeroInZeroOut(new Composition(new BinaryPacking(), new VariableByte()));
testZeroInZeroOut(new Composition(new NewPFD(), new VariableByte()));
testZeroInZeroOut(new Composition(new NewPFDS9(), new VariableByte()));
testZeroInZeroOut(new Composition(new NewPFDS16(), new VariableByte()));
testZeroInZeroOut(new Composition(new OptPFD(), new VariableByte()));
testZeroInZeroOut(new Composition(new OptPFDS9(), new VariableByte()));
testZeroInZeroOut(new Composition(new OptPFDS16(), new VariableByte()));
testZeroInZeroOut(new Composition(new FastPFOR(), new VariableByte()));
testZeroInZeroOut(new IntegratedComposition(new IntegratedBinaryPacking(), new IntegratedVariableByte()));
}
private static void testSpurious(IntegerCODEC c) {
int[] x = new int[1024];
int[] y = new int[0];
IntWrapper i0 = new IntWrapper(0);
IntWrapper i1 = new IntWrapper(0);
for (int inlength = 0; inlength < 128; ++inlength) {
c.compress(x, i0, inlength, y, i1);
assertEquals(0, i1.intValue());
}
}
private static void testZeroInZeroOut(IntegerCODEC c) {
int[] x = new int[0];
int[] y = new int[0];
IntWrapper i0 = new IntWrapper(0);
IntWrapper i1 = new IntWrapper(0);
c.compress(x, i0, 0, y, i1);
assertEquals(0, i1.intValue());
int[] out = new int[0];
IntWrapper outpos = new IntWrapper(0);
c.uncompress(y, i1, 0, out, outpos);
assertEquals(0, outpos.intValue());
}
private static void test(IntegerCODEC c, int N, int nbr) {
ClusteredDataGenerator cdg = new ClusteredDataGenerator();
for (int sparsity = 1; sparsity < 31 - nbr; sparsity += 4) {
int[][] data = new int[N][];
int max = (1 << (nbr + sparsity));
for (int k = 0; k < N; ++k) {
data[k] = cdg.generateClustered((1 << nbr), max);
}
testCodec(c, data, max);
}
}
private static void test(int N, int nbr) {
ClusteredDataGenerator cdg = new ClusteredDataGenerator();
for (int sparsity = 1; sparsity < 31 - nbr; sparsity += 4) {
int[][] data = new int[N][];
int max = (1 << (nbr + sparsity));
for (int k = 0; k < N; ++k) {
data[k] = cdg.generateClustered((1 << nbr), max);
}
testCodec(new IntegratedComposition(new IntegratedBinaryPacking(),
new IntegratedVariableByte()), data, max);
testCodec(new JustCopy(), data, max);
testCodec(new VariableByte(), data, max);
testCodec(new IntegratedVariableByte(), data, max);
testCodec(new Composition(new BinaryPacking(), new VariableByte()),
data, max);
testCodec(new Composition(new NewPFD(), new VariableByte()), data,
max);
testCodec(new Composition(new NewPFDS9(), new VariableByte()),
data, max);
testCodec(new Composition(new NewPFDS16(), new VariableByte()),
data, max);
testCodec(new Composition(new OptPFD(), new VariableByte()), data,
max);
testCodec(new Composition(new OptPFDS9(), new VariableByte()),
data, max);
testCodec(new Composition(new OptPFDS16(), new VariableByte()),
data, max);
testCodec(new Composition(new FastPFOR(), new VariableByte()),
data, max);
testCodec(new Simple9(), data, max);
}
}
private static void testCodec(IntegerCODEC c, int[][] data, int max) {
int N = data.length;
int maxlength = 0;
for (int k = 0; k < N; ++k) {
if (data[k].length > maxlength)
maxlength = data[k].length;
}
int[] buffer = new int[maxlength + 1024];
int[] dataout = new int[4 * maxlength + 1024];
// 4x + 1024 to account for the possibility of some negative
// compression.
IntWrapper inpos = new IntWrapper();
IntWrapper outpos = new IntWrapper();
for (int k = 0; k < N; ++k) {
int[] backupdata = Arrays.copyOf(data[k], data[k].length);
inpos.set(1);
outpos.set(0);
if (!(c instanceof IntegratedIntegerCODEC)) {
Delta.delta(backupdata);
}
c.compress(backupdata, inpos, backupdata.length - inpos.get(),
dataout, outpos);
final int thiscompsize = outpos.get() + 1;
inpos.set(0);
outpos.set(1);
buffer[0] = backupdata[0];
c.uncompress(dataout, inpos, thiscompsize - 1, buffer, outpos);
if (!(c instanceof IntegratedIntegerCODEC))
Delta.fastinverseDelta(buffer);
// Check assertions.
assertEquals("length is not match", outpos.get(), data[k].length);
int[] bufferCutout = Arrays.copyOf(buffer, outpos.get());
assertArrayEquals("failed to reconstruct original data",
data[k], bufferCutout);
}
}
/**
* Test for unsorted array.
*/
@SuppressWarnings("static-method")
@Test
public void testUnsortedExample() {
testUnsorted(new VariableByte());
testUnsorted(new IntegratedVariableByte());
testUnsorted(new Composition(new BinaryPacking(), new VariableByte()));
testUnsorted(new Composition(new NewPFD(), new VariableByte()));
testUnsorted(new Composition(new NewPFDS9(), new VariableByte()));
testUnsorted(new Composition(new NewPFDS16(), new VariableByte()));
testUnsorted(new Composition(new OptPFD(), new VariableByte()));
testUnsorted(new Composition(new OptPFDS9(), new VariableByte()));
testUnsorted(new Composition(new OptPFDS16(), new VariableByte()));
testUnsorted(new Composition(new FastPFOR(), new VariableByte()));
testUnsorted(new IntegratedComposition(new IntegratedBinaryPacking(),
new IntegratedVariableByte()));
testUnsorted(new Composition(new IntegratedBinaryPacking(), new VariableByte()));
}
public void testUnsorted(IntegerCODEC codec)
{
int[] lengths = { 133, 1333333 };
for (int N : lengths) {
int[] data = new int[N];
// initialize the data (most will be small
for (int k = 0; k < N; k += 1)
data[k] = 3;
// throw some larger values
for (int k = 0; k < N; k += 5)
data[k] = 100;
for (int k = 0; k < N; k += 533)
data[k] = 10000;
data[5] = -311;
// could need more compressing
int[] compressed = new int[(int) Math.ceil(N * 1.01) + 1024];
IntWrapper inputoffset = new IntWrapper(0);
IntWrapper outputoffset = new IntWrapper(0);
codec.compress(data, inputoffset, data.length, compressed, outputoffset);
// we can repack the data: (optional)
compressed = Arrays.copyOf(compressed, outputoffset.intValue());
int[] recovered = new int[N];
IntWrapper recoffset = new IntWrapper(0);
codec.uncompress(compressed, new IntWrapper(0),
compressed.length, recovered, recoffset);
assertArrayEquals(data, recovered);
}
}
}