Skip to content

Commit

Permalink
Fixing issue with StreamSegment.Adjust
Browse files Browse the repository at this point in the history
It was improperly checking the offset parameter when it should have been checking the length parameter.
I added a test to cover the various adjust cases to ensure that it works as expected.
  • Loading branch information
Keboo committed Feb 25, 2024
1 parent de64824 commit 235b2c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DotNext.IO/IO/StreamSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public StreamSegment(Stream stream, bool leaveOpen = true)
/// </remarks>
/// <param name="offset">The offset in the underlying stream.</param>
/// <param name="length">The length of the segment.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is larger than the reamining length of the underlying stream; or <paramref name="offset"/> if greater than the length of the underlying stream.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is larger than the remaining length of the underlying stream; or <paramref name="offset"/> if greater than the length of the underlying stream.</exception>
public void Adjust(long offset, long length)
{
ArgumentOutOfRangeException.ThrowIfGreaterThan((ulong)offset, (ulong)BaseStream.Length, nameof(offset));
ArgumentOutOfRangeException.ThrowIfGreaterThan((ulong)offset, (ulong)(BaseStream.Length - offset), nameof(length));
ArgumentOutOfRangeException.ThrowIfGreaterThan((ulong)length, (ulong)(BaseStream.Length - offset), nameof(length));

this.length = length;
this.offset = offset;
Expand Down
17 changes: 16 additions & 1 deletion src/DotNext.Tests/IO/StreamSegmentTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
namespace DotNext.IO;
using System.Text;

namespace DotNext.IO;

public sealed class StreamSegmentTests : Test
{
[Theory]
[InlineData(0, 4, "This")]
[InlineData(5, 2, "is")]
[InlineData(10, 4, "test")]
public static void AdjustSetsSegmentOfStream(int offset, int length, string expected)
{
using var ms = new MemoryStream(Encoding.UTF8.GetBytes("This is a test"));
using var segment = new StreamSegment(ms);
segment.Adjust(offset, length);
StreamReader reader = new(segment);
Equal(expected, reader.ReadToEnd());
}

[Fact]
public static void ReadByteSequentially()
{
Expand Down

0 comments on commit 235b2c0

Please sign in to comment.