def test_contains(self): s = TreeSet() s.add('A') s.add('B') s.add('C') assert s.contains('A') is True assert s.contains('B') is True assert s.contains('D') is False s.remove('A') assert s.contains('A') is False assert s.size == 2
def test_remove(self): elements = ['I', 'D', 'W', 'T'] set = TreeSet(elements) with self.assertRaises(KeyError): set.remove('K') # Doesn't exist with self.assertRaises(KeyError): set.remove('M') # Doesn't exist set.remove('I') set.remove('T') assert set.contains('U') is False assert set.contains('Q') is False with self.assertRaises(KeyError): set.remove('Q') # Doesn't exist anymore
def test_remove_tuples(self): """Test remove method with tuples""" tree_set = TreeSet([('C', 3), ('B', 2), ('A', 1), ('E', 5), ('D', 4)]) assert tree_set.size == 5 assert tree_set.contains(('E', 5)) tree_set.remove(('E', 5)) assert not tree_set.contains(('E', 5)) assert tree_set.tree.root.data == ('C', 3) assert tree_set.tree.root.right.data == ('D', 4) assert tree_set.tree.root.left.data == ('B', 2) assert tree_set.size == 4 assert tree_set.contains(('B', 2)) tree_set.remove(('B', 2)) assert not tree_set.contains(('B', 2)) assert tree_set.tree.root.data == ('C', 3) assert tree_set.tree.root.right.data == ('D', 4) assert tree_set.tree.root.left.data == ('A', 1) assert tree_set.size == 3 assert tree_set.contains(('C', 3)) tree_set.remove(('C', 3)) assert not tree_set.contains(('C', 3)) assert tree_set.tree.root.data == ('D', 4) assert tree_set.tree.root.right is None assert tree_set.tree.root.left.data == ('A', 1) assert tree_set.size == 2 assert tree_set.contains(('D', 4)) tree_set.remove(('D', 4)) assert not tree_set.contains(('D', 4)) assert tree_set.tree.root.data == ('A', 1) assert tree_set.tree.root.right is None assert tree_set.tree.root.left is None assert tree_set.size == 1 assert tree_set.contains(('A', 1)) tree_set.remove(('A', 1)) assert not tree_set.contains(('A', 1)) assert tree_set.tree.root is None assert tree_set.size == 0
def test_remove_ints(self): """Test remove method with ints""" tree_set = TreeSet([3, 2, 1, 5, 4]) assert tree_set.size == 5 assert tree_set.contains(5) tree_set.remove(5) assert not tree_set.contains(5) assert tree_set.tree.root.data == 3 assert tree_set.tree.root.right.data == 4 assert tree_set.tree.root.left.data == 2 assert tree_set.size == 4 assert tree_set.contains(2) tree_set.remove(2) assert not tree_set.contains(2) assert tree_set.tree.root.data == 3 assert tree_set.tree.root.right.data == 4 assert tree_set.tree.root.left.data == 1 assert tree_set.size == 3 assert tree_set.contains(3) tree_set.remove(3) assert not tree_set.contains(3) assert tree_set.tree.root.data == 4 assert tree_set.tree.root.right is None assert tree_set.tree.root.left.data == 1 assert tree_set.size == 2 assert tree_set.contains(4) tree_set.remove(4) assert not tree_set.contains(4) assert tree_set.tree.root.data == 1 assert tree_set.tree.root.right is None assert tree_set.tree.root.left is None assert tree_set.size == 1 assert tree_set.contains(1) tree_set.remove(1) assert not tree_set.contains(1) assert tree_set.tree.root is None assert tree_set.size == 0
def test_remove_strings(self): """Test remove method with strings""" tree_set = TreeSet(['C', 'B', 'A', 'E', 'D']) assert tree_set.size == 5 assert tree_set.contains('E') tree_set.remove('E') assert not tree_set.contains('E') assert tree_set.tree.root.data == 'C' assert tree_set.tree.root.right.data == 'D' assert tree_set.tree.root.left.data == 'B' assert tree_set.size == 4 assert tree_set.contains('B') tree_set.remove('B') assert not tree_set.contains('B') assert tree_set.tree.root.data == 'C' assert tree_set.tree.root.right.data == 'D' assert tree_set.tree.root.left.data == 'A' assert tree_set.size == 3 assert tree_set.contains('C') tree_set.remove('C') assert not tree_set.contains('C') assert tree_set.tree.root.data == 'D' assert tree_set.tree.root.right is None assert tree_set.tree.root.left.data == 'A' assert tree_set.size == 2 assert tree_set.contains('D') tree_set.remove('D') assert not tree_set.contains('D') assert tree_set.tree.root.data == 'A' assert tree_set.tree.root.right is None assert tree_set.tree.root.left is None assert tree_set.size == 1 assert tree_set.contains('A') tree_set.remove('A') assert not tree_set.contains('A') assert tree_set.tree.root is None assert tree_set.size == 0