-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary_test.go
146 lines (140 loc) · 3 KB
/
dictionary_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
package fakeword
import (
"reflect"
"testing"
)
func TestDictionary_Add(t *testing.T) {
tests := []struct {
name string
prefixLength int
words []string
want *Dictionary
}{
{
"simple",
4,
[]string{"hi"},
&Dictionary{
counter: map[string]map[string]int{
"^": {"h": 1},
"^h": {"i": 1},
"^hi": {"$": 1},
"h": {"i": 1},
"hi": {"$": 1},
"i": {"$": 1},
},
PrefixLength: 4,
},
},
{
"short prefix",
2,
[]string{"hi"},
&Dictionary{
counter: map[string]map[string]int{
"^": {"h": 1},
"h": {"i": 1},
"i": {"$": 1},
},
PrefixLength: 2,
},
},
{
"empty",
defaultPrefixLength,
[]string{},
&Dictionary{
PrefixLength: defaultPrefixLength,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &Dictionary{PrefixLength: tt.prefixLength}
if got := w.Add(tt.words...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Dictionary.Add() = %v, want %v", got, tt.want)
}
})
}
}
func TestDictionary_Generator(t *testing.T) {
tests := []struct {
name string
counter map[string]map[string]int
want map[string]map[string]float32
}{
{
"simple",
map[string]map[string]int{"a": {"b": 2, "c": 2}},
map[string]map[string]float32{"a": {"b": 0.5, "c": 0.5}},
},
{
"simple",
map[string]map[string]int{"a": {"b": 2, "c": 2}, "b": {"c": 2}},
map[string]map[string]float32{"a": {"b": 0.5, "c": 0.5}, "b": {"c": 1}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &Dictionary{
counter: tt.counter,
}
got := w.Generator()
if !reflect.DeepEqual(got, Generator{Probabilities: tt.want}) {
t.Errorf("Words.Generator() = %v, want %v", got, tt.want)
}
})
}
}
func TestDictionary_count(t *testing.T) {
tests := []struct {
name string
substrs []string
want map[string]map[string]int
}{
{
"simple",
[]string{"ab", "ab"},
map[string]map[string]int{"a": {"b": 2}},
},
{
"simple",
[]string{"ab", "ab", "ac", "bc"},
map[string]map[string]int{"a": {"b": 2, "c": 1}, "b": {"c": 1}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &Dictionary{}
for _, substr := range tt.substrs {
w.count(substr)
}
if got := w.counter; !reflect.DeepEqual(got, tt.want) {
t.Errorf("Words.count() = %v, want %v", got, tt.want)
}
})
}
}
func Test_splitToLength(t *testing.T) {
type args struct {
s string
length int
}
tests := []struct {
name string
args args
want []string
}{
{"basic", args{"test", 2}, []string{"te", "es", "st"}},
{"empty", args{"", 2}, []string{}},
{"single", args{"test", 1}, []string{"t", "e", "s", "t"}},
{"equal length", args{"test", 4}, []string{"test"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := splitToLength(tt.args.s, tt.args.length); !reflect.DeepEqual(got, tt.want) {
t.Errorf("splitToLength() = %v, want %v", got, tt.want)
}
})
}
}