-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest.go
374 lines (342 loc) · 7.52 KB
/
unittest.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package unittest
import (
"bytes"
"fmt"
"math"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"unicode"
"unsafe"
)
var (
sizeOfInt = int(unsafe.Sizeof(int(0)))
sizeOfInt16 = int(unsafe.Sizeof(int16(0)))
sizeOfInt32 = int(unsafe.Sizeof(int32(0)))
sizeOfInt64 = int(unsafe.Sizeof(int64(0)))
sizeOfUint = int(unsafe.Sizeof(uint(0)))
sizeOfUint16 = int(unsafe.Sizeof(uint16(0)))
sizeOfUint32 = int(unsafe.Sizeof(uint32(0)))
sizeOfUint64 = int(unsafe.Sizeof(uint64(0)))
sizeOfFloat32 = int(unsafe.Sizeof(float32(0)))
sizeOfFloat64 = int(unsafe.Sizeof(float64(0)))
)
type toString interface {
String() string
}
type Equals interface {
Equals(interface{}) bool
}
func Check(t *testing.T, condition bool, args ...interface{}) bool {
return check(t, condition, "check", t.Fail, args...)
}
func Assert(t *testing.T, condition bool, args ...interface{}) {
check(t, condition, "assert", t.FailNow, args...)
}
func check(t *testing.T, condition bool, tp string, f func(), args ...interface{}) bool {
if condition {
return true
}
var buf bytes.Buffer
buf.WriteString(tp + " fail\n")
for i := 0; i < len(args); i++ {
buf.WriteString(" args[")
buf.WriteString(strconv.Itoa(i))
buf.WriteString("] = %#v")
if i < len(args)-1 {
buf.WriteByte('\n')
}
}
log(2, fmt.Sprintf(buf.String(), args...))
f()
return false
}
func IsNil(t *testing.T, v interface{}) bool {
return isNil(t, v, t.Fail)
}
func IsNilNow(t *testing.T, v interface{}) {
isNil(t, v, t.FailNow)
}
func isNil(t *testing.T, v interface{}, f func()) bool {
if v == nil {
return true
}
switch vv := v.(type) {
case toString:
log(2, fmt.Sprintf(`not nil
v = %#v`, vv.String()))
case error:
log(2, fmt.Sprintf(`not nil
v = %#v`, vv.Error()))
case []byte:
log(2, fmt.Sprintf(`not nil
v = %#v`, vv))
default:
log(2, fmt.Sprintf(`not nil
v = %v`, vv))
}
f()
return false
}
func NotNil(t *testing.T, v interface{}) bool {
return notNil(t, v, t.Fail)
}
func NotNilNow(t *testing.T, v interface{}) {
notNil(t, v, t.FailNow)
}
func notNil(t *testing.T, v interface{}, f func()) bool {
if v != nil {
return true
}
log(2, fmt.Sprintf(`is nil`))
f()
return false
}
func DeepEqual(t *testing.T, a, b interface{}) bool {
return deepEqual(t, a, b, t.Fail)
}
func DeepEqualNow(t *testing.T, a, b interface{}) {
deepEqual(t, a, b, t.FailNow)
}
func deepEqual(t *testing.T, a, b interface{}, f func()) bool {
if reflect.DeepEqual(a, b) {
return true
}
log(2, fmt.Sprintf(`not deep equal
a = %#v
b = %#v`, a, b))
f()
return false
}
func Equal(t *testing.T, a, b interface{}) bool {
return equal(t, a, b, t.Fail)
}
func EqualNow(t *testing.T, a, b interface{}) {
equal(t, a, b, t.FailNow)
}
func equal(t *testing.T, a, b interface{}, f func()) bool {
if a == nil || b == nil {
return a == b
}
var ok bool
var printable bool
switch va := a.(type) {
case int:
ok = va == b.(int)
case int8:
ok = va == int8Val(b)
case int16:
ok = va == int16Val(b)
//case int32:
//ok = va == b.(int32)
case rune:
vb := int32Val(b)
ok = va == vb
printable = unicode.IsPrint(va) && unicode.IsPrint(rune(vb))
case int64:
ok = va == int64Val(b)
case uint:
ok = va == uintVal(b)
case uint8:
ok = va == uint8Val(b)
case uint16:
ok = va == uint16Val(b)
case uint32:
ok = va == uint32Val(b)
case uint64:
ok = va == uint64Val(b)
case float32:
ok = va == float32Val(b)
case float64:
ok = va == float64Val(b)
case string:
ok = va == b.(string)
case []byte:
ok = bytes.Equal(va, b.([]byte))
case []int:
ok = unsafeEqual(a, b.([]int), sizeOfInt)
case []int16:
ok = unsafeEqual(a, b.([]int16), sizeOfInt16)
case []int32:
ok = unsafeEqual(a, b.([]int32), sizeOfInt32)
case []int64:
ok = unsafeEqual(a, b.([]int64), sizeOfInt64)
case []uint:
ok = unsafeEqual(a, b.([]uint), sizeOfUint)
case []uint16:
ok = unsafeEqual(a, b.([]uint16), sizeOfUint16)
case []uint32:
ok = unsafeEqual(a, b.([]uint32), sizeOfUint32)
case []uint64:
ok = unsafeEqual(a, b.([]uint64), sizeOfUint64)
case []float32:
ok = unsafeEqual(a, b.([]float32), sizeOfFloat32)
case []float64:
ok = unsafeEqual(a, b.([]float64), sizeOfFloat64)
case Equals:
ok = va.Equals(b)
default:
ok = reflect.DeepEqual(a, b)
}
if ok {
return true
}
if printable {
log(2, fmt.Sprintf(`not equal
a = '%c' = %#v
b = '%c' = %#v`, a, a, b, b))
} else {
log(2, fmt.Sprintf(`not equal
a = %#v
b = %#v`, a, b))
}
f()
return false
}
func int8Val(b interface{}) int8 {
switch v := b.(type) {
case int:
if int(math.MinInt8) <= v && v <= int(math.MaxInt8) {
return int8(v)
}
case int8:
return int8(v)
}
panic("can't convert to int8 value")
}
func uint8Val(b interface{}) uint8 {
switch v := b.(type) {
case int:
if 0 <= v && v <= int(math.MaxUint8) {
return uint8(v)
}
case uint8:
return uint8(v)
}
panic("can't convert to uint8 value")
}
func int16Val(b interface{}) int16 {
switch v := b.(type) {
case int:
if int(math.MinInt16) <= v && v <= int(math.MaxInt16) {
return int16(v)
}
case int16:
return int16(v)
}
panic("can't convert to int16 value")
}
func uint16Val(b interface{}) uint16 {
switch v := b.(type) {
case int:
if 0 <= v && v <= int(math.MaxUint16) {
return uint16(v)
}
case uint16:
return uint16(v)
}
panic("can't convert to uint16 value")
}
func int32Val(b interface{}) int32 {
switch v := b.(type) {
case int:
if int(math.MinInt32) <= v && v <= int(math.MaxInt32) {
return int32(v)
}
case int32:
return int32(v)
}
panic("can't convert to int32 value")
}
func uint32Val(b interface{}) uint32 {
switch v := b.(type) {
case int:
if 0 <= v && v <= int(math.MaxUint32) {
return uint32(v)
}
case uint32:
return uint32(v)
}
panic("can't convert to uint32 value")
}
func int64Val(b interface{}) int64 {
switch v := b.(type) {
case int:
return int64(v)
case int64:
return int64(v)
}
panic("can't convert to int64 value")
}
func uintVal(b interface{}) uint {
switch v := b.(type) {
case int:
if 0 <= v {
return uint(v)
}
case uint:
return uint(v)
}
panic("can't convert to uint value")
}
func uint64Val(b interface{}) uint64 {
switch v := b.(type) {
case int:
if 0 <= v {
return uint64(v)
}
case uint64:
return uint64(v)
}
panic("can't convert to uint64 value")
}
func float32Val(b interface{}) float32 {
switch v := b.(type) {
case int:
return float32(v)
case float32:
return float32(v)
}
panic("can't convert to float32 value")
}
func float64Val(b interface{}) float64 {
switch v := b.(type) {
case int:
return float64(v)
case float32:
return float64(v)
case float64:
return float64(v)
}
panic("can't convert to float64 value")
}
func unsafeEqual(ia, ib interface{}, size int) bool {
a := (*reflect.SliceHeader)((*(*[2]unsafe.Pointer)(unsafe.Pointer(&ia)))[1])
b := (*reflect.SliceHeader)((*(*[2]unsafe.Pointer)(unsafe.Pointer(&ib)))[1])
return bytes.Equal(
*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: a.Data,
Cap: a.Cap * size,
Len: a.Len * size,
})),
*(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: b.Data,
Cap: b.Cap * size,
Len: b.Len * size,
})),
)
}
func log(depth int, val string) {
if _, file, line, ok := runtime.Caller(1 + depth); ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
fmt.Fprintf(os.Stderr, " %s:%d: %s\n", file, line, val)
}
}