| using Lucene.Net.Support; |
| using NUnit.Framework; |
| using RandomizedTesting.Generators; |
| using System; |
| using Assert = Lucene.Net.TestFramework.Assert; |
| using RandomInts = RandomizedTesting.Generators.RandomNumbers; |
| |
| namespace Lucene.Net.Codecs.Compressing |
| { |
| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| using ByteArrayDataInput = Lucene.Net.Store.ByteArrayDataInput; |
| using ByteArrayDataOutput = Lucene.Net.Store.ByteArrayDataOutput; |
| using BytesRef = Lucene.Net.Util.BytesRef; |
| using LuceneTestCase = Lucene.Net.Util.LuceneTestCase; |
| using TestUtil = Lucene.Net.Util.TestUtil; |
| |
| [TestFixture] |
| public abstract class AbstractTestCompressionMode : LuceneTestCase |
| { |
| internal CompressionMode mode; |
| |
| internal static byte[] RandomArray() |
| { |
| int max = Random.NextBoolean() |
| ? Random.Next(4) |
| : Random.Next(256); |
| int length = Random.NextBoolean() |
| ? Random.Next(20) |
| : Random.Next(192 * 1024); |
| return RandomArray(length, max); |
| } |
| |
| internal static byte[] RandomArray(int length, int max) |
| { |
| var arr = new byte[length]; |
| for (int i = 0; i < arr.Length; ++i) |
| { |
| arr[i] = (byte)RandomInts.RandomInt32Between(Random, 0, max); |
| } |
| return arr; |
| } |
| |
| internal virtual byte[] Compress(byte[] decompressed, int off, int len) |
| { |
| Compressor compressor = mode.NewCompressor(); |
| return Compress(compressor, decompressed, off, len); |
| } |
| |
| internal static byte[] Compress(Compressor compressor, byte[] decompressed, int off, int len) |
| { |
| var compressed = new byte[len * 2 + 16]; // should be enough |
| ByteArrayDataOutput @out = new ByteArrayDataOutput(compressed); |
| compressor.Compress(decompressed, off, len, @out); |
| int compressedLen = @out.Position; |
| return Arrays.CopyOf(compressed, compressedLen); |
| } |
| |
| internal virtual byte[] Decompress(byte[] compressed, int originalLength) |
| { |
| Decompressor decompressor = mode.NewDecompressor(); |
| return Decompress(decompressor, compressed, originalLength); |
| } |
| |
| internal static byte[] Decompress(Decompressor decompressor, byte[] compressed, int originalLength) |
| { |
| BytesRef bytes = new BytesRef(); |
| decompressor.Decompress(new ByteArrayDataInput(compressed), originalLength, 0, originalLength, bytes); |
| return Arrays.CopyOfRange(bytes.Bytes, bytes.Offset, bytes.Offset + bytes.Length); |
| } |
| |
| internal virtual byte[] Decompress(byte[] compressed, int originalLength, int offset, int length) |
| { |
| Decompressor decompressor = mode.NewDecompressor(); |
| BytesRef bytes = new BytesRef(); |
| decompressor.Decompress(new ByteArrayDataInput(compressed), originalLength, offset, length, bytes); |
| return Arrays.CopyOfRange(bytes.Bytes, bytes.Offset, bytes.Offset + bytes.Length); |
| } |
| |
| [Test] |
| public virtual void TestDecompress() |
| { |
| int iterations = AtLeast(10); |
| for (int i = 0; i < iterations; ++i) |
| { |
| var decompressed = RandomArray(); |
| int off = Random.NextBoolean() ? 0 : TestUtil.NextInt32(Random, 0, decompressed.Length); |
| int len = Random.NextBoolean() ? decompressed.Length - off : TestUtil.NextInt32(Random, 0, decompressed.Length - off); |
| var compressed = Compress(decompressed, off, len); |
| var restored = Decompress(compressed, len); |
| Assert.AreEqual(Arrays.CopyOfRange(decompressed, off, off + len), restored);//was AssertArrayEquals |
| } |
| } |
| |
| [Test] |
| public virtual void TestPartialDecompress() |
| { |
| int iterations = AtLeast(10); |
| for (int i = 0; i < iterations; ++i) |
| { |
| var decompressed = RandomArray(); |
| var compressed = Compress(decompressed, 0, decompressed.Length); |
| int offset, length; |
| if (decompressed.Length == 0) |
| { |
| offset = length = 0; |
| } |
| else |
| { |
| offset = Random.Next(decompressed.Length); |
| length = Random.Next(decompressed.Length - offset); |
| } |
| var restored = Decompress(compressed, decompressed.Length, offset, length); |
| Assert.AreEqual(Arrays.CopyOfRange(decompressed, offset, offset + length), restored); //was AssertArrayEquals |
| } |
| } |
| |
| public virtual byte[] Test(byte[] decompressed) |
| { |
| return Test(decompressed, 0, decompressed.Length); |
| } |
| |
| public virtual byte[] Test(byte[] decompressed, int off, int len) |
| { |
| var compressed = Compress(decompressed, off, len); |
| var restored = Decompress(compressed, len); |
| Assert.AreEqual(len, restored.Length); |
| return compressed; |
| } |
| |
| [Test] |
| public virtual void TestEmptySequence() |
| { |
| Test(Array.Empty<byte>()); // LUCENENET specific: was new byte[0] |
| } |
| |
| [Test] |
| public virtual void TestShortSequence() |
| { |
| Test(new[] { (byte)Random.Next(256) }); |
| } |
| |
| [Test] |
| public virtual void TestIncompressible() |
| { |
| var decompressed = new byte[RandomInts.RandomInt32Between(Random, 20, 256)]; |
| for (int i = 0; i < decompressed.Length; ++i) |
| { |
| decompressed[i] = (byte)i; |
| } |
| Test(decompressed); |
| } |
| |
| [Test] |
| public virtual void TestConstant() |
| { |
| var decompressed = new byte[TestUtil.NextInt32(Random, 1, 10000)]; |
| Arrays.Fill(decompressed, (byte)Random.Next()); |
| Test(decompressed); |
| } |
| |
| [Test] |
| public virtual void TestLUCENE5201() |
| { |
| sbyte[] data = |
| { |
| 14, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 72, 14, 72, 14, 85, 3, 72, 14, 72, 14, 72, 14, 72, 14, 72, 14, 72, 14, 85, 3, 72, |
| 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 50, 64, 0, 46, -1, 0, 0, 0, 29, 3, 85, |
| 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, |
| 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, |
| 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, |
| 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 50, 64, 0, 47, -105, 0, 0, 0, 30, 3, -97, 6, 0, 68, -113, |
| 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, |
| 8, -113, 0, 68, -97, 3, 0, 2, -97, 6, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, |
| 6, 0, 68, -113, 0, 120, 64, 0, 48, 4, 0, 0, 0, 31, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, |
| 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, |
| 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, |
| 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, |
| 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, |
| 41, 72, 32, 72, 18, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 0, 39, 24, 32, 34, 124, 0, 120, 64, 0, 48, 80, 0, 0, 0, 31, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, |
| 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, |
| 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, |
| 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, |
| 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, |
| 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, |
| 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, |
| 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, |
| 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, |
| 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, |
| 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, |
| 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, |
| 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 50, 64, 0, 49, 20, 0, 0, 0, 32, 3, -97, 6, 0, |
| 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, |
| 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, |
| 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, |
| 3, -97, 6, 0, 50, 64, 0, 50, 53, 0, 0, 0, 34, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, |
| 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, |
| -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, |
| 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, |
| 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, |
| 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, |
| -97, 6, 0, 50, 64, 0, 51, 85, 0, 0, 0, 36, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, |
| 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, -97, 5, 0, 2, 3, 85, 8, -113, 0, 68, |
| -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, |
| 68, -113, 0, 2, 3, -97, 6, 0, 50, -64, 0, 51, -45, 0, 0, 0, 37, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, |
| 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, |
| 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 120, 64, 0, 52, -88, 0, 0, |
| 0, 39, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, |
| 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, |
| 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, |
| 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, |
| 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 72, |
| 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, -19, -24, -101, -35 |
| }; |
| Test(data.ToByteArray(), 9, data.Length - 9); |
| } |
| } |
| } |