using System; namespace Amphora.UnitTests { public class ReinterpretArrayTests : UnitTestSharp.TestFixture { public class AsIntsTests : UnitTestSharp.TestFixture { public void ReturnsTheSameArray() { var keys = new uint[] { 1, 2, 3 }; CheckTrue(ReferenceEquals(keys, ReinterpretArray.AsInts(keys))); } public void IndexingReadsTheSameBitsAsInts() { int[] ints = ReinterpretArray.AsInts(new uint[] { 0, 5, uint.MaxValue, 1u << 31 }); CheckEqual(0, ints[0]); CheckEqual(5, ints[1]); CheckEqual(-1, ints[2]); CheckEqual(int.MinValue, ints[3]); } public void EnumeratingAsACollectionStillSeesUints() { System.Collections.IEnumerable ints = ReinterpretArray.AsInts(new uint[] { uint.MaxValue }); foreach (object element in ints) { CheckEqual(typeof(uint), element.GetType()); } } public void KeepsTheLength() { CheckEqual(7, ReinterpretArray.AsInts(new uint[7]).Length); } public void AWriteThroughTheIntsShowsInTheUints() { var keys = new uint[3]; ReinterpretArray.AsInts(keys)[1] = -1; CheckEqual(uint.MaxValue, keys[1]); } public void AnEmptyArrayStaysEmpty() { CheckEqual(0, ReinterpretArray.AsInts(new uint[0]).Length); } public void NullStaysNull() { CheckNull(ReinterpretArray.AsInts(null)); } public void TheIntViewIsTheSameObjectTheRuntimeKnowsAsUints() { object keys = ReinterpretArray.AsInts(new uint[2]); CheckEqual(typeof(uint[]), keys.GetType()); } } public class AsUintsTests : UnitTestSharp.TestFixture { public void ReturnsTheSameArray() { var keys = new[] { 1, 2, 3 }; CheckTrue(ReferenceEquals(keys, ReinterpretArray.AsUints(keys))); } public void IndexingReadsTheSameBitsAsUints() { uint[] uints = ReinterpretArray.AsUints(new[] { 0, 5, -1, int.MinValue }); CheckEqual(0u, uints[0]); CheckEqual(5u, uints[1]); CheckEqual(uint.MaxValue, uints[2]); CheckEqual(1u << 31, uints[3]); } public void EnumeratingAsACollectionStillSeesInts() { System.Collections.IEnumerable uints = ReinterpretArray.AsUints(new[] { -1 }); foreach (object element in uints) { CheckEqual(typeof(int), element.GetType()); } } public void AWriteThroughTheUintsShowsInTheInts() { var keys = new int[3]; ReinterpretArray.AsUints(keys)[2] = 1u << 31; CheckEqual(int.MinValue, keys[2]); } public void NullStaysNull() { CheckNull(ReinterpretArray.AsUints(null)); } public void ARoundTripGivesTheOriginalArray() { var keys = new uint[] { 4, 5 }; CheckTrue(ReferenceEquals(keys, ReinterpretArray.AsUints(ReinterpretArray.AsInts(keys)))); } } } }