Add additional per-chunk read/write test

This commit is contained in:
James Jackson-South 2022-12-05 08:20:21 +10:00
Родитель 7f0962a8e2
Коммит 0bbfaef58d
4 изменённых файлов: 78 добавлений и 23 удалений

Просмотреть файл

@ -1,7 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// Uncomment to use DeflateQuick // Uncomment to use DeflateQuick - Probs should remove as compression is really poor.
// TODO: Make an option. // TODO: Make an option.
// #define USE_QUICK // #define USE_QUICK
using System; using System;
@ -281,7 +281,7 @@ namespace SixLabors.ZlibStream
} }
#if USE_QUICK #if USE_QUICK
if (level == ZlibCompressionLevel.ZBESTSPEED) if (level == CompressionLevel.BestSpeed)
{ {
windowBits = 13; windowBits = 13;
} }

Просмотреть файл

@ -17,7 +17,7 @@
<Choose> <Choose>
<When Condition="'$(SIXLABORS_TESTING)' == 'true'"> <When Condition="'$(SIXLABORS_TESTING)' == 'true'">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1;netstandard2.1;netstandard2.0;netstandard1.3;net472</TargetFrameworks> <TargetFrameworks>net6.0;netcoreapp3.1;netcoreapp2.1;netstandard2.1;netstandard2.0;netstandard1.3;net472</TargetFrameworks>
</PropertyGroup> </PropertyGroup>
</When> </When>
<Otherwise> <Otherwise>

Просмотреть файл

@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<AssemblyName>ZlibStream.Benchmarks</AssemblyName> <AssemblyName>ZlibStream.Benchmarks</AssemblyName>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1;net472</TargetFrameworks> <TargetFrameworks>net6.0;netcoreapp3.1;netcoreapp2.1;net472</TargetFrameworks>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<!--Used to hide test project from dotnet test--> <!--Used to hide test project from dotnet test-->
<IsTestProject>false</IsTestProject> <IsTestProject>false</IsTestProject>

Просмотреть файл

@ -9,7 +9,7 @@ using Xunit;
namespace ZlibStream.Tests namespace ZlibStream.Tests
{ {
public partial class ZlibStreamTests public class ZlibStreamTests
{ {
[Theory] [Theory]
[InlineData(CompressionLevel.NoCompression)] [InlineData(CompressionLevel.NoCompression)]
@ -49,10 +49,69 @@ namespace ZlibStream.Tests
compressed.Position = 0; compressed.Position = 0;
using (var inflate = new ZlibInputStream(compressed)) using var inflate = new ZlibInputStream(compressed);
{
inflate.Read(actual, 0, actual.Length); inflate.Read(actual, 0, actual.Length);
} }
for (int i = 0; i < expected.Length; i++)
{
byte e = expected[i];
byte r = reference[i];
byte a = actual[i];
Assert.Equal(e, r);
Assert.Equal(e, a);
}
}
}
[Theory]
[InlineData(CompressionLevel.NoCompression)]
[InlineData(CompressionLevel.Level1)]
[InlineData(CompressionLevel.Level2)]
[InlineData(CompressionLevel.Level3)]
[InlineData(CompressionLevel.Level4)]
[InlineData(CompressionLevel.Level5)]
[InlineData(CompressionLevel.Level6)]
[InlineData(CompressionLevel.Level7)]
[InlineData(CompressionLevel.BestCompression)]
[InlineData(CompressionLevel.DefaultCompression)]
public void EncodeDecodePerChunk(CompressionLevel level)
{
foreach (CompressionStrategy strategy in (CompressionStrategy[])Enum.GetValues(typeof(CompressionStrategy)))
{
const int count = 2 * 4096 * 4;
const int chunk = 2 * 4096;
byte[] expected = GetBuffer(count);
byte[] reference = new byte[count];
byte[] actual = new byte[count];
using (var compressed = new MemoryStream())
{
var options = new ZlibOptions { CompressionStrategy = strategy, CompressionLevel = level };
using (var deflate = new ZlibOutputStream(compressed, options))
{
for (int i = 0; i < expected.Length; i += chunk)
{
deflate.Write(expected, i, chunk);
}
}
compressed.Position = 0;
using (var refInflate = new InflaterInputStream(compressed))
{
refInflate.IsStreamOwner = false;
refInflate.Read(reference, 0, reference.Length);
}
compressed.Position = 0;
using var inflate = new ZlibInputStream(compressed);
for (int i = 0; i < expected.Length; i += chunk)
{
inflate.Read(actual, i, chunk);
}
} }
for (int i = 0; i < expected.Length; i++) for (int i = 0; i < expected.Length; i++)
@ -72,33 +131,29 @@ namespace ZlibStream.Tests
public void DeflateProfileTest() public void DeflateProfileTest()
{ {
const int count = 1000 * 1000 * 4; const int count = 1000 * 1000 * 4;
var expected = GetBuffer(count); byte[] expected = GetBuffer(count);
using (var compressed = new MemoryStream()) using var compressed = new MemoryStream();
using (var deflate = new ZlibOutputStream(compressed, CompressionLevel.Level6)) using var deflate = new ZlibOutputStream(compressed, CompressionLevel.Level6);
{
deflate.Write(expected, 0, expected.Length); deflate.Write(expected, 0, expected.Length);
} }
}
[Fact] [Fact]
public void DeflateMemoryProfileTest() public void DeflateMemoryProfileTest()
{ {
var expected = GetImageBytes(3500, 3500); byte[] expected = GetImageBytes(3500, 3500);
using (var compressed = new MemoryStream()) using var compressed = new MemoryStream();
using (var deflate = new ZlibOutputStream(compressed, CompressionLevel.Level1)) using var deflate = new ZlibOutputStream(compressed, CompressionLevel.Level1);
{
deflate.Write(expected, 0, expected.Length); deflate.Write(expected, 0, expected.Length);
} }
}
private static byte[] GetImageBytes(int width, int height) private static byte[] GetImageBytes(int width, int height)
{ {
var bytes = new byte[width * height * 4]; byte[] bytes = new byte[width * height * 4];
for (var y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
for (var x = 0; x < width * 4; x += 4) for (int x = 0; x < width * 4; x += 4)
{ {
int i = 4 * y * width; int i = 4 * y * width;
bytes[i + x] = (byte)((x + y) % 256); // R bytes[i + x] = (byte)((x + y) % 256); // R
@ -113,7 +168,7 @@ namespace ZlibStream.Tests
private static byte[] GetBuffer(int length) private static byte[] GetBuffer(int length)
{ {
var data = new byte[length]; byte[] data = new byte[length];
new Random(1).NextBytes(data); new Random(1).NextBytes(data);
return data; return data;