View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     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.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   * Tests for BHSDCodec
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              // Test encode-decode with a selection of numbers within the
60              // range of the codec
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          // Test encode-decode with 0
85          assertEquals(0, codec.decode(new ByteArrayInputStream(codec.encode(0, 0)), 0));
86      }
87  
88  }