-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundex_test.go
107 lines (97 loc) · 1.59 KB
/
soundex_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
package phonetic
import (
"errors"
"fmt"
"testing"
)
func TestSoundex(t *testing.T) {
testCases := []struct {
source string
hash string
err error
}{
{
source: "WILLIAMS",
hash: "W452",
err: nil,
},
{
source: "BARAGWANATH",
hash: "B625",
err: nil,
},
{
source: "DONNELL",
hash: "D540",
err: nil,
},
{
source: "LLOYD",
hash: "L300",
err: nil,
},
{
source: "WOOLCOCK",
hash: "W422",
err: nil,
},
{
source: "ROBERT",
hash: "R163",
err: nil,
},
{
source: "RUPERT",
hash: "R163",
err: nil,
},
{
source: "MICHAEL",
hash: "M240",
err: nil,
},
{
source: "test",
hash: "T230",
err: nil,
},
{
source: "TeST",
hash: "T230",
err: nil,
},
{
source: "brown",
hash: "B650",
err: nil,
},
}
for i, testCase := range testCases {
testCase := testCase
t.Run(fmt.Sprintf("testCase #%d", i+1), func(t *testing.T) {
encoder := NewSoundexEncoder()
hash, err := encoder.Encode(testCase.source)
if !errors.Is(err, testCase.err) {
t.Errorf("expected error %v, got %v", testCase.err, err)
}
if hash != testCase.hash {
t.Errorf("expected hash %v, got %v", testCase.hash, hash)
}
})
}
}
func BenchmarkSoundex(b *testing.B) {
words := []string{
"inpdendence",
"approxmiation",
"testing",
"accuracy",
}
encoder := NewSoundexEncoder()
for i := 0; i < b.N; i++ {
source := words[i%len(words)]
if _, err := encoder.Encode(source); err != nil {
b.Fatalf("unexpected error: %v", err)
}
}
}