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

Advance Position during MemoryStream.Read(SpanByte buffer) #74

Merged
merged 2 commits into from
Feb 4, 2025
Merged
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
16 changes: 8 additions & 8 deletions System.IO.Streams/MemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,30 +195,30 @@ public override long Position

/// <inheritdoc/>
/// <exception cref="ObjectDisposedException">The current stream instance is closed.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="count"/> is negative.</exception>
/// <exception cref="ArgumentException"><paramref name="offset"/> subtracted from the buffer length is less than <paramref name="count"/>.</exception>
public override int Read(SpanByte buffer)
{
EnsureOpen();

int n = _length - _position;
var bytesToRead = _length - _position;

if (n > buffer.Length)
if (bytesToRead > buffer.Length)
{
n = buffer.Length;
bytesToRead = buffer.Length;
}

if (n <= 0)
if (bytesToRead <= 0)
{
return 0;
}

for(int i = 0; i < n; i++)
for(var i = 0; i < bytesToRead; i++)
{
buffer[i] = _buffer[_position + i];
}

return n;
_position += bytesToRead;

return bytesToRead;
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions System.IO.Streams/System.IO.Streams.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
<Content Include="packages.lock.json" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib, Version=1.16.11.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="mscorlib">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.16.11\lib\mscorlib.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.3.16.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.System.Text">
<HintPath>..\packages\nanoFramework.System.Text.1.3.16\lib\nanoFramework.System.Text.dll</HintPath>
</Reference>
</ItemGroup>
Expand Down
14 changes: 9 additions & 5 deletions UnitTests/MemoryStreamUnitTests/MemoryStreamUnitTests.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,28 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib, Version=1.16.11.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="mscorlib">
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.16.11\lib\mscorlib.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.3.16.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.System.Text">
<HintPath>..\..\packages\nanoFramework.System.Text.1.3.16\lib\nanoFramework.System.Text.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.TestFramework, Version=3.0.60.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.TestFramework">
<HintPath>..\..\packages\nanoFramework.TestFramework.3.0.60\lib\nanoFramework.TestFramework.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<Reference Include="nanoFramework.UnitTestLauncher">
<HintPath>..\..\packages\nanoFramework.TestFramework.3.0.60\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.IO.Streams\System.IO.Streams.nfproj" />
</ItemGroup>
<ItemGroup>
<None Include="nano.runsettings" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.IO.Streams\System.IO.Streams.nfproj" />
<Content Include="packages.lock.json" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<!-- MANUAL UPDATE HERE -->
Expand Down
17 changes: 14 additions & 3 deletions UnitTests/MemoryStreamUnitTests/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ private bool TestRead(MemoryStream ms, int BufferLength, int BytesToRead, int By

#endregion Local Helper methods

#region Test Cases

[TestMethod]
public void InvalidCases()
{
Expand Down Expand Up @@ -192,6 +190,19 @@ public void VanillaRead()
}
}

#endregion Test Cases
[TestMethod]
public void Read_SpanByte_advances_Position_property()
{
var data = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
var buffer = new byte[4];


using var sut = new MemoryStream(data);

var bytesRead = sut.Read(buffer);

Assert.AreEqual(buffer.Length, bytesRead);
Assert.AreEqual(4, sut.Position);
}
}
}
17 changes: 17 additions & 0 deletions UnitTests/MemoryStreamUnitTests/nano.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
<TargetPlatform>x64</TargetPlatform>
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>None</Logging> <!--Set to the desired level of logging for Unit Test execution. Possible values are: None, Detailed, Verbose, Error. -->
<IsRealHardware>False</IsRealHardware><!--Set to true to run tests on real hardware. -->
<RealHardwarePort>COM3</RealHardwarePort><!--Specify the COM port to use to connect to a nanoDevice. If none is specified, a device detection is performed and the 1st available one will be used. -->
<CLRVersion></CLRVersion><!--Specify the nanoCLR version to use. If not specified, the latest available will be used. -->
<PathToLocalCLRInstance></PathToLocalCLRInstance><!--Specify the path to a local nanoCLR instance. If not specified, the default one installed with nanoclr CLR witll be used. -->
</nanoFrameworkAdapter>
</RunSettings>