-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
75 lines (70 loc) · 1.74 KB
/
testing.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
package approxity
import (
"bufio"
"io/fs"
"os"
"path/filepath"
)
type TestingDataset[T []byte] struct {
Name string
Positives []T
Negatives []T
All []T
}
var datasets []TestingDataset[[]byte]
func init() {
fread := func(dst [][]byte, path string) ([][]byte, error) {
fh, err := os.Open(path)
if err != nil {
return dst, err
}
defer func() { _ = fh.Close() }()
scr := bufio.NewScanner(fh)
for scr.Scan() {
if b := scr.Bytes(); len(b) > 0 {
dst = append(dst, append([]byte(nil), b...))
}
}
return dst, scr.Err()
}
probes := []string{
"testdata",
"../testdata",
"../../testdata",
}
for _, path := range probes {
_ = filepath.Walk(path, func(cpath string, info fs.FileInfo, err error) error {
if info == nil || !info.IsDir() || cpath == path {
return nil
}
var ds TestingDataset[[]byte]
if ds.Positives, err = fread(ds.Positives, cpath+"/positive.txt"); err != nil {
return err
}
if ds.Negatives, err = fread(ds.Negatives, cpath+"/negative.txt"); err != nil {
return err
}
ds.Name, ds.All = info.Name(), append(ds.Positives, ds.Negatives...)
datasets = append(datasets, ds)
return nil
})
}
// Try to compose TestingDataset based on system's dictionaries.
sysDS := TestingDataset[[]byte]{Name: "system/dict"}
if words, err := fread(nil, "/usr/share/dict/words"); err == nil && len(words) > 0 {
sysDS.All = words
for i := 0; i < len(words); i++ {
if i%2 == 0 {
sysDS.Positives = append(sysDS.Positives, words[i])
} else {
sysDS.Negatives = append(sysDS.Negatives, words[i])
}
}
datasets = append(datasets, sysDS)
}
}
func EachTestingDataset(f func(i int, ds *TestingDataset[[]byte])) {
for i := 0; i < len(datasets); i++ {
f(i, &datasets[i])
}
}