forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits_test.go
205 lines (170 loc) · 5.8 KB
/
bits_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
package ion
import (
"bytes"
"fmt"
"math"
"math/big"
"testing"
"time"
)
func TestAppendUint(t *testing.T) {
test := func(val uint64, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("%x", val), func(t *testing.T) {
len := uintLen(val)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendUint(nil, val)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
test(0, 1, []byte{0})
test(0xFF, 1, []byte{0xFF})
test(0x1FF, 2, []byte{0x01, 0xFF})
test(math.MaxUint64, 8, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
}
func TestAppendInt(t *testing.T) {
test := func(val int64, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("%x", val), func(t *testing.T) {
len := intLen(val)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendInt(nil, val)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
test(0, 0, []byte{})
test(0x7F, 1, []byte{0x7F})
test(-0x7F, 1, []byte{0xFF})
test(0xFF, 2, []byte{0x00, 0xFF})
test(-0xFF, 2, []byte{0x80, 0xFF})
test(0x7FFF, 2, []byte{0x7F, 0xFF})
test(-0x7FFF, 2, []byte{0xFF, 0xFF})
test(math.MaxInt64, 8, []byte{0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
test(-math.MaxInt64, 8, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
test(math.MinInt64, 9, []byte{0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
}
func TestAppendBigInt(t *testing.T) {
test := func(val *big.Int, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("%x", val), func(t *testing.T) {
len := bigIntLen(val)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendBigInt(nil, val)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
test(big.NewInt(0), 0, []byte{})
test(big.NewInt(0x7F), 1, []byte{0x7F})
test(big.NewInt(-0x7F), 1, []byte{0xFF})
test(big.NewInt(0xFF), 2, []byte{0x00, 0xFF})
test(big.NewInt(-0xFF), 2, []byte{0x80, 0xFF})
test(big.NewInt(0x7FFF), 2, []byte{0x7F, 0xFF})
test(big.NewInt(-0x7FFF), 2, []byte{0xFF, 0xFF})
}
func TestAppendVarUint(t *testing.T) {
test := func(val uint64, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("%x", val), func(t *testing.T) {
len := varUintLen(val)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendVarUint(nil, val)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
test(0, 1, []byte{0x80})
test(0x7F, 1, []byte{0xFF})
test(0xFF, 2, []byte{0x01, 0xFF})
test(0x1FF, 2, []byte{0x03, 0xFF})
test(0x3FFF, 2, []byte{0x7F, 0xFF})
test(0x7FFF, 3, []byte{0x01, 0x7F, 0xFF})
test(0x7FFFFFFFFFFFFFFF, 9, []byte{0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
test(0xFFFFFFFFFFFFFFFF, 10, []byte{0x01, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
}
func TestAppendVarInt(t *testing.T) {
test := func(val int64, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("%x", val), func(t *testing.T) {
len := varIntLen(val)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendVarInt(nil, val)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
test(0, 1, []byte{0x80})
test(0x3F, 1, []byte{0xBF}) // 1011 1111
test(-0x3F, 1, []byte{0xFF})
test(0x7F, 2, []byte{0x00, 0xFF})
test(-0x7F, 2, []byte{0x40, 0xFF})
test(0x1FFF, 2, []byte{0x3F, 0xFF})
test(-0x1FFF, 2, []byte{0x7F, 0xFF})
test(0x3FFF, 3, []byte{0x00, 0x7F, 0xFF})
test(-0x3FFF, 3, []byte{0x40, 0x7F, 0xFF})
test(0x3FFFFFFFFFFFFFFF, 9, []byte{0x3F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
test(-0x3FFFFFFFFFFFFFFF, 9, []byte{0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
test(math.MaxInt64, 10, []byte{0x00, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
test(-math.MaxInt64, 10, []byte{0x40, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
test(math.MinInt64, 10, []byte{0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80})
}
func TestAppendTag(t *testing.T) {
test := func(code byte, vlen uint64, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("(%x,%v)", code, vlen), func(t *testing.T) {
len := tagLen(vlen)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendTag(nil, code, vlen)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
test(0x20, 1, 1, []byte{0x21})
test(0x30, 0x0D, 1, []byte{0x3D})
test(0x40, 0x0E, 2, []byte{0x4E, 0x8E})
test(0x50, math.MaxInt64, 10, []byte{0x5E, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0xFF})
}
func TestAppendTime(t *testing.T) {
test := func(val time.Time, elen uint64, ebits []byte) {
t.Run(fmt.Sprintf("%x", val), func(t *testing.T) {
_, offset := val.Zone()
offset /= 60
utc := val.In(time.UTC)
len := timeLen(offset, utc)
if len != elen {
t.Errorf("expected len=%v, got len=%v", elen, len)
}
bits := appendTime(nil, offset, utc)
if !bytes.Equal(bits, ebits) {
t.Errorf("expected %v, got %v", fmtbytes(ebits), fmtbytes(bits))
}
})
}
nowish, _ := time.Parse(time.RFC3339Nano, "2019-08-04T18:15:43.863494+10:00")
test(time.Time{}, 7, []byte{0x80, 0x81, 0x81, 0x81, 0x80, 0x80, 0x80})
test(nowish, 14, []byte{
0x04, 0xD8, // offset: +600 minutes (+10:00)
0x0F, 0xE3, // year: 2019
0x88, // month: 8
0x84, // day: 4
0x88, // hour: 8 utc (18 local)
0x8F, // minute: 15
0xAB, // second: 43
0xC9, // exp: -9
0x33, 0x77, 0xDF, 0x70, // nsec: 863494000
})
}