-
Notifications
You must be signed in to change notification settings - Fork 2
/
bytes_test.go
292 lines (265 loc) · 8.14 KB
/
bytes_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -----------------------------------------------------------------------------
// ZR Library zr/[bytes_test.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package zr
// # Special Constructors
// Test_byts_BytesAlloc_
// Test_byts_BytesAllocUTF8_
// Test_byts_BytesWrap_
//
// # Read-Only Properties / Methods
// Test_byts_Bytes_Cap_
// Test_byts_Bytes_FindByte_
// Test_byts_Bytes_GetByte_
// Test_byts_Bytes_GetChar_
// Test_byts_Bytes_Size_
//
// # fmt.Stringer Interface
// Test_byts_Bytes_String_
//
// # Methods
// Test_byts_Bytes_Append_
// Test_byts_Bytes_AppendChar_
// Test_byts_Bytes_GetBytes_
// Test_byts_Bytes_Insert_
// Test_byts_Bytes_Remove_
// Test_byts_Bytes_Reset_
// Test_byts_Bytes_Resize_
// Test_byts_Bytes_SetByte_
// Test_byts_Bytes_Slice_
// to test all items in bytes.go use:
// go test --run Test_byts_
//
// to generate a test coverage report for the whole module use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
import (
"testing"
// "bytes"
// "compress/zlib"
// "io"
// "unicode/utf8"
)
// -----------------------------------------------------------------------------
// # Special Constructors
// go test --run Test_byts_BytesAlloc_
func Test_byts_BytesAlloc_(t *testing.T) {
TBegin(t)
//
// BytesAlloc(size int) Bytes
//
o := BytesAlloc(4096) // 4KiB
TEqual(t, cap(o.ar), 4096)
} // Test_byts_BytesAlloc_
// go test --run Test_byts_BytesAllocUTF8_
func Test_byts_BytesAllocUTF8_(t *testing.T) {
TBegin(t)
//
// BytesAllocUTF8(s string) Bytes
//
o := BytesAllocUTF8("Привет !")
TEqual(t, len(o.ar), 14)
} // Test_byts_BytesAllocUTF8_
// go test --run Test_byts_BytesWrap_
func Test_byts_BytesWrap_(t *testing.T) {
TBegin(t)
//
// BytesWrap(ar []byte) Bytes
//
ar := []byte{'H', 'e', 'l', 'l', 'o', '!'}
o := BytesWrap(ar)
TEqual(t, len(o.ar), 6)
//
// TODO: Test how 'ar' is affected when object is changed
} // Test_byts_BytesWrap_
// -----------------------------------------------------------------------------
// # Read-Only Properties / Methods
// go test --run Test_byts_Bytes_Cap_
func Test_byts_Bytes_Cap_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Cap() int
//
const CAP = 1234
o := BytesAlloc(CAP)
TEqual(t, cap(o.ar), CAP)
TEqual(t, o.Cap(), CAP)
} // Test_byts_Bytes_Cap_
// go test --run Test_byts_Bytes_FindByte_
func Test_byts_Bytes_FindByte_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) FindByte(b byte) int
//
// TODO: TEST return bytes.IndexByte(ob.ar, b)
} // Test_byts_Bytes_FindByte_
// go test --run Test_byts_Bytes_GetByte_
func Test_byts_Bytes_GetByte_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) GetByte(index int) byte
//
// TODO: TEST return ob.ar[index]
} // Test_byts_Bytes_GetByte_
// go test --run Test_byts_Bytes_GetChar_
func Test_byts_Bytes_GetChar_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) GetChar(index int) CharSize
//
// TODO: TEST r, size := utf8.DecodeRune(ob.ar[index:])
// TODO: TEST return CharSize{Val: r, Size: size}
} // Test_byts_Bytes_GetChar_
// go test --run Test_byts_Bytes_Size_
func Test_byts_Bytes_Size_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Size() int
//
// TODO: TEST return len(ob.ar)
} // Test_byts_Bytes_Size_
// -----------------------------------------------------------------------------
// # fmt.Stringer Interface
// go test --run Test_byts_Bytes_String_
func Test_byts_Bytes_String_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) String() string
//
// TODO: TEST return string(ob.ar)
} // Test_byts_Bytes_String_
// -----------------------------------------------------------------------------
// # Methods
// go test --run Test_byts_Bytes_Append_
func Test_byts_Bytes_Append_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Append(b Bytes)
//
// TODO: TEST ob.ar = append(ob.ar, b.ar...)
} // Test_byts_Bytes_Append_
// go test --run Test_byts_Bytes_AppendChar_
func Test_byts_Bytes_AppendChar_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) AppendChar(ch rune) int
//
// TODO: TEST Bytes.AppendChar():
// size := utf8.RuneLen(ch)
// if size == -1 {
// Error("utf8.RuneLen(ch) == -1")
// return -1
// }
// var buf [utf8.UTFMax]byte
// ret := utf8.EncodeRune(buf[:], ch)
// ob.ar = append(ob.ar, buf[:ret]...)
// return ret
} // Test_byts_Bytes_AppendChar_
// go test --run Test_byts_Bytes_GetBytes_
func Test_byts_Bytes_GetBytes_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) GetBytes() []byte
//
// TODO: TEST return ob.ar
} // Test_byts_Bytes_GetBytes_
// go test --run Test_byts_Bytes_Insert_
func Test_byts_Bytes_Insert_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Insert(index int, data Bytes, count int) int
//
// TODO: TEST Bytes.Insert():
// if index < 0 || index > len(ob.ar) {
// Error("Index", index, "out of range; array:", len(ob.ar))
// return 0
// }
// src := data.ar
// if count != -1 {
// src = data.ar[:count]
// }
// srcLen := len(src)
// if srcLen == 0 {
// return 0
// }
// ob.ar = append(ob.ar, src...)
// copy(ob.ar[index+srcLen:], ob.ar[index:])
// copy(ob.ar[index:], src)
// return srcLen
} // Test_byts_Bytes_Insert_
// go test --run Test_byts_Bytes_Remove_
func Test_byts_Bytes_Remove_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Remove(index, count int)
//
// TODO: TEST Bytes.Remove():
// if index < 0 || index >= len(ob.ar) {
// Error("Index", index, "out of range; array:", len(ob.ar))
// return
// }
// if count == 0 {
// return
// }
// ob.ar = ob.ar[:index+copy(ob.ar[index:], ob.ar[index+count:])]
//
} // Test_byts_Bytes_Remove_
// go test --run Test_byts_Bytes_Reset_
func Test_byts_Bytes_Reset_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Reset()
//
// TODO: TEST ob.ar = ob.ar[:0]
} // Test_byts_Bytes_Reset_
// TODO: Bytes.Resize() can return old size and error
// go test --run Test_byts_Bytes_Resize_
func Test_byts_Bytes_Resize_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Resize(size int)
//
// TODO: TEST Bytes.Resize():
// current := len(ob.ar)
// if size == current {
// return
// }
// if size < current {
// ob.ar = ob.ar[:size]
// } else if size > current {
// extra := size - current
// zeros := bytes.Repeat([]byte{0}, extra)
// ob.ar = append(ob.ar, zeros...)
// }
} // Test_byts_Bytes_Resize_
// go test --run Test_byts_Bytes_SetByte_
func Test_byts_Bytes_SetByte_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) SetByte(index int, val byte)
//
// TODO: Implement Bytes.SetByte()
// TODO: Bytes.SetByte() can return previous byte
} // Test_byts_Bytes_SetByte_
// go test --run Test_byts_Bytes_Slice_
func Test_byts_Bytes_Slice_(t *testing.T) {
TBegin(t)
//
// (ob *Bytes) Slice(beginIndex, endIndex int) Bytes
//
// TODO: TEST Bytes.Slice():
// if beginIndex < 0 {
// Error("Treating beginIndex", beginIndex, "as 0")
// beginIndex = 0
// }
// if endIndex < 0 || endIndex > len(ob.ar) {
// // -1 is acceptable for endIndex
// if endIndex != -1 {
// Error("Treating endIndex", endIndex, "as length", len(ob.ar))
// }
// endIndex = len(ob.ar)
// }
// return Bytes(Bytes{ar: ob.ar[beginIndex:endIndex]})
} // Test_byts_Bytes_Slice_
// end