-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_test.go
166 lines (143 loc) · 3.52 KB
/
merge_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package trie
import (
"bytes"
"testing"
)
// func TestMergeEdges(t *testing.T) {
// // testMerge := func(a, b edges) (edges, int) {
// // return merge(new(Txn), 0, a, b, false)
// // }
// }
//
// func TestMergeNodes_complete(t *testing.T) {
//
// }
func TestMergeNodes_nilValue(t *testing.T) {
n := node("food", 1, edges{
node("foodz", 2, nil),
})
o := node("foo", 1, edges{
node("food", nil, edges{
node("foods", 2, nil),
node("foodz", 3, nil),
}),
})
p := n.Merge(o)
q := o.Merge(n)
expectedP := node("foo", 1, edges{
node("food", 1, edges{
node("foods", 2, nil),
node("foodz", 3, nil),
}),
})
expectedQ := node("foo", 1, edges{
node("food", 1, edges{
node("foods", 2, nil),
node("foodz", 2, nil),
}),
})
if !Equal(p, expectedP) {
t.Errorf("expected %#v, got %#v", expectedP, p)
}
if !Equal(q, expectedQ) {
t.Errorf("expected %#v, got %#v", expectedQ, q)
}
}
func TestMergeNodes(t *testing.T) {
n := node("foodz", 4, nil)
o := node("foodz", 2, nil)
if res := o.Merge(n); res != n {
t.Errorf("expected %q.merge(%q) to result in %p, got %p", o.key, n.key, n, res)
}
n = node("food", 1, edges{
node("foodi", nil, edges{
node("foodie", 2, edges{
node("foodies", 3, nil),
}),
node("fooding", 4, nil),
}),
node("foodz", 5, nil),
})
o = node("foo", 1, edges{
node("foo bar", 2, nil),
node("food", nil, edges{
node("fooding", 3, nil),
node("foods", 4, edges{
node("foods with friends", 5, nil),
}),
}),
})
p := n.Merge(o)
q := o.Merge(n)
expectedP := node("foo", 1, edges{
node("foo bar", 2, nil),
node("food", 1, edges{
node("foodi", nil, edges{
node("foodie", 2, edges{
node("foodies", 3, nil),
}),
node("fooding", 3, nil),
}),
node("foods", 4, edges{
node("foods with friends", 5, nil),
}),
node("foodz", 5, nil),
}),
})
expectedQ := node("foo", 1, edges{
node("foo bar", 2, nil),
node("food", 1, edges{
node("foodi", nil, edges{
node("foodie", 2, edges{
node("foodies", 3, nil),
}),
node("fooding", 4, nil),
}),
node("foods", 4, edges{
node("foods with friends", 5, nil),
}),
node("foodz", 5, nil),
}),
})
if !Equal(p, expectedP) {
t.Errorf("expected %#v, got %#v", expectedP, p)
}
if !Equal(q, expectedQ) {
t.Errorf("expected %#v, got %#v", expectedQ, q)
}
}
func TestMerge_1(t *testing.T) {
n := &Node{key: fooBar, value: []byte("12345")}
m := n.Put(fooBaz, []byte("2"))
tx := new(Txn)
tx.Put(fooBar, []byte("12345"))
tx.Put(fooBaz, []byte("2"))
tx.Merge(m)
o := tx.Commit()
if res := o.GetString("foo bar").([]byte); !bytes.Equal(res, []byte("12345")) {
t.Errorf(`expected "12345", got %q`, res)
}
if res := o.GetString("foo baz").([]byte); !bytes.Equal(res, []byte("2")) {
t.Errorf(`expected "2", got %q`, res)
}
}
func TestMerge_2(t *testing.T) {
n := node("foo bar", []byte("12345"), nil)
o := n.Merge(n.PutString("foo baz", []byte("2")))
if res := o.GetString("foo bar").([]byte); !bytes.Equal(res, []byte("12345")) {
t.Fatalf(`expected "12345", got %q`, res)
}
if res := o.GetString("foo baz").([]byte); !bytes.Equal(res, []byte("2")) {
t.Fatalf(`expected "2", got %q`, res)
}
}
func TestMerge_3(t *testing.T) {
n := node("foo bar", []byte("12345"), nil)
o := n.Merge(n.PutString("foo", []byte("2")))
if res := o.GetString("foo bar").([]byte); !bytes.Equal(res, []byte("12345")) {
t.Fatalf(`expected "12345", got %q`, res)
}
if res := o.GetString("foo").([]byte); !bytes.Equal(res, []byte("2")) {
t.Fatalf(`expected "2", got %q`, res)
}
}