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.harmony.pack200.tests;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.EOFException;
021: import java.io.IOException;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.harmony.pack200.BHSDCodec;
026: import org.apache.harmony.pack200.Codec;
027: import org.apache.harmony.pack200.Pack200Exception;
028: import org.apache.harmony.pack200.RunCodec;
029:
030: /**
031: *
032: */
033: public class CodecTest extends TestCase {
034:
035: public void testInvalidCodings() {
036: for (int i = 0; i < 256; i++) {
037: try {
038: new BHSDCodec(1, i);
039: fail("b=1 -> h=256");
040: } catch (IllegalArgumentException e) {
041: assertTrue(true);
042: }
043: }
044: for (int i = 1; i <= 5; i++) {
045: try {
046: new BHSDCodec(i, 256);
047: if (i == 5)
048: fail("h=256 -> b!=5");
049: } catch (IllegalArgumentException e) {
050: assertTrue(true);
051: }
052: }
053:
054: }
055:
056: public void testCodecToString() {
057: assertEquals("(1,256)", Codec.BYTE1.toString());
058: assertEquals("(3,128)", Codec.CHAR3.toString());
059: assertEquals("(5,4)", Codec.BCI5.toString());
060: assertEquals("(5,4,2)", Codec.BRANCH5.toString());
061: assertEquals("(5,64)", Codec.UNSIGNED5.toString());
062: assertEquals("(5,64,1)", Codec.SIGNED5.toString());
063: assertEquals("(5,64,0,1)", Codec.UDELTA5.toString());
064: assertEquals("(5,64,1,1)", Codec.DELTA5.toString());
065: assertEquals("(5,64,2,1)", Codec.MDELTA5.toString());
066: assertEquals("(5,64)", Codec.UNSIGNED5.toString());
067: assertEquals("(5,64,1)", Codec.SIGNED5.toString());
068: assertEquals("(5,64,1,1)", Codec.DELTA5.toString());
069: assertEquals("(5,64,2,1)", Codec.MDELTA5.toString());
070: }
071:
072: public void testByte1() throws Exception {
073: for (int i = 0; i < 255; i++)
074: decode(Codec.BYTE1, new byte[] { (byte) i }, i, 0);
075: }
076:
077: public void testByte1Delta() throws Exception {
078: Codec BYTE1D = new BHSDCodec(1, 256, 0, 1);
079: long last = 0;
080: for (int i = 1; i < 255; i++)
081: last = decode(BYTE1D, new byte[] { (byte) 1 }, i, last);
082: }
083:
084: public void testByte1DeltaException() throws Exception {
085: Codec BYTE1D = new BHSDCodec(1, 256, 0, 1);
086: try {
087: BYTE1D.decode(new ByteArrayInputStream(
088: new byte[] { (byte) 1 }));
089: fail("Decoding with a delta stream and not passing a last value should throw exception");
090: } catch (Pack200Exception e) {
091: assertTrue(true);
092: }
093: }
094:
095: public void testByte1Signed() throws Exception {
096: Codec BYTE1S2 = new BHSDCodec(1, 256, 2);
097: decode(BYTE1S2, new byte[] { 0 }, 0, 0);
098: decode(BYTE1S2, new byte[] { 1 }, 1, 0);
099: decode(BYTE1S2, new byte[] { 2 }, 2, 0);
100: decode(BYTE1S2, new byte[] { 3 }, -1, 0);
101: decode(BYTE1S2, new byte[] { 4 }, 3, 0);
102: decode(BYTE1S2, new byte[] { 5 }, 4, 0);
103: decode(BYTE1S2, new byte[] { 6 }, 5, 0);
104: decode(BYTE1S2, new byte[] { 7 }, -2, 0);
105: decode(BYTE1S2, new byte[] { 8 }, 6, 0);
106: decode(BYTE1S2, new byte[] { 9 }, 7, 0);
107: decode(BYTE1S2, new byte[] { 10 }, 8, 0);
108: decode(BYTE1S2, new byte[] { 11 }, -3, 0);
109: }
110:
111: public void testCardinality() throws Exception {
112: BHSDCodec byte1 = Codec.BYTE1;
113: assertEquals(256, byte1.cardinality());
114: assertEquals(0, byte1.smallest());
115: assertEquals(255, byte1.largest());
116: assertFalse(byte1.encodes(-257));
117: assertFalse(byte1.encodes(-256));
118: assertFalse(byte1.encodes(-255));
119: assertFalse(byte1.encodes(-129));
120: assertFalse(byte1.encodes(-128));
121: assertFalse(byte1.encodes(-127));
122: assertFalse(byte1.encodes(-1));
123: assertTrue(byte1.encodes(0));
124: assertTrue(byte1.encodes(1));
125: assertTrue(byte1.encodes(255));
126: assertFalse(byte1.encodes(256));
127: BHSDCodec byte1s = new BHSDCodec(1, 256, 1);
128: assertEquals(256, byte1s.cardinality());
129: assertEquals(-128, byte1s.smallest());
130: assertEquals(127, byte1s.largest());
131: assertFalse(byte1s.encodes(-257));
132: assertFalse(byte1s.encodes(-256));
133: assertFalse(byte1s.encodes(-255));
134: assertFalse(byte1s.encodes(-129));
135: assertTrue(byte1s.encodes(-128));
136: assertTrue(byte1s.encodes(-127));
137: assertTrue(byte1s.encodes(-1));
138: assertTrue(byte1s.encodes(0));
139: assertTrue(byte1s.encodes(1));
140: assertTrue(byte1s.encodes(127));
141: assertFalse(byte1s.encodes(128));
142: assertFalse(byte1s.encodes(129));
143: assertFalse(byte1s.encodes(255));
144: assertFalse(byte1s.encodes(256));
145: BHSDCodec byte2s = new BHSDCodec(1, 256, 2);
146: assertEquals(256, byte2s.cardinality());
147: assertEquals(-64, byte2s.smallest());
148: assertEquals(191, byte2s.largest());
149: assertFalse(byte2s.encodes(-257));
150: assertFalse(byte2s.encodes(-256));
151: assertFalse(byte2s.encodes(-255));
152: assertFalse(byte2s.encodes(-129));
153: assertFalse(byte2s.encodes(-128));
154: assertFalse(byte2s.encodes(-127));
155: assertFalse(byte2s.encodes(-65));
156: assertTrue(byte2s.encodes(-64));
157: assertTrue(byte2s.encodes(-64));
158: assertTrue(byte2s.encodes(-1));
159: assertTrue(byte2s.encodes(0));
160: assertTrue(byte2s.encodes(1));
161: assertTrue(byte2s.encodes(127));
162: assertTrue(byte2s.encodes(128));
163: assertTrue(byte2s.encodes(191));
164: assertFalse(byte2s.encodes(192));
165: assertFalse(byte2s.encodes(256));
166: }
167:
168: public void testRunCodec() throws Exception {
169: RunCodec runCodec = new RunCodec(1, Codec.UNSIGNED5,
170: Codec.BYTE1);
171: ByteArrayInputStream bais = new ByteArrayInputStream(
172: new byte[] { (byte) 192, 0, (byte) 192, 0 });
173: assertEquals(192, runCodec.decode(bais));
174: assertEquals(192, runCodec.decode(bais));
175: assertEquals(0, runCodec.decode(bais));
176: assertEquals(0, bais.available());
177: runCodec = new RunCodec(1, Codec.BYTE1, Codec.UNSIGNED5);
178: bais = new ByteArrayInputStream(new byte[] { (byte) 192, 0,
179: (byte) 192, 0 });
180: assertEquals(192, runCodec.decode(bais));
181: assertEquals(0, runCodec.decode(bais));
182: assertEquals(192, runCodec.decode(bais));
183: assertEquals(0, bais.available());
184: }
185:
186: public void testUnsigned5() throws Exception {
187: decode(Codec.UNSIGNED5, new byte[] { 1 }, 1, 0);
188: decode(Codec.UNSIGNED5, new byte[] { (byte) 191 }, 191, 0);
189: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, 0 }, 192, 0);
190: decode(Codec.UNSIGNED5, new byte[] { (byte) 193, 0 }, 193, 0);
191: decode(Codec.UNSIGNED5, new byte[] { (byte) 255, 0 }, 255, 0);
192: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, 1 }, 256, 0);
193: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, 5 }, 512, 0);
194: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, 13 }, 1024, 0);
195: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, 29 }, 2048, 0);
196: decode(Codec.UNSIGNED5, new byte[] { (byte) 255, (byte) 191 },
197: 12479, 0);
198:
199: decode(Codec.UNSIGNED5,
200: new byte[] { (byte) 192, (byte) 192, 0 }, 12480, 0);
201: decode(Codec.UNSIGNED5, new byte[] { (byte) 255, (byte) 255,
202: (byte) 191 }, 798911, 0);
203: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, (byte) 192,
204: (byte) 192, 0 }, 798912, 0);
205: decode(Codec.UNSIGNED5, new byte[] { (byte) 255, (byte) 255,
206: (byte) 255, (byte) 191 }, 51130559, 0);
207: decode(Codec.UNSIGNED5, new byte[] { (byte) 192, (byte) 192,
208: (byte) 192, (byte) 192, 0 }, 51130560, 0);
209: decode(Codec.UNSIGNED5, new byte[] { (byte) 255, (byte) 252,
210: (byte) 252, (byte) 252, (byte) 252 }, 0xFFFFFFFFL, 0);
211: decodeFail(Codec.UNSIGNED5, new byte[] { (byte) 192 });
212: decodeFail(Codec.UNSIGNED5,
213: new byte[] { (byte) 192, (byte) 192 });
214: decodeFail(Codec.UNSIGNED5, new byte[] { (byte) 192,
215: (byte) 192, (byte) 192 });
216: decodeFail(Codec.UNSIGNED5, new byte[] { (byte) 192,
217: (byte) 192, (byte) 192, (byte) 192 });
218: }
219:
220: private void decodeFail(final Codec codec, final byte[] data)
221: throws IOException, Pack200Exception {
222: try {
223: decode(codec, data, 0, 0);
224: fail("Should have detected an EOFException");
225: } catch (EOFException e) {
226: assertTrue(true);
227: }
228: }
229:
230: private long decode(final Codec codec, final byte[] data,
231: final long value, final long last) throws IOException,
232: Pack200Exception {
233: final ByteArrayInputStream in = new ByteArrayInputStream(data);
234: assertEquals(value, codec.decode(in, last));
235: assertEquals(-1, in.read());
236: return (value);
237: }
238: }
|