-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlookup_test.go
89 lines (71 loc) · 2.21 KB
/
lookup_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
// Copyright 2016 David Lavieri. All rights reserved.
// Use of this source code is governed by a MIT License
// License that can be found in the LICENSE file.
package goradix
import "testing"
func TestSourceLookUp(t *testing.T) {
radix := New(false)
insertData(radix, sampleData)
checkLookUp := func(toLook, expected string, expectedLeaf bool, expectedError error) {
node, _, err := radix.sLookUp([]byte(toLook))
if err != expectedError {
t.Logf("Expected Error: %v; Got: %v. Node: %s", expectedError, err, node.Path)
t.Fail()
}
if expectedError == ErrNoMatchFound && node == nil {
return
}
if node == nil {
t.Fail()
t.Logf("Expected node to not be: %v; Got: %v", nil, node)
return
}
if node.leaf != expectedLeaf {
t.Fail()
t.Logf("Expected Leaf: %v; Got: %v", expectedLeaf, node.leaf)
}
expectedPath := []byte(expected)
if len(node.Path) != len(expectedPath) {
t.Fail()
t.Logf("Expected path lenght: %d; Got: %d", len(expectedPath), len(node.Path))
return
}
for i, v := range node.Path {
if v != expectedPath[i] {
t.Fail()
t.Logf("Expected: %d; Got: %d", expectedPath[i], v)
}
}
}
// Correct
checkLookUp("t", "t", false, nil)
checkLookUp("toast", "oast", false, nil)
checkLookUp("toasting", "ing", true, nil)
checkLookUp("test", "est", true, nil)
checkLookUp("slow", "slow", false, nil)
checkLookUp("slowly", "ly", true, nil)
checkLookUp("toaster", "er", true, nil)
// Intentional fails
checkLookUp("toastar", "", false, ErrNoMatchFound)
checkLookUp("toasto", "", false, ErrNoMatchFound)
checkLookUp("tast", "", false, ErrNoMatchFound)
checkLookUp("slowe", "", false, ErrNoMatchFound)
}
func TestLookUp(t *testing.T) {
radix := New(false)
insertData(radix, sampleData)
toLookUp := []string{"t", "test", "toast", "toasting", "slowly"}
expectedValues := []interface{}{nil, 0, nil, 2, 4}
expectedError := []error{ErrNoMatchFound, nil, ErrNoMatchFound, nil, nil}
for i, s := range toLookUp {
v, err := radix.LookUp(s)
if expectedError[i] != err {
t.Logf("Expected Error: %v; Got: %v", expectedError[i], err)
t.FailNow()
}
if expectedValues[i] != v {
t.Logf("Expected Value: %v; Got: %v", expectedValues[i], v)
t.FailNow()
}
}
}