1 package utilities_test
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/grpc-ecosystem/grpc-gateway/utilities"
8 )
9
10 func TestMaxCommonPrefix(t *testing.T) {
11 for _, spec := range []struct {
12 da utilities.DoubleArray
13 tokens []string
14 want bool
15 }{
16 {
17 da: utilities.DoubleArray{},
18 tokens: nil,
19 want: false,
20 },
21 {
22 da: utilities.DoubleArray{},
23 tokens: []string{"foo"},
24 want: false,
25 },
26 {
27 da: utilities.DoubleArray{
28 Encoding: map[string]int{
29 "foo": 0,
30 },
31 Base: []int{1, 1, 0},
32 Check: []int{0, 1, 2},
33 },
34 tokens: nil,
35 want: false,
36 },
37 {
38 da: utilities.DoubleArray{
39 Encoding: map[string]int{
40 "foo": 0,
41 },
42 Base: []int{1, 1, 0},
43 Check: []int{0, 1, 2},
44 },
45 tokens: []string{"foo"},
46 want: true,
47 },
48 {
49 da: utilities.DoubleArray{
50 Encoding: map[string]int{
51 "foo": 0,
52 },
53 Base: []int{1, 1, 0},
54 Check: []int{0, 1, 2},
55 },
56 tokens: []string{"bar"},
57 want: false,
58 },
59 {
60
61 da: utilities.DoubleArray{
62 Encoding: map[string]int{
63 "foo": 0,
64 "bar": 1,
65 },
66 Base: []int{1, 1, 2, 0, 0},
67 Check: []int{0, 1, 1, 2, 3},
68
69
70
71
72
73 },
74 tokens: []string{"foo"},
75 want: true,
76 },
77 {
78
79 da: utilities.DoubleArray{
80 Encoding: map[string]int{
81 "foo": 0,
82 "bar": 1,
83 },
84 Base: []int{1, 1, 2, 0, 0},
85 Check: []int{0, 1, 1, 2, 3},
86
87
88
89
90
91 },
92 tokens: []string{"bar"},
93 want: true,
94 },
95 {
96
97 da: utilities.DoubleArray{
98 Encoding: map[string]int{
99 "foo": 0,
100 "bar": 1,
101 },
102 Base: []int{1, 1, 2, 0, 0},
103 Check: []int{0, 1, 1, 2, 3},
104
105
106
107
108
109 },
110 tokens: []string{"something-else"},
111 want: false,
112 },
113 {
114
115 da: utilities.DoubleArray{
116 Encoding: map[string]int{
117 "foo": 0,
118 "bar": 1,
119 },
120 Base: []int{1, 1, 2, 0, 0},
121 Check: []int{0, 1, 1, 2, 3},
122
123
124
125
126
127 },
128 tokens: []string{"foo", "bar"},
129 want: true,
130 },
131 {
132
133 da: utilities.DoubleArray{
134 Encoding: map[string]int{
135 "foo": 0,
136 "bar": 1,
137 },
138 Base: []int{1, 3, 1, 0, 4, 0, 0},
139 Check: []int{0, 1, 1, 3, 2, 2, 5},
140
141
142
143
144
145
146
147 },
148 tokens: []string{"foo"},
149 want: true,
150 },
151 {
152
153 da: utilities.DoubleArray{
154 Encoding: map[string]int{
155 "foo": 0,
156 "bar": 1,
157 },
158 Base: []int{1, 3, 1, 0, 4, 0, 0},
159 Check: []int{0, 1, 1, 3, 2, 2, 5},
160
161
162
163
164
165
166
167 },
168 tokens: []string{"foo", "bar"},
169 want: true,
170 },
171 {
172
173 da: utilities.DoubleArray{
174 Encoding: map[string]int{
175 "foo": 0,
176 "bar": 1,
177 },
178 Base: []int{1, 3, 1, 0, 4, 0, 0},
179 Check: []int{0, 1, 1, 3, 2, 2, 5},
180
181
182
183
184
185
186
187 },
188 tokens: []string{"bar"},
189 want: true,
190 },
191 {
192
193 da: utilities.DoubleArray{
194 Encoding: map[string]int{
195 "foo": 0,
196 "bar": 1,
197 },
198 Base: []int{1, 3, 1, 0, 4, 0, 0},
199 Check: []int{0, 1, 1, 3, 2, 2, 5},
200
201
202
203
204
205
206
207 },
208 tokens: []string{"something-else"},
209 want: false,
210 },
211 {
212
213 da: utilities.DoubleArray{
214 Encoding: map[string]int{
215 "foo": 0,
216 "bar": 1,
217 },
218 Base: []int{1, 3, 1, 0, 4, 0, 0},
219 Check: []int{0, 1, 1, 3, 2, 2, 5},
220
221
222
223
224
225
226
227 },
228 tokens: []string{"foo", "bar", "baz"},
229 want: true,
230 },
231 } {
232 got := spec.da.HasCommonPrefix(spec.tokens)
233 if got != spec.want {
234 t.Errorf("%#v.HasCommonPrefix(%v) = %v; want %v", spec.da, spec.tokens, got, spec.want)
235 }
236 }
237 }
238
239 func TestAdd(t *testing.T) {
240 for _, spec := range []struct {
241 tokens [][]string
242 want utilities.DoubleArray
243 }{
244 {
245 want: utilities.DoubleArray{
246 Encoding: make(map[string]int),
247 },
248 },
249 {
250 tokens: [][]string{{"foo"}},
251 want: utilities.DoubleArray{
252 Encoding: map[string]int{"foo": 0},
253 Base: []int{1, 1, 0},
254 Check: []int{0, 1, 2},
255
256
257
258 },
259 },
260 {
261 tokens: [][]string{{"foo"}, {"bar"}},
262 want: utilities.DoubleArray{
263 Encoding: map[string]int{
264 "foo": 0,
265 "bar": 1,
266 },
267 Base: []int{1, 1, 2, 0, 0},
268 Check: []int{0, 1, 1, 2, 3},
269
270
271
272
273
274 },
275 },
276 {
277 tokens: [][]string{{"foo", "bar"}, {"foo", "baz"}},
278 want: utilities.DoubleArray{
279 Encoding: map[string]int{
280 "foo": 0,
281 "bar": 1,
282 "baz": 2,
283 },
284 Base: []int{1, 1, 1, 2, 0, 0},
285 Check: []int{0, 1, 2, 2, 3, 4},
286
287
288
289
290
291
292 },
293 },
294 {
295 tokens: [][]string{{"foo", "bar"}, {"foo", "baz"}, {"qux"}},
296 want: utilities.DoubleArray{
297 Encoding: map[string]int{
298 "foo": 0,
299 "bar": 1,
300 "baz": 2,
301 "qux": 3,
302 },
303 Base: []int{1, 1, 1, 2, 3, 0, 0, 0},
304 Check: []int{0, 1, 2, 2, 1, 3, 4, 5},
305
306
307
308
309
310
311
312
313 },
314 },
315 {
316 tokens: [][]string{
317 {"foo", "bar"},
318 {"foo", "baz", "bar"},
319 {"qux", "foo"},
320 },
321 want: utilities.DoubleArray{
322 Encoding: map[string]int{
323 "foo": 0,
324 "bar": 1,
325 "baz": 2,
326 "qux": 3,
327 },
328 Base: []int{1, 1, 1, 5, 8, 0, 3, 0, 5, 0},
329 Check: []int{0, 1, 2, 2, 1, 3, 4, 7, 5, 9},
330
331
332
333
334
335
336
337
338
339
340 },
341 },
342 } {
343 da := utilities.NewDoubleArray(spec.tokens)
344 if got, want := da.Encoding, spec.want.Encoding; !reflect.DeepEqual(got, want) {
345 t.Errorf("da.Encoding = %v; want %v; tokens = %#v", got, want, spec.tokens)
346 }
347 if got, want := da.Base, spec.want.Base; !compareArray(got, want) {
348 t.Errorf("da.Base = %v; want %v; tokens = %#v", got, want, spec.tokens)
349 }
350 if got, want := da.Check, spec.want.Check; !compareArray(got, want) {
351 t.Errorf("da.Check = %v; want %v; tokens = %#v", got, want, spec.tokens)
352 }
353 }
354 }
355
356 func compareArray(got, want []int) bool {
357 var i int
358 for i = 0; i < len(got) && i < len(want); i++ {
359 if got[i] != want[i] {
360 return false
361 }
362 }
363 if i < len(want) {
364 return false
365 }
366 for ; i < len(got); i++ {
367 if got[i] != 0 {
368 return false
369 }
370 }
371 return true
372 }
373
View as plain text