-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcompl_test.go
118 lines (99 loc) · 2.33 KB
/
fcompl_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
package fcompl
import (
"bufio"
"log"
"os"
"strings"
"testing"
"time"
pinyin "github.com/mozillazg/go-pinyin"
"github.com/stretchr/testify/assert"
)
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func Test_completion_fromfile(t *testing.T) {
f, err := os.OpenFile("phrases.txt", os.O_RDONLY, os.ModePerm)
check(err)
defer f.Close()
rd := bufio.NewReader(f)
root := New(rd, true)
table := []struct {
input string
expect []int
}{
{"the batman", []int{0, 7}},
{"any batman", []int{0, 7}},
{"american", []int{2, 3}},
{"wonder", []int{4, 5}},
{"A robin", []int{6}},
{"wonder anything", []int{4, 5}},
{"我", []int{8, 9}},
{"我是", []int{8, 9}},
}
root.print()
t1 := time.Now()
for _, ca := range table {
if detectLang(ca.input) == zh {
pin := pinyin.LazyConvert(ca.input, &pinyinCfg)
contentsEqual(root.Find(strings.Join(pin, " ")), ca.expect)
continue
}
contentsEqual(root.Find(ca.input), ca.expect)
}
log.Println(time.Since(t1))
}
func Test_completion_fromString(t *testing.T) {
s := "A ball in the ground\nThe bat in the sky\nThe ball hit his head\n"
rd := bufio.NewReader(strings.NewReader(s))
f := New(rd, true)
contentsEqual(f.Find("Ball"), []int{0, 2})
contentsEqual(f.Find("bat"), []int{1})
SetLimit(1)
contentsEqual(f.Find("Ball"), []int{0})
SetLimit(6)
contentsEqual(f.Find("Ball"), []int{0, 2})
}
func Test_compress(t *testing.T) {
f, err := os.OpenFile("phrases.txt", os.O_RDONLY, os.ModePerm)
check(err)
defer f.Close()
rd := bufio.NewReader(f)
root := New(rd, true)
log.Println("Before compress:")
root.print()
log.Println("After compress:")
Compress(root)
root.print()
}
func Test_serialize(t *testing.T) {
f, err := os.OpenFile("phrases.txt", os.O_RDONLY, os.ModePerm)
check(err)
defer f.Close()
rd := bufio.NewReader(f)
root := New(rd, true)
Compress(root)
err = root.Save("./fcompl.bin")
defer os.Remove("./fcompl.bin")
check(err)
loadedRoot, err := Load("./fcompl.bin")
check(err)
assert.Equal(t, *root, *loadedRoot)
}
func contentsEqual(input []int, expect []int) {
if len(input) != len(expect) {
log.Fatalf("expect %v, but got %v", expect, input)
}
for i, v := range input {
if v != expect[i] {
log.Fatalf("expect %v, but got %v", expect, input)
}
}
}
func Test_setLimit(t *testing.T) {
l := limit
SetLimit(-3)
assert.Equal(t, l, limit)
}