Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#96 #98

Merged
merged 1 commit into from
Mar 17, 2024
Merged

#96 #98

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.3.8 (2023/03/17)
* FIXED #96: fixed issue returning wrong compressed length when compressing into Span<byte>

## 1.3.7-beta (2023/10/03)
* ADDED #61: Partial decompression support (see #61, #90) (thanks: Sewer56)
* ADDED #61: partial decompression support (see #61, #90) (thanks: Sewer56)
* CHANGED: build process from FAKE (sorry!) to Nuke

## 1.3.6 (2023/08/15)
Expand Down
84 changes: 84 additions & 0 deletions src/K4os.Compression.LZ4.Streams.Tests/Issue96.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Buffers;
using TestHelpers;
using Xunit;

namespace K4os.Compression.LZ4.Streams.Tests;

public class Issue96
{
private static int EstimateLZ4FrameSize(byte[] source)
{
using var memory = new MemoryStream();
using (var target = LZ4Stream.Encode(memory, leaveOpen: true))
target.Write(source, 0, source.Length);
return (int)memory.Length;
}

public (byte[] Source, int Expected, int Maximum) PrepareTestCase(int length)
{
var source = Lorem.Create(length);
var expected = EstimateLZ4FrameSize(source);
var maximum = LZ4Codec.MaximumOutputSize(source.Length);
return (source, expected, maximum);
}

[Theory]
[InlineData(1337)]
[InlineData(0x10000)]
public void SequenceToBufferWriter(int length)
{
var testCase = PrepareTestCase(length);
var source = new ReadOnlySequence<byte>(testCase.Source);
var target = new BufferWriter();
var actual = LZ4Frame.Encode(source, target).WrittenMemory.Length;
Assert.Equal(testCase.Expected, actual);
}

[Theory]
[InlineData(1337)]
[InlineData(0x10000)]
public void SpanToBufferWriter(int length)
{
var testCase = PrepareTestCase(length);
var source = testCase.Source.AsSpan();
var target = new BufferWriter();
var actual = LZ4Frame.Encode(source, target).WrittenMemory.Length;
Assert.Equal(testCase.Expected, actual);
}

[Theory]
[InlineData(1337)]
[InlineData(0x10000)]
public void SequenceToSpan(int length)
{
var testCase = PrepareTestCase(length);
var source = new ReadOnlySequence<byte>(testCase.Source);
var target = new byte[testCase.Maximum].AsSpan();
var actual = LZ4Frame.Encode(source, target);
Assert.Equal(testCase.Expected, actual);
}

[Theory]
[InlineData(1337)]
[InlineData(0x10000)]
public void SpanToSpan(int length)
{
var testCase = PrepareTestCase(length);
var source = testCase.Source.AsSpan();
var target = new byte[testCase.Maximum].AsSpan();
var actual = LZ4Frame.Encode(source, target);
Assert.Equal(testCase.Expected, actual);
}

[Theory]
[InlineData(1337)]
[InlineData(0x10000)]
public void CallbackToSpan(int length)
{
var testCase = PrepareTestCase(length);
var source = testCase.Source;
var target = new byte[testCase.Maximum].AsSpan();
var actual = LZ4Frame.Encode(w => w.WriteManyBytes(source), target);
Assert.Equal(testCase.Expected, actual);
}
}
12 changes: 6 additions & 6 deletions src/K4os.Compression.LZ4.Streams/LZ4Frame.encode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public static unsafe int Encode(
settings ??= LZ4EncoderSettings.Default;
fixed (byte* stream0 = target)
{
using var encoder = new ByteSpanLZ4FrameWriter(
var encoder = new ByteSpanLZ4FrameWriter(
UnsafeByteSpan.Create(stream0, target.Length),
i => i.CreateEncoder(settings.CompressionLevel, settings.ExtraMemory),
settings.CreateDescriptor());
encoder.CopyFrom(source);
using (encoder) encoder.CopyFrom(source);
return encoder.CompressedLength;
}
}
Expand All @@ -118,11 +118,11 @@ public static unsafe int Encode(
settings ??= LZ4EncoderSettings.Default;
fixed (byte* stream0 = target)
{
using var encoder = new ByteSpanLZ4FrameWriter(
var encoder = new ByteSpanLZ4FrameWriter(
UnsafeByteSpan.Create(stream0, target.Length),
i => i.CreateEncoder(settings.CompressionLevel, settings.ExtraMemory),
settings.CreateDescriptor());
encoder.WriteManyBytes(source);
using (encoder) encoder.WriteManyBytes(source);
return encoder.CompressedLength;
}
}
Expand All @@ -141,11 +141,11 @@ public static unsafe int Encode(
settings ??= LZ4EncoderSettings.Default;
fixed (byte* stream0 = target)
{
using var encoder = new ByteSpanLZ4FrameWriter(
var encoder = new ByteSpanLZ4FrameWriter(
UnsafeByteSpan.Create(stream0, target.Length),
i => i.CreateEncoder(settings.CompressionLevel, settings.ExtraMemory),
settings.CreateDescriptor());
source(encoder);
using (encoder) source(encoder);
return encoder.CompressedLength;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/TestHelpers/Lorem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public static class Lorem

public static readonly byte[] Bytes = Encoding.UTF8.GetBytes(Text);

public static byte[] Create(int length)
{
var result = new byte[length];
Fill(result, 0, length);
return result;
}

public static void Fill(byte[] output, int outputIndex, int outputLength)
{
while (outputLength > 0)
Expand Down
Loading