-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdajare_test.go
More file actions
105 lines (93 loc) · 2.47 KB
/
dajare_test.go
File metadata and controls
105 lines (93 loc) · 2.47 KB
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
package dajareGo
import (
"bufio"
"os"
"testing"
"unicode/utf8"
"github.com/ikawaha/kagome-dict/ipa"
)
func benchmarkIsDajare(b *testing.B, pn bool, testCasePath string, lengthLimit bool) {
// loading testCase
f, err := os.Open(testCasePath)
if err != nil {
panic(err)
}
defer f.Close()
// prepare for output
out, err := os.Create("testdata/failedDajare.txt")
if err != nil {
panic(err)
}
defer out.Close()
// timer reset
b.ResetTimer()
if err := Init(); err != nil {
panic(err)
}
// scan
testCount := 0
passCount := 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
test := scanner.Text()
if lengthLimit && utf8.RuneCountInString(test) >= 18 {
continue
}
testCount += 1
r := IsDajare(test)
isAC := (r.IsDajare == pn)
if isAC {
passCount += 1
} else {
// fmt.Println(r.IsDajare, r)
_, err := out.WriteString(test + "\n")
if err != nil {
panic(err)
}
}
}
b.StopTimer()
if err = scanner.Err(); err != nil {
panic(err)
}
b.ReportMetric((float64(passCount) / float64(testCount)), "AC/cases")
}
func BenchmarkIsDajarePositive(b *testing.B) {
benchmarkIsDajare(b, true, "testdata/positive/dajareList.txt", false)
}
func BenchmarkIsDajareNegative(b *testing.B) {
benchmarkIsDajare(b, false, "testdata/negative/kokoro.txt", false)
}
func BenchmarkIsDajarePositiveLengthLimit(b *testing.B) {
benchmarkIsDajare(b, true, "testdata/positive/dajareList.txt", true)
}
func BenchmarkIsDajareNegativeLengthLimit(b *testing.B) {
benchmarkIsDajare(b, false, "testdata/negative/kokoro.txt", true)
}
func TestIsDajare(t *testing.T) {
if err := Init(); err != nil {
panic(err)
}
result := IsDajare("アルミ缶の上にあるミカン")
if !result.IsDajare {
t.Errorf("failed: アルミ缶の上にあるミカン is Dajare but returned false")
}
result = IsDajare("布団が吹っ飛んだ")
if !result.IsDajare {
t.Errorf("failed: 布団が吹っ飛んだ is Dajare but returned false")
}
result = IsDajare("人民の人民による人民のための政治")
if result.IsDajare {
t.Errorf("failed: 人民の人民による人民のための政治 is NOT Dajare but returned true")
}
result = IsDajare("グァテマラに行ってきた")
if result.IsDajare {
t.Errorf("failed: グァテマラに行ってきた is NOT Dajare but returned true")
}
}
func TestCustomDictionary(t *testing.T) {
dict := ipa.Dict()
if err := SetCustomDictionary(dict); err != nil {
t.Error("failed: an error occurred. the error is", err)
}
}