avoid some ugly border cases
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/f874b8c2 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/f874b8c2 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/f874b8c2 Branch: refs/heads/master Commit: f874b8c258f89b464698f86a562812030c5f45f9 Parents: dbb9d65 Author: Stefan Bodewig <[email protected]> Authored: Mon Jan 9 18:23:00 2017 +0100 Committer: Stefan Bodewig <[email protected]> Committed: Mon Jan 9 18:23:00 2017 +0100 ---------------------------------------------------------------------- .../compressors/lz77support/Parameters.java | 34 +++++++++++--------- .../lz77support/LZ77CompressorTest.java | 9 +++--- .../compressors/lz77support/ParametersTest.java | 20 ++++++------ 3 files changed, 34 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/f874b8c2/src/main/java/org/apache/commons/compress/compressors/lz77support/Parameters.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/compressors/lz77support/Parameters.java b/src/main/java/org/apache/commons/compress/compressors/lz77support/Parameters.java index 012f844..e4f02e1 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lz77support/Parameters.java +++ b/src/main/java/org/apache/commons/compress/compressors/lz77support/Parameters.java @@ -28,7 +28,7 @@ public final class Parameters { /** * Initializes the compressor's parameters with a * <code>minMatchLength</code> of 3 and <code>max*Length</code> - * equal to <code>windowSize</code>. + * equal to <code>windowSize - 1</code>. * * @param windowSize the size of the sliding window - this * determines the maximum offset a back-reference can take. @@ -36,7 +36,7 @@ public final class Parameters { * is smaller than <code>minMatchLength</code>. */ public Parameters(int windowSize) { - this(windowSize, TRUE_MIN_MATCH_LENGTH, windowSize, windowSize, windowSize); + this(windowSize, TRUE_MIN_MATCH_LENGTH, windowSize - 1, windowSize - 1, windowSize); } /** @@ -48,14 +48,18 @@ public final class Parameters { * @param minMatchLength the minimal length of a match found. A * true minimum of 3 is hard-coded inside of this implemention * but bigger lengths can be configured. - * @param maxMatchLength maximal site of a match found. A value - * smaller than <code>minMatchLength</code> is interpreted as - * infinite (actually {@link Integer.MAX_VALUE}). + * @param maxMatchLength maximal length of a match found. A value + * smaller than <code>minMatchLength</code> as well as values + * bigger than <code>windowSize - 1</code> are interpreted as + * <code>windowSize - 1</code>. * @param maxOffset maximal offset of a back-reference. A - * non-positive value is interpreted as <code>windowSize</code>. - * @param maxLiteralLength maximal length of a literal block. Negative - * numbers and 0 as well as values bigger than <code>2 * - * windowSize</code> are interpreted as <code>windowSize</code>. + * non-positive value as well as values bigger than + * <code>windowSize - 1</code> are interpreted as <code>windowSize + * - 1</code>. + * @param maxLiteralLength maximal length of a literal + * block. Negative numbers and 0 as well as values bigger than + * <code>windowSize</code> are interpreted as + * <code>windowSize</code>. * @throws IllegalArgumentException if <code>windowSize</code> is * smaller than <code>minMatchLength</code> or not a power of two. */ @@ -69,12 +73,12 @@ public final class Parameters { throw new IllegalArgumentException("windowSize must be a power of two"); } this.windowSize = windowSize; - this.maxOffset = maxOffset < 1 ? this.windowSize - : Math.min(maxOffset, this.windowSize); - this.maxMatchLength = maxMatchLength < this.minMatchLength ? Integer.MAX_VALUE - : maxMatchLength; - this.maxLiteralLength = maxLiteralLength < 1 || maxLiteralLength > 2 * windowSize - ? windowSize : maxLiteralLength; + int limit = windowSize - 1; + this.maxOffset = maxOffset < 1 ? limit : Math.min(maxOffset, limit); + this.maxMatchLength = maxMatchLength < this.minMatchLength ? limit + : Math.min(maxMatchLength, limit); + this.maxLiteralLength = maxLiteralLength < 1 ? windowSize + : Math.min(maxLiteralLength, windowSize); } /** http://git-wip-us.apache.org/repos/asf/commons-compress/blob/f874b8c2/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java index f2fa1f3..6f7523b 100644 --- a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java @@ -147,12 +147,13 @@ public class LZ77CompressorTest { @Test public void blaExampleSmallerWindowSize() throws IOException { List<LZ77Compressor.Block> blocks = compress(new Parameters(8), BLA); - assertSize(5, blocks); + assertSize(6, blocks); assertLiteralBlock("Blah b", blocks.get(0)); assertEquals(LZ77Compressor.BackReference.class, blocks.get(1).getClass()); - assertBackReference(5, 8, blocks.get(1)); - assertBackReference(5, 8, blocks.get(2)); - assertLiteralBlock("ah!", blocks.get(3)); + assertBackReference(5, 7, blocks.get(1)); + assertBackReference(5, 3, blocks.get(2)); + assertBackReference(5, 7, blocks.get(3)); + assertLiteralBlock("h!", blocks.get(4)); } @Test http://git-wip-us.apache.org/repos/asf/commons-compress/blob/f874b8c2/src/test/java/org/apache/commons/compress/compressors/lz77support/ParametersTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/compressors/lz77support/ParametersTest.java b/src/test/java/org/apache/commons/compress/compressors/lz77support/ParametersTest.java index b26cd85..22e5bdf 100644 --- a/src/test/java/org/apache/commons/compress/compressors/lz77support/ParametersTest.java +++ b/src/test/java/org/apache/commons/compress/compressors/lz77support/ParametersTest.java @@ -29,8 +29,8 @@ public class ParametersTest { Parameters p = new Parameters(128); assertEquals(128, p.getWindowSize()); assertEquals(3, p.getMinMatchLength()); - assertEquals(128, p.getMaxMatchLength()); - assertEquals(128, p.getMaxOffset()); + assertEquals(127, p.getMaxMatchLength()); + assertEquals(127, p.getMaxOffset()); assertEquals(128, p.getMaxLiteralLength()); } @@ -41,9 +41,9 @@ public class ParametersTest { } @Test - public void maxMatchLengthIsInfiniteWhenSmallerThanMinMatchLength() { + public void maxMatchLengthIsWinsizeMinus1WhenSmallerThanMinMatchLength() { Parameters p = new Parameters(128, 2, 2, 4, 5); - assertEquals(Integer.MAX_VALUE, p.getMaxMatchLength()); + assertEquals(127, p.getMaxMatchLength()); } @Test @@ -53,21 +53,21 @@ public class ParametersTest { } @Test - public void maxOffsetIsWindowSizeIfSetTo0() { + public void maxOffsetIsWindowSizeMinus1IfSetTo0() { Parameters p = new Parameters(128, 2, 3, 0, 5); - assertEquals(128, p.getMaxOffset()); + assertEquals(127, p.getMaxOffset()); } @Test - public void maxOffsetIsWindowSizeIfSetToANegativeValue() { + public void maxOffsetIsWindowSizeMinus1IfSetToANegativeValue() { Parameters p = new Parameters(128, 2, 3, -1, 5); - assertEquals(128, p.getMaxOffset()); + assertEquals(127, p.getMaxOffset()); } @Test - public void maxOffsetIsWindowSizeIfBiggerThanWindowSize() { + public void maxOffsetIsWindowSizeMinus1IfBiggerThanWindowSize() { Parameters p = new Parameters(128, 2, 3, 129, 5); - assertEquals(128, p.getMaxOffset()); + assertEquals(127, p.getMaxOffset()); } @Test
