-
Notifications
You must be signed in to change notification settings - Fork 4
/
bingo_test.go
139 lines (114 loc) · 3.6 KB
/
bingo_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
package bingo_test
import (
"bytes"
"errors"
"testing"
. "github.com/iancmcc/bingo"
. "github.com/smartystreets/goconvey/convey"
)
func TestBingo(t *testing.T) {
Convey("Bingo", t, func() {
Convey("should pack values while preserving order", func() {
a, err := Pack(1, "hi", int64(67))
So(err, ShouldBeNil)
b, err := Pack(1, "hi 1", int64(67))
So(err, ShouldBeNil)
So(bytes.Compare(a, b), ShouldEqual, -1)
})
Convey("should pack values panicking on invalid type", func() {
So(func() { MustPack(1, "hi", int64(67)) }, ShouldNotPanic)
So(func() { MustPack(1, struct{}{}, int64(67)) }, ShouldPanic)
So(func() { WithDesc().MustPack(1, "hi", int64(67)) }, ShouldNotPanic)
So(func() { WithDesc().MustPack(1, struct{}{}, int64(67)) }, ShouldPanic)
})
Convey("should pack values into a given array while preserving order", func() {
buf := make([]byte, 32)
bufd := make([]byte, 32)
PackTo(buf, 1, "hi", int64(67))
PackTo(bufd, 1, "hi 1", int64(67))
So(bytes.Compare(buf, bufd), ShouldEqual, -1)
})
Convey("should pack values to a given array panicking on invalid type", func() {
buf := make([]byte, 32)
So(func() { MustPackTo(buf, 1, "hi", int64(67)) }, ShouldNotPanic)
So(func() { MustPackTo(buf, 1, struct{}{}, int64(67)) }, ShouldPanic)
So(func() { WithDesc().MustPackTo(buf, 1, "hi", int64(67)) }, ShouldNotPanic)
So(func() { WithDesc().MustPackTo(buf, 1, struct{}{}, int64(67)) }, ShouldPanic)
})
Convey("should pack mixed-order values while preserving order", func() {
s := WithDesc(false, true, false)
a, err := s.Pack(1, "hi", int64(67))
So(err, ShouldBeNil)
b, err := s.Pack(1, "hi 1", int64(67))
So(err, ShouldBeNil)
So(bytes.Compare(a, b), ShouldEqual, 1)
})
Convey("should pack mixed-order values into a given byte array while preserving order", func() {
buf := make([]byte, 32)
bufd := make([]byte, 32)
s := WithDesc(false, true, false)
s.PackTo(buf, 1, "hi", int64(67))
s.PackTo(bufd, 1, "hi 1", int64(67))
So(bytes.Compare(buf, bufd), ShouldEqual, 1)
})
Convey("should unpack values", func() {
a := "this is a test"
b := int8(69)
c := float32(1.61803398875)
packed, err := Pack(a, b, c)
So(err, ShouldBeNil)
var (
adest string
bdest int8
cdest float32
)
Unpack(packed, &adest, &bdest, &cdest)
So(a, ShouldEqual, adest)
So(b, ShouldEqual, bdest)
So(c, ShouldEqual, cdest)
})
Convey("should unpack only those asked for", func() {
a := "this is a test"
b := int8(69)
c := float32(1.61803398875)
packed, err := Pack(a, b, c)
So(err, ShouldBeNil)
var (
adest string
cdest float32
)
Unpack(packed, &adest, nil, &cdest)
So(a, ShouldEqual, adest)
So(c, ShouldEqual, cdest)
})
Convey("should unpack a value at a specific index", func() {
a := "this is a test"
b := int8(69)
c := float32(1.61803398875)
packed, err := Pack(a, b, c)
So(err, ShouldBeNil)
var (
adest string
cdest float32
)
UnpackIndex(packed, 2, &cdest)
So(c, ShouldEqual, cdest)
UnpackIndex(packed, 0, &adest)
So(a, ShouldEqual, adest)
})
Convey("should error when a nonexistent index is requested", func() {
a := "this is a test"
b := int8(69)
c := float32(1.61803398875)
packed, err := Pack(a, b, c)
So(err, ShouldBeNil)
var cdest float32
So(UnpackIndex(packed, 7, &cdest), ShouldResemble, errors.New("No data at index 7"))
})
Convey("should error when unpacking a random string", func() {
b := []byte("abcde")
var adest string
So(Unpack(b, &adest), ShouldResemble, errors.New("unknown type"))
})
})
}