Skip to content

Commit

Permalink
issue 96
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszKrajewski committed Mar 17, 2024
1 parent 52f4345 commit f1394d3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
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

0 comments on commit f1394d3

Please sign in to comment.