View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Tests for BHSDCodec
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              // Test encode-decode with a selection of numbers within the
62              // range of the codec
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          // Test encode-decode with 0
87          assertEquals(0, codec.decode(new ByteArrayInputStream(codec.encode(0, 0)), 0));
88      }
89  
90  }