-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbites_test.go
184 lines (169 loc) · 4.88 KB
/
bites_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package bites
import (
"testing"
)
func TestCapacity(t *testing.T) {
b := Put(nil).Capacity(16)
if len(b) != 0 {
t.Fatalf("FAIL! Expected len 0, got %d", len(b))
}
if cap(b) != 16 {
t.Fatalf("FAIL! Expected cap 16, got cap %d", cap(b))
}
b = b.Extend(16)
if cap(b) != 16 || len(b) != 16 {
t.Fatalf("FAIL! Invalid len(%d) or cap(%d), expected 16 for both", len(b), cap(b))
}
b = b.Reuse()
if cap(b) != 16 {
t.Fatalf("FAIL! Reuse should not change cap")
}
if len(b) != 0 {
t.Fatalf("FAIL! Reuse should set len to 0")
}
b = b.Capacity(10)
if cap(b) != 16 {
t.Fatalf("FAIL! Short capacity should not change cap")
}
}
func TestExtendShort(t *testing.T) {
b := make(Put, 50).Set(1)
for i := range b {
if b[i] != 1 {
t.Fatalf("FAIL! Set did not set everything")
}
}
origcap := cap(b)
b = b.Reuse().Extend(20)
if cap(b) != origcap {
t.Fatalf("FAIL! Short extend changed cap")
}
if len(b) != 20 {
t.Fatalf("FAIL! Short extend did not set len properly, got %d expect %d", len(b), 20)
}
}
func TestExtendMid(t *testing.T) {
b := make(Put, 50).Set(1)
origcap := cap(b)
b = b.Reuse().Extend(500)
if cap(b) == origcap {
t.Fatalf("FAIL! Mid extend did not change cap")
}
if len(b) != 500 {
t.Fatalf("FAIL! Mid extend did not set len properly, got %d expect %d", len(b), 500)
}
}
func TestExtendLong(t *testing.T) {
b := make(Put, 50).Set(1)
origcap := cap(b)
b = b.Reuse().Extend(5000)
if cap(b) == origcap {
t.Fatalf("FAIL! Long extend did not change cap")
}
if len(b) != 5000 {
t.Fatalf("FAIL! Long extend did not set len properly, got %d expect %d", len(b), 5000)
}
}
func TestSlicing(t *testing.T) {
b := Get("0123456789")
if b.Last(3).String() != "789" {
t.Fatalf("FAIL! Last 3 bytes should be 789, were %s", b.Last(3))
}
if b.Snip(4).String() != "012345" {
t.Fatalf("FAIL! Snipping the last 4 bytes should result in 012345, but became %s", b.Snip(4))
}
if b.Skip(6).String() != "6789" {
t.Fatalf("FAIL! Skipping the first 6 bytes should result in 6789, but became %s", b.Skip(6))
}
}
func TestSimpleString(t *testing.T) {
b := Put(nil).PutString("Hello").PutByte(',').PutRune(' ', nil).PutSlice([]byte("world!"))
if b.String() != "Hello, world!" {
t.Fatalf("FAIL! Expected 'Hello, world!', got '%s'", b.String())
}
}
func TestRune(t *testing.T) {
var os1 int
b := Put(nil).PutRune('世', &os1).PutString("界!")
if os1 != 3 {
t.Fatalf("First rune put mismatch: expected %d, got %d", 3, os1)
}
var r1, r2, r3 rune
var s1, s3 int
b.Get().GetRune(&r1, &s1).GetRune(&r2, nil).GetRune(&r3, &s3)
if r1 != '世' {
t.Fatalf("First rune mismatch: expected %d, got %d", '世', r1)
}
if r2 != '界' {
t.Fatalf("Second rune mismatch: expected %d, got %d", '界', r2)
}
if r3 != '!' {
t.Fatalf("Third rune mismatch: expected %d, got %d", '!', r3)
}
if s1 != 3 {
t.Fatalf("Invalid rune size: %d expected %d", s1, 3)
}
if s3 != 1 {
t.Fatalf("Invalid rune size: %d expected %d", s3, 1)
}
}
func TestCloneZero(t *testing.T) {
b1 := Put(nil).PutString("Hello")
b2 := b1.Clone()
b1 = b1.Reuse().PutString("moo")
if b2.String() != "Hello" {
t.Fatalf("FAIL! Write after clone should not overwrite")
}
b2.Zero()
if b1.String() != "moo" {
t.Fatalf("FAIL! Write after clone should not overwrite (%s)", b1.String())
}
for i := range b2 {
if b2[i] != 0 {
t.Fatalf("FAIL! Zero did not zero slice")
}
}
}
func TestSplit(t *testing.T) {
b1, b2 := Put(nil).PutString("123987654").Get().Split(3)
if b1.String() != "123" {
t.Fatalf("FAIL! First part of split is wrong, expected '123' got %d", b1.String())
}
if b2.String() != "987654" {
t.Fatalf("FAIL! Second part of split is wrong, expected '987654' got %d", b2.String())
}
}
func TestBool(t *testing.T) {
b := Put(nil).PutBool(true, false, false, true, true, false, false, true, true, false, true)
if len(b) != 2 {
t.Fatalf("FAIL! Expected size 2, got %d", len(b))
}
if b[0] != 153 || b[1] != 160 {
t.Fatalf("FAIL! Expected 153 and 160, got %d and %d", b[0], b[1])
}
var b1, b2, b3, b6, b7, b8, b9, b10, b11 bool
after := b.Get().GetBool(&b1, &b2, &b3, nil, nil, &b6, &b7, &b8, &b9, &b10, &b11)
if len(after) != 0 {
t.Fatalf("FAIL! GetBool didn't snip")
}
if b1 != true || b2 != false || b3 != false || b6 != false || b7 != false || b8 != true || b9 != true || b10 != false || b11 != true {
t.Fatalf("FAIL! Bad booleans returned")
}
}
func TestBoolBoundary(t *testing.T) {
b := Put(nil).PutBool(true, false, false, true, true, false, false, true)
if len(b) != 1 {
t.Fatalf("FAIL! Expected size 1, got %d", len(b))
}
if b[0] != 153 {
t.Fatalf("FAIL! Expected 153, got %d and %d", b[0])
}
var b1, b2, b3, b6, b7, b8 bool
after := b.Get().GetBool(&b1, &b2, &b3, nil, nil, &b6, &b7, &b8)
if len(after) != 0 {
t.Fatalf("FAIL! GetBool didn't snip")
}
if b1 != true || b2 != false || b3 != false || b6 != false || b7 != false || b8 != true {
t.Fatalf("FAIL! Bad booleans returned")
}
}