using System; using UnitTestSharp; namespace Amphora.UnitTests { public unsafe class CharViewTests : TestFixture { public static char[] Letters(int length) { char[] returnMe = new char[length]; for (int i = 0; i < length; ++i) { returnMe[i] = (char)('a' + i); } return returnMe; } public class CtorTests : TestFixture { public void Range_ViewsFromStartUpToEnd() { CharView view = new CharView(Letters(10), startIndex: 2, endIndex: 5); CheckEqual(2, view.StartIndex); CheckEqual(5, view.EndIndex); CheckEqual(3, view.Length); } public void Range_AllowsAnEmptyRunAtTheEnd() { CheckEqual(0, new CharView(Letters(10), startIndex: 10, endIndex: 10).Length); } public void Range_NullArray_Throws() { CheckThrow(typeof(ArgumentNullException)); new CharView(null, startIndex: 0, endIndex: 0); } public void Range_NegativeStart_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(10), startIndex: -1, endIndex: 3); } public void Range_StartPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(10), startIndex: 11, endIndex: 11); } public void Range_NegativeEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(10), startIndex: 0, endIndex: -1); } public void Range_EndPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(10), startIndex: 0, endIndex: 11); } public void Range_EndBeforeStart_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(10), startIndex: 5, endIndex: 4); } public void WholeArray_ViewsEveryCharacter() { CharView view = new CharView(Letters(10)); CheckEqual(0, view.StartIndex); CheckEqual(10, view.EndIndex); } public void WholeArray_NullArray_Throws() { CheckThrow(typeof(ArgumentNullException)); new CharView(null); } public void Pointer_ViewsTheLengthFromIndexZero() { char* memory = stackalloc char[4]; CharView view = new CharView(memory, length: 4); CheckEqual(0, view.StartIndex); CheckEqual(4, view.EndIndex); } public void Pointer_NegativeLength_Throws() { char* memory = stackalloc char[4]; CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(memory, length: -1); } public void Pointer_NullWithALength_Throws() { CheckThrow(typeof(ArgumentNullException)); new CharView(null, length: 1); } public void Pointer_NullWithNoLength_IsAllowed() { CheckEqual(0, new CharView(null, length: 0).Length); } public void ImplicitFromArray_ViewsEveryCharacter() { CharView view = Letters(6); CheckEqual(6, view.Length); } public void ImplicitFromArray_NullArray_Throws() { char[] chars = null; CheckThrow(typeof(ArgumentNullException)); CharView view = chars; } } public class LengthTests : TestFixture { public void Array_IsTheRunBetweenStartAndEnd() { CheckEqual(4, new CharView(Letters(10), startIndex: 3, endIndex: 7).Length); } public void Pointer_IsTheLengthGiven() { char* memory = stackalloc char[8]; CheckEqual(8, new CharView(memory, length: 8).Length); } } public class EmptyTests : TestFixture { public void EmptyRun_IsEmpty() { CheckTrue(new CharView(Letters(10), startIndex: 4, endIndex: 4).Empty); } public void NonEmptyRun_IsNotEmpty() { CheckFalse(new CharView(Letters(10), startIndex: 4, endIndex: 5).Empty); } public void ZeroLengthPointer_IsEmpty() { char* memory = stackalloc char[1]; CheckTrue(new CharView(memory, length: 0).Empty); } } public class IsDefaultTests : TestFixture { public void Default_IsDefault() { CheckTrue(default(CharView).IsDefault); } public void EmptyRunOfAnArray_IsNotDefault() { CheckFalse(new CharView(new char[0]).IsDefault); } public void ZeroLengthPointer_IsNotDefault() { char* memory = stackalloc char[1]; CheckFalse(new CharView(memory, length: 0).IsDefault); } } public class GetPinnableReferenceTests : TestFixture { public void Array_PinsTheFirstCharacterOfTheRun() { CharView view = new CharView(Letters(10), startIndex: 3, endIndex: 6); fixed (char* pinned = view) { CheckEqual('d', pinned[0]); CheckEqual('f', pinned[2]); } } public void Array_WritesThroughThePointerReachTheArray() { char[] chars = Letters(10); CharView view = new CharView(chars, startIndex: 3, endIndex: 6); fixed (char* pinned = view) { pinned[1] = 'Z'; } CheckEqual('Z', chars[4]); } public void Pointer_PinsTheMemoryItself() { char* memory = stackalloc char[4]; CharView view = new CharView(memory, length: 4); fixed (char* pinned = view) { pinned[2] = 'Q'; CheckTrue(memory == pinned); } CheckEqual('Q', memory[2]); } public void Empty_PinsACharacterOfNoConsequence() { fixed (char* pinned = new CharView(Letters(3), startIndex: 3, endIndex: 3)) { CheckTrue(pinned != null); } } public void Default_PinsACharacterOfNoConsequence() { fixed (char* pinned = default(CharView)) { CheckTrue(pinned != null); } } } public class IndexerTests : TestFixture { public void Array_IsCountedFromTheStartOfTheRun() { CharView view = new CharView(Letters(10), startIndex: 3, endIndex: 6); CheckEqual('d', view[0]); CheckEqual('f', view[2]); } public void Array_WritesReachTheArray() { char[] chars = Letters(10); CharView view = new CharView(chars, startIndex: 3, endIndex: 6); view[1] = 'Z'; CheckEqual('Z', chars[4]); } public void Pointer_ReadsAndWritesTheMemory() { char* memory = stackalloc char[3]; CharView view = new CharView(memory, length: 3); view[2] = 'Q'; CheckEqual('Q', memory[2]); CheckEqual('Q', view[2]); } public void NegativeIndex_Throws() { CharView view = new CharView(Letters(10), startIndex: 3, endIndex: 6); CheckThrow(typeof(ArgumentOutOfRangeException)); char ignored = view[-1]; } public void IndexAtTheEndOfTheRun_Throws() { CharView view = new CharView(Letters(10), startIndex: 3, endIndex: 6); CheckThrow(typeof(ArgumentOutOfRangeException)); char ignored = view[3]; } public void Pointer_IndexAtTheEnd_Throws() { char* memory = stackalloc char[3]; CharView view = new CharView(memory, length: 3); CheckThrow(typeof(ArgumentOutOfRangeException)); char ignored = view[3]; } } public class CopyFromTests : TestFixture { public void Array_WritesFromTheIndexOfTheRun() { char[] chars = Letters(10); CharView view = new CharView(chars, startIndex: 2, endIndex: 8); view.CopyFrom("XY", index: 1); CheckEqual("abcXYfghij", new string(chars)); } public void Pointer_WritesFromTheIndex() { char* memory = stackalloc char[4]; CharView view = new CharView(memory, length: 4); view.CopyFrom("XY", index: 2); CheckEqual('X', memory[2]); CheckEqual('Y', memory[3]); } public void TextFillingTheRestOfTheView_Fits() { char[] chars = Letters(4); new CharView(chars).CopyFrom("XYZ", index: 1); CheckEqual("aXYZ", new string(chars)); } public void NoText_AtTheEndOfTheView_Fits() { char[] chars = Letters(4); new CharView(chars).CopyFrom("", index: 4); CheckEqual("abcd", new string(chars)); } public void NullText_Throws() { CheckThrow(typeof(ArgumentNullException)); new CharView(Letters(4)).CopyFrom((string)null, index: 0); } public void NegativeIndex_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).CopyFrom("X", index: -1); } public void IndexPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).CopyFrom("", index: 5); } public void TextPastTheEndOfTheRun_Throws() { CharView view = new CharView(Letters(10), startIndex: 2, endIndex: 5); CheckThrow(typeof(ArgumentException)); view.CopyFrom("XY", index: 2); } public void TextPastTheEndOfTheRun_WritesNothing() { char[] chars = Letters(10); CharView view = new CharView(chars, startIndex: 2, endIndex: 5); try { view.CopyFrom("XY", index: 2); } catch (ArgumentException) { } CheckEqual("abcdefghij", new string(chars)); } } public class CopyFromStringViewTests : TestFixture { public void Array_WritesTheViewsCharactersFromTheIndex() { char[] chars = Letters(10); var text = new StringView("12345", startIndex: 1, endIndex: 3); new CharView(chars, startIndex: 2, endIndex: 8).CopyFrom(text, index: 1); CheckEqual("abc23fghij", new string(chars)); } public void Pointer_WritesTheViewsCharactersFromTheIndex() { char* memory = stackalloc char[4]; var text = new StringView(Letters(3)); new CharView(memory, length: 4).CopyFrom(text, index: 1); CheckEqual('a', memory[1]); CheckEqual('c', memory[3]); } public void ADefaultView_WritesNothing() { char[] chars = Letters(4); new CharView(chars).CopyFrom(default(StringView), index: 4); CheckEqual("abcd", new string(chars)); } public void TextPastTheEndOfTheRun_Throws() { CharView view = new CharView(Letters(10), startIndex: 2, endIndex: 5); CheckThrow(typeof(ArgumentException)); view.CopyFrom(new StringView("XY"), index: 2); } public void IndexPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).CopyFrom(new StringView("X"), index: 5); } } public class CopyFromCharViewTests : TestFixture { public void Array_WritesTheViewsCharactersFromTheIndex() { char[] chars = Letters(10); var text = new CharView(Letters(5), startIndex: 1, endIndex: 3); new CharView(chars, startIndex: 2, endIndex: 8).CopyFrom(text, index: 1); CheckEqual("abcbcfghij", new string(chars)); } public void Pointer_WritesToAndFromMemory() { char* from = stackalloc char[3]; char* to = stackalloc char[4]; from[0] = 'x'; from[1] = 'y'; from[2] = 'z'; new CharView(to, length: 4).CopyFrom(new CharView(from, length: 3), index: 1); CheckEqual('x', to[1]); CheckEqual('z', to[3]); } public void OverlappingForward_CopiesWhatWasThere() { char[] chars = Letters(6); new CharView(chars).CopyFrom(new CharView(chars, startIndex: 0, endIndex: 4), index: 2); CheckEqual("ababcd", new string(chars)); } public void OverlappingBackward_CopiesWhatWasThere() { char[] chars = Letters(6); new CharView(chars).CopyFrom(new CharView(chars, startIndex: 2, endIndex: 6), index: 0); CheckEqual("cdefef", new string(chars)); } public void ADefaultView_WritesNothing() { char[] chars = Letters(4); new CharView(chars).CopyFrom(default(CharView), index: 4); CheckEqual("abcd", new string(chars)); } public void TextPastTheEndOfTheRun_Throws() { CharView view = new CharView(Letters(10), startIndex: 2, endIndex: 5); CheckThrow(typeof(ArgumentException)); view.CopyFrom(new CharView(Letters(2)), index: 2); } public void IndexPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).CopyFrom(new CharView(Letters(1)), index: 5); } } public class ToStringTests : TestFixture { public void Array_IsTheFirstCharactersOfTheRun() { CharView view = new CharView(Letters(10), startIndex: 3, endIndex: 8); CheckEqual("de", view.ToString(2)); } public void Pointer_IsTheFirstCharactersOfTheMemory() { char* memory = stackalloc char[3]; memory[0] = 'x'; memory[1] = 'y'; memory[2] = 'z'; CheckEqual("xy", new CharView(memory, length: 3).ToString(2)); } public void NoCharacters_IsTheEmptyString() { CheckTrue(ReferenceEquals(string.Empty, new CharView(Letters(4)).ToString(0))); } public void Default_WithNoCharacters_IsTheEmptyString() { CheckEqual("", default(CharView).ToString(0)); } public void TheWholeRun_IsAllowed() { CheckEqual("abc", new CharView(Letters(3)).ToString(3)); } public void NegativeCount_Throws() { CharView view = new CharView(Letters(4)); CheckThrow(typeof(ArgumentOutOfRangeException)); view.ToString(-1); } public void CountPastTheRun_Throws() { CharView view = new CharView(Letters(10), startIndex: 3, endIndex: 6); CheckThrow(typeof(ArgumentOutOfRangeException)); view.ToString(4); } } public class RequireRoomTests : TestFixture { public void CharactersThatFillTheRun_Fit() { new CharView(Letters(10), startIndex: 2, endIndex: 6).RequireRoom(count: 4, index: 0); } public void CharactersThatFillTheRestOfTheRun_Fit() { new CharView(Letters(10), startIndex: 2, endIndex: 6).RequireRoom(count: 1, index: 3); } public void NoCharactersAtTheEnd_Fit() { new CharView(Letters(4)).RequireRoom(count: 0, index: 4); } public void ADefaultView_FitsNoCharacters() { default(CharView).RequireRoom(count: 0, index: 0); } public void OneCharacterTooMany_Throws() { CheckThrow(typeof(ArgumentException)); new CharView(Letters(10), startIndex: 2, endIndex: 6).RequireRoom(count: 5, index: 0); } public void CharactersThatRunPastTheEndFromTheIndex_Throw() { CheckThrow(typeof(ArgumentException)); new CharView(Letters(10), startIndex: 2, endIndex: 6).RequireRoom(count: 2, index: 3); } public void AnyCharactersInADefaultView_Throw() { CheckThrow(typeof(ArgumentException)); default(CharView).RequireRoom(count: 1, index: 0); } public void AnIndexPastTheEnd_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).RequireRoom(count: 0, index: 5); } public void ANegativeIndex_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).RequireRoom(count: 1, index: -1); } public void ANegativeCount_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new CharView(Letters(4)).RequireRoom(count: -1, index: 0); } public void APointerViewIsChecked_AsAnArrayViewIs() { char* memory = stackalloc char[4]; var view = new CharView(memory, 4); view.RequireRoom(count: 4, index: 0); CheckThrow(typeof(ArgumentException)); view.RequireRoom(count: 4, index: 1); } } } }