1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.harmony.pack200;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.stream.Stream;
26
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.Arguments;
30 import org.junit.jupiter.params.provider.MethodSource;
31
32 public class PopulationCodecTest {
33
34 static Stream<Arguments> populationCodec() {
35 return Stream.of(Arguments.of(new byte[] { 4, 5, 6, 4, 2, 1, 3, 0, 7 }, new long[] { 5, 4, 6, 7 }, Codec.BYTE1),
36
37
38
39 Arguments.of(new byte[] { 4 * 2, 4 * 2 - 1, 6 * 2, 4 * 2, 2 * 2, 1 * 2, 3 * 2, 0, 7 * 2 }, new long[] { -4, 4, 6, 7 }, Codec.SIGNED5),
40 Arguments.of(new byte[] { 4 * 2 - 1, 4 * 2, 6 * 2, 4 * 2, 2 * 2, 1 * 2, 3 * 2, 0, 7 * 2 }, new long[] { 4, -4, 6, 7 }, Codec.SIGNED5),
41 Arguments.of(new byte[] { 1, 1, 1 }, new long[] { 1 }, Codec.BYTE1), Arguments.of(new byte[] { 2, 2, 1 }, new long[] { 2 }, Codec.BYTE1),
42 Arguments.of(new byte[] { 1, 1, 2 }, new long[] { -1 }, Codec.SIGNED5),
43 Arguments.of(new byte[] { 2, 2, 0, 1, 3 }, new long[] { 3, 2 }, Codec.BYTE1),
44 Arguments.of(new byte[] { 1, 2, 3, 4, 4, 2, 3, 4, 0, 1 }, new long[] { 2, 3, 4, 1 }, Codec.BYTE1),
45 Arguments.of(new byte[] { 3, 2, 1, 4, 4, 2, 3, 4, 0, 1 }, new long[] { 2, 1, 4, 1 }, Codec.BYTE1),
46 Arguments.of(new byte[] { 3, 2, 1, 4, 1, 2, 3, 4, 0, 1 }, new long[] { 2, 1, 4, 1 }, Codec.BYTE1));
47 }
48
49 @Test
50 public void testEncodeSingleValue() {
51 assertThrows(Pack200Exception.class, () -> new PopulationCodec(Codec.SIGNED5, Codec.SIGNED5, Codec.UDELTA5).encode(5),
52 "Should not allow a single value to be encoded as we don't know which codec to use");
53 assertThrows(Pack200Exception.class, () -> new PopulationCodec(Codec.SIGNED5, Codec.SIGNED5, Codec.UDELTA5).encode(5, 8),
54 "Should not allow a single value to be encoded as we don't know which codec to use");
55 }
56
57 @ParameterizedTest
58 @MethodSource("populationCodec")
59 public void testPopulationCodec(final byte[] data, final long[] expectedResult, final Codec codec) throws IOException, Pack200Exception {
60 try (InputStream in = new ByteArrayInputStream(data)) {
61 final int[] result = new PopulationCodec(codec, codec, codec).decodeInts(expectedResult.length, in);
62 assertEquals(expectedResult.length, result.length);
63 for (int i = 0; i < expectedResult.length; i++) {
64 assertEquals(expectedResult[i], result[i]);
65 }
66 assertEquals(0, in.available());
67 }
68 }
69
70 }