Repository navigation
Expand file tree
/
Copy pathZLibEncoderDecoderTestBase.cs
More file actions
90 lines (71 loc) · 3.65 KB
/
Copy pathZLibEncoderDecoderTestBase.cs
File metadata and controls
90 lines (71 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using Xunit;
namespace System.IO.Compression
{
public abstract class ZLibEncoderDecoderTestBase : EncoderDecoderTestBase
{
protected override bool SupportsDictionaries => false;
protected override bool SupportsReset => true;
protected override string WindowLogParamName => "windowLog2";
protected override string InputLengthParamName => "inputLength";
// Quality maps to zlib compression level (0-9)
protected override int ValidQuality => 6;
protected override int ValidWindowLog => 15;
protected override int MinQuality => 0;
protected override int MaxQuality => 9;
protected override int MinWindowLog => 8;
protected override int MaxWindowLog => 15;
protected override int InvalidQualityTooLow => -2;
protected override int InvalidQualityTooHigh => 10;
protected override int InvalidWindowLogTooLow => 7;
protected override int InvalidWindowLogTooHigh => 16;
protected abstract EncoderAdapter CreateEncoder(ZLibCompressionOptions options);
protected override EncoderAdapter CreateEncoder(DictionaryAdapter dictionary, int windowLog) =>
throw new NotSupportedException();
protected override DecoderAdapter CreateDecoder(DictionaryAdapter dictionary) =>
throw new NotSupportedException();
protected override DictionaryAdapter CreateDictionary(ReadOnlySpan<byte> dictionaryData, int quality) =>
throw new NotSupportedException();
protected override bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, DictionaryAdapter dictionary, int windowLog2) =>
throw new NotSupportedException();
protected override bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, DictionaryAdapter dictionary) =>
throw new NotSupportedException();
[Fact]
public void Encoder_Ctor_NullOptions_Throws()
{
Assert.Throws<ArgumentNullException>(() => CreateEncoder(null!));
}
[Theory]
[InlineData(ZLibCompressionStrategy.Default)]
[InlineData(ZLibCompressionStrategy.Filtered)]
[InlineData(ZLibCompressionStrategy.HuffmanOnly)]
[InlineData(ZLibCompressionStrategy.RunLengthEncoding)]
[InlineData(ZLibCompressionStrategy.Fixed)]
public void Encoder_CompressionStrategies(ZLibCompressionStrategy strategy)
{
byte[] input = CreateTestData();
var options = new ZLibCompressionOptions
{
CompressionLevel = 6,
CompressionStrategy = strategy
};
using var encoder = CreateEncoder(options);
byte[] destination = new byte[GetMaxCompressedLength(input.Length)];
OperationStatus status = encoder.Compress(input, destination, out int consumed, out int written, isFinalBlock: true);
Assert.Equal(OperationStatus.Done, status);
Assert.Equal(input.Length, consumed);
Assert.True(written > 0);
}
[Fact]
public void Decoder_InvalidData_ReturnsInvalidData()
{
byte[] invalidData = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
using var decoder = CreateDecoder();
byte[] decompressed = new byte[100];
OperationStatus status = decoder.Decompress(invalidData, decompressed, out _, out _);
Assert.Equal(OperationStatus.InvalidData, status);
}
}
}