コード例 #1
0
class TestEmptyTree(unittest.TestCase):
    def setUp(self) -> None:
        self.empty_tree = BST()

    def test_empty(self):
        self.assertTrue(self.empty_tree.empty)

    def test_value(self):
        self.assertIsNone(self.empty_tree.value)

    def test_left(self):
        self.assertEqual(self.empty_tree.left, BST())

    def test_right(self):
        self.assertEqual(self.empty_tree.right, BST())

    def test_min(self):
        self.assertRaises(ValueError, lambda: self.empty_tree.min)

    def test_max(self):
        self.assertRaises(ValueError, lambda: self.empty_tree.max)

    def test_values(self):
        self.assertEqual(self.empty_tree.values, frozenset())

    def test_contains(self):
        self.assertNotIn(1, self.empty_tree)
        self.assertNotIn("foo", self.empty_tree)
        self.assertNotIn(-42, self.empty_tree)
        self.assertNotIn("bar", self.empty_tree)

    def test_closest_to(self):
        self.assertRaises(ValueError, lambda: self.empty_tree.closest_to(1, distance=None))
コード例 #2
0
 def test_closest_to_four_with_degenerate_tree(self):
     spy_float_distance = mock.MagicMock()
     spy_float_distance.side_effect = self.float_distance
     tree = BST(1, None, BST(2, None, BST(3)))
     closest_to, distance = tree.closest_to(4, spy_float_distance)
     self.assertEqual(closest_to, 3)
     self.assertEqual(distance, 1)
     calls_args = spy_float_distance.call_args_list
     expected_calls_args = [mock.call(4, 3)]
     self.assertEqual(calls_args, expected_calls_args)
コード例 #3
0
 def test_closest_to_two_and_three_quarters_without_three(self):
     spy_float_distance = mock.MagicMock()
     spy_float_distance.side_effect = self.float_distance
     tree = BST(2., BST(1.))
     closest_to, distance = tree.closest_to(2.75, spy_float_distance)
     self.assertEqual(closest_to, 2)
     self.assertEqual(distance, 0.75)
     calls_args = spy_float_distance.call_args_list
     expected_calls_args = [mock.call(2.75, 2)]
     self.assertEqual(calls_args, expected_calls_args)
コード例 #4
0
class TestBST(unittest.TestCase):
    one_leaf = BST(1.)
    three_leaf = BST(3.)

    def setUp(self) -> None:
        self.tree = BST(2., self.one_leaf, self.three_leaf)

    def test_empty(self):
        self.assertFalse(self.tree.empty)

    def test_value(self):
        self.assertEqual(self.tree.value, 2)

    def test_left(self):
        self.assertEqual(self.tree.left, self.one_leaf)

    def test_right(self):
        self.assertEqual(self.tree.right, self.three_leaf)

    def test_min(self):
        self.assertEqual(self.tree.min, 1)

    def test_max(self):
        self.assertEqual(self.tree.max, 3)

    def test_contains(self):
        self.assertIn(1, self.tree)
        self.assertIn(2, self.tree)
        self.assertIn(3, self.tree)

        self.assertNotIn(4, self.tree)
        self.assertNotIn(5, self.tree)
        self.assertNotIn(6, self.tree)

    def test_values(self):
        self.assertEqual(self.tree.values, frozenset({1, 2, 3}))

    def test_closest_to_zero(self):
        spy_float_distance = mock.MagicMock()
        spy_float_distance.side_effect = self.float_distance
        self.assertEqual(self.tree.closest_to(0, spy_float_distance), (1, 1))
        calls_args = spy_float_distance.call_args_list
        expected_calls_args = [mock.call(0, 1)]
        self.assertEqual(calls_args, expected_calls_args)

    def test_closest_to_one_and_three_quarters(self):
        spy_float_distance = mock.MagicMock()
        spy_float_distance.side_effect = self.float_distance
        closest_to, distance = self.tree.closest_to(1.75, spy_float_distance)
        self.assertEqual(closest_to, 2)
        self.assertEqual(distance, 0.25)
        calls_args = spy_float_distance.call_args_list
        expected_calls_args = [mock.call(1.75, 2), mock.call(1.75, 1)]
        self.assertEqual(calls_args, expected_calls_args)

    def test_closest_to_two_and_three_quarters(self):
        spy_float_distance = mock.MagicMock()
        spy_float_distance.side_effect = self.float_distance
        closest_to, distance = self.tree.closest_to(2.75, spy_float_distance)
        self.assertEqual(closest_to, 3)
        self.assertEqual(distance, 0.25)
        calls_args = spy_float_distance.call_args_list
        expected_calls_args = [mock.call(2.75, 2), mock.call(2.75, 3)]
        self.assertEqual(calls_args, expected_calls_args)

    def test_closest_to_two_and_three_quarters_without_three(self):
        spy_float_distance = mock.MagicMock()
        spy_float_distance.side_effect = self.float_distance
        tree = BST(2., BST(1.))
        closest_to, distance = tree.closest_to(2.75, spy_float_distance)
        self.assertEqual(closest_to, 2)
        self.assertEqual(distance, 0.75)
        calls_args = spy_float_distance.call_args_list
        expected_calls_args = [mock.call(2.75, 2)]
        self.assertEqual(calls_args, expected_calls_args)

    def test_closest_to_zero_with_degenerate_tree(self):
        spy_float_distance = mock.MagicMock()
        spy_float_distance.side_effect = self.float_distance
        tree = BST(3, BST(2, BST(1)))
        closest_to, distance = tree.closest_to(0, spy_float_distance)
        self.assertEqual(closest_to, 1)
        self.assertEqual(distance, 1)
        calls_args = spy_float_distance.call_args_list
        expected_calls_args = [mock.call(0, 1)]
        self.assertEqual(calls_args, expected_calls_args)

    def test_closest_to_four_with_degenerate_tree(self):
        spy_float_distance = mock.MagicMock()
        spy_float_distance.side_effect = self.float_distance
        tree = BST(1, None, BST(2, None, BST(3)))
        closest_to, distance = tree.closest_to(4, spy_float_distance)
        self.assertEqual(closest_to, 3)
        self.assertEqual(distance, 1)
        calls_args = spy_float_distance.call_args_list
        expected_calls_args = [mock.call(4, 3)]
        self.assertEqual(calls_args, expected_calls_args)

    @staticmethod
    def float_distance(a, b):
        return abs(a - b)