]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/compression/DecompressionTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / compression / DecompressionTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2005, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.compression;\r
8 \r
9 import com.ibm.icu.text.UnicodeDecompressor;\r
10 import com.ibm.icu.dev.test.TestFmwk;\r
11 \r
12 public class DecompressionTest extends TestFmwk {\r
13     public static void main(String[] args) throws Exception {\r
14         new DecompressionTest().run(args);\r
15     }\r
16 \r
17     /** Print out a segment of a character array, if in verbose mode */\r
18     private void log(char [] chars, int start, int count) {\r
19         log("|");\r
20         for(int i = start; i < start + count; ++i) {\r
21             log(String.valueOf(chars[i]));\r
22         }\r
23         log("|");\r
24     }\r
25 \r
26     /** Print out a segment of a character array, followed by a newline */\r
27     private void logln(char [] chars, int start, int count)\r
28     {\r
29         log(chars, start, count);\r
30         logln("");\r
31     }\r
32 \r
33     /** Decompress the two segments */\r
34     private String decompressTest(byte [] segment1, byte [] segment2) {\r
35         StringBuffer s = new StringBuffer();\r
36         UnicodeDecompressor myDecompressor = new UnicodeDecompressor();\r
37 \r
38         int [] bytesRead = new int[1];\r
39         char [] charBuffer = new char [2*(segment1.length + segment2.length)];\r
40         int count1 = 0, count2 = 0;\r
41 \r
42         count1 = myDecompressor.decompress(segment1, 0, segment1.length,\r
43                                            bytesRead,\r
44                                            charBuffer, 0, charBuffer.length);\r
45         \r
46         logln("Segment 1 (" + segment1.length + " bytes) " +\r
47                 "decompressed into " + count1  + " chars");\r
48         logln("Bytes consumed: " + bytesRead[0]);\r
49 \r
50         logln("Got chars: ");\r
51         logln(charBuffer, 0, count1);\r
52         s.append(charBuffer, 0, count1);\r
53 \r
54         count2 = myDecompressor.decompress(segment2, 0, segment2.length,\r
55                                            bytesRead,\r
56                                            charBuffer, count1, \r
57                                            charBuffer.length);\r
58         \r
59         logln("Segment 2 (" + segment2.length + " bytes) " +\r
60                 "decompressed into " + count2  + " chars");\r
61         logln("Bytes consumed: " + bytesRead[0]);\r
62 \r
63         logln("Got chars: ");\r
64         logln(charBuffer, count1, count2);\r
65         \r
66         s.append(charBuffer, count1, count2);\r
67 \r
68         logln("Result: ");\r
69         logln(charBuffer, 0, count1 + count2);\r
70         logln("====================");\r
71 \r
72         return s.toString();\r
73     }\r
74 \r
75 \r
76     public void testDecompression() throws Exception {\r
77         String result;\r
78 \r
79         // compressed segment breaking on a define window sequence\r
80         /*                   B     o     o     t     h     SD1  */\r
81         byte [] segment1 = { 0x42, 0x6f, 0x6f, 0x74, 0x68, 0x19 };\r
82 \r
83         // continuation\r
84         /*                   IDX   ,           S     .          */\r
85         byte [] segment2 = { 0x01, 0x2c, 0x20, 0x53, 0x2e };\r
86         \r
87         result = decompressTest(segment1, segment2);\r
88         if(! result.equals("Booth, S.")) {\r
89             errln("Decompression test failed");\r
90             return;\r
91         }\r
92 \r
93         // compressed segment breaking on a quote unicode sequence\r
94         /*                   B     o     o     t     SQU        */\r
95         byte [] segment3 = { 0x42, 0x6f, 0x6f, 0x74, 0x0e, 0x00 };\r
96 \r
97         // continuation\r
98         /*                   h     ,           S     .          */\r
99         byte [] segment4 = { 0x68, 0x2c, 0x20, 0x53, 0x2e };\r
100 \r
101         result = decompressTest(segment3, segment4);\r
102         if(! result.equals("Booth, S.")) {\r
103             errln("Decompression test failed");\r
104             return;\r
105         }\r
106 \r
107 \r
108         // compressed segment breaking on a quote unicode sequence\r
109         /*                   SCU   UQU                         */\r
110         byte [] segment5 = { 0x0f, (byte)0xf0, 0x00 };\r
111 \r
112         // continuation\r
113         /*                   B                                 */\r
114         byte [] segment6 = { 0x42 };\r
115 \r
116         result = decompressTest(segment5, segment6);\r
117         if(! result.equals("B")) {\r
118             errln("Decompression test failed");\r
119             return;\r
120         }\r
121     }\r
122 \r
123 }\r