-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
147 lines (126 loc) · 3.22 KB
/
generator_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
package headlines
import (
"bytes"
"io/ioutil"
"log"
"os"
"reflect"
"testing"
)
func Test_Index_Add(t *testing.T) {
i := index{}
examples := []struct {
token string
expected []string
}{
{token: "foo", expected: []string{"foo"}},
{token: "foo", expected: []string{"foo"}},
{token: "hello", expected: []string{"foo", "hello"}},
{token: "abc", expected: []string{"abc", "foo", "hello"}},
{token: "boo", expected: []string{"abc", "boo", "foo", "hello"}},
{token: "boo", expected: []string{"abc", "boo", "foo", "hello"}},
}
for _, ex := range examples {
i.Add(ex.token)
if !reflect.DeepEqual(i.tokens, ex.expected) {
t.Fatalf("expected %v, got %v\n", ex.expected, i.tokens)
}
}
}
func Test_Index_Get(t *testing.T) {
i := index{
tokens: []string{"abc", "boo", "foo", "hello"},
}
examples := []struct {
i int
expected string
}{
{i: 0, expected: "abc"},
{i: 0, expected: "abc"},
{i: 2, expected: "foo"},
}
for _, ex := range examples {
if v := i.Get(ex.i); v != ex.expected {
t.Fatalf("expected %v, got %v\n", ex.expected, v)
}
}
}
func Test_Index_Find(t *testing.T) {
i := index{
tokens: []string{"abc", "boo", "foo", "hello"},
}
examples := []struct {
token string
expected int32
}{
{token: "foo", expected: 2},
{token: "hello", expected: 3},
{token: "HELLO", expected: -1},
{token: "fo", expected: -1},
{token: "abc", expected: 0},
}
for _, ex := range examples {
if pos := i.Find(ex.token); pos != ex.expected {
t.Fatalf("expected %v, got %v\n", ex.expected, pos)
}
}
}
func Test_BuildChain(t *testing.T) {
c := NewChain(2)
data := `the quick brown fox, jumps over the lazy dog.
foo bar brown fox, hello.
foo bar zoo.`
if err := c.Build(bytes.NewBufferString(data)); err != nil {
t.Fatal(err)
}
expectedTokens := []string{
"bar", "brown", "dog.",
"foo", "fox,", "hello.",
"jumps", "lazy", "over",
"quick", "the", "zoo.",
}
if !reflect.DeepEqual(expectedTokens, c.tokensIdx.tokens) {
t.Fatalf("expected %v, got %v\n", expectedTokens, c.tokensIdx.tokens)
}
expectedPrefixes := []string{"foo bar", "the quick"}
if !reflect.DeepEqual(expectedPrefixes, c.startingPrefixIdx.tokens) {
t.Fatalf("expected %v, got %v\n", expectedPrefixes, c.startingPrefixIdx.tokens)
}
expectedsp := []int32{1, 0, 0}
if !reflect.DeepEqual(expectedsp, c.sp) {
t.Fatalf("expected %v, got %v\n", expectedsp, c.sp)
}
expectedC := map[string][]int32{
"the quick": []int32{1}, "quick brown": []int32{4},
"brown fox,": []int32{6, 5}, "fox, jumps": []int32{8},
"jumps over": []int32{10}, "over the": []int32{7},
"the lazy": []int32{2}, "foo bar": []int32{1, 11},
"bar brown": []int32{4},
}
if !reflect.DeepEqual(expectedC, c.chain) {
t.Fatalf("expected %v, got %v\n", expectedC, c.chain)
}
}
func benchmark_Build(prefix int, b *testing.B) {
log.SetOutput(ioutil.Discard)
for i := 0; i < b.N; i++ {
b.StopTimer()
c := NewChain(prefix)
// open corpus
f, err := os.Open("bench.txt")
if err != nil {
panic(err)
}
b.StartTimer()
if err := c.Build(f); err != nil {
b.Fatal(err)
}
f.Close()
}
}
func Benchmark_Build_Prefix_2(b *testing.B) {
benchmark_Build(2, b)
}
func Benchmark_Build_Prefix_3(b *testing.B) {
benchmark_Build(3, b)
}