]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/compression/DecompressionTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / compression / DecompressionTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, 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.dev.test.TestFmwk;\r
10 import com.ibm.icu.text.UnicodeDecompressor;\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     /* Testing the method\r
124      *      public int decompress(*** \r
125      */\r
126     public void TestDecompress(){\r
127         char[] charBufferBlank = {};\r
128         char[] charBuffer1 = {'a'};\r
129         char[] charValid = {'d','u','m','m','y'};\r
130         \r
131         // Test when "if(charBuffer.length < 2 || (charBufferLimit - charBufferStart) < 2)" is true\r
132         //      The following tests when "charBuffer.length < 2"\r
133         UnicodeDecompressor ud = new UnicodeDecompressor();\r
134         try{\r
135             ud.decompress(null, 0, 0, null, null, 4, 0);\r
136             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
137         } catch(Exception e){}\r
138         \r
139         try{\r
140             ud.decompress(null, 0, 0, null, charBufferBlank, 4, 0);\r
141             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
142         } catch(Exception e){}\r
143         \r
144         try{\r
145             ud.decompress(null, 0, 0, null, charBuffer1, 4, 0);\r
146             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
147         } catch(Exception e){}\r
148         \r
149         //      The following tests when "(charBufferLimit - charBufferStart) < 2"\r
150         try{\r
151             ud.decompress(null, 0, 0, null, charValid, 0, 0);\r
152             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
153         } catch(Exception e){}\r
154         \r
155         try{\r
156             ud.decompress(null, 0, 0, null, charValid, 1, 0);\r
157             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
158         } catch(Exception e){}\r
159         \r
160         try{\r
161             ud.decompress(null, 0, 0, null, charValid, 1, 1);\r
162             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
163         } catch(Exception e){}\r
164         \r
165         try{\r
166             ud.decompress(null, 0, 0, null, charValid, 0, 1);\r
167             errln("UnicodeDecompressor.decompress was suppose to return an exception.");\r
168         } catch(Exception e){}\r
169         \r
170         try{\r
171             ud = new UnicodeDecompressor();\r
172             byte[] b = {\r
173                     (byte) 0x80, (byte) 0x81, (byte) 0x82, (byte) 0x83, (byte) 0x84, \r
174                     (byte) 0x85, (byte) 0x86, (byte) 0x87, (byte) 0x88, (byte) 0x89,\r
175                     (byte) 0x8A, (byte) 0x8B, (byte) 0x8C, (byte) 0x8D, (byte) 0x8E,\r
176                     (byte) 0x8F, (byte) 0x90, (byte) 0x91, (byte) 0x92, (byte) 0x93,\r
177                     (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97, (byte) 0x98,\r
178                     (byte) 0x99, (byte) 0x9A, (byte) 0x9B, (byte) 0x9C, (byte) 0x9D,\r
179                     (byte) 0x9E, (byte) 0x9F, (byte) 0xA0, (byte) 0xA1, (byte) 0xA2,\r
180                     (byte) 0xA3, (byte) 0xA4, (byte) 0xA5, (byte) 0xA6, (byte) 0xA7,\r
181                     (byte) 0xA8, (byte) 0xA9, (byte) 0xAA, (byte) 0xAB, (byte) 0xAC,\r
182                     (byte) 0xAD, (byte) 0xAE, (byte) 0xAF, (byte) 0xB0, (byte) 0xB1,\r
183                     (byte) 0xB2, (byte) 0xB3, (byte) 0xB4, (byte) 0xB5, (byte) 0xB6,\r
184                     (byte) 0xB7, (byte) 0xB8, (byte) 0xB9, (byte) 0xBA, (byte) 0xBB,\r
185                     (byte) 0xBC, (byte) 0xBD, (byte) 0xBE, (byte) 0xBF, (byte) 0xC0,\r
186                     (byte) 0xC1, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4, (byte) 0xC5,\r
187                     (byte) 0xC6, (byte) 0xC7, (byte) 0xC8, (byte) 0xC9, (byte) 0xCA,\r
188                     (byte) 0xCB, (byte) 0xCC, (byte) 0xCD, (byte) 0xCE, (byte) 0xCF,\r
189                     (byte) 0xD0, (byte) 0xD1, (byte) 0xD2, (byte) 0xD3, (byte) 0xD4,\r
190                     (byte) 0xD5, (byte) 0xD6, (byte) 0xD7, (byte) 0xD8, (byte) 0xD9,\r
191                     (byte) 0xDA, (byte) 0xDB, (byte) 0xDC, (byte) 0xDD, (byte) 0xDE,\r
192                     (byte) 0xDF, (byte) 0xE0, (byte) 0xE1, (byte) 0xE2, (byte) 0xE3,\r
193                     (byte) 0xE4, (byte) 0xE5, (byte) 0xE6, (byte) 0xE7, (byte) 0xE8,\r
194                     (byte) 0xE9, (byte) 0xEA, (byte) 0xEB, (byte) 0xEC, (byte) 0xED,\r
195                     (byte) 0xEE, (byte) 0xEF, (byte) 0xF0, (byte) 0xF1, (byte) 0xF2,\r
196                     (byte) 0xF3, (byte) 0xF4, (byte) 0xF5, (byte) 0xF6, (byte) 0xF7,\r
197                     (byte) 0xF8, (byte) 0xF9, (byte) 0xFA, (byte) 0xFB, (byte) 0xFC,\r
198                     (byte) 0xFD, (byte) 0xFE, (byte) 0xFF,\r
199                     (byte) 0x00, (byte) 0x09, (byte) 0x0A, (byte) 0x0D,\r
200                     (byte) 0x20, (byte) 0x21, (byte) 0x22, (byte) 0x23, (byte) 0x24,\r
201                     (byte) 0x25, (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,\r
202                     (byte) 0x2A, (byte) 0x2B, (byte) 0x2C, (byte) 0x2D, (byte) 0x2E,\r
203                     (byte) 0x2F, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33,\r
204                     (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37, (byte) 0x38,\r
205                     (byte) 0x39, (byte) 0x3A, (byte) 0x3B, (byte) 0x3C, (byte) 0x3D,\r
206                     (byte) 0x3E, (byte) 0x3F, (byte) 0x40, (byte) 0x41, (byte) 0x42,\r
207                     (byte) 0x43, (byte) 0x44, (byte) 0x45, (byte) 0x46, (byte) 0x47,\r
208                     (byte) 0x48, (byte) 0x49, (byte) 0x4A, (byte) 0x4B, (byte) 0x4C,\r
209                     (byte) 0x4D, (byte) 0x4E, (byte) 0x4F, (byte) 0x50, (byte) 0x51,\r
210                     (byte) 0x52, (byte) 0x53, (byte) 0x54, (byte) 0x55, (byte) 0x56,\r
211                     (byte) 0x57, (byte) 0x58, (byte) 0x59, (byte) 0x5A, (byte) 0x5B,\r
212                     (byte) 0x5C, (byte) 0x5D, (byte) 0x5E, (byte) 0x5F, (byte) 0x60,\r
213                     (byte) 0x61, (byte) 0x62, (byte) 0x63, (byte) 0x64, (byte) 0x65,\r
214                     (byte) 0x66, (byte) 0x67, (byte) 0x68, (byte) 0x69, (byte) 0x6A,\r
215                     (byte) 0x6B, (byte) 0x6C, (byte) 0x6D, (byte) 0x6E, (byte) 0x6F,\r
216                     (byte) 0x70, (byte) 0x71, (byte) 0x72, (byte) 0x73, (byte) 0x74,\r
217                     (byte) 0x75, (byte) 0x76, (byte) 0x77, (byte) 0x78, (byte) 0x79,\r
218                     (byte) 0x7A, (byte) 0x7B, (byte) 0x7C, (byte) 0x7D, (byte) 0x7E,\r
219                     (byte) 0x7F,\r
220                     (byte) UnicodeDecompressor.SQUOTEU,\r
221                     (byte) UnicodeDecompressor.SCHANGEU, \r
222                     (byte) UnicodeDecompressor.SQUOTE0, (byte) UnicodeDecompressor.SQUOTE1, (byte) UnicodeDecompressor.SQUOTE2, (byte) UnicodeDecompressor.SQUOTE3,\r
223                     (byte) UnicodeDecompressor.SQUOTE4, (byte) UnicodeDecompressor.SQUOTE5, (byte) UnicodeDecompressor.SQUOTE6, (byte) UnicodeDecompressor.SQUOTE7,\r
224                     (byte) UnicodeDecompressor.SCHANGE0, (byte) UnicodeDecompressor.SCHANGE1, (byte) UnicodeDecompressor.SCHANGE2, (byte) UnicodeDecompressor.SCHANGE3,\r
225                     (byte) UnicodeDecompressor.SCHANGE4, (byte) UnicodeDecompressor.SCHANGE5, (byte) UnicodeDecompressor.SCHANGE6, (byte) UnicodeDecompressor.SCHANGE7,\r
226                     (byte) UnicodeDecompressor.SDEFINE0, (byte) UnicodeDecompressor.SDEFINE1, (byte) UnicodeDecompressor.SDEFINE2, (byte) UnicodeDecompressor.SDEFINE3,\r
227                     (byte) UnicodeDecompressor.SDEFINE4, (byte) UnicodeDecompressor.SDEFINE5, (byte) UnicodeDecompressor.SDEFINE6, (byte) UnicodeDecompressor.SDEFINE7,\r
228                     (byte) UnicodeDecompressor.SDEFINEX, (byte) UnicodeDecompressor.SRESERVED,\r
229                     };\r
230             char[] c = new char[b.length];\r
231             ud.decompress(b, 0, b.length, null, c, 0, c.length);\r
232         } catch(Exception e){\r
233             errln("UnicodeDecompressor.decompress() was not suppose to return an exception.");\r
234         }\r
235     }\r
236 \r
237 }\r