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
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'
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
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_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_from_json_multiple(): t = Trie.from_json({'x': ('value1', {'y': 'value2'}), 'z': 'value3'}) assert t.flatten() == {'x': 'value1', 'x/y': 'value2', 'z': 'value3'}
def test_trie_dict_del_missing_nested_key(): t = Trie() t.put(['x'], 'value') assert t.discard(['x', 'y']) is None
def test_trie_dict_get_missing_nested_key(): t = Trie() assert t.put(['x'], 'value') is None assert t[['x', 'y']] is None
def test_trie_dict_key_insert(): t = Trie() assert t.put(['x'], 'value_x') is None assert t[['x']] == 'value_x'
def test_to_json_empty(): t = Trie() assert t.to_json() == {}
def test_trie_dict_items_empty(): assert _join_items(Trie().items([])) == []
def test_trie_dict_keys_empty(): t = Trie() assert set(t.keys([])) == set()
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
def test_trie_dict_nested_key_insert(): t = Trie() t.put(['x', 'y'], 'value_x') assert t[['x', 'y']] == 'value_x'
def test_trie_dict_truthiness_empty(): t = Trie() assert not bool(t)
def test_to_json_single(): t = Trie() t.put(['x'], 'value') assert t.to_json() == {'x': 'value'}
def test_trie_dict_truthiness_non_empty(): t = Trie() t.put(['x'], 'value') assert bool(t)
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'}
def test_trie_dict_get_missing_key(): t = Trie() assert t[['x']] is None
def test_from_json_empty(): t = Trie.from_json({}) assert t.flatten() == {}
def test_trie_dict_del_key(): t = Trie() t.put(['x'], 'value') assert t.discard(['x']) == 'value' assert t.flatten() == {}
def test_from_json_single(): t = Trie.from_json({'x': 'value'}) assert t.flatten() == {'x': 'value'}
def test_trie_dict_del_missing_key(): t = Trie() assert t.discard(['x']) is None
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'}