-
Notifications
You must be signed in to change notification settings - Fork 22
/
pickle_writer_test.go
411 lines (316 loc) · 6.79 KB
/
pickle_writer_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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package stalecucumber
import "testing"
import "bytes"
import "io"
import "reflect"
import "math/big"
import "github.com/hydrogen18/stalecucumber/struct_export_test"
func TestPickleBadTypes(t *testing.T) {
c := make(chan int)
assertPicklingFails(c,
PicklingError{Err: ErrTypeNotPickleable, V: c},
t)
}
func assertPicklingFails(v interface{}, expect error, t *testing.T) {
buf := &bytes.Buffer{}
p := NewPickler(buf)
_, err := p.Pickle(v)
if err == nil {
t.Fatalf("Pickling (%T)%v should have failed", v, v)
}
if !reflect.DeepEqual(err, expect) {
t.Fatalf("Expected error (%T)%v got (%T)%v", expect, expect, err, err)
}
}
func TestPickleFloat64(t *testing.T) {
roundTrip(1337.42, t)
}
func TestPickleByte(t *testing.T) {
inAndOut(byte(1), int64(1), t)
}
func TestPickleFloat32(t *testing.T) {
var f float32
f = 1337.42
inAndOut(f, float64(f), t)
}
func TestPickleString(t *testing.T) {
roundTrip("test string #1", t)
}
func TestPickleInt(t *testing.T) {
var i int
i = 2300000
inAndOut(i, int64(i), t)
}
func TestPickleInt8(t *testing.T) {
var i int8
i = 43
inAndOut(i, int64(i), t)
}
func TestPickleInt16(t *testing.T) {
var i int16
i = 4200
inAndOut(i, int64(i), t)
}
func TestPickleInt32(t *testing.T) {
var i int32
i = 42
inAndOut(i, int64(i), t)
}
func TestPickleUint(t *testing.T) {
var i uint
i = 13556263
inAndOut(i, int64(i), t)
}
func TestPickleUint8(t *testing.T) {
var i uint8
i = 22
inAndOut(i, int64(i), t)
}
func TestPickleUint16(t *testing.T) {
var i uint16
i = 10000
inAndOut(i, int64(i), t)
}
func TestPickleUint32(t *testing.T) {
var i uint32
i = 2
inAndOut(i, int64(i), t)
i = 4294967295
bi := big.NewInt(int64(i))
inAndOut(i, bi, t)
}
func TestPickleUint64(t *testing.T) {
var i uint64
i = 1580137
inAndOut(i, int64(i), t)
i = 18446744073709551615
/**
buf := &bytes.Buffer{}
p := NewPickler(buf)
_, err := p.Pickle(i)
if err != nil {
t.Fatal(err)
}
t.Fatalf("%v", buf.Bytes())
**/
bi := big.NewInt(0)
bi.SetUint64(i)
inAndOut(i, bi, t)
var ui uint
ui = ^uint(0)
bi.SetUint64(uint64(ui))
inAndOut(ui, bi, t)
}
func TestPickleInt64(t *testing.T) {
var i int64
i = 1337
roundTrip(i, t)
i = 1 << 48
inAndOut(i, big.NewInt(i), t)
i *= -1
inAndOut(i, big.NewInt(i), t)
i = 1 << 32
i *= -1
inAndOut(i, big.NewInt(i), t)
}
func TestPickleSlice(t *testing.T) {
data := make([]interface{}, 3)
data[0] = "meow"
data[1] = int64(1336)
data[2] = float64(42.0)
roundTrip(data, t)
var array [3]int
out := make([]interface{}, len(array))
array[0] = 100
array[1] = 200
array[2] = 300
for i, v := range array {
out[i] = int64(v)
}
inAndOut(array, out, t)
}
func TestPickleMap(t *testing.T) {
data := make(map[interface{}]interface{})
data[int64(1)] = int64(10)
data[int64(2)] = int64(20)
data[int64(3)] = "foobar"
data[int64(4)] = float64(4.0)
data[int64(5)] = float64(2.0)
data["meow"] = "foooooocool"
roundTrip(data, t)
in := make(map[string]float32)
out := make(map[interface{}]interface{})
in["foo"] = 2.0
out["foo"] = 2.0
in["bar"] = 4.0
out["bar"] = 4.0
inAndOut(in, out, t)
}
type exampleStruct struct {
Apple int
Banana int32
Cat uint32
Dog int8
Elephant string
Fart float32 `pickle:"fart"`
Golf uint64
}
type exampleEmbeddedStruct struct {
exampleStruct
Hiking string
}
func TestRoundTripStruct(t *testing.T) {
in := exampleStruct{
1,
2,
3,
4,
"hello world!",
1151356.0,
9223372036854775807,
}
inAndUnpack(in, t)
in2 := struct_export_test.Struct1{}
in2.Joker = "meow"
in2.Killer = 23.37
in2.Lawnmower = 23
in2.Duplicate = 42.12
inAndUnpack(in2, t)
in3 := exampleEmbeddedStruct{
exampleStruct: in,
Hiking: "is fun",
}
inAndUnpack(in3, t)
}
func TestPickleSnowman(t *testing.T) {
roundTrip("This is a snowman: ☃", t)
}
func TestPicklePointer(t *testing.T) {
var myptr *int
myptr = new(int)
*myptr = 42
inAndUnpack(myptr, t)
inAndUnpack(&myptr, t)
myptr = nil
inAndUnpack(&myptr, t)
}
func TestPickleStruct(t *testing.T) {
example := struct {
Apple uint64
Banana float32
C string
dog func(int) //Not exported, ignored by pickler
}{
1,
2,
"hello pickles",
nil,
}
out := make(map[interface{}]interface{})
out["Apple"] = int64(1)
out["Banana"] = float64(2.0)
out["C"] = example.C
inAndOut(example, out, t)
example2 := struct {
Elephant int
Fart string `pickle:"fart"`
Golf float32
}{
14,
"woohoo",
13.37,
}
out = make(map[interface{}]interface{})
out["Elephant"] = int64(example2.Elephant)
out["Golf"] = float64(example2.Golf)
out["fart"] = example2.Fart
inAndOut(example2, out, t)
}
func TestPickleBool(t *testing.T) {
var b bool
roundTrip(b, t)
b = true
roundTrip(b, t)
}
func TestPickleBigInt(t *testing.T) {
i := big.NewInt(1)
i.Lsh(i, 42)
roundTrip(i, t)
i.SetUint64(1)
i.Lsh(i, 256*8)
roundTrip(i, t)
i.Mul(i, big.NewInt(-1))
roundTrip(i, t)
i = big.NewInt(0)
roundTrip(i, t)
}
func TestPickleTuple(t *testing.T) {
myTuple := NewTuple(1, 2, "foobar")
buf := &bytes.Buffer{}
pickler := NewPickler(buf)
_, err := pickler.Pickle(myTuple)
if err != nil {
t.Fatal(err)
}
out := []interface{}{int64(1),
int64(2),
"foobar"}
//Verify it can be read back
inAndOut(myTuple, out, t)
myTuple = NewTuple()
out = []interface{}{}
inAndOut(myTuple, out, t)
myTuple = NewTuple("a")
out = []interface{}{"a"}
inAndOut(myTuple, out, t)
myTuple = NewTuple("a", "b")
out = []interface{}{"a", "b"}
inAndOut(myTuple, out, t)
myTuple = NewTuple("a", "b", "c", nil, "d")
out = []interface{}{"a", "b", "c", PickleNone{}, "d"}
inAndOut(myTuple, out, t)
}
func inAndUnpack(v interface{}, t *testing.T) {
buf := &bytes.Buffer{}
p := NewPickler(buf)
_, err := p.Pickle(v)
if err != nil {
t.Fatalf("Failed writing type %T:\n%v", v, err)
}
w := reflect.New(reflect.TypeOf(v))
err = UnpackInto(w.Interface()).From(Unpickle(bytes.NewReader(buf.Bytes())))
if err != nil {
t.Fatalf("Failed unpickling %T:%v", w, err)
}
wi := reflect.Indirect(w).Interface()
if !reflect.DeepEqual(v, wi) {
t.Fatalf("\nFrom:%x\n---EXPECTED:(%T)\n%v\n---GOT:(%T)\n%v", buf.Bytes(), v, v, wi, wi)
}
}
func inAndOut(v, w interface{}, t *testing.T) {
buf := &bytes.Buffer{}
p := NewPickler(buf)
_, err := p.Pickle(v)
if err != nil {
t.Fatalf("Failed writing type %T:%v", v, err)
}
sanityCheck(buf, t, w)
}
func roundTrip(v interface{}, t *testing.T) {
buf := &bytes.Buffer{}
p := NewPickler(buf)
_, err := p.Pickle(v)
if err != nil {
t.Fatalf("Failed writing type %T:%v", v, err)
}
sanityCheck(buf, t, v)
}
func sanityCheck(r io.Reader, t *testing.T, expect interface{}) {
v, err := Unpickle(r)
if err != nil {
t.Fatalf("Failed to unpickle own output:%v", err)
}
if !reflect.DeepEqual(v, expect) {
t.Fatalf("\n---EXPECTED:(%T)\n%v\n---GOT:(%T)\n%v", expect, expect, v, v)
}
}