From 5cbe58f22c71cfe3f6d21b4661914c255ac80e3d Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 14 Feb 2020 10:37:06 +0100 Subject: [PATCH] Add back assertions removed by LUCENE-9187. (#1236) This time they would only apply to TestFastLZ4/TestHighLZ4 and avoid slowing down all tests. --- .../org/apache/lucene/util/compress/LZ4.java | 16 ++++++++++ .../lucene/util/compress/LZ4TestCase.java | 31 +++++++++++++++++++ .../lucene/util/compress/TestFastLZ4.java | 3 +- .../lucene/util/compress/TestHighLZ4.java | 3 +- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java b/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java index 0b2945efc79..0b7f5d85a29 100644 --- a/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java +++ b/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java @@ -202,6 +202,9 @@ static abstract class HashTable { * offsets. A return value of {@code -1} indicates that no other index could * be found. */ abstract int previous(int off); + + // For testing + abstract boolean assertReset(); } /** @@ -263,6 +266,11 @@ public int previous(int off) { return -1; } + @Override + boolean assertReset() { + return true; + } + } /** @@ -369,6 +377,14 @@ int previous(int off) { } return -1; } + + @Override + boolean assertReset() { + for (int i = 0; i < chainTable.length; ++i) { + assert chainTable[i] == (short) 0xFFFF : i; + } + return true; + } } /** diff --git a/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java b/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java index e49cf4803e1..81f84e47e78 100644 --- a/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java +++ b/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java @@ -30,6 +30,37 @@ public abstract class LZ4TestCase extends LuceneTestCase { protected abstract LZ4.HashTable newHashTable(); + protected static class AssertingHashTable extends LZ4.HashTable { + + private final LZ4.HashTable in; + + AssertingHashTable(LZ4.HashTable in) { + this.in = in; + } + + @Override + void reset(byte[] b, int off, int len) { + in.reset(b, off, len); + assertTrue(in.assertReset()); + } + + @Override + int get(int off) { + return in.get(off); + } + + @Override + int previous(int off) { + return in.previous(off); + } + + @Override + boolean assertReset() { + throw new UnsupportedOperationException(); + } + + } + private void doTest(byte[] data, LZ4.HashTable hashTable) throws IOException { int offset = random().nextBoolean() ? random().nextInt(10) diff --git a/lucene/core/src/test/org/apache/lucene/util/compress/TestFastLZ4.java b/lucene/core/src/test/org/apache/lucene/util/compress/TestFastLZ4.java index 076d04c6635..58709523802 100644 --- a/lucene/core/src/test/org/apache/lucene/util/compress/TestFastLZ4.java +++ b/lucene/core/src/test/org/apache/lucene/util/compress/TestFastLZ4.java @@ -22,7 +22,8 @@ public class TestFastLZ4 extends LZ4TestCase { @Override protected HashTable newHashTable() { - return new LZ4.FastCompressionHashTable(); + LZ4.HashTable hashTable = new LZ4.FastCompressionHashTable(); + return new AssertingHashTable(hashTable); } } diff --git a/lucene/core/src/test/org/apache/lucene/util/compress/TestHighLZ4.java b/lucene/core/src/test/org/apache/lucene/util/compress/TestHighLZ4.java index b118f947384..207940a0dc2 100644 --- a/lucene/core/src/test/org/apache/lucene/util/compress/TestHighLZ4.java +++ b/lucene/core/src/test/org/apache/lucene/util/compress/TestHighLZ4.java @@ -22,7 +22,8 @@ public class TestHighLZ4 extends LZ4TestCase { @Override protected HashTable newHashTable() { - return new LZ4.HighCompressionHashTable(); + LZ4.HashTable hashTable = new LZ4.HighCompressionHashTable(); + return new AssertingHashTable(hashTable); } }