Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please supply n (n must be greater than 0):");
            string input = Console.ReadLine();
            int n = 0;

            int.TryParse(input, out n);

            while (n < 1)
            {
                Console.WriteLine("Sorry. Couldn't parse that number or it was less than 1");
                Console.WriteLine("Please supply n (n must be greater than 0):");
                input = Console.ReadLine();
                int.TryParse(input, out n);
            }

            ITableEngine engine = new PrimeNumberEngine();
            int?[,] output = engine.GenerateTable((uint)n);

            //output

            Console.WriteLine();

            int maxstringlength = 0;

            //just to make display nice we need to find maximum string length
            for (int x = 0; x < n + 1; x++)
            {
                for (int y = 0; y < n + 1; y++)
                {
                    if (output[x, y].HasValue)
                    {
                        string tempstring = output[x, y].ToString();
                        maxstringlength = tempstring.Length > maxstringlength ? tempstring.Length : maxstringlength;
                    }
                }
            }

            StringBuilder sb = new StringBuilder();
            char seperator = '|';
            char space = ' ';

            for (int x = 0; x < n + 1; x++)
            {
                for (int y = 0; y < n + 1; y++)
                {
                    sb.Append(PaddedString(output[x, y], maxstringlength, space));
                    sb.Append(seperator);
                }
                sb.Append(Environment.NewLine);
            }

            Console.Write(sb.ToString());

            Console.WriteLine();
            Console.WriteLine("please press any key to exit");
            Console.ReadKey();
        }
        public void CreateSimpleTable()
        {
            Mock<INumberGenerator> numberGenerator = new Mock<INumberGenerator>(MockBehavior.Strict);
            Mock<TableGenerator> tableGenerator = new Mock<TableGenerator>(MockBehavior.Strict);

            PrimeNumberEngine engine = new PrimeNumberEngine();

            engine.numberGenerator = numberGenerator.Object;
            engine.tableGenerator = tableGenerator.Object;

            int?[,] table = new int?[3, 3] { { null, 1, 2 }, { 1, 1, 2 }, { 2, 2, 4 } };

            tableGenerator.Setup(x => x.GetMultiplicationTable(2)).Returns(table);

            var answer = engine.GenerateTable(2);

            Assert.AreEqual(table, answer);
        }