/
rht_test.go
203 lines (186 loc) · 4.64 KB
/
rht_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package crdt_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yorkie-team/yorkie/pkg/document/crdt"
"github.com/yorkie-team/yorkie/test/helper"
)
func TestRHT_Marshal(t *testing.T) {
tests := []struct {
desc string
insertKey string
insertVal string
expectStr string
}{
{
desc: `1. empty hash table`,
insertKey: ``,
insertVal: ``,
expectStr: `{}`,
},
{
desc: `2. only one element`,
insertKey: "hello\\\\\\t",
insertVal: "world\"\f\b",
expectStr: `{"hello\\\\\\t":"world\"\f\b"}`,
},
{
desc: `3. non-empty hash table`,
insertKey: "hi",
insertVal: `test\r`,
expectStr: `{"hello\\\\\\t":"world\"\f\b","hi":"test\\r"}`,
},
}
root := helper.TestRoot()
ctx := helper.TextChangeContext(root)
rht := crdt.NewRHT()
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if len(tt.insertKey) > 0 {
rht.Set(tt.insertKey, tt.insertVal, ctx.IssueTimeTicket())
}
assert.Equal(t, tt.expectStr, rht.Marshal())
})
}
}
func TestRHT_Set(t *testing.T) {
key1, val1 := `key1`, `value1`
key2, val2 := `key2`, `value2`
tests := []struct {
desc string
insertKey []string
insertVal []string
expectStr string
expectSize int
}{
{
desc: `1. set elements`,
insertKey: []string{key1, key2},
insertVal: []string{val1, val2},
expectStr: `{"key1":"value1","key2":"value2"}`,
expectSize: 2,
},
{
desc: `2. change elements`,
insertKey: []string{key1, key2},
insertVal: []string{val2, val1},
expectStr: `{"key1":"value2","key2":"value1"}`,
expectSize: 2,
},
}
root := helper.TestRoot()
ctx := helper.TextChangeContext(root)
rht := crdt.NewRHT()
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
for i, key := range tt.insertKey {
rht.Set(key, tt.insertVal[i], ctx.IssueTimeTicket())
}
assert.Equal(t, tt.expectStr, rht.Marshal())
assert.Equal(t, tt.expectSize, rht.Len())
})
}
}
func TestRHT_Remove(t *testing.T) {
key1, val1, val11 := `key1`, `value1`, `value11`
key2, val2, val22 := `key2`, `value2`, `value22`
tests := []struct {
desc string
insertKey []string
insertVal []string
deleteKey []string
deleteVal []string
expectXML string
expectJSON string
expectSize int
}{
{
desc: `1. set elements`,
insertKey: []string{key1, key2},
insertVal: []string{val1, val2},
deleteKey: []string{},
deleteVal: []string{},
expectXML: `key1="value1" key2="value2"`,
expectJSON: `{"key1":"value1","key2":"value2"}`,
expectSize: 2,
},
{
desc: `2. remove element`,
insertKey: []string{},
insertVal: []string{},
deleteKey: []string{key1},
deleteVal: []string{val1},
expectXML: `key2="value2"`,
expectJSON: `{"key2":"value2"}`,
expectSize: 1,
},
{
desc: `3. set after remove`,
insertKey: []string{key1},
insertVal: []string{val11},
deleteKey: []string{},
deleteVal: []string{},
expectXML: `key1="value11" key2="value2"`,
expectJSON: `{"key1":"value11","key2":"value2"}`,
expectSize: 2,
},
{
desc: `4. remove element`,
insertKey: []string{key2},
insertVal: []string{val22},
deleteKey: []string{key1},
deleteVal: []string{val11},
expectXML: `key2="value22"`,
expectJSON: `{"key2":"value22"}`,
expectSize: 1,
},
{
desc: `5. remove element again`,
insertKey: []string{},
insertVal: []string{},
deleteKey: []string{key1},
deleteVal: []string{``},
expectXML: `key2="value22"`,
expectJSON: `{"key2":"value22"}`,
expectSize: 1,
},
{
desc: `6. remove element(cleared)`,
insertKey: []string{},
insertVal: []string{},
deleteKey: []string{key2},
deleteVal: []string{val22},
expectXML: ``,
expectJSON: `{}`,
expectSize: 0,
},
{
desc: `7. remove not exist key`,
insertKey: []string{},
insertVal: []string{},
deleteKey: []string{`not-exist-key`},
deleteVal: []string{``},
expectXML: ``,
expectJSON: `{}`,
expectSize: 0,
},
}
root := helper.TestRoot()
ctx := helper.TextChangeContext(root)
rht := crdt.NewRHT()
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
for i, key := range tt.insertKey {
rht.Set(key, tt.insertVal[i], ctx.IssueTimeTicket())
}
for i, key := range tt.deleteKey {
removedElement := rht.Remove(key, ctx.IssueTimeTicket())
assert.Equal(t, tt.deleteVal[i], removedElement)
}
assert.Equal(t, tt.expectJSON, rht.Marshal())
assert.Equal(t, tt.expectSize, rht.Len())
assert.Equal(t, tt.expectSize, len(rht.Nodes()))
assert.Equal(t, tt.expectSize, len(rht.Elements()))
})
}
}