-
Notifications
You must be signed in to change notification settings - Fork 0
/
memws_test.go
114 lines (103 loc) · 1.47 KB
/
memws_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
package memws
import "testing"
type SeekCase struct {
NPos int64
Whence int
EPos int64
Success bool
}
func TestWrite(t *testing.T) {
in := &WriteSeek{}
out := []string{
"漢字",
"漢字foo",
"漢字fbar",
}
in.Write([]byte(out[0]))
if out[0] != in.String() {
t.Errorf("%s should be %s", in, out[0])
}
in.Write([]byte("foo"))
if out[1] != in.String() {
t.Errorf("%s should be %s", in, out[1])
}
in.pos = 7
in.Write([]byte("bar"))
if out[2] != in.String() {
t.Errorf("%s should be %s", in, out[2])
}
}
func TestSeek(t *testing.T) {
testCases := []SeekCase{
SeekCase{
-10,
0,
0,
false,
},
SeekCase{
10,
0,
10,
true,
},
SeekCase{
5,
1,
15,
true,
},
SeekCase{
-4,
1,
11,
true,
},
SeekCase{
10,
2,
21,
true,
},
SeekCase{
0,
-1,
0,
false,
},
SeekCase{
0,
100,
0,
false,
},
}
w := &WriteSeek{}
for i, tc := range testCases {
_, err := w.Seek(tc.NPos, tc.Whence)
if err != nil {
if tc.Success {
t.Errorf("tc n° %d shouldn't have failed", i)
break
}
return
}
if !tc.Success {
t.Errorf("tc n° %d should have failed", i)
break
}
if w.pos != tc.EPos {
t.Errorf("tc n° %d position is %d but should be %d", i, w.pos, tc.EPos)
}
}
}
func TestString(t *testing.T) {
in := &WriteSeek{
[]byte("漢字"),
0,
}
out := "漢字"
if out != in.String() {
t.Fatalf("string mismatch %s should be %s", in, out)
}
}