forked from go-gcfg/gcfg
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathidx_test.go
77 lines (71 loc) · 1.61 KB
/
idx_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
package gcfg
import (
"testing"
)
var idxerIdxCaseTests = [][]string{
[]string{"a", "A"},
[]string{"Hello", "HELLO", "hello"},
}
func TestIdxerIdxCase(t *testing.T) {
var idxer Idxer
for _, ns := range idxerIdxCaseTests {
idxer.add(ns[0])
for _, ns := range idxerIdxCaseTests {
idx := idxer.Idx(ns[0])
for _, n := range ns[1:] {
if idx2 := idxer.Idx(n); idx != idx2 {
t.Errorf("Idxer.Idx(%q)==%q; "+
"want same as Idxer.Idx(%q)==%q", n, idx, ns[0], idx2)
}
}
}
}
}
func TestIdxerIdxCaseWithCased(t *testing.T) {
tsts := []string{"Hello", "HELLO", "hello"}
var idxer CasedIdxer
for _, ns := range tsts {
if (idxer.Idx(ns) != Idx{}) {
t.Fatalf("name %q already exists", ns)
}
idxer.add(ns)
if (idxer.Idx(ns) == Idx{}) {
t.Fatalf("name %q missing after add", ns)
}
}
}
var idxerNamesTests = [][]string{
[]string{},
[]string{"a"},
[]string{"a", "b"},
}
func TestIdxerNames(t *testing.T) {
for _, ns := range idxerNamesTests {
var idxer Idxer
vals := make(map[Idx]int)
for i, n := range ns {
idxer.add(n)
vals[idxer.Idx(n)] = i
}
ins := idxer.Names()
if len(ns) != len(ins) {
t.Errorf("len(Idxer.Names())=%d; want len(ns)=%d; ns=%v, ins=%v",
len(ins), len(ns), ns, ins)
}
seen := make(map[int]struct{})
for _, n := range ins {
i := idxer.Idx(n)
v := vals[i]
if _, exists := seen[v]; exists {
t.Errorf("Idxer.Names()=%v contains duplicate; seen=%v",
ins, seen)
} else {
seen[v] = struct{}{}
}
}
if len(seen) != len(ns) {
t.Errorf("len(seen)=%d; want len(Idxer.Names())=%d; ns=%v",
len(seen), len(ns), ns)
}
}
}