public void GetMaxCompressedSize_Basic() { Assert.Throws <ArgumentOutOfRangeException>("inputSize", () => BrotliEncoder.GetMaxCompressedLength(-1)); Assert.Throws <ArgumentOutOfRangeException>("inputSize", () => BrotliEncoder.GetMaxCompressedLength(2147483133)); Assert.InRange(BrotliEncoder.GetMaxCompressedLength(2147483132), 0, int.MaxValue); Assert.Equal(1, BrotliEncoder.GetMaxCompressedLength(0)); }
public void RoundTrip_Chunks() { int chunkSize = 100; int totalSize = 20000; BrotliEncoder encoder = default; BrotliDecoder decoder = default; for (int i = 0; i < totalSize; i += chunkSize) { byte[] uncompressed = new byte[chunkSize]; Random.Shared.NextBytes(uncompressed); byte[] compressed = new byte[BrotliEncoder.GetMaxCompressedLength(chunkSize)]; byte[] decompressed = new byte[chunkSize]; var uncompressedSpan = new ReadOnlySpan <byte>(uncompressed); var compressedSpan = new Span <byte>(compressed); var decompressedSpan = new Span <byte>(decompressed); int totalWrittenThisIteration = 0; var compress = encoder.Compress(uncompressedSpan, compressedSpan, out int bytesConsumed, out int bytesWritten, isFinalBlock: false); totalWrittenThisIteration += bytesWritten; compress = encoder.Flush(compressedSpan.Slice(bytesWritten), out bytesWritten); totalWrittenThisIteration += bytesWritten; var res = decoder.Decompress(compressedSpan.Slice(0, totalWrittenThisIteration), decompressedSpan, out int decompressbytesConsumed, out int decompressbytesWritten); Assert.Equal(totalWrittenThisIteration, decompressbytesConsumed); Assert.Equal(bytesConsumed, decompressbytesWritten); Assert.Equal <byte>(uncompressed, decompressedSpan.ToArray()); } }
public void GetMaxCompressedSize() { string uncompressedFile = UncompressedTestFile(); string compressedFile = CompressedTestFile(uncompressedFile); int maxCompressedSize = BrotliEncoder.GetMaxCompressedLength((int)new FileInfo(uncompressedFile).Length); int actualCompressedSize = (int)new FileInfo(compressedFile).Length; Assert.True(maxCompressedSize >= actualCompressedSize, $"MaxCompressedSize: {maxCompressedSize}, ActualCompressedSize: {actualCompressedSize}"); }
public void WriteWithoutState(string testFile) { byte[] correctUncompressedBytes = File.ReadAllBytes(testFile); byte[] compressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)]; byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length]; Compress_WithoutState(correctUncompressedBytes, compressedBytes); Decompress_WithoutState(compressedBytes, actualUncompressedBytes); Assert.Equal <byte>(correctUncompressedBytes, actualUncompressedBytes); }
public void WriteFully(string testFile) { byte[] correctUncompressedBytes = File.ReadAllBytes(testFile); byte[] compressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)]; byte[] actualUncompressedBytes = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)]; Span <byte> destination = new Span <byte>(compressedBytes); Assert.True(BrotliEncoder.TryCompress(correctUncompressedBytes, destination, out int bytesWritten)); Assert.True(BrotliDecoder.TryDecompress(destination, actualUncompressedBytes, out bytesWritten)); Assert.Equal(correctUncompressedBytes.Length, bytesWritten); Assert.Equal <byte>(correctUncompressedBytes, actualUncompressedBytes.AsSpan(0, correctUncompressedBytes.Length).ToArray()); }
public void Compress_Canterbury_WithoutState(string uncompressedFileName, CompressionLevel compressLevel) { byte[] bytes = File.ReadAllBytes(uncompressedFileName); ReadOnlySpan <byte> uncompressedData = new ReadOnlySpan <byte>(bytes); int maxCompressedSize = BrotliEncoder.GetMaxCompressedLength(bytes.Length); byte[] compressedDataArray = new byte[maxCompressedSize]; int compressLevelBrotli = compressLevel == CompressionLevel.Optimal ? 11 : compressLevel == CompressionLevel.Fastest ? 1 : 0; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { Assert.True(BrotliEncoder.TryCompress(uncompressedData, compressedDataArray, out int bytesWritten, compressLevelBrotli, 22)); } } }
public void Compress_Canterbury_WithState(string uncompressedFileName, CompressionLevel compressLevel) { byte[] bytes = File.ReadAllBytes(uncompressedFileName); ReadOnlySpan <byte> uncompressedData = new ReadOnlySpan <byte>(bytes); int maxCompressedSize = BrotliEncoder.GetMaxCompressedLength(bytes.Length); byte[] compressedDataArray = new byte[maxCompressedSize]; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) using (BrotliEncoder encoder = new BrotliEncoder()) { Span <byte> output = new Span <byte>(compressedDataArray); ReadOnlySpan <byte> input = uncompressedData; while (!input.IsEmpty && !output.IsEmpty) { encoder.Compress(input, output, out int bytesConsumed, out int written, isFinalBlock: false); input = input.Slice(bytesConsumed); output = output.Slice(written); } encoder.Compress(input, output, out int bytesConsumed2, out int written2, isFinalBlock: true); } } }