Ejemplo n.º 1
0
    def test_empty_dict(self):
        """Test creation of bimap from empty dictionary."""
        bimap = BiMap(dict())

        self.assertEqual(0, len(bimap))

        self.assertEqual(dict(), bimap.get_key_to_value())
        self.assertEqual(dict(), bimap.get_value_to_key())
Ejemplo n.º 2
0
    def test_add_to_nonempty(self):
        """Test adding multiple items to nonempty bimap."""
        bimap = BiMap({1: "first", 2: "second"})
        bimap.update_by_value({"third": 3, "fourth": 4})
        bimap.update_by_key({2: "secondNew", 3: "thirdNew"})

        self.assertEqual(4, len(bimap))
        self.assertEqual("first", bimap.get_key(1))
        self.assertEqual("secondNew", bimap.get_key(2))
        self.assertEqual(3, bimap.get_value("thirdNew"))
        self.assertEqual(4, bimap.get_value("fourth"))
Ejemplo n.º 3
0
class TestCreateEmpty(unittest.TestCase):
    """
    Test the constructor.
    """
    def setUp(self):
        self.bimap = BiMap()

    def test_new_map_is_empty(self):
        """Test whether a newly created bimap is empty if not given any arguments."""
        self.assertEqual(dict(), self.bimap.get_key_to_value())
        self.assertEqual(dict(), self.bimap.get_value_to_key())
        self.assertEqual(0, len(self.bimap))
Ejemplo n.º 4
0
    def test_add_to_nonempty(self):
        """Test adding a single item to nonempty bimap."""
        bimap = BiMap({1: "first", 2: "second"})
        bimap.add_key(3, "third")
        bimap.add_value("fourth", 4)

        self.assertEqual(4, len(bimap))
        self.assertEqual("second", bimap.get_key(2))
        self.assertEqual("fourth", bimap.get_key(4))
        self.assertEqual(3, bimap.get_value("third"))
Ejemplo n.º 5
0
    def test_add_by_value_to_empty(self):
        """Test adding a single item to empty bimap by value."""
        bimap = BiMap()
        bimap.add_value("first", 1)

        self.assertEqual({1: "first"}, bimap.get_key_to_value())
        self.assertEqual(1, len(bimap))
        self.assertEqual(1, bimap.get_value("first"))
        self.assertEqual("first", bimap.get_key(1))
Ejemplo n.º 6
0
    def test_add_by_value_to_empty(self):
        """Test adding multiple items to an empty bimap by value."""
        bimap = BiMap()
        bimap.update_by_value({"first": 1, "second": 2})

        self.assertEqual(2, len(bimap))
        self.assertEqual("first", bimap.get_key(1))
        self.assertEqual(2, bimap.get_value("second"))
Ejemplo n.º 7
0
    def test_nonempty_dict(self):
        """Test creation of bimap from nonemtpy dictionary."""
        dictionary = {1: "first", 2: "second"}
        bimap = BiMap(dictionary)

        reverse = dict()
        for key in dictionary:
            reverse[dictionary[key]] = key

        self.assertEqual(2, len(bimap))

        self.assertEqual(dictionary, bimap.get_key_to_value())
        self.assertEqual("first", bimap.get_key(1))

        self.assertEqual(2, bimap.get_value("second"))
        self.assertEqual(reverse, bimap.get_value_to_key())
Ejemplo n.º 8
0
 def setUp(self):
     self.bimap = BiMap()
Ejemplo n.º 9
0
    def test_clear_nonempty(self):
        """Clear a nonempty bimap."""
        bimap = BiMap({1: "first", 2: "second"})
        bimap.clear()

        self.assertEqual(0, len(bimap))
Ejemplo n.º 10
0
    def test_clear_empty(self):
        """Clear an empty bimap."""
        bimap = BiMap()
        bimap.clear()

        self.assertEqual(0, len(bimap))