01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.pack200.tests;
18:
19: import java.io.ByteArrayInputStream;
20: import java.io.IOException;
21: import java.io.InputStream;
22:
23: import junit.framework.TestCase;
24:
25: import org.apache.harmony.pack200.Codec;
26: import org.apache.harmony.pack200.Pack200Exception;
27: import org.apache.harmony.pack200.PopulationCodec;
28:
29: public class PopulationCodecTest extends TestCase {
30: public void testPopulationCodec() throws IOException,
31: Pack200Exception {
32: checkDecode(new byte[] { 4, 5, 6, 4, 2, 1, 3, 0, 7 },
33: new long[] { 5, 4, 6, 7 }, Codec.BYTE1);
34: // Codec.SIGNED5 can be trivial for small n, because the encoding is 2n if even, 2n-1 if odd
35: // Therefore they're left here to explain what the values are :-)
36: checkDecode(new byte[] { 4 * 2, 4 * 2 - 1, 6 * 2, 4 * 2, 2 * 2,
37: 1 * 2, 3 * 2, 0, 7 * 2 }, new long[] { -4, 4, 6, 7 },
38: Codec.SIGNED5);
39: checkDecode(new byte[] { 4 * 2 - 1, 4 * 2, 6 * 2, 4 * 2, 2 * 2,
40: 1 * 2, 3 * 2, 0, 7 * 2 }, new long[] { 4, -4, 6, 7 },
41: Codec.SIGNED5);
42: checkDecode(new byte[] { 1, 1, 1 }, new long[] { 1 },
43: Codec.BYTE1);
44: checkDecode(new byte[] { 2, 2, 1 }, new long[] { 2 },
45: Codec.BYTE1);
46: checkDecode(new byte[] { 1, 1, 2 }, new long[] { -1 },
47: Codec.SIGNED5);
48: checkDecode(new byte[] { 2, 2, 0, 1, 3 }, new long[] { 3, 2 },
49: Codec.BYTE1);
50: checkDecode(new byte[] { 1, 2, 3, 4, 4, 2, 3, 4, 0, 1 },
51: new long[] { 2, 3, 4, 1 }, Codec.BYTE1);
52: checkDecode(new byte[] { 3, 2, 1, 4, 4, 2, 3, 4, 0, 1 },
53: new long[] { 2, 1, 4, 1 }, Codec.BYTE1);
54: checkDecode(new byte[] { 3, 2, 1, 4, 1, 2, 3, 4, 0, 1 },
55: new long[] { 2, 1, 4, 1 }, Codec.BYTE1);
56: }
57:
58: private void checkDecode(byte[] data, long[] expectedResult,
59: Codec codec) throws IOException, Pack200Exception {
60: InputStream in = new ByteArrayInputStream(data);
61:
62: long[] result = new PopulationCodec(codec, codec, codec)
63: .decode(expectedResult.length, in);
64: assertEquals(expectedResult.length, result.length);
65: for (int i = 0; i < expectedResult.length; i++) {
66: assertEquals(expectedResult[i], result[i]);
67: }
68: assertEquals(0, in.available());
69: }
70:
71: }
|