-
Notifications
You must be signed in to change notification settings - Fork 1
/
scalar_test.go
780 lines (708 loc) · 15.6 KB
/
scalar_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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
package protoscan
import (
"bytes"
"encoding/json"
"io"
"testing"
"github.com/paulmach/protoscan/internal/testmsg"
"google.golang.org/protobuf/proto"
)
func TestDecodeScalar_numbers(t *testing.T) {
cases := []struct {
name string
message *testmsg.Scalar
}{
{
name: "positive float",
message: &testmsg.Scalar{Flt: proto.Float32(123.4567)},
},
{
name: "negative float",
message: &testmsg.Scalar{Flt: proto.Float32(-23.4567)},
},
{
name: "zero float",
message: &testmsg.Scalar{Flt: proto.Float32(0)},
},
{
name: "positive double",
message: &testmsg.Scalar{Dbl: proto.Float64(123.4567)},
},
{
name: "negative double",
message: &testmsg.Scalar{Dbl: proto.Float64(-23.4567)},
},
{
name: "zero double",
message: &testmsg.Scalar{Dbl: proto.Float64(0)},
},
{
name: "float and double",
message: &testmsg.Scalar{Dbl: proto.Float64(123.4567), Flt: proto.Float32(34)},
},
{
name: "positive int32",
message: &testmsg.Scalar{I32: proto.Int32(5280)},
},
{
name: "negative int32",
message: &testmsg.Scalar{I32: proto.Int32(-123_567_890)},
},
{
name: "zero int32",
message: &testmsg.Scalar{I32: proto.Int32(0)},
},
{
name: "positive int64",
message: &testmsg.Scalar{I64: proto.Int64(5280)},
},
{
name: "big int64",
message: &testmsg.Scalar{I64: proto.Int64(9_828_385_280)},
},
{
name: "negative int64",
message: &testmsg.Scalar{I64: proto.Int64(-111_123_567_890)},
},
{
name: "zero int64",
message: &testmsg.Scalar{I64: proto.Int64(0)},
},
{
name: "positive uint32",
message: &testmsg.Scalar{U32: proto.Uint32(5280)},
},
{
name: "zero uint32",
message: &testmsg.Scalar{U32: proto.Uint32(0)},
},
{
name: "positive uint64",
message: &testmsg.Scalar{U64: proto.Uint64(5280)},
},
{
name: "big uint64",
message: &testmsg.Scalar{U64: proto.Uint64(9_828_385_280)},
},
{
name: "zero uint64",
message: &testmsg.Scalar{U64: proto.Uint64(0)},
},
{
name: "positive sint32",
message: &testmsg.Scalar{S32: proto.Int32(5280)},
},
{
name: "negative sint32",
message: &testmsg.Scalar{S32: proto.Int32(-123_567_890)},
},
{
name: "zero sint32",
message: &testmsg.Scalar{S32: proto.Int32(0)},
},
{
name: "positive sint64",
message: &testmsg.Scalar{S64: proto.Int64(5280)},
},
{
name: "big sint64",
message: &testmsg.Scalar{S64: proto.Int64(9_828_385_280)},
},
{
name: "negative sint64",
message: &testmsg.Scalar{S64: proto.Int64(-111_123_567_890)},
},
{
name: "zero sint64",
message: &testmsg.Scalar{S64: proto.Int64(0)},
},
{
name: "positive fixed32",
message: &testmsg.Scalar{F32: proto.Uint32(5280)},
},
{
name: "zero fixed32",
message: &testmsg.Scalar{F32: proto.Uint32(0)},
},
{
name: "positive fixed64",
message: &testmsg.Scalar{F64: proto.Uint64(5280)},
},
{
name: "big fixed64",
message: &testmsg.Scalar{F64: proto.Uint64(9_828_385_280)},
},
{
name: "zero fixed64",
message: &testmsg.Scalar{F64: proto.Uint64(0)},
},
{
name: "positive sfixed32",
message: &testmsg.Scalar{Sf32: proto.Int32(5280)},
},
{
name: "negative sfixed32",
message: &testmsg.Scalar{Sf32: proto.Int32(-5280)},
},
{
name: "zero sfixed32",
message: &testmsg.Scalar{Sf32: proto.Int32(0)},
},
{
name: "positive sfixed64",
message: &testmsg.Scalar{Sf64: proto.Int64(5280)},
},
{
name: "negative sfixed64",
message: &testmsg.Scalar{Sf64: proto.Int64(-1_234_567)},
},
{
name: "big sfixed64",
message: &testmsg.Scalar{Sf64: proto.Int64(9_828_385_280)},
},
{
name: "zero sfixed64",
message: &testmsg.Scalar{Sf64: proto.Int64(0)},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data, err := proto.Marshal(tc.message)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
v := decodeScalar(t, data, 0)
compare(t, v, tc.message)
})
}
}
func TestDecodeScalar_bool(t *testing.T) {
cases := []struct {
name string
message *testmsg.Scalar
}{
{
name: "true",
message: &testmsg.Scalar{Bool: proto.Bool(true)},
},
{
name: "false",
message: &testmsg.Scalar{Bool: proto.Bool(false)},
},
{
name: "none",
message: &testmsg.Scalar{Bool: nil},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data, err := proto.Marshal(tc.message)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
r := &testmsg.Scalar{}
err = proto.Unmarshal(data, r)
if err != nil {
t.Fatalf("unable to unmarshal: %v", err)
}
v := decodeScalar(t, data, 0)
compare(t, v, tc.message)
})
}
}
func TestDecodeScalar_boolAsInt64(t *testing.T) {
// true value, 1 encoded with some unnecessary leading zeros.
msg := New([]byte{0x08, 0x81, 0x00})
for msg.Next() {
if n := msg.FieldNumber(); n != 1 {
t.Errorf("incorrect field number: %d != 1", n)
}
v, err := msg.Bool()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !v {
t.Errorf("incorrect value: %v != true", v)
}
}
// is a number other than 1, so should be false
msg = New([]byte{0x08, 0x81, 0x0F})
for msg.Next() {
if n := msg.FieldNumber(); n != 1 {
t.Errorf("incorrect field number: %d != 1", n)
}
v, err := msg.Bool()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if v {
t.Errorf("incorrect value: %v != true", v)
}
}
}
func TestDecodeScalar_string(t *testing.T) {
cases := []struct {
name string
message *testmsg.Scalar
}{
{
name: "empty",
message: &testmsg.Scalar{Str: proto.String("")},
},
{
name: "present",
message: &testmsg.Scalar{Str: proto.String("message")},
},
{
name: "emoji",
message: &testmsg.Scalar{Str: proto.String("123 😃 456")},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data, err := proto.Marshal(tc.message)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
r := &testmsg.Scalar{}
err = proto.Unmarshal(data, r)
if err != nil {
t.Fatalf("unable to unmarshal: %v", err)
}
v := decodeScalar(t, data, 0)
compare(t, v, tc.message)
})
}
}
func TestDecodeScalar_bytes(t *testing.T) {
cases := []struct {
name string
message *testmsg.Scalar
}{
{
name: "empty",
message: &testmsg.Scalar{Byte: []byte{}},
},
{
name: "empty",
message: &testmsg.Scalar{Byte: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}},
},
{
name: "long",
message: &testmsg.Scalar{Byte: make([]byte, 10000)},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data, err := proto.Marshal(tc.message)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
r := &testmsg.Scalar{}
err = proto.Unmarshal(data, r)
if err != nil {
t.Fatalf("unable to unmarshal: %v", err)
}
v := decodeScalar(t, data, 0)
compare(t, v, tc.message)
})
}
}
func TestDecodeScalar_skip(t *testing.T) {
cases := []struct {
name string
skip int
message *testmsg.Scalar
}{
{
name: "skip float",
skip: 1,
message: &testmsg.Scalar{Flt: proto.Float32(1.5), After: proto.Bool(true)},
},
{
name: "skip double",
skip: 2,
message: &testmsg.Scalar{Dbl: proto.Float64(1.5), After: proto.Bool(true)},
},
{
name: "skip int32",
skip: 3,
message: &testmsg.Scalar{I32: proto.Int32(1_234_567), After: proto.Bool(true)},
},
{
name: "skip int64",
skip: 4,
message: &testmsg.Scalar{I64: proto.Int64(-1_234_567), After: proto.Bool(true)},
},
{
name: "skip uint32",
skip: 5,
message: &testmsg.Scalar{U32: proto.Uint32(1_234_567), After: proto.Bool(true)},
},
{
name: "skip uint64",
skip: 6,
message: &testmsg.Scalar{U64: proto.Uint64(1_234_567), After: proto.Bool(true)},
},
{
name: "skip sint32",
skip: 7,
message: &testmsg.Scalar{S32: proto.Int32(1_234_567), After: proto.Bool(true)},
},
{
name: "skip sint64",
skip: 8,
message: &testmsg.Scalar{S64: proto.Int64(-1_234_567), After: proto.Bool(true)},
},
{
name: "skip fixed32",
skip: 9,
message: &testmsg.Scalar{F32: proto.Uint32(1_234_567), After: proto.Bool(true)},
},
{
name: "skip fixed64",
skip: 10,
message: &testmsg.Scalar{F64: proto.Uint64(1_234_567), After: proto.Bool(true)},
},
{
name: "skip sfixed32",
skip: 11,
message: &testmsg.Scalar{Sf32: proto.Int32(1_234_567), After: proto.Bool(true)},
},
{
name: "skip sfixed64",
skip: 12,
message: &testmsg.Scalar{Sf64: proto.Int64(-1_234_567), After: proto.Bool(true)},
},
{
name: "skip bool",
skip: 13,
message: &testmsg.Scalar{Bool: proto.Bool(false), After: proto.Bool(true)},
},
{
name: "skip string",
skip: 14,
message: &testmsg.Scalar{
Str: proto.String("abcdefghij qwerty"),
After: proto.Bool(true),
},
},
{
name: "skip bytes",
skip: 15,
message: &testmsg.Scalar{
Byte: make([]byte, 10000),
After: proto.Bool(true),
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data, err := proto.Marshal(tc.message)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
r := &testmsg.Scalar{}
err = proto.Unmarshal(data, r)
if err != nil {
t.Fatalf("unable to unmarshal: %v", err)
}
v := decodeScalar(t, data, tc.skip)
compare(t, v, &testmsg.Scalar{After: proto.Bool(true)})
})
}
}
func TestMessage_Varint32(t *testing.T) {
t.Run("overflow", func(t *testing.T) {
msg := New([]byte{230, 230, 230, 230, 230, 230})
_, err := msg.Varint32()
if err != ErrIntOverflow {
t.Errorf("wrong error: %v", err)
}
})
t.Run("end of input", func(t *testing.T) {
msg := New([]byte{230, 230})
_, err := msg.Varint32()
if err != io.ErrUnexpectedEOF {
t.Errorf("wrong error: %v", err)
}
})
}
func TestMessage_Varint64(t *testing.T) {
t.Run("overflow", func(t *testing.T) {
msg := New([]byte{230, 230, 230, 230, 230, 230, 230, 230, 230, 230})
_, err := msg.Varint64()
if err != ErrIntOverflow {
t.Errorf("wrong error: %v", err)
}
})
t.Run("end of input", func(t *testing.T) {
msg := New([]byte{230, 230})
_, err := msg.Varint64()
if err != io.ErrUnexpectedEOF {
t.Errorf("wrong error: %v", err)
}
})
}
func TestMessage_Int32(t *testing.T) {
// tests reading an int32 field as an int64
message := &testmsg.Scalar{
I32: proto.Int32(1_234_567),
}
data, err := proto.Marshal(message)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
r := &testmsg.Scalar{}
err = proto.Unmarshal(data, r)
if err != nil {
t.Fatalf("unable to unmarshal: %v", err)
}
msg := New(data)
var val int64
for msg.Next() {
if msg.FieldNumber() == 3 {
v, err := msg.Int64()
if err != nil {
t.Fatalf("unable to read: %v", err)
}
val = v
} else {
msg.Skip()
}
}
if val != 1_234_567 {
t.Errorf("incorrect value: %d != %d", val, 1_234_567)
}
}
func decodeScalar(t testing.TB, data []byte, skip int) *testmsg.Scalar {
msg := New(data)
s := &testmsg.Scalar{}
for msg.Next() {
if msg.FieldNumber() == skip {
msg.Skip()
continue
}
switch msg.FieldNumber() {
case 1:
v, err := msg.Float()
if err != nil {
t.Fatalf("unable to read float: %v", err)
}
s.Flt = &v
case 2:
v, err := msg.Double()
if err != nil {
t.Fatalf("unable to read double: %v", err)
}
s.Dbl = &v
case 3:
v, err := msg.Int32()
if err != nil {
t.Fatalf("unable to read int32: %v", err)
}
s.I32 = &v
case 4:
v, err := msg.Int64()
if err != nil {
t.Fatalf("unable to read int64: %v", err)
}
s.I64 = &v
case 5:
v, err := msg.Uint32()
if err != nil {
t.Fatalf("unable to read uint32: %v", err)
}
s.U32 = &v
case 6:
v, err := msg.Uint64()
if err != nil {
t.Fatalf("unable to read uint64: %v", err)
}
s.U64 = &v
case 7:
v, err := msg.Sint32()
if err != nil {
t.Fatalf("unable to read sint32: %v", err)
}
s.S32 = &v
case 8:
v, err := msg.Sint64()
if err != nil {
t.Fatalf("unable to read sint64: %v", err)
}
s.S64 = &v
case 9:
v, err := msg.Fixed32()
if err != nil {
t.Fatalf("unable to read fixed32: %v", err)
}
s.F32 = &v
case 10:
v, err := msg.Fixed64()
if err != nil {
t.Fatalf("unable to read fixed64: %v", err)
}
s.F64 = &v
case 11:
v, err := msg.Sfixed32()
if err != nil {
t.Fatalf("unable to read sfixed32: %v", err)
}
s.Sf32 = &v
case 12:
v, err := msg.Sfixed64()
if err != nil {
t.Fatalf("unable to read sfixed64: %v", err)
}
s.Sf64 = &v
case 13:
v, err := msg.Bool()
if err != nil {
t.Fatalf("unable to read bool: %v", err)
}
s.Bool = &v
case 14:
v, err := msg.String()
if err != nil {
t.Fatalf("unable to read string: %v", err)
}
s.Str = &v
case 15:
v, err := msg.Bytes()
if err != nil {
t.Fatalf("unable to read bytes: %v", err)
}
s.Byte = v
case 32:
v, err := msg.Bool()
if err != nil {
t.Fatalf("unable to read after bool: %v", err)
}
s.After = &v
default:
msg.Skip()
}
}
if err := msg.Err(); err != nil {
t.Fatalf("scanning error: %v", err)
}
return s
}
func compare(t *testing.T, v, expected interface{}) {
t.Helper()
// the private fields mess with reflect.DeepEqual so json marshalling.
vd, err := json.Marshal(v)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
ed, err := json.Marshal(expected)
if err != nil {
t.Fatalf("unable to marshal: %v", err)
}
if !bytes.Equal(vd, ed) {
t.Logf("%v", string(vd))
t.Logf("%v", string(ed))
t.Errorf("results not equal")
}
}
var bscalar = &testmsg.Scalar{
Flt: proto.Float32(1_234_567),
Dbl: proto.Float64(1_234_567),
I32: proto.Int32(1_234_567),
I64: proto.Int64(1_234_567),
U32: proto.Uint32(1_234_567),
U64: proto.Uint64(1_234_567),
S32: proto.Int32(1_234_567),
S64: proto.Int64(1_234_567),
F32: proto.Uint32(1_234_567),
F64: proto.Uint64(1_234_567),
Sf32: proto.Int32(1_234_567),
Sf64: proto.Int64(1_234_567),
}
func BenchmarkScalar_standard(b *testing.B) {
data, err := proto.Marshal(bscalar)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := &testmsg.Scalar{}
err = proto.Unmarshal(data, r)
if err != nil {
b.Fatalf("unable to unmarshal: %v", err)
}
}
}
func BenchmarkScalar_protoscan(b *testing.B) {
data, err := proto.Marshal(bscalar)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
decodeScalar(b, data, 0)
}
}
func BenchmarkVarint32(b *testing.B) {
m := New([]byte{200, 199, 198, 6, 0, 0, 0, 0})
// test it out
v, err := m.Varint32()
if err != nil {
b.Fatal(err)
}
if v != 13738952 {
b.Fatalf("incorrect value %v != 13738952", v)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Index = 0
_, err := m.Varint32()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkInt64(b *testing.B) {
m := New([]byte{200, 199, 198, 6, 0, 0, 0, 0})
// test it out
v, err := m.Int64()
if err != nil {
b.Fatal(err)
}
if v != 13738952 {
b.Fatalf("incorrect value %v != 13738952", v)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Index = 0
_, err := m.Int64()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkBool(b *testing.B) {
m := New([]byte{1, 0, 1, 0, 0, 0, 0, 0})
// test it out
v, err := m.Bool()
if err != nil {
b.Fatal(err)
}
if !v {
b.Fatalf("incorrect bool")
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Index = 0
_, err := m.Bool()
if err != nil {
b.Fatal(err)
}
}
}