public void WhenInvalidSquareDimension_ThrowsException(int dimension)
        {
            var magicSquare = new MagicSquare();

            Should.Throw <ArgumentException>(() => magicSquare.IsValid(new int[dimension, dimension]))
            .Message.ShouldBe("Invalid Magic Square dimensions");
        }
        public void WhenInputEmpty_ThrowsException()
        {
            var magicSquare = new MagicSquare();

            Should.Throw <ArgumentException>(() => magicSquare.IsValid(new int[0, 0]))
            .Message.ShouldBe("Input is invalid");
        }
        public void WhenInputIsNotASquare_ThrowsException()
        {
            var magicSquare = new MagicSquare();

            var result = magicSquare.IsValid(new int[2, 0]);

            result.ShouldBe(false);
        }
        public void WhenAnyValueInSquareIsZero_ReturnFalse()
        {
            var testData = new[, ] {
                { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 }
            };

            var magicSquare = new MagicSquare();

            magicSquare.IsValid(testData).ShouldBe(false);
        }
Exemple #5
0
        static void Main(string[] args)
        {
            var numbers = args?.Select(arg =>
            {
                if (!int.TryParse(arg, out var result))
                {
                    return(-1);
                }
                return(result);
            }).ToArray() ?? new int[0];
            var size   = (int)Math.Sqrt(numbers.Length);
            var square = new int[size, size];

            for (var i = 0; i < numbers.Length; i++)
            {
                var x = i % size;
                var y = i / size;
                square[y, x] = numbers[i];
            }

            RenderSquare(square);

            var magicSquare = new MagicSquare();

            try
            {
                var isValid = magicSquare.IsValid(square);
                if (isValid)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Input is valid magic square.");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Input is not valid magic square.");
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine("Unable to process input data.");
                Console.Error.WriteLine(ex.Message);
            }

            Console.WriteLine();
        }
 public void IsValid_ThrowsException_WhenInputNull()
 {
     Should.Throw <ArgumentNullException>(() => MagicSquare.IsValid(null));
 }
        public void WhenInputNull_ThrowsException()
        {
            var magicSquare = new MagicSquare();

            Should.Throw <ArgumentNullException>(() => magicSquare.IsValid(null));
        }