예제 #1
0
 public void InvalidQuality()
 {
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => new BrotliEncoder(-1, 11));
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => new BrotliEncoder(12, 11));
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, -1, 13));
     Assert.Throws <ArgumentOutOfRangeException>("quality", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 12, 13));
 }
예제 #2
0
 public void InvalidWindow()
 {
     Assert.Throws <ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, -1));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, 9));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => new BrotliEncoder(10, 25));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 6, -1));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 6, 9));
     Assert.Throws <ArgumentOutOfRangeException>("window", () => BrotliEncoder.TryCompress(new ReadOnlySpan <byte>(), new Span <byte>(), out int bytesWritten, 6, 25));
 }
예제 #3
0
        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());
        }
예제 #4
0
        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));
                }
            }
        }
예제 #5
0
        public void Compress_WithEmptySource()
        {
            byte[] sourceBytes              = new byte[0];
            byte[] destinationBytes         = new byte[100000];
            ReadOnlySpan <byte> source      = new ReadOnlySpan <byte>(sourceBytes);
            Span <byte>         destination = new Span <byte>(destinationBytes);

            Assert.True(BrotliEncoder.TryCompress(source, destination, out int bytesWritten));
            // The only byte written should be the Brotli end of stream byte which varies based on the window/quality
            Assert.Equal(1, bytesWritten);

            BrotliEncoder encoder = default;
            var           result  = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);

            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.Done, result);

            result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(1, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.Done, result);
        }
예제 #6
0
        public void Compress_WithEmptyDestination()
        {
            string testFile = UncompressedTestFile();

            byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
            byte[] empty = new byte[0];
            ReadOnlySpan <byte> source      = new ReadOnlySpan <byte>(correctUncompressedBytes);
            Span <byte>         destination = new Span <byte>(empty);

            Assert.False(BrotliEncoder.TryCompress(source, destination, out int bytesWritten), "TryCompress completed successfully but should have failed due to too short of a destination array");
            Assert.Equal(0, bytesWritten);

            BrotliEncoder encoder = default;
            var           result  = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);

            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.DestinationTooSmall, result);

            result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.DestinationTooSmall, result);
        }
예제 #7
0
 private static void Compress_WithoutState(ReadOnlySpan <byte> input, Span <byte> output)
 {
     BrotliEncoder.TryCompress(input, output, out int bytesWritten);
 }
예제 #8
0
 public bool Compress_WithoutState()
 => BrotliEncoder.TryCompress(CompressedFile.UncompressedData, CompressedFile.UncompressedData, out int bytesWritten, GetQuality(level), Window);