-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc_test.go
100 lines (68 loc) · 1.6 KB
/
wc_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
package main
import (
"testing"
"fmt"
)
func TestWordCounter(t *testing.T) {
c := &WordCounter{0,make([]byte,0)}
c.Write([]byte("hello, world"))
if c.n != 1 {
t.Errorf("'hello,' should have been counted")
}
c.Write([]byte(" "))
if c.n != 2 {
t.Errorf("'world' should have been counted")
}
c.Write([]byte("o"))
if c.n != 2 {
t.Errorf("not yet a word")
}
c.Write([]byte("f"))
if c.n != 2 {
t.Errorf("not yet a word (bis)")
}
// NOTE: will call ScanWords twice: a first time because
// we consume "of", and a second time to read that second
// space (and generate no words).
c.Write([]byte(" "))
if c.n != 3 {
t.Errorf("'of' should have been read")
}
}
func TestLineCounter(t *testing.T) {
c := &LineCounter{0,make([]byte,0)}
c.Write([]byte("hello, world"))
if c.n != 0 {
t.Errorf("Not yet a line")
}
c.Write([]byte(" "))
if c.n != 0 {
t.Errorf("Not yet a line")
}
c.Write([]byte("o"))
if c.n != 0 {
t.Errorf("Not yet a line")
}
c.Write([]byte("f"))
if c.n != 0 {
t.Errorf("Not yet a line")
}
c.Write([]byte(" \n"))
if c.n != 1 {
t.Errorf("One first line")
}
}
func TestCountingWriter(t *testing.T) {
c, n := CountingWriter(&WordCounter{0,make([]byte,0)})
// Haven't tested this before here actually (used as an io.Writer)
fmt.Fprintf(c, "hello, world")
if *n != int64(len("hello, ")) {
t.Errorf("We should have counted %d bytes (`hello, `); have %d",
len("hello "), *n)
}
c.Write([]byte(" "))
if *n != int64(len("hello, world ")) {
t.Errorf("We should have counted %d bytes (`hello, `); have %d",
len("hello, world "), *n)
}
}