01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.unit;
07:
08: import java.util.Random;
09:
10: import org.h2.test.TestBase;
11: import org.h2.tools.CompressTool;
12:
13: /**
14: * Data compression tests.
15: */
16: public class TestCompress extends TestBase {
17:
18: public void test() throws Exception {
19: if (config.big) {
20: for (int i = 0; i < 100; i++) {
21: test(i);
22: }
23: for (int i = 100; i < 10000; i += (i + i + 1)) {
24: test(i);
25: }
26: } else {
27: test(0);
28: test(1);
29: test(7);
30: test(50);
31: test(200);
32: }
33: }
34:
35: void test(int len) throws Exception {
36: Random r = new Random(len);
37: for (int pattern = 0; pattern < 4; pattern++) {
38: byte[] buff = new byte[len];
39: switch (pattern) {
40: case 0:
41: // leave empty
42: break;
43: case 1: {
44: for (int x = 0; x < len; x++) {
45: buff[x] = (byte) (x & 10);
46: }
47: break;
48: }
49: case 2: {
50: r.nextBytes(buff);
51: break;
52: }
53: case 3: {
54: for (int x = 0; x < len; x++) {
55: buff[x] = (byte) (x / 10);
56: }
57: break;
58: }
59: }
60: if (r.nextInt(2) < 1) {
61: for (int x = 0; x < len; x++) {
62: if (r.nextInt(20) < 1) {
63: buff[x] = (byte) (r.nextInt(255));
64: }
65: }
66: }
67: String[] algorithm = new String[] { "LZF", "Deflate", "No" };
68: CompressTool utils = CompressTool.getInstance();
69: for (int i = 0; i < algorithm.length; i++) {
70: byte[] out = utils.compress(buff, algorithm[i]);
71: byte[] test = utils.expand(out);
72: check(test.length, buff.length);
73: check(buff, test);
74: }
75: }
76: }
77:
78: }
|