-
Notifications
You must be signed in to change notification settings - Fork 18
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 #28 from jongleur1983/master
#13 unit tests for StreamExtensions
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
Source/MoreDotNet.Test/Extensions/Common/StreamExtensions/ToByteArrayTests.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,50 @@ | ||
namespace MoreDotNet.Tests.Extensions.Common.StreamExtensions | ||
{ | ||
using System; | ||
using System.IO; | ||
using MoreDotNet.Extensions.Common; | ||
using Xunit; | ||
|
||
public class ToByteArrayTests | ||
{ | ||
[Fact] | ||
public void ToByteArray_OnNullInput_ShouldThrowArgumentNullException() | ||
{ | ||
Stream input = null; | ||
Assert.Throws<ArgumentNullException>(() => input.ToByteArray()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new byte[0])] | ||
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })] | ||
[InlineData(new byte[] { 0, 255, 4, 123 })] | ||
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })] | ||
public void ToByteArray_OnStream_ShouldReturnCorrectByteArray(byte[] expectedByteArray) | ||
{ | ||
var stream = new MemoryStream(expectedByteArray); | ||
Assert.Equal(expectedByteArray, stream.ToByteArray()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(10)] | ||
[InlineData(100)] | ||
[InlineData(1000)] | ||
[InlineData(10000)] | ||
[InlineData(100000)] | ||
[InlineData(1000000)] | ||
[InlineData(10000000)] | ||
public void ToByteArray_OnStreamsOfReallyBigSize_ShouldReturnCorrectByteArray(int length) | ||
{ | ||
var array = new byte[length]; | ||
for (int i = 0; i < length; i++) | ||
{ | ||
array[i] = (byte)(i % 256); | ||
} | ||
|
||
var stream = new MemoryStream(array); | ||
Assert.Equal(array, stream.ToByteArray()); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Source/MoreDotNet.Test/Extensions/Common/StreamExtensions/ToStreamTests.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,38 @@ | ||
namespace MoreDotNet.Tests.Extensions.Common.StreamExtensions | ||
{ | ||
using System; | ||
using System.IO; | ||
using MoreDotNet.Extensions.Common; | ||
using Xunit; | ||
|
||
public class ToStreamTests | ||
{ | ||
[Fact] | ||
public void ToStream_OnNullInput_ShouldThrowArgumentNullException() | ||
{ | ||
byte[] input = null; | ||
Assert.Throws<ArgumentNullException>(() => input.ToStream()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new byte[0])] | ||
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })] | ||
[InlineData(new byte[] { 0, 255, 4, 123 })] | ||
[InlineData(new byte[] { 0, 1, 2, 3, 4, 5 })] | ||
public void ToStream_OnByteArray_ReturnsCorrectStream(byte[] inputByteArray) | ||
{ | ||
var expectedStream = new MemoryStream(inputByteArray); | ||
var actualStream = inputByteArray.ToStream(); | ||
|
||
var expectedBuffer = new byte[16]; | ||
int expected; | ||
var actualBuffer = new byte[16]; | ||
|
||
while ((expected = expectedStream.Read(expectedBuffer, 0, expectedBuffer.Length)) > 0) | ||
{ | ||
var actual = actualStream.Read(actualBuffer, 0, actualBuffer.Length); | ||
Assert.Equal(expected, actual); | ||
} | ||
} | ||
} | ||
} |
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