Skip to content

Commit 187018c

Browse files
authored
Advance Position during MemoryStream.Read(SpanByte buffer) (#74)
1 parent fc047a0 commit 187018c

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

System.IO.Streams/MemoryStream.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,30 @@ public override long Position
195195

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

204-
int n = _length - _position;
202+
var bytesToRead = _length - _position;
205203

206-
if (n > buffer.Length)
204+
if (bytesToRead > buffer.Length)
207205
{
208-
n = buffer.Length;
206+
bytesToRead = buffer.Length;
209207
}
210208

211-
if (n <= 0)
209+
if (bytesToRead <= 0)
212210
{
213211
return 0;
214212
}
215213

216-
for(int i = 0; i < n; i++)
214+
for(var i = 0; i < bytesToRead; i++)
217215
{
218216
buffer[i] = _buffer[_position + i];
219217
}
220218

221-
return n;
219+
_position += bytesToRead;
220+
221+
return bytesToRead;
222222
}
223223

224224
/// <inheritdoc/>

System.IO.Streams/System.IO.Streams.nfproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
<Content Include="packages.lock.json" />
5454
</ItemGroup>
5555
<ItemGroup>
56-
<Reference Include="mscorlib, Version=1.16.11.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
56+
<Reference Include="mscorlib">
5757
<HintPath>..\packages\nanoFramework.CoreLibrary.1.16.11\lib\mscorlib.dll</HintPath>
5858
</Reference>
59-
<Reference Include="nanoFramework.System.Text, Version=1.3.16.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
59+
<Reference Include="nanoFramework.System.Text">
6060
<HintPath>..\packages\nanoFramework.System.Text.1.3.16\lib\nanoFramework.System.Text.dll</HintPath>
6161
</Reference>
6262
</ItemGroup>

UnitTests/MemoryStreamUnitTests/MemoryStreamUnitTests.nfproj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,28 @@
4646
<Compile Include="Properties\AssemblyInfo.cs" />
4747
</ItemGroup>
4848
<ItemGroup>
49-
<Reference Include="mscorlib, Version=1.16.11.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
49+
<Reference Include="mscorlib">
5050
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.16.11\lib\mscorlib.dll</HintPath>
5151
</Reference>
52-
<Reference Include="nanoFramework.System.Text, Version=1.3.16.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
52+
<Reference Include="nanoFramework.System.Text">
5353
<HintPath>..\..\packages\nanoFramework.System.Text.1.3.16\lib\nanoFramework.System.Text.dll</HintPath>
5454
</Reference>
55-
<Reference Include="nanoFramework.TestFramework, Version=3.0.60.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
55+
<Reference Include="nanoFramework.TestFramework">
5656
<HintPath>..\..\packages\nanoFramework.TestFramework.3.0.60\lib\nanoFramework.TestFramework.dll</HintPath>
5757
</Reference>
58-
<Reference Include="nanoFramework.UnitTestLauncher, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
58+
<Reference Include="nanoFramework.UnitTestLauncher">
5959
<HintPath>..\..\packages\nanoFramework.TestFramework.3.0.60\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
6060
</Reference>
6161
</ItemGroup>
6262
<ItemGroup>
63+
<ProjectReference Include="..\..\System.IO.Streams\System.IO.Streams.nfproj" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<None Include="nano.runsettings" />
6367
<None Include="packages.config" />
6468
</ItemGroup>
6569
<ItemGroup>
66-
<ProjectReference Include="..\..\System.IO.Streams\System.IO.Streams.nfproj" />
70+
<Content Include="packages.lock.json" />
6771
</ItemGroup>
6872
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
6973
<!-- MANUAL UPDATE HERE -->

UnitTests/MemoryStreamUnitTests/Read.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ private bool TestRead(MemoryStream ms, int BufferLength, int BytesToRead, int By
5656

5757
#endregion Local Helper methods
5858

59-
#region Test Cases
60-
6159
[TestMethod]
6260
public void InvalidCases()
6361
{
@@ -192,6 +190,19 @@ public void VanillaRead()
192190
}
193191
}
194192

195-
#endregion Test Cases
193+
[TestMethod]
194+
public void Read_SpanByte_advances_Position_property()
195+
{
196+
var data = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
197+
var buffer = new byte[4];
198+
199+
200+
using var sut = new MemoryStream(data);
201+
202+
var bytesRead = sut.Read(buffer);
203+
204+
Assert.AreEqual(buffer.Length, bytesRead);
205+
Assert.AreEqual(4, sut.Position);
206+
}
196207
}
197208
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<!-- Configurations that affect the Test Framework -->
4+
<RunConfiguration>
5+
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
6+
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
7+
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
8+
<TargetPlatform>x64</TargetPlatform>
9+
</RunConfiguration>
10+
<nanoFrameworkAdapter>
11+
<Logging>None</Logging> <!--Set to the desired level of logging for Unit Test execution. Possible values are: None, Detailed, Verbose, Error. -->
12+
<IsRealHardware>False</IsRealHardware><!--Set to true to run tests on real hardware. -->
13+
<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. -->
14+
<CLRVersion></CLRVersion><!--Specify the nanoCLR version to use. If not specified, the latest available will be used. -->
15+
<PathToLocalCLRInstance></PathToLocalCLRInstance><!--Specify the path to a local nanoCLR instance. If not specified, the default one installed with nanoclr CLR witll be used. -->
16+
</nanoFrameworkAdapter>
17+
</RunSettings>

0 commit comments

Comments
 (0)