Exemplo n.º 1
0
    def test1(self):
        # Make a dummy puzzle
        puzzle = PuzzleTester.build_dummy_puzzle()

        # Get the distance info
        dist_info = InterPieceDistance(puzzle.pieces, PuzzlePiece.calculate_asymmetric_distance, PuzzleType.type2)
        # Verify the best buddy info for neighboring pieces
        for i in range(0, len(puzzle.pieces)):
            # Check not an end piece on the right side of the image
            if i % PuzzleTester.GRID_SIZE[1] != PuzzleTester.GRID_SIZE[1] - 1:
                self.assertTrue(set(dist_info.best_buddies(i, PuzzlePieceSide.right)) == set([(i + 1, PuzzlePieceSide.left)]))
                self.assertTrue(0 == dist_info.asymmetric_distance(i, PuzzlePieceSide.right, i+1, PuzzlePieceSide.left)
                                == dist_info.asymmetric_distance(i + 1, PuzzlePieceSide.left, i, PuzzlePieceSide.right))

            # Check not an end piece on the top of the image
            if i >= PuzzleTester.GRID_SIZE[1]:
                self.assertTrue(dist_info.best_buddies(i, PuzzlePieceSide.top) == [(i - PuzzleTester.GRID_SIZE[1], PuzzlePieceSide.bottom)])

        # Verify the middle piece is selected as the starting piece
        middle_piece = 4
        self.assertTrue(dist_info.next_starting_piece() == middle_piece)

        # Verify you get the same result even if passed an array of pieces.
        all_pieces_false = [False] * len(puzzle.pieces)
        self.assertTrue(dist_info.next_starting_piece(all_pieces_false) == middle_piece)

        # Verify that if all other pieces with more than one neighbor are excluded, then the only piece
        # with exactly one neighbor is selected.
        seed_piece_mask = [False] * len(puzzle.pieces)
        seed_piece_mask[1] = seed_piece_mask[3] = seed_piece_mask[4] = seed_piece_mask[5] = False
        self.assertTrue(dist_info.next_starting_piece(seed_piece_mask) == 7)
Exemplo n.º 2
0
    def test_accuracy_calculator(self):

        # Build a known test puzzle.
        dummy_puzzle = PuzzleTester.build_dummy_puzzle()
        for piece in dummy_puzzle.pieces:
            piece._assign_to_original_location()
            piece._set_id_number_to_original_id()
            piece.rotation = PuzzlePieceRotation.degree_0

        # Test different conditions
        for i in xrange(0, 2):

            if i == 0:
                # Set all piece rotation to the default
                for piece in dummy_puzzle.pieces:
                    piece.rotation = PuzzlePieceRotation.degree_0
            elif i == 1:
                dummy_puzzle.pieces[0].rotation = PuzzlePieceRotation.degree_90

            # Build the results information collection
            results_information = PuzzleResultsCollection([dummy_puzzle.pieces], "." + os.sep + "dummy.jpg")
            results_information.calculate_accuracies([dummy_puzzle])

            # Verify the neighbor results
            numb_pieces = len(dummy_puzzle.pieces)
            results = results_information.results[0]

            if i == 0:
                assert results.standard_direct_accuracy.numb_correct_placements == numb_pieces
                assert results.standard_direct_accuracy.numb_wrong_rotation == 0

                assert results.modified_direct_accuracy.numb_correct_placements == numb_pieces
                assert results.modified_direct_accuracy.numb_wrong_rotation == 0
            elif i == 1:
                assert results.standard_direct_accuracy.numb_correct_placements == numb_pieces - 1
                assert results.standard_direct_accuracy.numb_wrong_rotation == 1

                assert results.modified_direct_accuracy.numb_correct_placements == numb_pieces - 1
                assert results.modified_direct_accuracy.numb_wrong_rotation == 1

            assert results.standard_direct_accuracy.numb_different_puzzle == 0
            assert results.standard_direct_accuracy.numb_wrong_location == 0
            assert results.standard_direct_accuracy.total_numb_pieces_in_solved_puzzle == numb_pieces

            assert results.modified_direct_accuracy.numb_wrong_location == 0
            assert results.modified_direct_accuracy.total_numb_pieces_in_solved_puzzle == numb_pieces

            # Check the neighbor accuracy
            numb_sides = PuzzlePieceSide.get_numb_sides()
            if i == 0:
                assert results.modified_neighbor_accuracy.correct_neighbor_count == numb_sides * numb_pieces
                assert results.modified_neighbor_accuracy.wrong_neighbor_count == 0
            elif i == 1:
                assert results.modified_neighbor_accuracy.correct_neighbor_count == numb_sides * numb_pieces - 5
                assert results.modified_neighbor_accuracy.wrong_neighbor_count == 5
            assert results.modified_neighbor_accuracy.total_numb_pieces_in_solved_puzzle == numb_pieces
Exemplo n.º 3
0
    def test_neighbor_id_numbers(self):
        """
        Test the functionality where the module gets the neighbor identification numbers for different puzzle pieces
        """

        # Build a known test puzzle.
        puzzle = PuzzleTester.build_dummy_puzzle()

        # Get the pieces from the dummy puzzle.
        pieces = puzzle.pieces

        # Check the neighbor coordinates of the first piece
        assert pieces[0].original_neighbor_id_numbers_and_sides == ((None, PuzzlePieceSide.top), (1, PuzzlePieceSide.right),
                                                                    (3, PuzzlePieceSide.bottom), (None, PuzzlePieceSide.left))
        assert pieces[1].original_neighbor_id_numbers_and_sides == ((None, PuzzlePieceSide.top), (2, PuzzlePieceSide.right),
                                                                    (4, PuzzlePieceSide.bottom), (0, PuzzlePieceSide.left))
        assert pieces[2].original_neighbor_id_numbers_and_sides == ((None, PuzzlePieceSide.top), (None, PuzzlePieceSide.right),
                                                                    (5, PuzzlePieceSide.bottom), (1, PuzzlePieceSide.left))
        assert pieces[3].original_neighbor_id_numbers_and_sides == ((0, PuzzlePieceSide.top), (4, PuzzlePieceSide.right),
                                                                    (6, PuzzlePieceSide.bottom), (None, PuzzlePieceSide.left))
        assert pieces[8].original_neighbor_id_numbers_and_sides == ((5, PuzzlePieceSide.top), (None, PuzzlePieceSide.right),
                                                                    (None, PuzzlePieceSide.bottom), (7, PuzzlePieceSide.left))
Exemplo n.º 4
0
    def test_puzzle_piece_maker(self):
        """
        Puzzle Piece Maker Checker

        Checks that puzzle pieces are made as expected.  It also checks the get puzzle piece row/column values.
        """

        # Build a known test puzzle.
        puzzle = PuzzleTester.build_dummy_puzzle()

        # Get the puzzle pieces
        pieces = puzzle.pieces
        for piece in pieces:
            orig_loc = piece._orig_loc
            upper_left_dim = orig_loc[0] * PuzzleTester.PIECE_WIDTH * PuzzleTester.row_to_row_step_size()
            upper_left_dim += orig_loc[1] * PuzzleTester.piece_to_piece_step_size()

            # Test the Extraction of row pixel values
            for row in range(0, PuzzleTester.PIECE_WIDTH):
                first_dim_val = upper_left_dim + row * PuzzleTester.row_to_row_step_size()

                # Test the extraction of pixel values.
                test_arr = PuzzleTester.build_pixel_list(first_dim_val, True)
                row_val = piece.get_row_pixels(row)
                assert(np.array_equal(row_val, test_arr))  # Verify the two arrays are equal.

                # Test the reversing
                reverse_list = True
                test_arr = PuzzleTester.build_pixel_list(first_dim_val, True, reverse_list)
                row_val = piece.get_row_pixels(row, reverse_list)
                assert(np.array_equal(row_val, test_arr))

            for col in range(0, PuzzleTester.PIECE_WIDTH):
                first_dim_val = upper_left_dim + col * PuzzleTester.NUMB_PIXEL_DIMENSIONS

                # Test the extraction of pixel values.
                is_col = False
                test_arr = PuzzleTester.build_pixel_list(first_dim_val, is_col)
                col_val = piece.get_column_pixels(col)
                assert(np.array_equal(col_val, test_arr))  # Verify the two arrays are equal.

                # Test the reversing
                reverse_list = True
                test_arr = PuzzleTester.build_pixel_list(first_dim_val, is_col, reverse_list)
                col_val = piece.get_column_pixels(col, reverse_list)
                assert(np.array_equal(col_val, test_arr))

        # Calculate the asymmetric distance for two neighboring pieces on ADJACENT SIDES
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[0], PuzzlePieceSide.right,
                                                              pieces[1], PuzzlePieceSide.left)
        assert(asym_dist == 0)
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[1], PuzzlePieceSide.left,
                                                              pieces[0], PuzzlePieceSide.right)
        assert(asym_dist == 0)
        # Calculate the asymmetric distance for two neighboring pieces on ADJACENT SIDES
        pieces_per_row = int(math.sqrt(PuzzleTester.NUMB_PUZZLE_PIECES))
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[0], PuzzlePieceSide.bottom,
                                                              pieces[pieces_per_row], PuzzlePieceSide.top)
        assert(asym_dist == 0)
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[pieces_per_row], PuzzlePieceSide.top,
                                                              pieces[0], PuzzlePieceSide.bottom)
        assert(asym_dist == 0)

        # Calculate the asymmetric distance for pieces two spaces away
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[0], PuzzlePieceSide.right,
                                                              pieces[2], PuzzlePieceSide.left)
        expected_dist = PuzzleTester.PIECE_WIDTH * PuzzleTester.NUMB_PIXEL_DIMENSIONS * PuzzleTester.piece_to_piece_step_size()
        assert(asym_dist == expected_dist)
        # Calculate the asymmetric distance for pieces two spaces away
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[2], PuzzlePieceSide.left,
                                                              pieces[0], PuzzlePieceSide.right)
        expected_dist = PuzzleTester.PIECE_WIDTH * PuzzleTester.NUMB_PIXEL_DIMENSIONS * PuzzleTester.piece_to_piece_step_size()
        assert(asym_dist == expected_dist)

        # Calculate the asymmetric distance for two neighboring pieces on non-adjacent
        asym_dist = PuzzlePiece.calculate_asymmetric_distance(pieces[0], PuzzlePieceSide.top,
                                                              pieces[1], PuzzlePieceSide.top)
        # Distance between first pixel in top row of piece to last pixel of piece j almost like to puzzle pieces
        pixel_to_pixel_dist = -1 * ((2 * PuzzleTester.PIECE_WIDTH - 1) * PuzzleTester.NUMB_PIXEL_DIMENSIONS)
        pixel_to_pixel_dist -= PuzzleTester.row_to_row_step_size()
        # Calculate the expected distance
        expected_dist = 0
        for i in range(0, PuzzleTester.PIECE_WIDTH * PuzzleTester.NUMB_PIXEL_DIMENSIONS):
            expected_dist += abs(pixel_to_pixel_dist)
            if i % PuzzleTester.NUMB_PIXEL_DIMENSIONS == PuzzleTester.NUMB_PIXEL_DIMENSIONS - 1:
                pixel_to_pixel_dist += 2 * PuzzleTester.NUMB_PIXEL_DIMENSIONS
        assert(asym_dist == expected_dist)