KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestCompress


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import java.util.Random JavaDoc;
8
9 import org.h2.test.TestBase;
10 import org.h2.tools.CompressTool;
11
12 public class TestCompress extends TestBase {
13     
14     public void test() throws Exception JavaDoc {
15         if(config.big) {
16             for(int i=0; i<100; i++) {
17                 test(i);
18             }
19             for(int i=100; i<10000; i += (i+i+1)) {
20                 test(i);
21             }
22         } else {
23             test(0);
24             test(1);
25             test(7);
26             test(50);
27             test(200);
28         }
29     }
30     
31     void test(int len) throws Exception JavaDoc {
32         for(int pattern = 0; pattern < 3; pattern++) {
33             byte[] buff = new byte[len];
34             switch(pattern) {
35             case 0:
36                 // leave empty
37
break;
38             case 1: {
39                 for(int x=0; x<len; x++) {
40                     buff[x] = (byte)(x & 10);
41                 }
42                 break;
43             }
44             case 2: {
45                 Random JavaDoc r = new Random JavaDoc(len);
46                 r.nextBytes(buff);
47                 break;
48             }
49             }
50             String JavaDoc[] algorithm= new String JavaDoc[]{
51                     "LZF", "Deflate", "No"
52             };
53             CompressTool utils = CompressTool.getInstance();
54             for(int i=0; i<algorithm.length; i++) {
55                 byte[] out = utils.compress(buff, algorithm[i]);
56                 byte[] test = utils.expand(out);
57                 check(test.length, buff.length);
58                 check(buff, test);
59             }
60         }
61     }
62
63 }
64
Popular Tags