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