Beispiel #1
0
    def test_fuzz_deletions():
        """A test that inserts random keys into the tree and checks that they were all inserted."""
        key_range = 2**64
        value_range = 1024
        key_set = set()

        d = OrderedTreeDict()
        for value in range(0, value_range):
            key = randint(0, key_range)
            d.put(key, value)
            key_set.add(key)

        sorted_keys = list(sorted(key_set))
        sorted_keys_slice = sorted_keys[0:len(sorted_keys) // 2]

        for key in sorted_keys_slice:
            d.delete(key)
            assert len(d) > 0
            assert key not in d
            assert d.depth() <= int(2 * math.log(
                len(d), 2)), "Should stay as balanced as a red black tree. "

        keys = list(d.keys())
        assert len(
            keys) == len(sorted_keys_slice
                         ), "Length should reflect number of items inserted."
        assert len(keys) == len(
            list(keys)), "Iteration should find all items in tree."
Beispiel #2
0
 def test_sorted():
     """A test to ensure the tree maintains sorted order."""
     keys = list(set([randint(0, 2 ^ 64) for i in range(0, 128)]))
     items = [(key, None) for key in keys]
     d = OrderedTreeDict(items)
     assert len(keys) == len(d)
     assert len(keys) == len(list(d))
     assert list(sorted(keys)) == list(d.keys())
Beispiel #3
0
    def test_fuzz_insertions():
        """A test that inserts random keys into the tree and checks that they were all inserted."""
        key_range = 2**64
        value_range = 1024
        key_set = set()

        d = OrderedTreeDict()
        for value in range(0, value_range):
            key = randint(0, key_range)
            d.put(key, value)
            key_set.add(key)

        keys = list(d.keys())
        assert len(keys) == len(
            key_set), "Length should reflect number of items inserted."
        assert len(keys) == len(
            list(keys)), "Iteration should find all items in tree."
        assert d.depth() <= math.ceil(1.44 * math.log2(len(d))), "Should stay as balanced as an avl tree as long as " \
                                                                 "there are only insertions. "