001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.compression;
008:
009: import com.ibm.icu.text.UnicodeDecompressor;
010: import com.ibm.icu.dev.test.TestFmwk;
011:
012: public class DecompressionTest extends TestFmwk {
013: public static void main(String[] args) throws Exception {
014: new DecompressionTest().run(args);
015: }
016:
017: /** Print out a segment of a character array, if in verbose mode */
018: private void log(char[] chars, int start, int count) {
019: log("|");
020: for (int i = start; i < start + count; ++i) {
021: log(String.valueOf(chars[i]));
022: }
023: log("|");
024: }
025:
026: /** Print out a segment of a character array, followed by a newline */
027: private void logln(char[] chars, int start, int count) {
028: log(chars, start, count);
029: logln("");
030: }
031:
032: /** Decompress the two segments */
033: private String decompressTest(byte[] segment1, byte[] segment2) {
034: StringBuffer s = new StringBuffer();
035: UnicodeDecompressor myDecompressor = new UnicodeDecompressor();
036:
037: int[] bytesRead = new int[1];
038: char[] charBuffer = new char[2 * (segment1.length + segment2.length)];
039: int count1 = 0, count2 = 0;
040:
041: count1 = myDecompressor.decompress(segment1, 0,
042: segment1.length, bytesRead, charBuffer, 0,
043: charBuffer.length);
044:
045: logln("Segment 1 (" + segment1.length + " bytes) "
046: + "decompressed into " + count1 + " chars");
047: logln("Bytes consumed: " + bytesRead[0]);
048:
049: logln("Got chars: ");
050: logln(charBuffer, 0, count1);
051: s.append(charBuffer, 0, count1);
052:
053: count2 = myDecompressor.decompress(segment2, 0,
054: segment2.length, bytesRead, charBuffer, count1,
055: charBuffer.length);
056:
057: logln("Segment 2 (" + segment2.length + " bytes) "
058: + "decompressed into " + count2 + " chars");
059: logln("Bytes consumed: " + bytesRead[0]);
060:
061: logln("Got chars: ");
062: logln(charBuffer, count1, count2);
063:
064: s.append(charBuffer, count1, count2);
065:
066: logln("Result: ");
067: logln(charBuffer, 0, count1 + count2);
068: logln("====================");
069:
070: return s.toString();
071: }
072:
073: public void testDecompression() throws Exception {
074: String result;
075:
076: // compressed segment breaking on a define window sequence
077: /* B o o t h SD1 */
078: byte[] segment1 = { 0x42, 0x6f, 0x6f, 0x74, 0x68, 0x19 };
079:
080: // continuation
081: /* IDX , S . */
082: byte[] segment2 = { 0x01, 0x2c, 0x20, 0x53, 0x2e };
083:
084: result = decompressTest(segment1, segment2);
085: if (!result.equals("Booth, S.")) {
086: errln("Decompression test failed");
087: return;
088: }
089:
090: // compressed segment breaking on a quote unicode sequence
091: /* B o o t SQU */
092: byte[] segment3 = { 0x42, 0x6f, 0x6f, 0x74, 0x0e, 0x00 };
093:
094: // continuation
095: /* h , S . */
096: byte[] segment4 = { 0x68, 0x2c, 0x20, 0x53, 0x2e };
097:
098: result = decompressTest(segment3, segment4);
099: if (!result.equals("Booth, S.")) {
100: errln("Decompression test failed");
101: return;
102: }
103:
104: // compressed segment breaking on a quote unicode sequence
105: /* SCU UQU */
106: byte[] segment5 = { 0x0f, (byte) 0xf0, 0x00 };
107:
108: // continuation
109: /* B */
110: byte[] segment6 = { 0x42 };
111:
112: result = decompressTest(segment5, segment6);
113: if (!result.equals("B")) {
114: errln("Decompression test failed");
115: return;
116: }
117: }
118:
119: }
|