1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.harmony.pack200;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.EOFException;
26 import java.io.IOException;
27 import java.util.stream.IntStream;
28 import java.util.stream.Stream;
29
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.params.ParameterizedTest;
32 import org.junit.jupiter.params.provider.Arguments;
33 import org.junit.jupiter.params.provider.MethodSource;
34
35
36
37
38 class BHSDCodecTest {
39
40 static Stream<Arguments> encodeDecodeRange() {
41 return IntStream.range(1, 116).mapToObj(Arguments::of);
42 }
43
44 @Test
45 void testDeltaEncodings() throws IOException, Pack200Exception {
46 final Codec c = Codec.UDELTA5;
47 final int[] sequence = { 0, 2, 4, 2, 2, 4 };
48 final byte[] encoded = c.encode(sequence);
49 final int[] decoded = c.decodeInts(6, new ByteArrayInputStream(encoded));
50 for (int i = 0; i < decoded.length; i++) {
51 assertEquals(sequence[i], decoded[i]);
52 }
53 }
54
55 @ParameterizedTest
56 @MethodSource("encodeDecodeRange")
57 void testEncodeDecode(final int i) throws IOException, Pack200Exception {
58 final BHSDCodec codec = (BHSDCodec) CodecEncoding.getCodec(i, null, null);
59
60 if (!codec.isDelta()) {
61
62
63 final long largest = codec.largest();
64 long smallest = codec.isSigned() ? codec.smallest() : 0;
65 if (smallest < Integer.MIN_VALUE) {
66 smallest = Integer.MIN_VALUE;
67 }
68 final long difference = (largest - smallest) / 4;
69 for (long j = smallest; j <= largest; j += difference) {
70 if (j > Integer.MAX_VALUE) {
71 break;
72 }
73 final byte[] encoded = codec.encode((int) j, 0);
74 long decoded = 0;
75 try {
76 decoded = codec.decode(new ByteArrayInputStream(encoded), 0);
77 } catch (final EOFException e) {
78 System.out.println(e);
79 }
80 if (j != decoded) {
81 fail("Failed with codec: " + i + ", " + codec + " expected: " + j + ", got: " + decoded);
82 }
83 }
84 }
85
86
87 assertEquals(0, codec.decode(new ByteArrayInputStream(codec.encode(0, 0)), 0));
88 }
89
90 }