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