using System; using System.Collections.Generic; using UnitTestSharp; namespace Annulus.UnitTests { public class HexLineTests : TestFixture { public static List Walk(HexIndex start, HexIndex end) { var returnMe = new List(); foreach (HexIndex hex in new HexLine(start, end)) { returnMe.Add(hex); } return returnMe; } public class CtorTests : TestFixture { public void KeepsStartAndEnd() { var line = new HexLine(new HexIndex(1, 2), new HexIndex(-3, 4)); CheckEqual(new HexIndex(1, 2), line.Start); CheckEqual(new HexIndex(-3, 4), line.End); } } public class EnumeratorTests : TestFixture { public void FromAHexToItself_YieldsThatHexOnce() { var hex = new HexIndex(3, -2); CheckEqual(new[] { hex }, Walk(hex, hex)); } public void ToANeighbor_YieldsBothHexesInOrder() { var start = new HexIndex(2, 2); HexIndex end = start.Neighbor(PointyHexDirection.SouthWest); CheckEqual(new[] { start, end }, Walk(start, end)); } public void AlongEachDirection_YieldsEveryHexInTurn() { var start = new HexIndex(-1, 4); for (var direction = PointyHexDirection.East; direction <= PointyHexDirection.NorthEast; ++direction) { HexIndex first = start.Neighbor(direction); HexIndex second = first.Neighbor(direction); HexIndex third = second.Neighbor(direction); CheckEqual(new[] { start, first, second, third }, Walk(start, third)); } } public void AcrossASkewedLine_RoundsEachStepToTheNearestHex() { var expected = new[] { new HexIndex(0, 0), new HexIndex(1, 0), new HexIndex(2, -1), new HexIndex(3, -1), new HexIndex(4, -2), new HexIndex(5, -2), }; CheckEqual(expected, Walk(new HexIndex(0, 0), new HexIndex(5, -2))); } public void AlongAnEdgeBetweenTwoHexes_PicksTheSameSideEitherWay() { var middle = new HexIndex(1, -1); CheckEqual(new[] { new HexIndex(0, 0), middle, new HexIndex(2, -1) }, Walk(new HexIndex(0, 0), new HexIndex(2, -1))); CheckEqual(new[] { new HexIndex(2, -1), middle, new HexIndex(0, 0) }, Walk(new HexIndex(2, -1), new HexIndex(0, 0))); } public void AcrossASkewedLine_ReversingTheEndsReversesTheHexes() { List forward = Walk(new HexIndex(0, 0), new HexIndex(5, -2)); forward.Reverse(); CheckEqual(forward, Walk(new HexIndex(5, -2), new HexIndex(0, 0))); } public void AtLargeCoordinates_RoundsExactly() { var start = new HexIndex(1000000000, -500000000); var end = new HexIndex(1000000003, -500000001); var expected = new[] { start, new HexIndex(1000000001, -500000000), new HexIndex(1000000002, -500000001), end, }; CheckEqual(expected, Walk(start, end)); } public void OnALongLine_EachHexIsANeighborOfThePrevious() { List hexes = Walk(new HexIndex(-7, 3), new HexIndex(11, -9)); CheckEqual(new HexIndex(-7, 3).DistanceTo(new HexIndex(11, -9)) + 1, hexes.Count); for (int i = 1; i < hexes.Count; ++i) { CheckEqual(1, hexes[i - 1].DistanceTo(hexes[i])); } } public void ForeachAllocatesNothing() { var start = new HexIndex(0, 0); var end = new HexIndex(9, -4); int count = 0; foreach (HexIndex hex in start.LineTo(end)) { count += hex.Q; } long before = GC.GetAllocatedBytesForCurrentThread(); for (int i = 0; i < 100; ++i) { foreach (HexIndex hex in start.LineTo(end)) { count += hex.Q; } } long allocated = GC.GetAllocatedBytesForCurrentThread() - before; CheckEqual(0L, allocated); Check(count != 0); } } } }