示例#1
0
        public static Coordinate Move(this Coordinate coordinates, Compass4Points direction, int steps = 1)
        {
            steps.ThrowIfBelow(1, nameof(steps));

            switch (direction)
            {
            case Compass4Points.North:
                coordinates = coordinates.Up(steps);
                break;

            case Compass4Points.South:
                coordinates = coordinates.Down(steps);
                break;

            case Compass4Points.East:
                coordinates = coordinates.Right(steps);
                break;

            case Compass4Points.West:
                coordinates = coordinates.Left(steps);
                break;

            default:
                var message = $"Unrecognised direction [{direction}]";
                throw new ArgumentException(message);
            }

            return(coordinates);
        }
        public void ARandomEnum_ShouldHaveAGoodDistribution()
        {
            var enumItems = EnumHelpers.Values <Compass4Points>().ToList();
            var enumCount = enumItems.Count;

            int numRolls = enumCount * 1000;

            var rolls = new Compass4Points[numRolls];

            var generator = new RandomNumberGenerator();

            for (int i = 1; i < numRolls; i++)
            {
                rolls[i] = generator.Enum <Compass4Points>();
            }

            var samples = enumItems.Select(enumItem =>
            {
                var count = rolls.Count(roll => enumItem == roll);
                return(enumItem: enumItem, count: count);
            }).ToList();

            var midpoint = 1000;
            var percent  = 10.0 / 100;
            var min      = midpoint - (midpoint * percent);
            var max      = midpoint + (midpoint * percent);

            var errorMessage = new StringBuilder();

            foreach (var sample in samples)
            {
                errorMessage.AppendLine($"Expected [{sample.enumItem}] to be in the range [{min},{max}] and it was [{sample.count}]");
            }

            foreach (var sample in samples)
            {
                Assert.True(sample.count >= min && sample.count <= max, errorMessage.ToString());
            }
        }