-
Notifications
You must be signed in to change notification settings - Fork 5
/
metaphone3_words_test.go
127 lines (106 loc) · 2.86 KB
/
metaphone3_words_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
package metaphone3
import (
"encoding/csv"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestBasicWords(t *testing.T) {
vals := []struct{ in, prim, sec string }{
{"A", "A", ""},
{"ack", "AK", ""},
{"eek", "AK", ""},
{"ache", "AK", "AX"},
}
e := &Encoder{}
for _, v := range vals {
prim, sec := e.Encode(v.in)
if prim != v.prim {
t.Errorf("Invalid primary output on '%v', wanted %v, got %v", v.in, v.prim, prim)
}
if sec != v.sec {
t.Errorf("Invalid secondary output on '%v', wanted %v, got %v", v.in, v.sec, sec)
}
}
}
func TestHarness(t *testing.T) {
debug = false
e := &Encoder{
EncodeVowels: true,
}
out, _ := e.Encode("supernode")
if want, got := "SAPARNAT", out; want != got {
t.Fatalf("want: %v, got %v", want, got)
}
}
func TestNameFiles(t *testing.T) {
debug = false
files, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
if filepath.Ext(file.Name()) != ".test" {
continue
}
csvFile, err := os.Open(filepath.Join("testdata", file.Name()))
if err != nil {
t.Fatal(err)
}
reader := csv.NewReader(csvFile)
enc := &Encoder{}
encV := &Encoder{EncodeVowels: true}
encE := &Encoder{EncodeExact: true}
encEV := &Encoder{EncodeVowels: true, EncodeExact: true}
var cnt, encErr, encVErr, encEErr, encEVErr int
for {
// line format of the test files:
// EncodeVowels - v == true, !v == false
// EncodeExact - e == true, !v == false
// originalWord,main !v!e,alt !v!e,main ve,alt ve,main !ve,alt !ve,main v!e,alt v!e
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
in := line[0]
cnt++
encodeSafe(t, "Enc", enc, in, line[1], line[2], &encErr)
encodeSafe(t, "EncEV", encEV, in, line[3], line[4], &encEVErr)
encodeSafe(t, "EncE", encE, in, line[5], line[6], &encEErr)
encodeSafe(t, "EncV", encV, in, line[7], line[8], &encVErr)
//if t.Failed() {
// t.FailNow()
//}
}
// now we're done with reading the file, output stats
csvFile.Close()
// output stats
outputStat(t, "Enc", encErr, cnt)
outputStat(t, "EncEV", encEVErr, cnt)
outputStat(t, "EncE", encEErr, cnt)
outputStat(t, "EncV", encVErr, cnt)
if encErr+encEVErr+encEErr+encVErr > 0 {
t.Errorf("Errors when processing %v", file.Name())
}
}
}
func outputStat(t *testing.T, name string, err, cnt int) {
percent := float32(err) * 100.0 / float32(cnt)
t.Logf("Encoder %v, error percent: %v%%", name, percent)
}
func encodeSafe(t *testing.T, name string, e *Encoder, in, main, alt string, errCt *int) {
out1, out2 := e.Encode(in)
if main != out1 {
t.Errorf("Error Encoding '%v' with %v. Out1 want '%v' got '%v'", in, name, main, out1)
}
if alt != out2 {
t.Errorf("Error Encoding '%v' with %v. Out2 want '%v' got '%v'", in, name, alt, out2)
}
if main != out1 || alt != out2 {
*errCt++
}
}