using Amphora; using System; using System.IO; using UnitTestSharp; namespace Susurrus.UnitTests { public class ByteViewStreamTests : TestFixture { public static ByteViewStream Window() { return new ByteViewStream(new ByteView(StreamBytesTests.Counting(10), startIndex: 3, endIndex: 8)); } public class CtorTests : TestFixture { public void DefaultView_Throws() { CheckThrow(typeof(ArgumentException)); new ByteViewStream(default(ByteView)); } public void EmptyView_IsAllowed() { CheckEqual(0L, new ByteViewStream(new ByteView(new byte[0])).Length); } public void StartsAtTheBeginning() { CheckEqual(0L, Window().Position); } } public class CapabilityTests : TestFixture { public void CanReadAndSeekButNotWrite() { ByteViewStream stream = Window(); CheckTrue(stream.CanRead); CheckTrue(stream.CanSeek); CheckFalse(stream.CanWrite); } } public class LengthTests : TestFixture { public void IsTheViewsLengthNotTheArrays() { CheckEqual(5L, Window().Length); } } public class PositionTests : TestFixture { public void SetMovesTheNextRead() { ByteViewStream stream = Window(); stream.Position = 2; CheckEqual(5, stream.ReadByte()); } public void SetToTheEnd_IsAllowed() { ByteViewStream stream = Window(); stream.Position = 5; CheckEqual(-1, stream.ReadByte()); } public void SetPastTheEnd_Throws() { ByteViewStream stream = Window(); CheckThrow(typeof(ArgumentOutOfRangeException)); stream.Position = 6; } public void SetBelowZero_Throws() { ByteViewStream stream = Window(); CheckThrow(typeof(ArgumentOutOfRangeException)); stream.Position = -1; } public void AdvancesWithReads() { ByteViewStream stream = Window(); stream.Read(new byte[2], 0, 2); CheckEqual(2L, stream.Position); } } public class ReadTests : TestFixture { public void ReadsFromTheStartOfTheViewNotTheArray() { byte[] buffer = new byte[3]; CheckEqual(3, Window().Read(buffer, 0, 3)); CheckEqual(new byte[] { 3, 4, 5 }, buffer); } public void ContinuesWhereTheLastReadStopped() { ByteViewStream stream = Window(); stream.Read(new byte[2], 0, 2); byte[] buffer = new byte[2]; stream.Read(buffer, 0, 2); CheckEqual(new byte[] { 5, 6 }, buffer); } public void FillsFromTheBufferOffset() { byte[] buffer = new byte[4]; Window().Read(buffer, 1, 2); CheckEqual(new byte[] { 0, 3, 4, 0 }, buffer); } public void NearTheEnd_ReturnsOnlyWhatIsLeft() { ByteViewStream stream = Window(); stream.Position = 3; byte[] buffer = new byte[4]; CheckEqual(2, stream.Read(buffer, 0, 4)); CheckEqual(new byte[] { 6, 7, 0, 0 }, buffer); } public void AtTheEnd_ReturnsZero() { ByteViewStream stream = Window(); stream.Position = 5; CheckEqual(0, stream.Read(new byte[4], 0, 4)); } public void AskingForNothing_ReturnsZeroAndStaysPut() { ByteViewStream stream = Window(); CheckEqual(0, stream.Read(new byte[4], 0, 0)); CheckEqual(0L, stream.Position); } public void NullBuffer_Throws() { CheckThrow(typeof(ArgumentNullException)); Window().Read(null, 0, 1); } public void NegativeOffset_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Window().Read(new byte[4], -1, 1); } public void NegativeCount_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Window().Read(new byte[4], 0, -1); } public void RangePastTheBuffer_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Window().Read(new byte[4], 2, 3); } } public class SeekTests : TestFixture { public void FromTheBegin_IsAbsolute() { ByteViewStream stream = Window(); CheckEqual(3L, stream.Seek(3, SeekOrigin.Begin)); CheckEqual(6, stream.ReadByte()); } public void FromTheCurrent_IsRelative() { ByteViewStream stream = Window(); stream.Position = 4; CheckEqual(2L, stream.Seek(-2, SeekOrigin.Current)); CheckEqual(5, stream.ReadByte()); } public void FromTheEnd_CountsBackFromTheViewsEnd() { ByteViewStream stream = Window(); CheckEqual(4L, stream.Seek(-1, SeekOrigin.End)); CheckEqual(7, stream.ReadByte()); } public void ToTheEnd_IsAllowed() { CheckEqual(5L, Window().Seek(0, SeekOrigin.End)); } public void PastTheEnd_Throws() { ByteViewStream stream = Window(); stream.Position = 2; CheckThrow(typeof(ArgumentOutOfRangeException)); stream.Seek(4, SeekOrigin.Current); } public void PastTheEnd_LeavesThePositionAlone() { ByteViewStream stream = Window(); stream.Position = 2; try { stream.Seek(4, SeekOrigin.Current); } catch (ArgumentOutOfRangeException) { } CheckEqual(2L, stream.Position); } public void BelowZero_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Window().Seek(-1, SeekOrigin.Begin); } public void UnknownOrigin_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Window().Seek(0, (SeekOrigin)99); } } public class FlushTests : TestFixture { public void ChangesNothing() { ByteViewStream stream = Window(); stream.Position = 2; stream.Flush(); CheckEqual(2L, stream.Position); } } public class SetLengthTests : TestFixture { public void Throws() { CheckThrow(typeof(NotSupportedException)); Window().SetLength(3); } } public class WriteTests : TestFixture { public void Throws() { CheckThrow(typeof(NotSupportedException)); Window().Write(new byte[1], 0, 1); } } } }