-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprimitives_test.go
100 lines (80 loc) · 2 KB
/
primitives_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 ikea
import (
"bytes"
"math/rand"
"reflect"
"testing"
)
func TestBool(t *testing.T) {
i := rand.Int()%2 == 1
typeTest(t, "TestBool", &i, i)
}
func TestByte(t *testing.T) {
i := byte(rand.Int() & 0xFF)
typeTest(t, "TestByte", &i, i)
}
func TestUint8(t *testing.T) {
i := uint8(rand.Int() & 0xFF)
typeTest(t, "TestUint8", &i, i)
}
func TestUint16(t *testing.T) {
i := uint16(rand.Int() & 0xFFFF)
typeTest(t, "TestUint16", &i, i)
}
func TestUint32(t *testing.T) {
i := rand.Uint32()
typeTest(t, "TestUint32", &i, i)
}
func TestUint64(t *testing.T) {
i := rand.Uint64()
typeTest(t, "TestUint64", &i, i)
}
func TestInt8(t *testing.T) {
i := int8(rand.Int() & 0xFF)
typeTest(t, "TestInt8", &i, i)
}
func TestInt16(t *testing.T) {
i := int16(rand.Int() & 0xFFFF)
typeTest(t, "TestInt16", &i, i)
}
func TestInt32(t *testing.T) {
i := rand.Int31()
typeTest(t, "TestInt32", &i, i)
}
func TestInt64(t *testing.T) {
i := rand.Int63()
typeTest(t, "TestInt64", &i, i)
}
func TestFloat32(t *testing.T) {
i := rand.Float32()
typeTest(t, "TestFloat32", &i, i)
}
func TestFloat64(t *testing.T) {
i := rand.Float64()
typeTest(t, "TestFloat32", &i, i)
}
func TestString(t *testing.T) {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, rand.Intn(30))
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
s := string(b)
typeTest(t, "TestString", &s, s)
}
func typeTest(t *testing.T, typ string, value, compare interface{}) {
var b bytes.Buffer
if err := Pack(&b, value); err != nil {
t.Errorf("Failing %s, could not write value: %s\n", typ, err.Error())
return
}
target := reflect.New(reflect.TypeOf(value).Elem())
if err := Unpack(&b, target.Interface()); err != nil {
t.Errorf("Failing %s, could not read value: %s\n", typ, err.Error())
return
}
dereference := target.Elem().Interface()
if dereference != compare {
t.Errorf("Failing %s, %T value %+v does not match original %T %+v\n", typ, dereference, dereference, compare, compare)
}
}