generated from dotnet/new-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dotutils/dev/add-unit-tests
Add unit tests and fix failures
- Loading branch information
Showing
8 changed files
with
497 additions
and
5 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
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
83 changes: 83 additions & 0 deletions
83
test/DotUtils.StreamUtils.Tests/ChunkedBufferStreamTests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using static DotUtils.StreamUtils.Tests.StreamTestExtensions; | ||
|
||
namespace DotUtils.StreamUtils.Tests | ||
{ | ||
public class ChunkedBufferStreamTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(StreamTestExtensions.EnumerateReadFunctionTypes), MemberType = typeof(StreamTestExtensions))] | ||
public void Write_CusesChunking(StreamFunctionType streamFunctionType) | ||
{ | ||
int chunkSize = 3; | ||
byte[] bytes = new byte[100]; | ||
using MemoryStream ms = new(bytes); | ||
using Stream stream = new ChunkedBufferStream(ms, chunkSize); | ||
|
||
WriteBytes writeBytes = stream.GetWriteFunc(streamFunctionType); | ||
|
||
writeBytes(new byte[]{1,2}); | ||
bytes.Should().AllBeEquivalentTo(0); | ||
|
||
writeBytes(new byte[] { 3, 4 }); | ||
bytes.Take(3).Should().BeEquivalentTo(new byte[] { 1, 2, 3 }); | ||
bytes.Skip(3).Should().AllBeEquivalentTo(0); | ||
|
||
writeBytes(new byte[] { 5, 6 }); | ||
bytes.Take(6).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6 }); | ||
bytes.Skip(6).Should().AllBeEquivalentTo(0); | ||
|
||
writeBytes(new byte[] { 7, 8 }); | ||
bytes.Take(6).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6 }); | ||
bytes.Skip(6).Should().AllBeEquivalentTo(0); | ||
|
||
writeBytes(new byte[] { 9, 10, 11, 12, 13, 14, 15, 16 }); | ||
bytes.Take(15).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }); | ||
bytes.Skip(15).Should().AllBeEquivalentTo(0); | ||
|
||
stream.Flush(); | ||
bytes.Take(16).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }); | ||
bytes.Skip(16).Should().AllBeEquivalentTo(0); | ||
} | ||
|
||
[Fact] | ||
public void WriteByte_CusesChunking() | ||
{ | ||
int chunkSize = 3; | ||
var bytes = new byte[100]; | ||
using MemoryStream ms = new(bytes); | ||
using Stream stream = new ChunkedBufferStream(ms, chunkSize); | ||
|
||
stream.WriteByte(1); | ||
bytes.Should().AllBeEquivalentTo(0); | ||
stream.WriteByte(2); | ||
bytes.Should().AllBeEquivalentTo(0); | ||
|
||
stream.WriteByte(3); | ||
bytes.Take(3).Should().BeEquivalentTo(new byte[] { 1, 2, 3 }); | ||
bytes.Skip(3).Should().AllBeEquivalentTo(0); | ||
|
||
stream.WriteByte(4); | ||
stream.WriteByte(5); | ||
bytes.Take(3).Should().BeEquivalentTo(new byte[] { 1, 2, 3 }); | ||
bytes.Skip(3).Should().AllBeEquivalentTo(0); | ||
|
||
stream.WriteByte(6); | ||
bytes.Take(6).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6 }); | ||
bytes.Skip(6).Should().AllBeEquivalentTo(0); | ||
|
||
stream.WriteByte(7); | ||
bytes.Take(6).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6 }); | ||
bytes.Skip(6).Should().AllBeEquivalentTo(0); | ||
|
||
stream.Flush(); | ||
bytes.Take(7).Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6, 7 }); | ||
bytes.Skip(7).Should().AllBeEquivalentTo(0); | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
test/DotUtils.StreamUtils.Tests/ConcatenatedReadStreamTests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using static DotUtils.StreamUtils.Tests.StreamTestExtensions; | ||
|
||
namespace DotUtils.StreamUtils.Tests | ||
{ | ||
public class ConcatenatedReadStreamTests | ||
{ | ||
[Fact] | ||
public void ReadByte_ReadsStreamSequentialy() | ||
{ | ||
using MemoryStream ms1 = new(new byte[]{1, 2, 3}); | ||
using MemoryStream ms2 = new(new byte[] { 4 }); | ||
using MemoryStream ms3 = new(new byte[] { 5, 6 }); | ||
|
||
Stream stream = new ConcatenatedReadStream(ms1, ms2, ms3); | ||
|
||
stream.ReadByte().Should().Be(1); | ||
stream.ReadByte().Should().Be(2); | ||
stream.ReadByte().Should().Be(3); | ||
stream.ReadByte().Should().Be(4); | ||
stream.ReadByte().Should().Be(5); | ||
stream.ReadByte().Should().Be(6); | ||
|
||
// cannot read anymore | ||
stream.ReadByte().Should().Be(-1); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(StreamTestExtensions.EnumerateReadFunctionTypes), MemberType = typeof(StreamTestExtensions))] | ||
public void Read_ReadsStreamSequentialy(StreamFunctionType streamFunctionType) | ||
{ | ||
using MemoryStream ms1 = new(new byte[] { 1, 2, 3 }); | ||
using MemoryStream ms2 = new(new byte[] { 4 }); | ||
using MemoryStream ms3 = new(new byte[] { 5, 6 }); | ||
using MemoryStream ms4 = new(new byte[] { 7, 8, 9 }); | ||
|
||
Stream stream = new ConcatenatedReadStream(ms1, ms2, ms3, ms4); | ||
|
||
ReadBytes readBytes = stream.GetReadFunc(streamFunctionType); | ||
|
||
var readBuffer = new byte[2]; | ||
|
||
readBytes(readBuffer).Should().Be(2); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 1, 2 }); | ||
|
||
readBytes(readBuffer).Should().Be(2); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 3, 4 }); | ||
|
||
readBytes(readBuffer).Should().Be(2); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 5, 6 }); | ||
|
||
readBytes(readBuffer).Should().Be(2); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 7, 8 }); | ||
|
||
// zero out for assertion clarity. | ||
Array.Clear(readBuffer); | ||
|
||
readBytes(readBuffer).Should().Be(1); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 9, 0 }); | ||
|
||
// zero out for assertion clarity. | ||
Array.Clear(readBuffer); | ||
|
||
// cannot read anymore | ||
readBytes(readBuffer).Should().Be(0); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 0, 0 }); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(StreamTestExtensions.EnumerateReadFunctionTypes), MemberType = typeof(StreamTestExtensions))] | ||
public void Read_ReadsStreamSequentialy_UsingMultipleSubstreams(StreamFunctionType streamFunctionType) | ||
{ | ||
using MemoryStream ms1 = new(new byte[] { 1, 2, 3 }); | ||
using MemoryStream ms2 = new(new byte[] { 4 }); | ||
using MemoryStream ms3 = new(new byte[] { 5, 6 }); | ||
using MemoryStream ms4 = new(new byte[] { 7, 8, 9, 10, 11 }); | ||
|
||
Stream stream = new ConcatenatedReadStream(ms1, ms2, ms3, ms4); | ||
|
||
ReadBytes readBytes = stream.GetReadFunc(streamFunctionType); | ||
|
||
var readBuffer = new byte[5]; | ||
|
||
readBytes(readBuffer).Should().Be(5); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5 }); | ||
|
||
readBytes(readBuffer).Should().Be(5); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 6, 7, 8, 9, 10 }); | ||
|
||
// zero out for assertion clarity. | ||
Array.Clear(readBuffer); | ||
|
||
readBytes(readBuffer).Should().Be(1); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 11, 0, 0, 0, 0 }); | ||
|
||
// zero out for assertion clarity. | ||
Array.Clear(readBuffer); | ||
|
||
// cannot read anymore | ||
readBytes(readBuffer).Should().Be(0); | ||
readBuffer.Should().BeEquivalentTo(new byte[] { 0, 0, 0, 0, 0 }); | ||
} | ||
} | ||
} |
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,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotUtils.StreamUtils.Tests | ||
{ | ||
public delegate int ReadBytes(byte[] bytes); | ||
|
||
public delegate void WriteBytes(byte[] bytes); | ||
|
||
|
||
public static class StreamTestExtensions | ||
{ | ||
public enum StreamFunctionType | ||
{ | ||
Array, | ||
Span, | ||
AsyncArray, | ||
AsyncMemory, | ||
} | ||
|
||
public static ReadBytes GetReadFunc(this Stream stream, StreamFunctionType streamFunctionType) | ||
=> streamFunctionType switch | ||
{ | ||
StreamFunctionType.Array => (bytes) => stream.Read(bytes, 0, bytes.Length), | ||
StreamFunctionType.Span => (bytes) => stream.Read(bytes), | ||
// We do sync over async here - since we are interested in just exercising the method code in tests. | ||
StreamFunctionType.AsyncArray => (bytes) => | ||
stream.ReadAsync(bytes, 0, bytes.Length, CancellationToken.None).Result, | ||
StreamFunctionType.AsyncMemory => (bytes) => stream.ReadAsync(bytes, CancellationToken.None).Result, | ||
_ => throw new ArgumentOutOfRangeException(nameof(streamFunctionType), streamFunctionType, null) | ||
}; | ||
|
||
public static WriteBytes GetWriteFunc(this Stream stream, StreamFunctionType streamFunctionType) | ||
=> streamFunctionType switch | ||
{ | ||
StreamFunctionType.Array => (bytes) => stream.Write(bytes, 0, bytes.Length), | ||
StreamFunctionType.Span => (bytes) => stream.Write(bytes), | ||
// We do sync over async here - since we are interested in just exercising the method code in tests. | ||
StreamFunctionType.AsyncArray => (bytes) => | ||
stream.WriteAsync(bytes, 0, bytes.Length, CancellationToken.None).Wait(), | ||
StreamFunctionType.AsyncMemory => (bytes) => stream.WriteAsync(bytes, CancellationToken.None).AsTask().Wait(), | ||
_ => throw new ArgumentOutOfRangeException(nameof(streamFunctionType), streamFunctionType, null) | ||
}; | ||
|
||
public static IEnumerable<object[]> EnumerateReadFunctionTypes() | ||
=> Enum.GetValues<StreamFunctionType>().Select(v => new object[] { v }); | ||
} | ||
} |
Oops, something went wrong.