-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordladder_test.go
118 lines (94 loc) · 2.6 KB
/
wordladder_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 wordladder
import (
"bufio"
"os"
"strings"
"testing"
"github.com/gastonsimone/go-dojo/strarray"
)
func TestWordLadderBasic(t *testing.T) {
const input = "a b c"
scanner := bufio.NewScanner(strings.NewReader(input))
scanner.Split(bufio.ScanWords)
var dict WordSet
var err error
if dict, err = LoadWordDict(scanner); err != nil {
t.Fatalf("error loading dictionary: %v", err)
}
ladder := WordLadder("a", "c", dict)
want := []string{"a", "c"}
if !strarray.AreEqual(ladder, want) {
t.Fatalf("got %v, want %v", ladder, want)
}
}
func TestWordLadderNoDict(t *testing.T) {
ladder := WordLadder("hit", "cog", nil)
var want []string
if !strarray.AreEqual(ladder, want) {
t.Fatalf("got %v, want %v", ladder, want)
}
}
func TestWordLadderDifferentLength(t *testing.T) {
ladder := WordLadder("small", "larger", nil)
var want []string
if !strarray.AreEqual(ladder, want) {
t.Fatalf("got %v, want %v", ladder, want)
}
}
func TestWordLadderExactDict(t *testing.T) {
const input = "hot dot dog"
scanner := bufio.NewScanner(strings.NewReader(input))
scanner.Split(bufio.ScanWords)
var dict WordSet
var err error
if dict, err = LoadWordDict(scanner); err != nil {
t.Fatalf("error loading dictionary: %v", err)
}
ladder := WordLadder("hit", "cog", dict)
want := []string{"hit", "hot", "dot", "dog", "cog"}
if !strarray.AreEqual(ladder, want) {
t.Fatalf("got %v, want %v", ladder, want)
}
}
func TestWordLadderLargeConversion(t *testing.T) {
file, err := os.Open("/usr/share/dict/words")
if err != nil {
t.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var dict WordSet
if dict, err = LoadWordDict(scanner); err != nil {
t.Fatalf("error loading dictionary: %v", err)
}
ladder := WordLadder("small", "large", dict)
want := []string{"small", "shall", "shale", "shave", "seave", "serve", "verve", "varve", "larve", "large"}
if !strarray.AreEqual(ladder, want) {
t.Fatalf("got %v, want %v", ladder, want)
}
}
func BenchmarkLoadWordDict(b *testing.B) {
const input = "one two three four five six seven eight nine ten eleven"
b.ResetTimer()
for i := 0; i < b.N; i++ {
scanner := bufio.NewScanner(strings.NewReader(input))
scanner.Split(bufio.ScanWords)
LoadWordDict(scanner)
}
}
func BenchmarkWordLadder(b *testing.B) {
file, err := os.Open("/usr/share/dict/words")
if err != nil {
b.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var dict WordSet
if dict, err = LoadWordDict(scanner); err != nil {
b.Fatalf("error loading dictionary: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
WordLadder("small", "large", dict)
}
}