Ejemplo n.º 1
0
def custom_trie():
    t = Trie()
    t.put(['x', 'y', 'w', 'a', 'b'], 'value1')
    t.put(['x', 'y'], 'value2')
    t.put(['x'], 'value3')
    t.put(['y'], 'value4')
    t.put(['z', 'a', 'b'], 'value5')
    return t
Ejemplo n.º 2
0
def test_count_up_or_set():
    t = Trie()
    assert t.count_up_or_set(['x']) is None
    assert t.count_up_or_set(['x']) == 1
    assert t.count_up_or_set(['x', 'y']) is None
    assert t.count_up_or_set(['x', 'y']) == 1
    assert t[['x']] == '2'
    assert t[['x', 'y']] == '2'
Ejemplo n.º 3
0
def test_count_down_or_del():
    t = Trie()
    t.put(['x'], '2')
    t.put(['x', 'y'], '2')
    assert t.count_down_or_del(['x']) == 2
    assert t.count_down_or_del(['x']) == 1
    assert t.count_down_or_del(['x']) is None
    assert t.count_down_or_del(['x', 'y']) == 2
    assert t.count_down_or_del(['x', 'y']) == 1
    assert t.count_down_or_del(['x', 'y']) is None
    assert t[['x']] is None
    assert t[['x', 'y']] is None
Ejemplo n.º 4
0
def test_trie_dict_hypothesis():
    random.seed(0)
    trials = 10000
    size = 8
    key_len = 8
    for _ in range(trials):
        t = Trie()
        normal_dict = {}
        # insert
        num_keys = random.randint(0, size)
        for _ in range(num_keys):
            key = ''.join(
                random.choices('a0!///', k=random.randint(1, key_len)))
            value = str(random.randint(1, key_len))
            clean_key = '/'.join(split(key))
            t.put(split(key), value)
            normal_dict[clean_key] = value
            assert split(key) in t
            assert t.flatten() == normal_dict
            assert set(_join_items(t.items([]))) == set(normal_dict.items())
            assert set(_join_keys(t.keys([]))) == set(normal_dict.keys())
            assert len(t) == len(normal_dict)
        # clone
        clone = t.clone()
        assert t.flatten() == clone.flatten()
        assert t.to_json() == clone.to_json()
        assert _join_keys(t.keys([])) == _join_keys(clone.keys([]))
        assert _join_items(t.items([])) == _join_items(clone.items([]))
        # delete
        for key in list(normal_dict.keys()):
            del normal_dict[key]
            t.discard(split(key))
            assert split(key) not in t
            assert t.flatten() == normal_dict
            assert set(_join_items(t.items([]))) == set(normal_dict.items())
            assert set(_join_keys(t.keys([]))) == set(normal_dict.keys())
            assert len(t) == len(normal_dict)
Ejemplo n.º 5
0
def test_trie_dict_copy():
    t = Trie()
    t.put(['x'], 'value')
    t.put(['x', 'y'], 'value')
    clone = t.clone()
    assert t.flatten() == clone.flatten()
    assert t.to_json() == clone.to_json()
    assert _join_keys(t.keys([])) == _join_keys(clone.keys([]))
    assert _join_items(t.items([])) == _join_items(clone.items([]))
Ejemplo n.º 6
0
def test_from_json_multiple():
    t = Trie.from_json({'x': ('value1', {'y': 'value2'}), 'z': 'value3'})
    assert t.flatten() == {'x': 'value1', 'x/y': 'value2', 'z': 'value3'}
Ejemplo n.º 7
0
def test_trie_dict_del_missing_nested_key():
    t = Trie()
    t.put(['x'], 'value')
    assert t.discard(['x', 'y']) is None
Ejemplo n.º 8
0
def test_trie_dict_get_missing_nested_key():
    t = Trie()
    assert t.put(['x'], 'value') is None
    assert t[['x', 'y']] is None
Ejemplo n.º 9
0
def test_trie_dict_key_insert():
    t = Trie()
    assert t.put(['x'], 'value_x') is None
    assert t[['x']] == 'value_x'
Ejemplo n.º 10
0
def test_to_json_empty():
    t = Trie()
    assert t.to_json() == {}
Ejemplo n.º 11
0
def test_trie_dict_items_empty():
    assert _join_items(Trie().items([])) == []
Ejemplo n.º 12
0
def test_trie_dict_keys_empty():
    t = Trie()
    assert set(t.keys([])) == set()
Ejemplo n.º 13
0
def test_trie_dict_nested_key_insert_len():
    t = Trie()
    t.put(['x'], 'value_x')
    t.put(['x', 'y'], 'value_y')
    assert len(t) == 2
Ejemplo n.º 14
0
def test_trie_dict_nested_key_insert():
    t = Trie()
    t.put(['x', 'y'], 'value_x')
    assert t[['x', 'y']] == 'value_x'
Ejemplo n.º 15
0
def test_trie_dict_truthiness_empty():
    t = Trie()
    assert not bool(t)
Ejemplo n.º 16
0
def test_to_json_single():
    t = Trie()
    t.put(['x'], 'value')
    assert t.to_json() == {'x': 'value'}
Ejemplo n.º 17
0
def test_trie_dict_truthiness_non_empty():
    t = Trie()
    t.put(['x'], 'value')
    assert bool(t)
Ejemplo n.º 18
0
def test_to_json_multiple():
    t = Trie()
    t.put(['x'], 'value1')
    t.put(['x', 'y'], 'value2')
    t.put(['z'], 'value3')
    assert t.to_json() == {'x': ('value1', {'y': 'value2'}), 'z': 'value3'}
Ejemplo n.º 19
0
def test_trie_dict_get_missing_key():
    t = Trie()
    assert t[['x']] is None
Ejemplo n.º 20
0
def test_from_json_empty():
    t = Trie.from_json({})
    assert t.flatten() == {}
Ejemplo n.º 21
0
def test_trie_dict_del_key():
    t = Trie()
    t.put(['x'], 'value')
    assert t.discard(['x']) == 'value'
    assert t.flatten() == {}
Ejemplo n.º 22
0
def test_from_json_single():
    t = Trie.from_json({'x': 'value'})
    assert t.flatten() == {'x': 'value'}
Ejemplo n.º 23
0
def test_trie_dict_del_missing_key():
    t = Trie()
    assert t.discard(['x']) is None
Ejemplo n.º 24
0
def test_trie_dict_del_nested_key():
    t = Trie()
    t.put(['x'], 'value1')
    t.put(['x', 'y'], 'value2')
    assert t.discard(['x', 'y']) == 'value2'
    assert t.flatten() == {'x': 'value1'}