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([]))
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)
def test_trie_dict_keys_empty(): t = Trie() assert set(t.keys([])) == set()