Пример #1
0
        public void Main()
        {
            // Invalid path
            string path = ".";

            Assert.Catch <Exception>(() => FileInterface.readFromFile(path));

            // Incorrect number of characters
            path = Constants.TEST_INPUT + @"\wrong_num_chars.txt";
            Assert.Catch <Exception>(() => FileInterface.readFromFile(path));

            // Working file
            path = Constants.TEST_INPUT + @"\working_file.txt";
            Board board = FileInterface.readFromFile(path);
            List <List <char> > expected = new List <List <char> >
            {
                new List <char> {
                    '2', '-', '3', '1'
                },
                new List <char> {
                    '1', '3', '-', '4'
                },
                new List <char> {
                    '3', '1', '4', '-'
                },
                new List <char> {
                    '-', '2', '1', '3'
                }
            };

            Assert.IsTrue(TestUtils.boardsEqual(expected, board.board));
            Board copied = new Board(4, new CharSet(new List <char> {
                '1', '2', '3', '4'
            }), expected);

            Assert.IsTrue(TestUtils.boardsEqual(expected, copied.board));

            // Copy constructor
            Board otherBoard = new Board(board);

            Assert.IsTrue(TestUtils.boardsEqual(otherBoard.board, board.board));

            Assert.Catch <Exception>(() => board.setCell(100, 100, '1'));
            Assert.Catch <Exception>(() => board.setCell(0, 0, '0'));

            Assert.IsTrue(board.isValidChar('-'));
            Assert.IsTrue(board.isValidChar('1'));
            Assert.IsFalse(board.isValidChar('5'));

            board.printBoard();
        }
Пример #2
0
        public void Main()
        {
            int     n       = 4;
            CharSet charSet = new CharSet(new List <char> {
                '0', '1', '2', '3'
            });
            Board board = new Board(n, charSet);

            board.setCell(0, 0, '-');
            Tracker tracker = new Tracker(board);

            tracker.progress[0][0].erase('0');
            tracker.progress[0][0].erase('1');
            tracker.progress[0][0].erase('2');

            Tracker otherTracker = new Tracker(tracker);

            Assert.IsTrue(TestUtils.boardsEqual(
                              tracker.board.board, otherTracker.board.board));

            otherTracker.eliminatePossibility(1, 1, '0');
            otherTracker.eliminatePossibility(1, 1, '1');
            otherTracker.eliminatePossibility(1, 1, '2');
            otherTracker.eliminatePossibility(1, 1, '3');
            Assert.IsFalse(otherTracker.valid);

            tracker.fillSquare(0, 0);
            Assert.AreEqual(board.board[0][0], '3');

            tracker.fillSquare(2, 2, '2');
            Assert.AreEqual(board.board[2][2], '2');

            Tuple <int, int> fromBlock = tracker.convertFromBlock(2, 1);

            Assert.AreEqual(new Tuple <int, int>(2, 1), fromBlock);


//			string path = Constants.EASY_INPUT;
//			string[] inputFiles = Directory.GetFiles(path);
//			foreach (string file in inputFiles)
//			{
//				Board board;
//				try
//				{
//					board = FileInterface.readFromFile(file);
//				}
//				catch (Exception e)
//				{
//					Console.WriteLine(e.Message);
//					continue;
//				}
//
//				Tracker tracker = new Tracker(board);
//				Solver solver = new Solver(tracker);
//				tracker = solver.run();
//
//				if (!tracker.valid)
//				{
//					Assert.IsFalse(TestUtils.isCorrectSolution(tracker.board));
//				}
//				else
//				{
//					if (tracker.solutionCnt > 1)
//					{
//						Console.WriteLine("WARNING: More than one solution found. One is shown.");
//					}
//					Assert.IsTrue(TestUtils.isCorrectSolution(tracker.board),
//						"ERROR: Could not find correct solution for file " + file + ".");
//				}
//			}
        }
Пример #3
0
        public void Main()
        {
            Assert.Catch(() => FileInterface.readFromFile("."));

            string path = Constants.TEST_INPUT + @"\file_too_short.txt";

            Assert.Catch(() => FileInterface.readFromFile(path));

            path = Constants.TEST_INPUT + @"\not_enough_lines.txt";
            Assert.Catch(() => FileInterface.readFromFile(path));

            path = Constants.TEST_INPUT + @"\long_characters.txt";
            Assert.Catch(() => FileInterface.readFromFile(path));

            path = Constants.TEST_INPUT + @"\duplicate_chars.txt";
            Assert.Catch(() => FileInterface.readFromFile(path));

            path = Constants.TEST_INPUT + @"\incorrect_line.txt";
            Assert.Catch(() => FileInterface.readFromFile(path));

            // Working file
            path = Constants.TEST_INPUT + @"\working_file.txt";
            Board board = FileInterface.readFromFile(path);
            List <List <char> > expected = new List <List <char> >
            {
                new List <char> {
                    '2', '-', '3', '1'
                },
                new List <char> {
                    '1', '3', '-', '4'
                },
                new List <char> {
                    '3', '1', '4', '-'
                },
                new List <char> {
                    '-', '2', '1', '3'
                }
            };

            Assert.True(TestUtils.boardsEqual(expected, board.board));

            long micros = 1294829015890289018;

            Assert.AreEqual(FileInterface.getTimeString(micros), "26:38:10.289018");

            Tracker tracker = new Tracker(board);
            Solver  solver  = new Solver(tracker);

            FileInterface.writeToFile(board, board, solver, 23087);

            List <string> expectedLines = new List <string>
            {
                "2 - 3 1",
                "1 3 - 4",
                "3 1 4 -",
                "- 2 1 3"
            };
            var lines = FileInterface.getBoardFileLines(board);

            for (int i = 0; i < 4; ++i)
            {
                Assert.AreEqual(lines[i], expectedLines[i]);
            }

            List <char> lc = new List <char> {
                'a', 'b'
            };

            Assert.AreEqual(FileInterface.lineToString(lc), "a b");
        }
Пример #4
0
        public void Main()
        {
            List <List <char> > a = new List <List <char> >
            {
                new List <char> {
                    '1', '2'
                },
                new List <char> {
                    '3', '4'
                }
            };
            List <List <char> > b = new List <List <char> >
            {
                new List <char> {
                    '1', '2'
                },
                new List <char> {
                    '3', '4'
                }
            };

            Assert.IsTrue(TestUtils.boardsEqual(a, b));
            a[0][0] = '0';
            Assert.IsFalse(TestUtils.boardsEqual(a, b));

            List <List <char> > c = new List <List <char> >
            {
                new List <char> {
                    '1', '2'
                }
            };

            Assert.IsFalse(TestUtils.boardsEqual(a, c));

            Assert.Catch(() => new Board(4, new CharSet(new List <char> {
                '0', '1', '2'
            })));

            List <List <char> > complete = new List <List <char> >
            {
                new List <char> {
                    '2', '4', '3', '1'
                },
                new List <char> {
                    '1', '3', '2', '4'
                },
                new List <char> {
                    '3', '1', '4', '2'
                },
                new List <char> {
                    '4', '2', '1', '3'
                }
            };

            Board board1 = new Board(4, new CharSet(new List <char> {
                '0', '1', '2', '3'
            }), complete);

            Assert.IsTrue(TestUtils.isCorrectSolution(board1));

            complete[0][0] = '4';
            Assert.IsFalse(TestUtils.isCorrectSolution(board1));

            List <List <char> > badCols = new List <List <char> >
            {
                new List <char> {
                    '2', '4', '3', '1'
                },
                new List <char> {
                    '2', '4', '3', '1'
                },
                new List <char> {
                    '2', '4', '3', '1'
                },
                new List <char> {
                    '2', '4', '3', '1'
                }
            };
            Board board2 = new Board(4, new CharSet(new List <char> {
                '0', '1', '2', '3'
            }), badCols);

            Assert.IsFalse(TestUtils.isCorrectSolution(board2));

            List <List <char> > badRegions = new List <List <char> >
            {
                new List <char> {
                    '2', '4', '3', '1'
                },
                new List <char> {
                    '4', '3', '1', '2'
                },
                new List <char> {
                    '3', '1', '2', '4'
                },
                new List <char> {
                    '1', '2', '4', '3'
                }
            };
            Board board3 = new Board(4, new CharSet(new List <char> {
                '0', '1', '2', '3'
            }), badRegions);

            Assert.IsFalse(TestUtils.isCorrectSolution(board3));
        }