Exemplo n.º 1
0
 def test_update(self):
     d = test_items_seq()
     tree = AnderssonTree()
     tree.update(d)
     self.assertEqual(len(tree), len(d))
     self.assertEqual(set(tree.keys()), set(x[0] for x in d))
     self.assertEqual(set(tree.values()), set(x[1] for x in d))
     self.check_aatree_properties(tree)
Exemplo n.º 2
0
 def test_setitem(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     self.check_aatree_properties(tree)
     keys = [x[0] for x in s]
     shuffle(keys)
     for k in keys:
         val = k * 10
         tree[k] = val
         self.assertEqual(tree.get(k), val)
Exemplo n.º 3
0
 def test_pickle_protocol(self):
     s = test_items_seq()
     tree1 = AnderssonTree(s)
     pickle_str = pickle.dumps(tree1, -1)
     tree2 = pickle.loads(pickle_str)
     self.assertEqual(len(tree1), len(tree2))
     self.assertEqual(list(tree1.keys()), list(tree2.keys()))
     self.assertEqual(list(tree1.values()), list(tree2.values()))
     self.check_aatree_properties(tree1)
     self.check_aatree_properties(tree2)
Exemplo n.º 4
0
 def test_get(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     self.check_aatree_properties(tree)
     keys = [x[0] for x in s]
     shuffle(keys)
     for k in keys:
         self.assertEqual(tree.get(k), k)
         self.assertEqual(tree[k], k)
     max_ = max(keys)
     self.assertEqual(tree[max_ + 1], None)
Exemplo n.º 5
0
    def test_traverse(self):
        s = test_items_seq()
        tree = AnderssonTree(s)
        visited = set()

        def visit(node):
            visited.add(node.key)

        tree.traverse(visit)
        keys = set([x[0] for x in s])
        self.assertEqual(keys, visited)
Exemplo n.º 6
0
 def test_insert(self):
     s = test_items_seq()
     tree = AnderssonTree()
     size = len(tree)
     keys = [x[0] for x in s]
     keyset = set()
     shuffle(keys)
     for k in keys:
         keyset.add(k)
         tree.insert(k, k)
         size += 1
         self.assertEqual(len(tree), size)
         self.assertEqual(set(tree.keys()), keyset)
         self.check_aatree_properties(tree)
Exemplo n.º 7
0
 def test_remove_root(self):
     s = test_items_seq(1000)
     tree = AnderssonTree(s)
     self.check_aatree_properties(tree)
     size = len(tree)
     keys = [x[0] for x in s]
     keyset = set(keys)
     while len(keyset) > 0:
         k = tree.root().key
         keyset.remove(k)
         tree.remove(k)
         size -= 1
         self.assertEqual(len(tree), size)
         self.assertEqual(set(tree.keys()), keyset)
         self.check_aatree_properties(tree)
Exemplo n.º 8
0
 def test_is_empty(self):
     s = test_items_seq()
     tree = AnderssonTree()
     self.assertTrue(tree.is_empty())
     tree.update(s)
     self.assertFalse(tree.is_empty())
     tree.clear()
     self.assertTrue(tree.is_empty())
Exemplo n.º 9
0
 def test_remove(self):
     for i in range(10):
         s = test_items_seq(100)
         tree = AnderssonTree(s)
         self.check_aatree_properties(tree)
         size = len(tree)
         keys = [x[0] for x in s]
         keyset = set(keys)
         shuffle(keys)
         for k in keys:
             keyset.remove(k)
             tree.remove(k)
             size -= 1
             self.assertEqual(len(tree), size)
             self.assertEqual(set(tree.keys()), keyset)
             self.check_aatree_properties(tree, dump=False)
Exemplo n.º 10
0
    def test_max_btree_level(self):
        def _map_levels(node, level, map_):
            if node is not None:
                map_[node.key] = level
                next_level = level + 1
                _map_levels(node.left, next_level, map_)
                _map_levels(node.right, next_level, map_)

        def _map_bin_tree_levels(root):
            """ level is binary tree level, not AATree level """
            map_ = {}
            _map_levels(root, 0, map_)
            max_level = max(map_.values())
            for k, v in map_.items():
                map_[k] = max_level - map_[k] + 1
            return map_
        keys = range(1, 14)
        tree = AnderssonTree(zip(keys, keys))
        mapping = _map_bin_tree_levels(tree.root())
        maxl = max(mapping.values())
        self.assertEqual(maxl, 5, 'Invalid max level %s!' % maxl)
Exemplo n.º 11
0
 def test_clear(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     self.check_aatree_properties(tree)
     tree.clear()
     self.assertEqual(len(tree), 0)
Exemplo n.º 12
0
 def test_values(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     values = set(x[1] for x in s)
     self.assertEqual(set(tree.values()), values)
Exemplo n.º 13
0
 def test_keys(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     keys = set(x[0] for x in s)
     self.assertEqual(set(tree.keys()), keys)
Exemplo n.º 14
0
 def test_root(self):
     x = [5, 6, 4]
     tree = AnderssonTree(zip(x, x))
     self.assertEqual(tree.root().key, 5)
Exemplo n.º 15
0
 def test_copy(self):
     s = test_items_seq()
     tree1 = AnderssonTree(s)
     tree2 = tree1.copy()
     self.assertEqual(len(tree1), len(tree2))
     self.assertEqual(repr(tree1), repr(tree2))
Exemplo n.º 16
0
 def test_iter_items_reversed(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     items = [x for x in tree.iter_items(reverse=True)]
     self.assertEqual(items, sorted(s, reverse=True))
Exemplo n.º 17
0
 def test_iter_items(self):
     s = test_items_seq()
     tree = AnderssonTree(s)
     items = [x for x in tree.iter_items()]
     self.assertEqual(items, sorted(s))