-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwc.go
49 lines (45 loc) · 1.22 KB
/
wc.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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wc // import "github.com/golang-id/tour/wc"
import "fmt"
// Test runs a test suite against f.
func Test(f func(string) map[string]int) {
ok := true
for _, c := range testCases {
got := f(c.in)
if len(c.want) != len(got) {
ok = false
} else {
for k := range c.want {
if c.want[k] != got[k] {
ok = false
}
}
}
if !ok {
fmt.Printf("FAIL\n f(%q) =\n %#v\n want:\n %#v",
c.in, got, c.want)
break
}
fmt.Printf("PASS\n f(%q) = \n %#v\n", c.in, got)
}
}
var testCases = []struct {
in string
want map[string]int
}{
{"I am learning Go!", map[string]int{
"I": 1, "am": 1, "learning": 1, "Go!": 1,
}},
{"The quick brown fox jumped over the lazy dog.", map[string]int{
"The": 1, "quick": 1, "brown": 1, "fox": 1, "jumped": 1,
"over": 1, "the": 1, "lazy": 1, "dog.": 1,
}},
{"I ate a donut. Then I ate another donut.", map[string]int{
"I": 2, "ate": 2, "a": 1, "donut.": 2, "Then": 1, "another": 1,
}},
{"A man a plan a canal panama.", map[string]int{
"A": 1, "man": 1, "a": 2, "plan": 1, "canal": 1, "panama.": 1,
}},
}