-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathSnappyTest.java
More file actions
executable file
·377 lines (319 loc) · 12.7 KB
/
SnappyTest.java
File metadata and controls
executable file
·377 lines (319 loc) · 12.7 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
376
377
/*--------------------------------------------------------------------------
* Copyright 2011 Taro L. Saito
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*--------------------------------------------------------------------------*/
//--------------------------------------
// snappy-java Project
//
// SnappyTest.java
// Since: 2011/03/30
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.snappy;
import static org.junit.Assert.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.junit.Assert;
import org.junit.Test;
import org.xerial.util.log.Logger;
public class SnappyTest
{
private static Logger _logger = Logger.getLogger(SnappyTest.class);
@Test
public void getVersion()
throws Exception
{
String version = Snappy.getNativeLibraryVersion();
_logger.debug("version: " + version);
}
@Test
public void directBufferCheck()
throws Exception
{
try {
ByteBuffer src = ByteBuffer.allocate(1024);
src.put("hello world".getBytes());
src.flip();
ByteBuffer dest = ByteBuffer.allocate(1024);
int maxCompressedLen = Snappy.compress(src, dest);
}
catch (SnappyError e) {
Assert.assertTrue(e.errorCode == SnappyErrorCode.NOT_A_DIRECT_BUFFER);
return;
}
fail("shouldn't reach here");
}
@Test
public void directBuffer()
throws Exception
{
StringBuilder s = new StringBuilder();
for (int i = 0; i < 20; ++i) {
s.append("Hello world!");
}
String origStr = s.toString();
byte[] orig = origStr.getBytes();
int BUFFER_SIZE = orig.length;
ByteBuffer src = ByteBuffer.allocateDirect(orig.length);
src.put(orig);
src.flip();
_logger.debug("input size: " + src.remaining());
int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
_logger.debug("max compressed length:" + maxCompressedLen);
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
int compressedSize = Snappy.compress(src, compressed);
_logger.debug("compressed length: " + compressedSize);
assertTrue(Snappy.isValidCompressedBuffer(compressed));
assertEquals(0, src.position());
assertEquals(orig.length, src.remaining());
assertEquals(orig.length, src.limit());
assertEquals(0, compressed.position());
assertEquals(compressedSize, compressed.limit());
assertEquals(compressedSize, compressed.remaining());
int uncompressedLen = Snappy.uncompressedLength(compressed);
_logger.debug("uncompressed length: " + uncompressedLen);
ByteBuffer extract = ByteBuffer.allocateDirect(uncompressedLen);
int uncompressedLen2 = Snappy.uncompress(compressed, extract);
assertEquals(uncompressedLen, uncompressedLen2);
assertEquals(uncompressedLen, extract.remaining());
byte[] b = new byte[uncompressedLen];
extract.get(b);
String decompressed = new String(b);
_logger.debug(decompressed);
assertEquals(origStr, decompressed);
}
@Test
public void directBufferToHeapBuffer() throws Exception
{
StringBuilder s = new StringBuilder();
for (int i = 0; i < 20; ++i) {
s.append("Hello world!");
}
String origStr = s.toString();
byte[] orig = origStr.getBytes();
ByteBuffer src = ByteBuffer.allocateDirect(orig.length);
src.put(orig);
src.flip();
_logger.debug("input size: " + src.remaining());
int maxCompressedLen = Snappy.maxCompressedLength(src.remaining());
_logger.debug("max compressed length:" + maxCompressedLen);
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLen);
int compressedSize = Snappy.compress(src, compressed);
_logger.debug("compressed length: " + compressedSize);
assertEquals(0, src.position());
assertEquals(orig.length, src.remaining());
assertEquals(orig.length, src.limit());
assertEquals(0, compressed.position());
assertEquals(compressedSize, compressed.limit());
assertEquals(compressedSize, compressed.remaining());
int uncompressedLen = Snappy.uncompressedLength(compressed);
_logger.debug("uncompressed length: " + uncompressedLen);
ByteBuffer extract = ByteBuffer.allocate(uncompressedLen);
int uncompressedLen2 = Snappy.uncompress(compressed, extract);
assertEquals(uncompressedLen, uncompressedLen2);
assertEquals(uncompressedLen, extract.remaining());
byte[] b = new byte[uncompressedLen];
extract.get(b);
String decompressed = new String(b);
_logger.debug(decompressed);
assertEquals(origStr, decompressed);
}
@Test
public void bufferOffset()
throws Exception
{
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] orig = m.getBytes();
final int offset = 100;
ByteBuffer input = ByteBuffer.allocateDirect(orig.length + offset);
input.position(offset);
input.put(orig);
input.flip();
input.position(offset);
// compress
int maxCompressedLength = Snappy.maxCompressedLength(input.remaining());
final int offset2 = 40;
ByteBuffer compressed = ByteBuffer.allocateDirect(maxCompressedLength + offset2);
compressed.position(offset2);
Snappy.compress(input, compressed);
assertTrue(Snappy.isValidCompressedBuffer(compressed));
// uncompress
final int offset3 = 80;
int uncompressedLength = Snappy.uncompressedLength(compressed);
ByteBuffer uncompressed = ByteBuffer.allocateDirect(uncompressedLength + offset3);
uncompressed.position(offset3);
Snappy.uncompress(compressed, uncompressed);
assertEquals(offset3, uncompressed.position());
assertEquals(offset3 + uncompressedLength, uncompressed.limit());
assertEquals(uncompressedLength, uncompressed.remaining());
// extract string
byte[] recovered = new byte[uncompressedLength];
uncompressed.get(recovered);
String m2 = new String(recovered);
assertEquals(m, m2);
}
@Test
public void byteArrayCompress()
throws Exception
{
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] input = m.getBytes();
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
int compressedSize = Snappy.compress(input, 0, input.length, output, 0);
byte[] uncompressed = new byte[input.length];
assertTrue(Snappy.isValidCompressedBuffer(output, 0, compressedSize));
int uncompressedSize = Snappy.uncompress(output, 0, compressedSize, uncompressed, 0);
String m2 = new String(uncompressed);
assertEquals(m, m2);
}
@Test
public void rangeCheck()
throws Exception
{
String m = "ACCAGGGGGGGGGGGGGGGGGGGGATAGATATTTCCCGAGATATTTTATATAAAAAAA";
byte[] input = m.getBytes();
byte[] output = new byte[Snappy.maxCompressedLength(input.length)];
int compressedSize = Snappy.compress(input, 0, input.length, output, 0);
assertTrue(Snappy.isValidCompressedBuffer(output, 0, compressedSize));
// Intentionally set an invalid range
assertFalse(Snappy.isValidCompressedBuffer(output, 0, compressedSize + 1));
assertFalse(Snappy.isValidCompressedBuffer(output, 1, compressedSize));
// Test the ByteBuffer API
ByteBuffer bin = ByteBuffer.allocateDirect(input.length);
bin.put(input);
bin.flip();
ByteBuffer bout = ByteBuffer.allocateDirect(Snappy.maxCompressedLength(bin.remaining()));
int compressedSize2 = Snappy.compress(bin, bout);
assertEquals(compressedSize, compressedSize2);
assertTrue(Snappy.isValidCompressedBuffer(bout));
// Intentionally set an invalid range
bout.limit(bout.limit() + 1);
assertFalse(Snappy.isValidCompressedBuffer(bout));
bout.limit(bout.limit() - 1);
bout.position(1);
assertFalse(Snappy.isValidCompressedBuffer(bout));
}
@Test
public void highLevelAPI()
throws Exception
{
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
byte[] input = m.getBytes();
byte[] output = Snappy.compress(input);
byte[] uncompressed = Snappy.uncompress(output);
String m2 = new String(uncompressed);
assertEquals(m, m2);
}
@Test
public void lowLevelAPI()
throws Exception
{
String m = "Hello! 01234 ACGDSFSDFJ World. FDSDF02394234 fdsfda03924";
byte[] input = m.getBytes();
byte[] output = Snappy.rawCompress(input, input.length);
byte[] uncompressed = Snappy.uncompress(output);
String m2 = new String(uncompressed);
assertEquals(m, m2);
}
@Test
public void simpleUsage()
throws Exception
{
String input = "Hello snappy-java! Snappy-java is a JNI-based wrapper"
+ " for using Snappy from Google (written in C++), a fast compresser/decompresser.";
byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
byte[] uncompressed = Snappy.uncompress(compressed);
String result = new String(uncompressed, "UTF-8");
_logger.debug(result);
}
@Test
public void floatArray()
throws Exception
{
float[] data = new float[] {1.0f, -0.3f, 1.3f, 234.4f, 34};
byte[] compressed = Snappy.compress(data);
float[] result = Snappy.uncompressFloatArray(compressed);
assertArrayEquals(data, result, 0.0f);
}
@Test
public void doubleArray()
throws Exception
{
double[] data = new double[] {1.0, -0.3, 1.3, 234.4, 34};
byte[] compressed = Snappy.compress(data);
double[] result = Snappy.uncompressDoubleArray(compressed);
assertArrayEquals(data, result, 0.0f);
}
@Test
public void longArray()
throws Exception
{
long[] data = new long[] {2, 3, 15, 4234, 43251531412342342L, 23423422342L};
byte[] compressed = Snappy.compress(data);
long[] result = Snappy.uncompressLongArray(compressed);
assertArrayEquals(data, result);
}
@Test
public void shortArray()
throws Exception
{
short[] data = new short[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1};
byte[] compressed = Snappy.compress(data);
short[] result = Snappy.uncompressShortArray(compressed);
assertArrayEquals(data, result);
}
@Test
public void intArray()
throws Exception
{
int[] data = new int[] {432, -32267, 1, 3, 34, 43, 34, Short.MAX_VALUE, -1, Integer.MAX_VALUE, 3424, 43};
byte[] compressed = Snappy.compress(data);
int[] result = Snappy.uncompressIntArray(compressed);
assertArrayEquals(data, result);
}
@Test
public void charArray()
throws Exception
{
char[] data = new char[] {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
byte[] compressed = Snappy.compress(data);
char[] result = Snappy.uncompressCharArray(compressed);
assertArrayEquals(data, result);
}
@Test
public void string()
throws Exception
{
String s = "Hello Snappy! Snappy! Snappy!";
byte[] compressed = Snappy.compress(s);
String uncompressedString = Snappy.uncompressString(compressed);
assertEquals(s, uncompressedString);
}
@Test
public void isValidCompressedData()
throws Exception
{
byte[] b = new byte[] {(byte) 91, (byte) 34, (byte) 80, (byte) 73, (byte) 34, (byte) 93};
assertFalse(Snappy.isValidCompressedBuffer(b));
try {
byte[] uncompressed = Snappy.uncompress(b);
fail("cannot reach here since the input is invalid data");
}
catch (IOException e) {
_logger.debug(e);
}
}
}