forked from fast-pack/JavaFastPFOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.java
More file actions
129 lines (108 loc) · 4.4 KB
/
Copy pathTestUtils.java
File metadata and controls
129 lines (108 loc) · 4.4 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
package me.lemire.integercompression;
import java.util.Arrays;
import me.lemire.integercompression.skippable.SkippableIntegerCODEC;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Static utility methods for test.
*/
public class TestUtils {
/**
*
*/
@Test
public void bogus() {}// to avoid useless complaining.
protected static void dumpIntArray(int[] data, String label) {
System.out.print(label);
for (int i = 0; i < data.length; ++i) {
if (i % 6 == 0) {
System.out.println();
}
System.out.format(" %1$11d", data[i]);
}
System.out.println();
}
protected static void dumpIntArrayAsHex(int[] data, String label) {
System.out.print(label);
for (int i = 0; i < data.length; ++i) {
if (i % 8 == 0) {
System.out.println();
}
System.out.format(" %1$08X", data[i]);
}
System.out.println();
}
/**
* Check that compress and uncompress keep original array.
*
* @param codec CODEC to test.
* @param orig original integers
*/
public static void assertSymmetry(IntegerCODEC codec, int... orig) {
// There are some cases that compressed array is bigger than original
// array. So output array for compress must be larger.
//
// Example:
// - VariableByte compresses an array like [ -1 ].
// - Composition compresses a short array.
final int EXTEND = 1;
int[] compressed = new int[orig.length + EXTEND];
IntWrapper c_inpos = new IntWrapper(0);
IntWrapper c_outpos = new IntWrapper(0);
codec.compress(orig, c_inpos, orig.length, compressed,
c_outpos);
assertTrue(c_outpos.get() <= orig.length + EXTEND);
// Uncompress an array.
int[] uncompressed = new int[orig.length];
IntWrapper u_inpos = new IntWrapper(0);
IntWrapper u_outpos = new IntWrapper(0);
codec.uncompress(compressed, u_inpos, c_outpos.get(),
uncompressed, u_outpos);
// Compare between uncompressed and orig arrays.
int[] target = Arrays.copyOf(uncompressed, u_outpos.get());
assertArrayEquals(orig, target);
}
protected static int[] compress(IntegerCODEC codec, int[] data) {
int[] outBuf = new int[data.length * 4];
IntWrapper inPos = new IntWrapper();
IntWrapper outPos = new IntWrapper();
codec.compress(data, inPos, data.length, outBuf, outPos);
return Arrays.copyOf(outBuf, outPos.get());
}
protected static int[] uncompress(IntegerCODEC codec, int[] data, int len) {
int[] outBuf = new int[len + 1024];
IntWrapper inPos = new IntWrapper();
IntWrapper outPos = new IntWrapper();
codec.uncompress(data, inPos, data.length, outBuf, outPos);
return Arrays.copyOf(outBuf, outPos.get());
}
protected static byte[] compress(ByteIntegerCODEC codec, int[] data) {
byte[] outBuf = new byte[data.length * 4 * 4];
IntWrapper inPos = new IntWrapper();
IntWrapper outPos = new IntWrapper();
codec.compress(data, inPos, data.length, outBuf, outPos);
return Arrays.copyOf(outBuf, outPos.get());
}
protected static int[] uncompress(ByteIntegerCODEC codec, byte[] data, int len) {
int[] outBuf = new int[len + 1024];
IntWrapper inPos = new IntWrapper();
IntWrapper outPos = new IntWrapper();
codec.uncompress(data, inPos, data.length, outBuf, outPos);
return Arrays.copyOf(outBuf, outPos.get());
}
protected static int[] compress(SkippableIntegerCODEC codec, int[] data) {
int[] outBuf = new int[data.length * 4];
IntWrapper inPos = new IntWrapper();
IntWrapper outPos = new IntWrapper();
codec.headlessCompress(data, inPos, data.length, outBuf, outPos);
return Arrays.copyOf(outBuf, outPos.get());
}
protected static int[] uncompress(SkippableIntegerCODEC codec, int[] data, int len) {
int[] outBuf = new int[len + 1024];
IntWrapper inPos = new IntWrapper();
IntWrapper outPos = new IntWrapper();
codec.headlessUncompress(data, inPos, data.length, outBuf, outPos,len);
if(outPos.get() < len) throw new RuntimeException("Insufficient output.");
return Arrays.copyOf(outBuf, outPos.get());
}
}