forked from nqd/flat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie_test.go
108 lines (104 loc) · 2.56 KB
/
trie_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package flat
import (
"reflect"
"strings"
"testing"
)
func TestTrie(t *testing.T) {
tests := []struct {
data map[string]interface{}
want map[string]interface{}
}{
{
map[string]interface{}{"hello": "world"},
map[string]interface{}{"hello": "world"},
},
{
map[string]interface{}{"hello.world.again": "good morning"},
map[string]interface{}{
"hello": map[string]interface{}{
"world": map[string]interface{}{
"again": "good morning",
},
},
},
},
{
map[string]interface{}{"a.0.0": 1, "a.0.1": 2, "a.1.0": 21, "a.1.1": 22},
map[string]interface{}{
"a": []interface{}{
[]interface{}{1, 2},
[]interface{}{21, 22},
},
},
},
{
map[string]interface{}{"a.0.0": "1", "a.0.1": "2", "a.1.0": "21", "a.1.1": "22"},
map[string]interface{}{
"a": []interface{}{
[]interface{}{"1", "2"},
[]interface{}{"21", "22"},
},
},
},
{
map[string]interface{}{"a.0": "1", "a.1": "2", "b": "21"},
map[string]interface{}{"a": []interface{}{"1", "2"}, "b": "21"},
},
{
map[string]interface{}{"a.b.0": "1", "a.b.1": "2", "c": "21"},
map[string]interface{}{"a": map[string]interface{}{"b": []interface{}{"1", "2"}}, "c": "21"},
},
{
map[string]interface{}{"a.0.b.0": "1", "a.0.b.1": "2", "a.1.b.0": "3", "a.1.b.1": "4", "c": "21"},
map[string]interface{}{
"a": []interface{}{
map[string]interface{}{
"b": []interface{}{"1", "2"},
},
map[string]interface{}{
"b": []interface{}{"3", "4"},
},
},
"c": "21",
},
},
{
map[string]interface{}{
"a.0.b.0.d": "1", "a.0.b.0.e": "2",
"a.0.b.1.d": "3", "a.0.b.1.e": "4",
"a.1.b.0.d": "11", "a.1.b.0.e": "12",
"a.1.b.0.f": "13", "a.1.b.0.g": "14",
"a.1.b.1.d": "15", "a.1.b.1.e": "16",
"a.1.b.1.f": "17", "a.1.b.1.g": "18",
"c": "21"},
map[string]interface{}{
"a": []interface{}{
map[string]interface{}{
"b": []interface{}{
map[string]interface{}{"d": "1", "e": "2"},
map[string]interface{}{"d": "3", "e": "4"},
},
},
map[string]interface{}{
"b": []interface{}{
map[string]interface{}{"d": "11", "e": "12", "f": "13", "g": "14"},
map[string]interface{}{"d": "15", "e": "16", "f": "17", "g": "18"},
},
},
},
"c": "21",
},
},
}
for i, test := range tests {
root := &TrieNode{}
for k, v := range test.data {
root.insert(strings.Split(k, "."), v)
}
got := root.unflatten()
if !reflect.DeepEqual(got, test.want) {
t.Errorf("%d: mismatch, got: %v want: %v", i+1, got, test.want)
}
}
}