mirror of
https://github.com/VSadov/Satori.git
synced 2025-06-08 03:27:04 +09:00
Update Compression unit tests.
This commit is contained in:
parent
af2b2f599c
commit
15c5c2c7ac
10 changed files with 81 additions and 85 deletions
|
@ -0,0 +1,49 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
||||
namespace System.IO.Compression;
|
||||
|
||||
internal static partial class ZLibNative
|
||||
{
|
||||
/// <summary>
|
||||
/// <p>ZLib can accept any integer value between 0 and 9 (inclusive) as a valid compression level parameter:
|
||||
/// 1 gives best speed, 9 gives best compression, 0 gives no compression at all (the input data is simply copied a block at a time).
|
||||
/// <code>CompressionLevel.DefaultCompression</code> = -1 requests a default compromise between speed and compression
|
||||
/// (currently equivalent to level 6).</p>
|
||||
///
|
||||
/// <p><strong>How to choose a compression level:</strong></p>
|
||||
///
|
||||
/// <p>The names <code>NoCompression</code>, <code>BestSpeed</code>, <code>DefaultCompression</code>, <code>BestCompression</code> are taken over from
|
||||
/// the corresponding ZLib definitions, which map to our public NoCompression, Fastest, Optimal, and SmallestSize respectively.</p>
|
||||
/// <p><em>Optimal Compression:</em></p>
|
||||
/// <p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.DefaultCompression;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 8;</code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
///
|
||||
///<p><em>Fastest compression:</em></p>
|
||||
///<p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.BestSpeed;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 8; </code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
///
|
||||
/// <p><em>No compression (even faster, useful for data that cannot be compressed such some image formats):</em></p>
|
||||
/// <p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.NoCompression;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 7;</code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
///
|
||||
/// <p><em>Smallest Size Compression:</em></p>
|
||||
/// <p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.BestCompression;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 8;</code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
/// </summary>
|
||||
public enum CompressionLevel : int
|
||||
{
|
||||
NoCompression = 0,
|
||||
BestSpeed = 1,
|
||||
DefaultCompression = -1,
|
||||
BestCompression = 9
|
||||
}
|
||||
}
|
|
@ -37,48 +37,6 @@ namespace System.IO.Compression
|
|||
VersionError = -6
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <p>ZLib can accept any integer value between 0 and 9 (inclusive) as a valid compression level parameter:
|
||||
/// 1 gives best speed, 9 gives best compression, 0 gives no compression at all (the input data is simply copied a block at a time).
|
||||
/// <code>CompressionLevel.DefaultCompression</code> = -1 requests a default compromise between speed and compression
|
||||
/// (currently equivalent to level 6).</p>
|
||||
///
|
||||
/// <p><strong>How to choose a compression level:</strong></p>
|
||||
///
|
||||
/// <p>The names <code>NoCompression</code>, <code>BestSpeed</code>, <code>DefaultCompression</code>, <code>BestCompression</code> are taken over from
|
||||
/// the corresponding ZLib definitions, which map to our public NoCompression, Fastest, Optimal, and SmallestSize respectively.</p>
|
||||
/// <p><em>Optimal Compression:</em></p>
|
||||
/// <p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.DefaultCompression;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 8;</code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
///
|
||||
///<p><em>Fastest compression:</em></p>
|
||||
///<p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.BestSpeed;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 8; </code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
///
|
||||
/// <p><em>No compression (even faster, useful for data that cannot be compressed such some image formats):</em></p>
|
||||
/// <p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.NoCompression;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 7;</code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
///
|
||||
/// <p><em>Smallest Size Compression:</em></p>
|
||||
/// <p><code>ZLibNative.CompressionLevel compressionLevel = ZLibNative.CompressionLevel.BestCompression;</code> <br />
|
||||
/// <code>int windowBits = 15; // or -15 if no headers required</code> <br />
|
||||
/// <code>int memLevel = 8;</code> <br />
|
||||
/// <code>ZLibNative.CompressionStrategy strategy = ZLibNative.CompressionStrategy.DefaultStrategy;</code> </p>
|
||||
/// </summary>
|
||||
public enum CompressionLevel : int
|
||||
{
|
||||
NoCompression = 0,
|
||||
BestSpeed = 1,
|
||||
DefaultCompression = -1,
|
||||
BestCompression = 9
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <p><strong>From the ZLib manual:</strong></p>
|
||||
/// <p><code>CompressionStrategy</code> is used to tune the compression algorithm.<br />
|
||||
|
|
|
@ -497,6 +497,30 @@ namespace System.IO.Compression
|
|||
Assert.True(optimalLength >= smallestLength);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(UncompressedTestFilesZLib))]
|
||||
public async Task ZLibCompressionOptions_SizeInOrder(string testFile)
|
||||
{
|
||||
using var uncompressedStream = await LocalMemoryStream.readAppFileAsync(testFile);
|
||||
|
||||
async Task<long> GetLengthAsync(int compressionLevel)
|
||||
{
|
||||
uncompressedStream.Position = 0;
|
||||
using var mms = new MemoryStream();
|
||||
using var compressor = CreateStream(mms, new ZLibCompressionOptions() { CompressionLevel = compressionLevel, CompressionStrategy = ZLibCompressionStrategy.Default }, leaveOpen: false);
|
||||
await uncompressedStream.CopyToAsync(compressor);
|
||||
await compressor.FlushAsync();
|
||||
return mms.Length;
|
||||
}
|
||||
|
||||
long fastestLength = await GetLengthAsync(1);
|
||||
long optimalLength = await GetLengthAsync(5);
|
||||
long smallestLength = await GetLengthAsync(9);
|
||||
|
||||
Assert.True(fastestLength >= optimalLength);
|
||||
Assert.True(optimalLength >= smallestLength);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ZLibOptionsRoundTripTestData))]
|
||||
public async Task RoundTripWithZLibCompressionOptions(string testFile, ZLibCompressionOptions options)
|
||||
|
@ -537,28 +561,6 @@ namespace System.IO.Compression
|
|||
return compressorOutput;
|
||||
}
|
||||
|
||||
protected async Task CompressionLevel_SizeInOrderBase(string testFile)
|
||||
{
|
||||
using var uncompressedStream = await LocalMemoryStream.readAppFileAsync(testFile);
|
||||
|
||||
async Task<long> GetLengthAsync(int compressionLevel)
|
||||
{
|
||||
uncompressedStream.Position = 0;
|
||||
using var mms = new MemoryStream();
|
||||
using var compressor = CreateStream(mms, new ZLibCompressionOptions() { CompressionLevel = compressionLevel, CompressionStrategy = ZLibCompressionStrategy.Default }, leaveOpen: false);
|
||||
await uncompressedStream.CopyToAsync(compressor);
|
||||
await compressor.FlushAsync();
|
||||
return mms.Length;
|
||||
}
|
||||
|
||||
long prev = await GetLengthAsync(0);
|
||||
for (int i = 1; i < 10; i++)
|
||||
{
|
||||
long cur = await GetLengthAsync(i);
|
||||
Assert.True(cur <= prev, $"Expected {cur} <= {prev} for quality {i}");
|
||||
prev = cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum TestScenario
|
||||
|
|
|
@ -27,6 +27,8 @@
|
|||
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
|
||||
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
|
||||
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.CompressionLevel.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.CompressionLevel.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
<Compile Include="System\IO\Compression\DeflateZLib\ZLibException.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.CompressionLevel.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.CompressionLevel.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.ZStream.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.ZStream.cs" />
|
||||
<Compile Include="System\IO\Compression\CompressionLevel.cs" />
|
||||
|
|
|
@ -219,12 +219,5 @@ namespace System.IO.Compression
|
|||
return base.WriteAsync(buffer, offset, count, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(UncompressedTestFilesZLib))]
|
||||
public async Task ZLibCompressionLevel_SizeInOrder(string testFile)
|
||||
{
|
||||
await CompressionLevel_SizeInOrderBase(testFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -441,12 +441,5 @@ namespace System.IO.Compression
|
|||
return base.WriteAsync(buffer, offset, count, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(UncompressedTestFilesZLib))]
|
||||
public async Task ZLibCompressionLevel_SizeInOrder(string testFile)
|
||||
{
|
||||
await CompressionLevel_SizeInOrderBase(testFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -150,12 +150,5 @@ namespace System.IO.Compression
|
|||
}
|
||||
}, testScenario.ToString()).Dispose();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(UncompressedTestFilesZLib))]
|
||||
public async Task ZLibCompressionLevel_SizeInOrder(string testFile)
|
||||
{
|
||||
await CompressionLevel_SizeInOrderBase(testFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
<Compile Include="$(CommonTestPath)System\IO\Compression\FileData.cs" Link="Common\System\IO\Compression\FileData.cs" />
|
||||
<Compile Include="$(CommonTestPath)System\IO\Compression\LocalMemoryStream.cs" Link="Common\System\IO\Compression\LocalMemoryStream.cs" />
|
||||
<Compile Include="$(CommonTestPath)System\IO\Compression\StreamHelpers.cs" Link="Common\System\IO\Compression\StreamHelpers.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.CompressionLevel.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.CompressionLevel.cs" />
|
||||
<Compile Include="$(CommonTestPath)System\IO\TempFile.cs" Link="Common\System\IO\TempFile.cs" />
|
||||
<Compile Include="$(CommonTestPath)System\IO\WrappedStream.cs" Link="Common\System\IO\WrappedStream.cs" />
|
||||
<Compile Include="$(CommonTestPath)System\IO\Compression\ZipTestHelper.cs" Link="Common\System\IO\Compression\ZipTestHelper.cs" />
|
||||
|
|
|
@ -37,6 +37,8 @@
|
|||
Link="Common\System\Net\WebSockets\WebSocketValidate.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.CompressionLevel.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.CompressionLevel.cs" />
|
||||
<Compile Include="$(CommonPath)System\IO\Compression\ZLibNative.ZStream.cs"
|
||||
Link="Common\System\IO\Compression\ZLibNative.ZStream.cs" />
|
||||
<Compile Include="$(CommonPath)Interop\Interop.zlib.cs"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue