From 235b2c0a2a7b4aeca22cc0622852ae681ddc0aae Mon Sep 17 00:00:00 2001 From: Kevin Bost Date: Sun, 25 Feb 2024 00:26:15 -0800 Subject: [PATCH] Fixing issue with StreamSegment.Adjust 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. --- src/DotNext.IO/IO/StreamSegment.cs | 4 ++-- src/DotNext.Tests/IO/StreamSegmentTests.cs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/DotNext.IO/IO/StreamSegment.cs b/src/DotNext.IO/IO/StreamSegment.cs index 58155d96d..58882666e 100644 --- a/src/DotNext.IO/IO/StreamSegment.cs +++ b/src/DotNext.IO/IO/StreamSegment.cs @@ -40,11 +40,11 @@ public StreamSegment(Stream stream, bool leaveOpen = true) /// /// The offset in the underlying stream. /// The length of the segment. - /// is larger than the reamining length of the underlying stream; or if greater than the length of the underlying stream. + /// is larger than the remaining length of the underlying stream; or if greater than the length of the underlying stream. 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; diff --git a/src/DotNext.Tests/IO/StreamSegmentTests.cs b/src/DotNext.Tests/IO/StreamSegmentTests.cs index a9b81b730..e1f6326b6 100644 --- a/src/DotNext.Tests/IO/StreamSegmentTests.cs +++ b/src/DotNext.Tests/IO/StreamSegmentTests.cs @@ -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() {