-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxim_test.go
148 lines (123 loc) · 4.16 KB
/
xim_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
package xim
import (
"testing"
)
func TestValidateConfig(t *testing.T) {
labels := make([]string, MaxCompositeIndexLabels+1)
for i := 0; i < len(labels); i++ {
labels[i] = string(rune('a' + i))
}
t.Run("len(CompositeIdxLabels)<=MaxCompositeIndexLabels", func(tr *testing.T) {
conf := &Config{CompositeIdxLabels: labels[:MaxCompositeIndexLabels]}
if _, err := ValidateConfig(conf); err != nil {
tr.Errorf("expected: error = nil, but was: [%v]\n", err)
}
if validated, _ := ValidateConfig(conf); validated != conf {
tr.Errorf("validated, _ := ValidateConfig(conf) expected: validated = conf, but was: validated = %#v\n", validated)
}
})
t.Run("len(CompositeIdxLabels)>MaxCompositeIndexLabels", func(tr *testing.T) {
conf := &Config{CompositeIdxLabels: labels[:MaxCompositeIndexLabels+1]}
if _, err := ValidateConfig(conf); err == nil {
tr.Error("CompositeIdxLabels > MaxCompositeIndexLabels expected: err != nil, but was: err = nil\n")
}
})
t.Run("ValidateConfig(DefaultConfig)", func(tr *testing.T) {
if _, err := ValidateConfig(DefaultConfig); err != nil {
tr.Errorf("expected: error = nil, but was: error = [%v]\n", err)
}
})
}
func TestMustValidateConfig(t *testing.T) {
labels := make([]string, MaxCompositeIndexLabels+1)
for i := 0; i < len(labels); i++ {
labels[i] = string(rune('a' + i))
}
t.Run("CompositeIdxLabels<=MaxCompositeIndexLabels", func(tr *testing.T) {
defer func() {
if rec := recover(); rec != nil {
tr.Errorf("expected: not panic, was: panic = [%v]\n", rec)
}
}()
conf := &Config{CompositeIdxLabels: labels[:MaxCompositeIndexLabels]}
MustValidateConfig(conf)
})
t.Run("CompositeIdxLabels>MaxCompositeIndexLabels", func(tr *testing.T) {
defer func() {
if r := recover(); r == nil {
tr.Error("expected: panic, was: not panic\n")
}
}()
conf := &Config{CompositeIdxLabels: labels[:MaxCompositeIndexLabels+1]}
MustValidateConfig(conf)
})
}
func TestAddIndexAndFilter(t *testing.T) {
idx := NewIndexes(nil)
idx.Add("label1", "abc dあいbCh", "sample")
filter := NewFilters(nil)
filter.Add("label1", "abc dあいbCh", "sample")
builtIndexes := idx.MustBuild()
builtFilters := filter.MustBuild()
// All the contents of filter are present in index
for builtFilter := range builtFilters {
if !contains(t, builtIndexes, builtFilter) {
t.Errorf("filter: %s not contains", builtFilter)
}
}
}
func TestAddBigramsIndexAndFilter(t *testing.T) {
idx := NewIndexes(nil)
idx.AddBigrams("label1", "abc dあいbCh")
builtIndexes := idx.MustBuild()
filter := NewFilters(nil)
filter.AddBigrams("label1", "dあいb") // mid match of idx
builtFilters := filter.MustBuild()
// All the contents of filter are present in index
for builtFilter := range builtFilters {
if !contains(t, builtIndexes, builtFilter) {
t.Errorf("filter: %s not contains", builtFilter)
}
}
}
func TestAddBiunigramsIndexAndFilter(t *testing.T) {
idx := NewIndexes(nil)
idx.AddBiunigrams("label1", "abc dあいbCh")
builtIndexes := idx.MustBuild()
filter := NewFilters(nil)
filter.AddBiunigrams("label1", "dあいb") // mid match of idx
builtFilters := filter.MustBuild()
// All the contents of filter are present in index
for builtFilter := range builtFilters {
if !contains(t, builtIndexes, builtFilter) {
t.Errorf("filter: %s not contains", builtFilter)
}
}
}
func TestInFilterIndexAndFilter(t *testing.T) {
inBuilder := NewInBuilder()
status1 := inBuilder.NewBit()
status2 := inBuilder.NewBit()
status3 := inBuilder.NewBit()
idx := NewIndexes(nil)
idx.Add("label1", inBuilder.Indexes(status1)...)
idx.Add("label2", inBuilder.Indexes(status1, status2, status3)...)
builtIndexes := idx.MustBuild()
filter := NewFilters(nil)
filter.Add("label1", inBuilder.Filter(status1, status2, status3))
filter.Add("label2", inBuilder.Filter(status1))
builtFilters := filter.MustBuild()
// All the contents of filter are present in index
for builtFilter := range builtFilters {
if !contains(t, builtIndexes, builtFilter) {
t.Errorf("filter: %s not contains", builtFilter)
}
}
}
func contains(t *testing.T, m map[string]bool, target string) bool {
t.Helper()
if _, ok := m[target]; ok {
return true
}
return false
}