-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for copying readable streams to writable streams and ad…
…ded test coverage.
- Loading branch information
1 parent
ea7d495
commit 5adb318
Showing
7 changed files
with
206 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
Schema Tests/util/SubstreamSharpTests.cs → ...l/streams/RangedReadableSubstreamTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.IO; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace schema.util.streams { | ||
public class ReadableStreamTests { | ||
[Test] | ||
public void TestPosition() { | ||
var ms = new MemoryStream(new byte[] { 1, 2, 3 }); | ||
var rs = new ReadableStream(ms); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, rs.Position); | ||
|
||
rs.Position = 2; | ||
Assert.AreEqual(2, ms.Position); | ||
Assert.AreEqual(2, rs.Position); | ||
} | ||
|
||
[Test] | ||
public void TestLength() { | ||
var ms = new MemoryStream(new byte[] { 1, 2, 3 }); | ||
var rs = new ReadableStream(ms); | ||
|
||
Assert.AreEqual(3, ms.Length); | ||
Assert.AreEqual(3, rs.Length); | ||
} | ||
|
||
[Test] | ||
public void TestReadByte() { | ||
var ms = new MemoryStream(new byte[] { 1, 2, 3 }); | ||
var rs = new ReadableStream(ms); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, rs.Position); | ||
|
||
Assert.AreEqual(1, rs.ReadByte()); | ||
Assert.AreEqual(1, ms.Position); | ||
Assert.AreEqual(1, rs.Position); | ||
|
||
Assert.AreEqual(2, rs.ReadByte()); | ||
Assert.AreEqual(2, ms.Position); | ||
Assert.AreEqual(2, rs.Position); | ||
} | ||
|
||
[Test] | ||
public void TestReadSpan() { | ||
var ms = new MemoryStream(new byte[] { 1, 2, 3 }); | ||
var rs = new ReadableStream(ms); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, rs.Position); | ||
|
||
Span<byte> span = stackalloc byte[5]; | ||
|
||
Assert.AreEqual(3, rs.Read(span)); | ||
Assert.AreEqual(3, ms.Position); | ||
Assert.AreEqual(3, rs.Position); | ||
|
||
CollectionAssert.AreEqual(new[] { 1, 2, 3, 0, 0 }, span.ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace schema.util.streams { | ||
public class WritableStreamTests { | ||
[Test] | ||
public void TestPosition() { | ||
var ms = new MemoryStream(new byte[] { 1, 2, 3 }); | ||
var ws = new WritableStream(ms); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, ws.Position); | ||
|
||
ws.Position = 2; | ||
Assert.AreEqual(2, ms.Position); | ||
Assert.AreEqual(2, ws.Position); | ||
} | ||
|
||
[Test] | ||
public void TestLength() { | ||
var ms = new MemoryStream(new byte[] { 1, 2, 3 }); | ||
var ws = new WritableStream(ms); | ||
|
||
Assert.AreEqual(3, ms.Length); | ||
Assert.AreEqual(3, ws.Length); | ||
} | ||
|
||
[Test] | ||
public void TestWriteByte() { | ||
var data = new byte[] { 1, 2, 3 }; | ||
var ms = new MemoryStream(data); | ||
var ws = new WritableStream(ms); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, ws.Position); | ||
|
||
ws.WriteByte(5); | ||
Assert.AreEqual(5, data[0]); | ||
Assert.AreEqual(1, ms.Position); | ||
Assert.AreEqual(1, ws.Position); | ||
|
||
ws.WriteByte(6); | ||
Assert.AreEqual(6, data[1]); | ||
Assert.AreEqual(2, ms.Position); | ||
Assert.AreEqual(2, ws.Position); | ||
} | ||
|
||
[Test] | ||
public void TestWriteSpan() { | ||
var ms = new MemoryStream(); | ||
var ws = new WritableStream(ms); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, ws.Position); | ||
|
||
ReadOnlySpan<byte> span = stackalloc byte[5] { 5, 6, 7, 8, 9 }; | ||
|
||
ws.Write(span); | ||
Assert.AreEqual(5, ms.Position); | ||
Assert.AreEqual(5, ws.Position); | ||
|
||
CollectionAssert.AreEqual(new[] { 5, 6, 7, 8, 9 }, ms.ToArray()); | ||
} | ||
|
||
[Test] | ||
public void TestWriteReadableStream() { | ||
var ms = new MemoryStream(); | ||
var ws = new WritableStream(ms); | ||
var rs = new ReadableStream(new byte[] { 5, 6, 7, 8, 9 }); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, ws.Position); | ||
Assert.AreEqual(0, rs.Position); | ||
|
||
ws.Write(rs); | ||
Assert.AreEqual(rs.Length, ms.Position); | ||
Assert.AreEqual(rs.Length, ws.Position); | ||
Assert.AreEqual(rs.Length, rs.Position); | ||
|
||
CollectionAssert.AreEqual(new[] { 5, 6, 7, 8, 9 }, ms.ToArray()); | ||
} | ||
|
||
[Test] | ||
public void TestWriteLongReadableStream() { | ||
var readData = | ||
Enumerable.Range(0, 300_000).Select(i => (byte) i).ToArray(); | ||
|
||
var ms = new MemoryStream(); | ||
var ws = new WritableStream(ms); | ||
var rs = new ReadableStream(readData); | ||
|
||
Assert.AreEqual(0, ms.Position); | ||
Assert.AreEqual(0, ws.Position); | ||
Assert.AreEqual(0, rs.Position); | ||
|
||
ws.Write(rs); | ||
Assert.AreEqual(readData.Length, ms.Position); | ||
Assert.AreEqual(readData.Length, ws.Position); | ||
Assert.AreEqual(readData.Length, rs.Position); | ||
|
||
CollectionAssert.AreEqual(readData, ms.ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters