1/*
2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.dev.test.compression;
8
9import com.ibm.icu.dev.test.TestFmwk;
10import com.ibm.icu.text.UnicodeDecompressor;
11
12public class DecompressionTest extends TestFmwk {
13    public static void main(String[] args) throws Exception {
14        new DecompressionTest().run(args);
15    }
16
17    /** Print out a segment of a character array, if in verbose mode */
18    private void log(char [] chars, int start, int count) {
19        log("|");
20        for(int i = start; i < start + count; ++i) {
21            log(String.valueOf(chars[i]));
22        }
23        log("|");
24    }
25
26    /** Print out a segment of a character array, followed by a newline */
27    private void logln(char [] chars, int start, int count)
28    {
29        log(chars, start, count);
30        logln("");
31    }
32
33    /** Decompress the two segments */
34    private String decompressTest(byte [] segment1, byte [] segment2) {
35        StringBuffer s = new StringBuffer();
36        UnicodeDecompressor myDecompressor = new UnicodeDecompressor();
37
38        int [] bytesRead = new int[1];
39        char [] charBuffer = new char [2*(segment1.length + segment2.length)];
40        int count1 = 0, count2 = 0;
41
42        count1 = myDecompressor.decompress(segment1, 0, segment1.length,
43                                           bytesRead,
44                                           charBuffer, 0, charBuffer.length);
45
46        logln("Segment 1 (" + segment1.length + " bytes) " +
47                "decompressed into " + count1  + " chars");
48        logln("Bytes consumed: " + bytesRead[0]);
49
50        logln("Got chars: ");
51        logln(charBuffer, 0, count1);
52        s.append(charBuffer, 0, count1);
53
54        count2 = myDecompressor.decompress(segment2, 0, segment2.length,
55                                           bytesRead,
56                                           charBuffer, count1,
57                                           charBuffer.length);
58
59        logln("Segment 2 (" + segment2.length + " bytes) " +
60                "decompressed into " + count2  + " chars");
61        logln("Bytes consumed: " + bytesRead[0]);
62
63        logln("Got chars: ");
64        logln(charBuffer, count1, count2);
65
66        s.append(charBuffer, count1, count2);
67
68        logln("Result: ");
69        logln(charBuffer, 0, count1 + count2);
70        logln("====================");
71
72        return s.toString();
73    }
74
75
76    public void TestDecompression() throws Exception {
77        String result;
78
79        // compressed segment breaking on a define window sequence
80        /*                   B     o     o     t     h     SD1  */
81        byte [] segment1 = { 0x42, 0x6f, 0x6f, 0x74, 0x68, 0x19 };
82
83        // continuation
84        /*                   IDX   ,           S     .          */
85        byte [] segment2 = { 0x01, 0x2c, 0x20, 0x53, 0x2e };
86
87        result = decompressTest(segment1, segment2);
88        if(! result.equals("Booth, S.")) {
89            errln("Decompression test failed");
90            return;
91        }
92
93        // compressed segment breaking on a quote unicode sequence
94        /*                   B     o     o     t     SQU        */
95        byte [] segment3 = { 0x42, 0x6f, 0x6f, 0x74, 0x0e, 0x00 };
96
97        // continuation
98        /*                   h     ,           S     .          */
99        byte [] segment4 = { 0x68, 0x2c, 0x20, 0x53, 0x2e };
100
101        result = decompressTest(segment3, segment4);
102        if(! result.equals("Booth, S.")) {
103            errln("Decompression test failed");
104            return;
105        }
106
107
108        // compressed segment breaking on a quote unicode sequence
109        /*                   SCU   UQU                         */
110        byte [] segment5 = { 0x0f, (byte)0xf0, 0x00 };
111
112        // continuation
113        /*                   B                                 */
114        byte [] segment6 = { 0x42 };
115
116        result = decompressTest(segment5, segment6);
117        if(! result.equals("B")) {
118            errln("Decompression test failed");
119            return;
120        }
121    }
122
123    /* Testing the method
124     *      public int decompress(***
125     */
126    public void TestDecompress(){
127        char[] charBufferBlank = {};
128        char[] charBuffer1 = {'a'};
129        char[] charValid = {'d','u','m','m','y'};
130
131        // Test when "if(charBuffer.length < 2 || (charBufferLimit - charBufferStart) < 2)" is true
132        //      The following tests when "charBuffer.length < 2"
133        UnicodeDecompressor ud = new UnicodeDecompressor();
134        try{
135            ud.decompress(null, 0, 0, null, null, 4, 0);
136            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
137        } catch(Exception e){}
138
139        try{
140            ud.decompress(null, 0, 0, null, charBufferBlank, 4, 0);
141            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
142        } catch(Exception e){}
143
144        try{
145            ud.decompress(null, 0, 0, null, charBuffer1, 4, 0);
146            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
147        } catch(Exception e){}
148
149        //      The following tests when "(charBufferLimit - charBufferStart) < 2"
150        try{
151            ud.decompress(null, 0, 0, null, charValid, 0, 0);
152            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
153        } catch(Exception e){}
154
155        try{
156            ud.decompress(null, 0, 0, null, charValid, 1, 0);
157            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
158        } catch(Exception e){}
159
160        try{
161            ud.decompress(null, 0, 0, null, charValid, 1, 1);
162            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
163        } catch(Exception e){}
164
165        try{
166            ud.decompress(null, 0, 0, null, charValid, 0, 1);
167            errln("UnicodeDecompressor.decompress was suppose to return an exception.");
168        } catch(Exception e){}
169
170        try{
171            ud = new UnicodeDecompressor();
172            byte[] b = {
173                    (byte) 0x80, (byte) 0x81, (byte) 0x82, (byte) 0x83, (byte) 0x84,
174                    (byte) 0x85, (byte) 0x86, (byte) 0x87, (byte) 0x88, (byte) 0x89,
175                    (byte) 0x8A, (byte) 0x8B, (byte) 0x8C, (byte) 0x8D, (byte) 0x8E,
176                    (byte) 0x8F, (byte) 0x90, (byte) 0x91, (byte) 0x92, (byte) 0x93,
177                    (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97, (byte) 0x98,
178                    (byte) 0x99, (byte) 0x9A, (byte) 0x9B, (byte) 0x9C, (byte) 0x9D,
179                    (byte) 0x9E, (byte) 0x9F, (byte) 0xA0, (byte) 0xA1, (byte) 0xA2,
180                    (byte) 0xA3, (byte) 0xA4, (byte) 0xA5, (byte) 0xA6, (byte) 0xA7,
181                    (byte) 0xA8, (byte) 0xA9, (byte) 0xAA, (byte) 0xAB, (byte) 0xAC,
182                    (byte) 0xAD, (byte) 0xAE, (byte) 0xAF, (byte) 0xB0, (byte) 0xB1,
183                    (byte) 0xB2, (byte) 0xB3, (byte) 0xB4, (byte) 0xB5, (byte) 0xB6,
184                    (byte) 0xB7, (byte) 0xB8, (byte) 0xB9, (byte) 0xBA, (byte) 0xBB,
185                    (byte) 0xBC, (byte) 0xBD, (byte) 0xBE, (byte) 0xBF, (byte) 0xC0,
186                    (byte) 0xC1, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4, (byte) 0xC5,
187                    (byte) 0xC6, (byte) 0xC7, (byte) 0xC8, (byte) 0xC9, (byte) 0xCA,
188                    (byte) 0xCB, (byte) 0xCC, (byte) 0xCD, (byte) 0xCE, (byte) 0xCF,
189                    (byte) 0xD0, (byte) 0xD1, (byte) 0xD2, (byte) 0xD3, (byte) 0xD4,
190                    (byte) 0xD5, (byte) 0xD6, (byte) 0xD7, (byte) 0xD8, (byte) 0xD9,
191                    (byte) 0xDA, (byte) 0xDB, (byte) 0xDC, (byte) 0xDD, (byte) 0xDE,
192                    (byte) 0xDF, (byte) 0xE0, (byte) 0xE1, (byte) 0xE2, (byte) 0xE3,
193                    (byte) 0xE4, (byte) 0xE5, (byte) 0xE6, (byte) 0xE7, (byte) 0xE8,
194                    (byte) 0xE9, (byte) 0xEA, (byte) 0xEB, (byte) 0xEC, (byte) 0xED,
195                    (byte) 0xEE, (byte) 0xEF, (byte) 0xF0, (byte) 0xF1, (byte) 0xF2,
196                    (byte) 0xF3, (byte) 0xF4, (byte) 0xF5, (byte) 0xF6, (byte) 0xF7,
197                    (byte) 0xF8, (byte) 0xF9, (byte) 0xFA, (byte) 0xFB, (byte) 0xFC,
198                    (byte) 0xFD, (byte) 0xFE, (byte) 0xFF,
199                    (byte) 0x00, (byte) 0x09, (byte) 0x0A, (byte) 0x0D,
200                    (byte) 0x20, (byte) 0x21, (byte) 0x22, (byte) 0x23, (byte) 0x24,
201                    (byte) 0x25, (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
202                    (byte) 0x2A, (byte) 0x2B, (byte) 0x2C, (byte) 0x2D, (byte) 0x2E,
203                    (byte) 0x2F, (byte) 0x30, (byte) 0x31, (byte) 0x32, (byte) 0x33,
204                    (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37, (byte) 0x38,
205                    (byte) 0x39, (byte) 0x3A, (byte) 0x3B, (byte) 0x3C, (byte) 0x3D,
206                    (byte) 0x3E, (byte) 0x3F, (byte) 0x40, (byte) 0x41, (byte) 0x42,
207                    (byte) 0x43, (byte) 0x44, (byte) 0x45, (byte) 0x46, (byte) 0x47,
208                    (byte) 0x48, (byte) 0x49, (byte) 0x4A, (byte) 0x4B, (byte) 0x4C,
209                    (byte) 0x4D, (byte) 0x4E, (byte) 0x4F, (byte) 0x50, (byte) 0x51,
210                    (byte) 0x52, (byte) 0x53, (byte) 0x54, (byte) 0x55, (byte) 0x56,
211                    (byte) 0x57, (byte) 0x58, (byte) 0x59, (byte) 0x5A, (byte) 0x5B,
212                    (byte) 0x5C, (byte) 0x5D, (byte) 0x5E, (byte) 0x5F, (byte) 0x60,
213                    (byte) 0x61, (byte) 0x62, (byte) 0x63, (byte) 0x64, (byte) 0x65,
214                    (byte) 0x66, (byte) 0x67, (byte) 0x68, (byte) 0x69, (byte) 0x6A,
215                    (byte) 0x6B, (byte) 0x6C, (byte) 0x6D, (byte) 0x6E, (byte) 0x6F,
216                    (byte) 0x70, (byte) 0x71, (byte) 0x72, (byte) 0x73, (byte) 0x74,
217                    (byte) 0x75, (byte) 0x76, (byte) 0x77, (byte) 0x78, (byte) 0x79,
218                    (byte) 0x7A, (byte) 0x7B, (byte) 0x7C, (byte) 0x7D, (byte) 0x7E,
219                    (byte) 0x7F,
220                    (byte) UnicodeDecompressor.SQUOTEU,
221                    (byte) UnicodeDecompressor.SCHANGEU,
222                    (byte) UnicodeDecompressor.SQUOTE0, (byte) UnicodeDecompressor.SQUOTE1, (byte) UnicodeDecompressor.SQUOTE2, (byte) UnicodeDecompressor.SQUOTE3,
223                    (byte) UnicodeDecompressor.SQUOTE4, (byte) UnicodeDecompressor.SQUOTE5, (byte) UnicodeDecompressor.SQUOTE6, (byte) UnicodeDecompressor.SQUOTE7,
224                    (byte) UnicodeDecompressor.SCHANGE0, (byte) UnicodeDecompressor.SCHANGE1, (byte) UnicodeDecompressor.SCHANGE2, (byte) UnicodeDecompressor.SCHANGE3,
225                    (byte) UnicodeDecompressor.SCHANGE4, (byte) UnicodeDecompressor.SCHANGE5, (byte) UnicodeDecompressor.SCHANGE6, (byte) UnicodeDecompressor.SCHANGE7,
226                    (byte) UnicodeDecompressor.SDEFINE0, (byte) UnicodeDecompressor.SDEFINE1, (byte) UnicodeDecompressor.SDEFINE2, (byte) UnicodeDecompressor.SDEFINE3,
227                    (byte) UnicodeDecompressor.SDEFINE4, (byte) UnicodeDecompressor.SDEFINE5, (byte) UnicodeDecompressor.SDEFINE6, (byte) UnicodeDecompressor.SDEFINE7,
228                    (byte) UnicodeDecompressor.SDEFINEX, (byte) UnicodeDecompressor.SRESERVED,
229                    };
230            char[] c = new char[b.length];
231            ud.decompress(b, 0, b.length, null, c, 0, c.length);
232        } catch(Exception e){
233            errln("UnicodeDecompressor.decompress() was not suppose to return an exception.");
234        }
235    }
236
237}
238