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   * http://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.compressors.lz77support;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import org.junit.jupiter.api.Test;
25  
26  public class ParametersTest {
27  
28      private static Parameters newParameters(final int windowSize) {
29          return Parameters.builder(windowSize).build();
30      }
31  
32      private static Parameters newParameters(final int windowSize, final int minBackReferenceLength, final int maxBackReferenceLength, final int maxOffset,
33              final int maxLiteralLength) {
34          return Parameters.builder(windowSize).withMinBackReferenceLength(minBackReferenceLength).withMaxBackReferenceLength(maxBackReferenceLength)
35                  .withMaxOffset(maxOffset).withMaxLiteralLength(maxLiteralLength).build();
36      }
37  
38      @Test
39      public void testAllParametersUsuallyTakeTheirSpecifiedValues() {
40          final Parameters p = newParameters(256, 4, 5, 6, 7);
41          assertEquals(256, p.getWindowSize());
42          assertEquals(4, p.getMinBackReferenceLength());
43          assertEquals(5, p.getMaxBackReferenceLength());
44          assertEquals(6, p.getMaxOffset());
45          assertEquals(7, p.getMaxLiteralLength());
46      }
47  
48      @Test
49      public void testDefaultConstructor() {
50          final Parameters p = newParameters(128);
51          assertEquals(128, p.getWindowSize());
52          assertEquals(3, p.getMinBackReferenceLength());
53          assertEquals(127, p.getMaxBackReferenceLength());
54          assertEquals(127, p.getMaxOffset());
55          assertEquals(128, p.getMaxLiteralLength());
56      }
57  
58      @Test
59      public void testMaxBackReferenceLengthIsMinBackReferenceLengthIfBothAreEqual() {
60          final Parameters p = newParameters(128, 2, 3, 4, 5);
61          assertEquals(3, p.getMaxBackReferenceLength());
62      }
63  
64      @Test
65      public void testMaxBackReferenceLengthIsMinBackReferenceLengthWhenSmallerThanMinBackReferenceLength() {
66          final Parameters p = newParameters(128, 2, 2, 4, 5);
67          assertEquals(3, p.getMaxBackReferenceLength());
68      }
69  
70      @Test
71      public void testMaxBackReferenceLengthIsMinBackReferenceLengthWhenSmallerThanMinBackReferenceLengthReversedInvocationOrder() {
72          final Parameters p = Parameters.builder(128).withMaxBackReferenceLength(2).withMinBackReferenceLength(2).withMaxOffset(4).withMaxLiteralLength(5)
73                  .build();
74          assertEquals(3, p.getMaxBackReferenceLength());
75      }
76  
77      @Test
78      public void testMaxLiteralLengthIsWindowSizeIfSetTo0() {
79          final Parameters p = newParameters(128, 2, 3, 4, 0);
80          assertEquals(128, p.getMaxLiteralLength());
81      }
82  
83      @Test
84      public void testMaxLiteralLengthIsWindowSizeIfSetToANegativeValue() {
85          final Parameters p = newParameters(128, 2, 3, 0, -1);
86          assertEquals(128, p.getMaxLiteralLength());
87      }
88  
89      @Test
90      public void testMaxLiteralLengthIsWindowSizeIfSetToAValueTooBigToHoldInSlidingWindow() {
91          final Parameters p = newParameters(128, 2, 3, 0, 259);
92          assertEquals(128, p.getMaxLiteralLength());
93      }
94  
95      @Test
96      public void testMaxOffsetIsWindowSizeMinus1IfBiggerThanWindowSize() {
97          final Parameters p = newParameters(128, 2, 3, 129, 5);
98          assertEquals(127, p.getMaxOffset());
99      }
100 
101     @Test
102     public void testMaxOffsetIsWindowSizeMinus1IfSetTo0() {
103         final Parameters p = newParameters(128, 2, 3, 0, 5);
104         assertEquals(127, p.getMaxOffset());
105     }
106 
107     @Test
108     public void testMaxOffsetIsWindowSizeMinus1IfSetToANegativeValue() {
109         final Parameters p = newParameters(128, 2, 3, -1, 5);
110         assertEquals(127, p.getMaxOffset());
111     }
112 
113     @Test
114     public void testMinBackReferenceLengthIsAtLeastThree() {
115         final Parameters p = newParameters(128, 2, 3, 4, 5);
116         assertEquals(3, p.getMinBackReferenceLength());
117     }
118 
119     @Test
120     public void testWindowSizeMustBeAPowerOfTwo() {
121         assertThrows(IllegalArgumentException.class, () -> newParameters(100, 200, 300, 400, 500));
122     }
123 
124     @Test
125     public void testWindowSizeMustNotBeSmallerThanMinBackReferenceLength() {
126         assertThrows(IllegalArgumentException.class, () -> newParameters(128, 200, 300, 400, 500));
127     }
128 }