예제 #1
0
class TestBoardImmove(TestBoard):
    def setUp(self):
        self.board = Board(
            columns=2, rows=2, n_shapes=3, immovable_shape=-1)
        board = np.array([
            [0, 1],
            [2, 0]
        ])
        self.board.set_board(board)

        self.board_wimove = Board(
            columns=2, rows=2, n_shapes=3, immovable_shape=-1)
        self.board_wimove.set_board(np.array([
            [-1, 0],
            [0, -1]
        ]))

    def test_availability(self):
        with self.assertRaises(ImmovableShapeError):
            self.board_wimove.move(Point(0, 0), Point(0, 1))

        with self.assertRaises(ImmovableShapeError):
            self.board_wimove.swap(Point(0, 0), Point(1, 1))

        with self.assertRaises(ImmovableShapeError):
            self.board_wimove.delete({Point(0, 0)})

    def test_putting(self):
        with self.assertRaises(ImmovableShapeError):
            mask = np.array([
                [True, False],
                [False, False]])
            self.board_wimove.put_mask(mask, [1])

        with self.assertRaises(ImmovableShapeError):
            ind = 0
            self.board_wimove.put_line(ind, np.array([1, 1]))

    def test_shuffle(self):
        self.board_wimove.shuffle()
        true = np.array([
            [True, False],
            [False, True]
        ])
        answer = self.board_wimove.board == -1
        np.testing.assert_array_equal(true, answer)
예제 #2
0
class TestBoard(unittest.TestCase):
    def setUp(self):
        self.board = Board(columns=2, rows=2, n_shapes=3)
        board = np.array([[0, 1], [2, 0]])
        self.board.set_board(board)

    def test_swap(self):
        """ test swapping two points """
        p1 = Point(0, 0)
        p2 = Point(0, 1)
        self.board.swap(p1, p2)
        correct = np.array([[1, 0], [2, 0]])
        self.assertEqual(correct.tolist(), self.board.board.tolist())

    def test_move(self):
        point = Point(1, 1)
        direction = Point(-1, 0)
        correct = np.array([[0, 0], [2, 1]])
        self.board.move(point, direction)
        self.assertEqual(self.board.board.tolist(), correct.tolist())

    def test_getting_validate_board_size(self):
        """ test getting board size """
        self.assertEqual(self.board.board_size, (2, 2))

    def test_setting_incorrect_shaped_board(self):
        """ test setting bad board  """
        new_board = np.array([[0, 0], [0, 10]])
        with self.assertRaises(ValueError):
            self.board.set_board(new_board)

    def test_setting_incorrect_sized_board(self):
        new_board = np.array([[0, 0, 0], [0, 0, 0]])
        with self.assertRaises(ValueError):
            self.board.set_board(new_board)

    def test_get_shape(self):
        """ test getting shape of points """
        points = [Point(0, 0), Point(1, 1)]
        correct_answers = [
            self.board.board[0, 0],
            self.board.board[1, 1],
        ]
        board_answers = [self.board.get_shape(point) for point in points]
        for ind, point in enumerate(points):
            with self.subTest(point=point.get_coord()):
                self.assertEqual(correct_answers[ind], board_answers[ind])

    def test_get_bad_shape(self):
        """ test getting shapes of incorrect points """
        points = [
            Point(-1, 0),
            Point(-1, -1),
            Point(1, -1),
            Point(10, 0),
            Point(0, 2),
        ]
        for point in points:
            with self.subTest(point=point.get_coord()):
                with self.assertRaises(OutOfBoardError):
                    self.board.get_shape(point)

    def test_delete(self):
        points = {Point(0, 0), Point(1, 1)}
        deleted_board = np.array([[np.nan, 1.], [2., np.nan]], dtype='float')
        self.board.delete(points)
        self.assertTrue(
            np.allclose(self.board.board, deleted_board, equal_nan=True))

    def test_get_line(self):
        true = np.array([0, 2])
        answer = self.board.get_line(0)
        self.assertEqual(true.tolist(), answer.tolist())

    def test_put_line(self):
        true = self.board.board.copy()
        true[:, 0] = [2, 2]
        self.board.put_line(0, np.array([2, 2]))
        answer = self.board.board
        self.assertEqual(true.tolist(), answer.tolist())

    def test_put_mask(self):
        mask = np.ones_like(self.board.board, dtype=bool)
        mask[1, 1] = False
        true = np.array([[1, 1], [1, 0]])
        self.board.put_mask(mask, [1, 1, 1])
        self.assertEqual(true.tolist(), self.board.board.tolist())