001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: */
017: package org.apache.poi.hdgf;
018:
019: import java.io.ByteArrayInputStream;
020:
021: import junit.framework.TestCase;
022:
023: public class TestHDGFLZW extends TestCase {
024: public static final byte[] testTrailerComp = new byte[] {
025: 123, // *mask bit*
026: -60,
027: 2,
028: -21,
029: -16, // 3 @ 4093
030: 1,
031: 0,
032: 0,
033: -72,
034: -13,
035: -16, // 3 @ 5
036: 78, // *mask bit*
037: -32,
038: -5, // 14 @ 4082
039: 1,
040: 0,
041: 3,
042: -21,
043: -16, // 3 @ 4093
044: 10,
045: 5, // 8 @ 28
046: 4,
047: -21,
048: -16, // 3 @ 4093
049: 21, // *mask bit*
050: 9,
051: -21,
052: -16, // 3 @ 4093
053: 103,
054: -21,
055: -16,
056: 34,
057: -36,
058: -1, // 18 @ 4078
059: 52,
060: 15, // 18 @ 70
061: 70,
062: 15, // 18 @ 88
063: 120, // *mask bit*
064: 88,
065: 15, // 18 @ 106
066: -7,
067: -2, // 17 @ 11
068: -28,
069: -9, // 10 @ 4086
070: -123,
071: 21,
072: 0,
073: 44,
074: -122,
075: 1, // 4 @ 152
076: -4, // *mask bit*
077: 104,
078: 15, // 18 @ 122
079: -24, -13, 40, -98, 32, 78, 102, -67, -1, -2, -30, 64, 40,
080: -67, -113, -73, 116, -98, -85, 2, 66, 123, 9, 109, -85, 2,
081: -89, 14, -56, -69, -83, -79, -34, -3, 120, 110, 75, -9,
082: -10, 20, -6, -25, -12, 22, -21, -16, -12, -81, 67, 1, -128,
083: -70, -21, -16, 84, -21, -16, 70, 0, 23, -21, -16, 76, 47,
084: -40, 79, 1, -44, -21, -16, 32, 3, 18, 12, 17, -43, -68, 17,
085: 16, -8, 21, 22, -1, -21, -16, -84, -1, -35, 79, -9, -10,
086: 96, 0, 46, -21, -16, 44, -39, -41, 79, 1, 119, -13, -16,
087: -106, -13, -16, 84, 0, 125, 26, -21, -16, 68, -38, 79, 1,
088: 17, 10, 0, -97, 50, 10, 0, 0, -42, -108, 15, 118, 31, 0,
089: -3, 29, -21, -16, -100, -25, 79, 1, -18, 97, -36, 76, 16,
090: -21, -16, 86, 0, 36, -5, 1, -5, 79, 63, 1, -124, 98, 0, 0,
091: 28, 3, 20, -34, -3, 125, 33, -21, -16, 100, -4, 79, 1, -92,
092: -91, 16, -22, 24, 19, 41, -21, -16, -44, -59, 16, 108, 100,
093: 0, -21, 0, 71, -105, 18, 39, 85, 17, -3, 79, 1, 95, -108,
094: 113, 0, 0, 104, 3, 18, 49, 49, 17, -1, 64, 85, 1, 0, 114,
095: 0, 0, -93, -36, -21, -16, 100, 31, 0, 0, -40, -21, -16,
096: -92, 66, 127, 85, 1, 98, 119, 0, 0, -48, 79, 18, -3, 50,
097: -17, 1, 67, 85, 1, 81, -127, 0, -41, 0, 14, 6, 4, 17, 63,
098: -63, 17, 68, 85, -65, 1, 30, -120, 0, 0, 42, 79, 18, 68,
099: 126, -21, -16, -76, 69, 85, 1, 102, -119, 72, 37, 0, 97, 33 };
100: public static final byte[] testTrailerDecomp = new byte[] { -60, 2,
101: 0, 0, 0, 1, 0, 0, -72, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102: 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 4,
103: 0, 0, 0, 9, 0, 0, 0, 103, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0,
104: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108: 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -123, 21, 0, 44, -123,
109: 21, 0, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110: 0, 0, 0, 0, 0, 0, 0, 0, 40, -98, 32, 78, 102, -67, -2, -30,
111: 64, 40, -67, -113, -73, 116, -67, -2, -30, 64, 40, 66, 123,
112: 9, 109, -67, -2, -30, 64, 40, -98, 32, 78, 102, -67, -2,
113: -30, 64, 40, -67, -113, -73, 116, -67, -2, -30, 64, -56,
114: -83, -79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115: 120, 110, 75, 1, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0,
116: 0, 0, 22, 0, 0, 0, -12, -81, 67, 1, -128, 0, 0, 0, 84, 0,
117: 0, 0, 70, 0, 23, 0, 0, 0, 76, -40, 79, 1, -44, 0, 0, 0, 32,
118: 0, 0, 0, 84, 0, 23, 0, 0, 0, -68, -40, 79, 1, -8, 0, 0, 0,
119: 32, 0, 0, 0, 84, 0, -1, 0, 0, 0, -84, -1, 79, 1, 0, 0, 0,
120: 0, 0, 0, 0, 0, 96, 0, 46, 0, 0, 0, 44, -39, 79, 1, 119, 1,
121: 0, 0, -106, 1, 0, 0, 84, 0, 26, 0, 0, 0, 68, -38, 79, 1,
122: 17, 3, 0, 0, 50, 10, 0, 0, -42, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124: 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, -100, -25, 79, 1, -18,
125: 97, 0, 0, -106, 0, 0, 0, 86, 0, 36, 0, 0, 0, -12, -5, 79,
126: 1, -124, 98, 0, 0, 28, 0, 0, 0, 84, 0, 0, 0, 0, 0, 0, 0, 0,
127: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, 100, -4, 79,
128: 1, -92, 98, 0, 0, 32, 0, 0, 0, 84, 0, 41, 0, 0, 0, -44, -4,
129: 79, 1, 108, 100, 0, 0, 71, 0, 0, 0, 86, 0, 39, 0, 0, 0, 68,
130: -3, 79, 1, -108, 113, 0, 0, 104, 0, 0, 0, 84, 0, 49, 0, 0,
131: 0, -84, 64, 85, 1, 0, 114, 0, 0, -93, 0, 0, 0, -42, 0, 0,
132: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -40, 0,
133: 0, 0, -92, 66, 85, 1, 98, 119, 0, 0, -48, 1, 0, 0, 84, 0,
134: 50, 0, 0, 0, 20, 67, 85, 1, 81, -127, 0, 0, 14, 6, 0, 0,
135: 84, 0, 63, 0, 0, 0, 100, 68, 85, 1, 30, -120, 0, 0, 42, 1,
136: 0, 0, 84, 0, 68, 0, 0, 0, -76, 69, 85, 1, 102, -119, 0, 0,
137: 42, 1, 0, 0, 84, 0, 0, 0, 0, 0 };
138:
139: public void testFromToInt() throws Exception {
140: byte b255 = -1;
141: assertEquals(255, HDGFLZW.fromByte(b255));
142: assertEquals(-1, HDGFLZW.fromInt(HDGFLZW.fromByte(b255)));
143: assertEquals(-1, HDGFLZW.fromInt(255));
144:
145: byte b11 = 11;
146: assertEquals(11, HDGFLZW.fromByte(b11));
147: assertEquals(11, HDGFLZW.fromInt(HDGFLZW.fromByte(b11)));
148: assertEquals(11, HDGFLZW.fromInt(11));
149:
150: byte b0 = 0;
151: assertEquals(0, HDGFLZW.fromByte(b0));
152: assertEquals(0, HDGFLZW.fromInt(HDGFLZW.fromByte(b0)));
153: assertEquals(0, HDGFLZW.fromInt(0));
154:
155: byte b127 = 127;
156: assertEquals(127, HDGFLZW.fromByte(b127));
157: assertEquals(127, HDGFLZW.fromInt(HDGFLZW.fromByte(b127)));
158: assertEquals(127, HDGFLZW.fromInt(127));
159:
160: byte b128 = -128;
161: assertEquals(128, HDGFLZW.fromByte(b128));
162: assertEquals(-128, HDGFLZW.fromInt(HDGFLZW.fromByte(b128)));
163: assertEquals(-128, HDGFLZW.fromInt(128));
164: }
165:
166: public void testCounts() throws Exception {
167: assertEquals(339, testTrailerComp.length);
168: assertEquals(632, testTrailerDecomp.length);
169:
170: // Decode it using our engine
171: HDGFLZW lzw = new HDGFLZW();
172: byte[] dec = lzw.decode(new ByteArrayInputStream(
173: testTrailerComp));
174:
175: // Check it's of the right size
176: assertEquals(632, dec.length);
177:
178: /*
179: // Encode it again using our engine
180: byte[] comp = lzw.compress(new ByteArrayInputStream(testTrailerDecomp));
181:
182: // Check it's of the right size
183: assertEquals(339, comp.length);
184: */
185: }
186:
187: public void testDecompress() throws Exception {
188: assertEquals(339, testTrailerComp.length);
189: assertEquals(632, testTrailerDecomp.length);
190:
191: // Decode it using our engine
192: HDGFLZW lzw = new HDGFLZW();
193: byte[] dec = lzw.decode(new ByteArrayInputStream(
194: testTrailerComp));
195:
196: // Now check it's the right data
197: assertEquals(632, dec.length);
198: for (int i = 0; i < dec.length; i++) {
199: if (dec[i] != testTrailerDecomp[i])
200: System.err.println(i + "\t" + dec[i] + "\t"
201: + testTrailerDecomp[i]);
202: }
203: }
204:
205: public void DISABLEDtestCompress() throws Exception {
206: assertEquals(339, testTrailerComp.length);
207: assertEquals(632, testTrailerDecomp.length);
208:
209: // Compress it using our engine
210: HDGFLZW lzw = new HDGFLZW();
211: byte[] comp = lzw.compress(new ByteArrayInputStream(
212: testTrailerDecomp));
213:
214: // Now check it's the right data
215: assertEquals(339, comp.length);
216: for (int i = 0; i < comp.length; i++) {
217: if (comp[i] != testTrailerComp[i])
218: System.err.println(i + "\t" + comp[i] + "\t"
219: + testTrailerComp[i]);
220: }
221: }
222: }
|