def test_compute_height(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        self.assertEqual(2,
                         algo.height,
                         "height should be -1")

        binaryImage = BinaryImage(IMG_TEST_CASE_1, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)
        self.assertEqual(None,
                         algo.height,
                         "height should be 0 in vertical image")
    def test_compute_anchor(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        self.assertEqual(algo.binaryImage.get_pixel(2, 4),
                         algo.compute_anchor(),
                         "Anchor should be (2, 4)")

        binaryImage = BinaryImage(IMG_TEST_CASE_1, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)
        self.assertEqual(None,
                         algo.compute_anchor(),
                         "Anchor should be None on vertical image")
    def test_is_elbow(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        for pixel in algo.binaryImage.get_black_pixels():
            self.assertEqual(pixel in algo.compute_all_elbows(),
                             algo.is_elbow(pixel))
    def test_compute_top_pixel(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        self.assertEqual(algo.binaryImage.get_pixel(5, 5),
                         algo.compute_top_pixel(),
                         "Anchor should be (5, 5)")
    def test_compute_lead_elbow(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        self.assertEqual(algo.binaryImage.get_pixel(4, 5),
                         algo.compute_lead_elbow(),
                         "lead elbow should be (4, 5)")
    def test_swap_pixels_creating_hole(self):
        binaryImage = BinaryImage(IMG_SWAP_HOLE, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        # Trying to swap the same 2 pixels with the same color
        self.assertEqual(False,
                         algo.swap_pixels(binaryImage.get_pixel(1, 2), binaryImage.get_pixel(2, 2), swap_active=True),
                         "The swap <(1,4), (2,3)> should return False (Swapping pixels create hole in whites)")

        # Testing if the image wasn't impacted by a wrong swaps
        self.assertEqual(IMG_SWAP_HOLE,
                         algo.binaryImage.convert_pixels_to_img(),
                         "The previous swaps were False, the image shouldn't have changed")
    def test_compute_frontier(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        frontierTest = [algo.binaryImage.get_pixel(1, 5),
                        algo.binaryImage.get_pixel(2, 5),
                        algo.binaryImage.get_pixel(4, 5),
                        algo.binaryImage.get_pixel(5, 5)]

        frontierAlgo, temp = algo.compute_frontier()

        self.assertEqual(frontierTest,
                         frontierAlgo,
                         "Pixels that should be in frontier [(1, 5), (2, 5), (4, 5), (5, 5)]")
    def test_compute_all_elbows(self):
        binaryImage = BinaryImage(IMG_ALGO_B4W4_ELEMENTS, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        elbowsTest = [algo.binaryImage.get_pixel(1, 1),
                        algo.binaryImage.get_pixel(1, 5),
                        algo.binaryImage.get_pixel(2, 2),
                        algo.binaryImage.get_pixel(4, 5),
                        algo.binaryImage.get_pixel(6, 3)]

        elbowsAlgo = algo.compute_all_elbows()

        self.assertEqual(elbowsTest,
                         elbowsAlgo,
                         "Pixels that should be in frontier [(1, 1), (1, 5), (2, 2), (4, 5), (6, 3)]")
    def test_swap_pixels_no_hole(self):
        binaryImage = BinaryImage(IMG_SWAP, BLACK_CONNEXITY, WHITE_CONNEXITY)
        algo = AlgoB4W4(binaryImage)

        # Not repescting B4W4 connexity
        self.assertEqual(False,
                         algo.swap_pixels(binaryImage.get_pixel(1, 2), binaryImage.get_pixel(1, 3), swap_active=True),
                         "The swap <(1,2), (1,3)> should return False (connexity not respected)")

        # Trying to swap the same pixel
        self.assertEqual(False,
                         algo.swap_pixels(binaryImage.get_pixel(1, 2), binaryImage.get_pixel(1, 2), swap_active=True),
                         "The swap <(1,2), (1,2)> should return False (Swapping the same pixel)")

        # Trying to swap the same 2 pixels with the same color
        self.assertEqual(False,
                         algo.swap_pixels(binaryImage.get_pixel(1, 2), binaryImage.get_pixel(2, 2), swap_active=True),
                         "The swap <(1,2), (2,2)> should return False (Swapping pixels with the same color)")

        # Testing if the image wasn't impacted by a wrong swaps
        self.assertEqual(IMG_SWAP,
                         algo.binaryImage.convert_pixels_to_img(),
                         "The previous swaps were False, the image shouldn't have changed")

        # Testing an authorized swap, without really swapping the pixels
        self.assertEqual(True,
                         algo.swap_pixels(binaryImage.get_pixel(1, 2), binaryImage.get_pixel(1, 1), swap_active=False),
                         "The swap <(1,2), (1,1)> should return True")

        # Testing if the swap_active parameter is working
        self.assertEqual(IMG_SWAP,
                         algo.binaryImage.convert_pixels_to_img(),
                         "The swap active was false, image shouldn't have change")

        # Testing an authorized swap and really swapping the pixels
        self.assertEqual(True,
                         algo.swap_pixels(binaryImage.get_pixel(1, 2), binaryImage.get_pixel(1, 1), swap_active=True),
                         "The swap <(1,2), (1,1)> should return True")

        # Testing if the swap is done correctly
        self.assertEqual([[0, 0, 0, 0],
                          [0, 1, 1, 0],
                          [0, 1, 0, 0],
                          [0, 0, 0, 0]],
                         algo.binaryImage.convert_pixels_to_img(),
                         "The swap should have been done (swap_active=True)")