-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsvreader_test.go
1513 lines (1362 loc) · 36.5 KB
/
dsvreader_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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dsvreader
import (
"bytes"
"fmt"
"io"
"math"
"math/rand"
"strconv"
"strings"
"testing"
)
func TestReaderSkipCol(t *testing.T) {
b := bytes.NewBufferString("foo\tbar2\t42\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true")
}
r.SkipCol()
r.SkipCol()
bb := r.Bytes()
if string(bb) != "42" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "42")
}
if r.HasCols() {
t.Fatalf("HasCols must return false")
}
if err := r.Error(); err != nil {
t.Fatalf("unexpected error: %s", err)
}
// This must fail, since there are no more columns
r.SkipCol()
err := r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
if errS := err.Error(); !strings.Contains(errS, "no more columns") {
t.Fatalf("unexpected error: %q; must contain `no more columns`", err)
}
}
func TestReaderHasCols(t *testing.T) {
b := bytes.NewBufferString("foo\t\n\n")
r := NewTSV(b)
if r.HasCols() {
t.Fatalf("HasCols must return false before calling Next")
}
if !r.Next() {
t.Fatalf("Next must return true")
}
bb := r.Bytes()
if string(bb) != "foo" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "foo")
}
if !r.HasCols() {
t.Fatalf("HasCols must return true")
}
bb = r.Bytes()
if len(bb) > 0 {
t.Fatalf("unexpected non-empty bytes: %q", bb)
}
if r.HasCols() {
t.Fatalf("HasCols must return false")
}
// An empty row is treated as it has a single empty column
if !r.Next() {
t.Fatalf("Next must return true")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
if r.HasCols() {
t.Fatalf("HasCols must return false")
}
// No more rows
if r.Next() {
t.Fatalf("Next must return false")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
if r.HasCols() {
t.Fatalf("HasCols must return false")
}
}
func TestReaderResetError(t *testing.T) {
b := bytes.NewBufferString("foo\tbar\n\nbaz\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true")
}
bb := r.Bytes()
if string(bb) != "foo" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "foo")
}
bb = r.Bytes()
if string(bb) != "bar" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "bar")
}
// Attempt to read the second (empty) row
if !r.Next() {
t.Fatalf("Next must return true")
}
bb = r.Bytes()
if len(bb) != 0 {
t.Fatalf("unexpected non-empty bytes: %q", bb)
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
bb = r.Bytes()
if len(bb) != 0 {
t.Fatalf("unexpected non-empty bytes: %q", bb)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}
errS := err.Error()
if !strings.Contains(errS, "no more columns") {
t.Fatalf("unexpected error: %s. Expecting %q", errS, "no more columns")
}
r.ResetError()
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
// Read the last error
if !r.Next() {
t.Fatalf("Next must return true")
}
bb = r.Bytes()
if string(bb) != "baz" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "baz")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
if r.Next() {
t.Fatalf("Next must return false")
}
}
func TestReaderEmpty(t *testing.T) {
b := bytes.NewBufferString("")
r := NewTSV(b)
if r.Next() {
t.Fatalf("Next must return false on empty data")
}
err := r.Error()
if err != nil {
t.Fatalf("unexpected error after reading empty data: %s", err)
}
// Make sure r.Next() returns false on subsequent calls.
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false at the end of data")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error at the end of data: %s", err)
}
}
}
func TestReaderNoNext(t *testing.T) {
b := bytes.NewBufferString("aaa\n")
r := NewTSV(b)
n := r.Int()
if n != 0 {
t.Fatalf("unexpected non-zero int: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS := err.Error()
if !strings.Contains(errS, "missing Next call") {
t.Fatalf("unexpected error: %s. Must contains %q", errS, "missing Next call")
}
}
func TestReaderEmptyCol(t *testing.T) {
b := bytes.NewBufferString("\t\tfoobar\t\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
for i := 0; i < 4; i++ {
bb := r.Bytes()
if i == 2 {
if string(bb) != "foobar" {
t.Fatalf("unexpected bytes on col #%d: %q. Expecting %q", i+1, bb, "foobar")
}
} else if len(bb) != 0 {
t.Fatalf("unexpected non-empty bytes on col #%d: %q", i+1, bb)
}
if r.Error() != nil {
t.Fatalf("unexpected error on col #%d: %s", i+1, r.Error())
}
}
}
func TestReaderNoNewline(t *testing.T) {
testReaderNoNewline(t, "foobar")
testReaderNoNewline(t, "foo\t")
testReaderNoNewline(t, "\t")
testReaderNoNewline(t, "\tfoo\t\tbar")
testReaderNoNewline(t, "\tfoo")
testReaderNoNewline(t, "\tfoo\t")
testReaderNoNewline(t, "foo\tbar")
testReaderNoNewline(t, "foo\x00bar")
testReaderNoNewline(t, "\x00")
}
func testReaderNoNewline(t *testing.T, s string) {
t.Helper()
b := bytes.NewBufferString(s)
r := NewTSV(b)
if r.Next() {
t.Fatalf("Next must return false when no newline; s: %q", s)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting error when no newline; s: %q", s)
}
errS := err.Error()
if !strings.Contains(errS, "cannot find newline") {
t.Fatalf("unexpected error: %s; must contain %q", s, "cannot find newline")
}
// Make sure r.Next() returns false on subsequent calls.
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false after error; s: %q", s)
}
err1 := r.Error()
if err1 != err {
t.Fatalf("unexpected error: %v. Expecting %s; s: %q", err1, err, s)
}
}
}
func TestReaderReset(t *testing.T) {
var r Reader
for i := 0; i < 10; i++ {
s := fmt.Sprintf("foobar %d\n", i)
b := bytes.NewBufferString(s)
r.Reset(b)
if !r.Next() {
t.Fatalf("Next must return true for TSV %q", s)
}
if r.Error() != nil {
t.Fatalf("unexpected error before reading TSV %q: %s", s, r.Error())
}
bb := r.Bytes()
if string(bb) != s[:len(s)-1] {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, s[:len(s)-1])
}
if r.Error() != nil {
t.Fatalf("unexpected error after reading TSV %q: %s", s, r.Error())
}
}
}
func TestReaderSingleRowBytesCol(t *testing.T) {
expectedS := "foobar"
b := bytes.NewBufferString(fmt.Sprintf("%s\n", expectedS))
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true on the first line. err: %v", r.Error())
}
err := r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first line: %s", err)
}
bb := r.Bytes()
if string(bb) != expectedS {
t.Fatalf("unexpected bytes read: %q. Expecting %q", bb, expectedS)
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first col: %s", err)
}
// Attempt to read the next col, which doesn't exist.
if r.Next() {
t.Fatalf("Next must return false on a single row")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error at the end of data: %s", err)
}
// Make sure r.Next() returns false on subsequent calls.
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false at the end of data")
}
if err != nil {
t.Fatalf("unexpected error at the end of data: %s", err)
}
}
}
func TestReaderSingleRowIntCol(t *testing.T) {
expectedN := 12346
b := bytes.NewBufferString(fmt.Sprintf("%d\n", expectedN))
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true on the first line. err: %v", r.Error())
}
err := r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first line: %s", err)
}
n := r.Int()
if n != expectedN {
t.Fatalf("unexpected int read: %d. Expecting %d", n, expectedN)
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first col: %s", err)
}
// Attempt to read the next col, which doesn't exist.
if r.Next() {
t.Fatalf("Next must return false on a single row")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error at the end of data: %s", err)
}
// Make sure r.Next() returns false on subsequent calls.
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false at the end of data")
}
if err != nil {
t.Fatalf("unexpected error at the end of data: %s", err)
}
}
}
func TestReaderInvalidColType(t *testing.T) {
b := bytes.NewBufferString("foobar\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true on the first line. err: %v", r.Error())
}
err := r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first line: %s", err)
}
n := r.Int()
if n != 0 {
t.Fatalf("unexpected n: %d. Expecting 0", n)
}
err = r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS := err.Error()
if !strings.Contains(errS, "cannot parse") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "cannot parse")
}
}
func TestReaderNoMoreCols(t *testing.T) {
b := bytes.NewBufferString("aaa\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true on the first line. err: %v", r.Error())
}
err := r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first line: %s", err)
}
bb := r.Bytes()
if string(bb) != "aaa" {
t.Fatalf("unexpected bytes value: %q. Expecting %q", bb, "aaa")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first col: %s", err)
}
// attempt to read more col
for i := 0; i < 10; i++ {
bb := r.Bytes()
if len(bb) > 0 {
t.Fatalf("unexpected non-empty bytes: %q", bb)
}
err = r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS := err.Error()
if !strings.Contains(errS, "no more columns") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "no more columns")
}
n := r.Int()
if n != 0 {
t.Fatalf("unexpected non-zero int: %d", n)
}
err = r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS = err.Error()
if !strings.Contains(errS, "no more columns") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "no more columns")
}
}
// atempt to read more rows
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false")
}
err = r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS := err.Error()
if !strings.Contains(errS, "no more columns") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "no more columns")
}
}
}
func TestReaderSingleRowMultiCols(t *testing.T) {
b := bytes.NewBufferString("foobar\t-42\t3\tbaz\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true on the first line. err: %v", r.Error())
}
err := r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first line: %s", err)
}
bb := r.Bytes()
if string(bb) != "foobar" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "foobar")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the first col: %s", err)
}
n := r.Int()
if n != -42 {
t.Fatalf("unexpected int: %d. Expecting %d", n, -42)
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the second col: %s", err)
}
n = r.Int()
if n != 3 {
t.Fatalf("unexpected int: %d. Expecting %d", n, 3)
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the third col: %s", err)
}
bb = r.Bytes()
if string(bb) != "baz" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "baz")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error after reading the fourth col: %s", err)
}
// Attempt to read more rows
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false")
}
err = r.Error()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
}
}
func TestReaderUnreadColsSingle(t *testing.T) {
b := bytes.NewBufferString("foo\tbar\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
bb := r.Bytes()
if string(bb) != "foo" {
t.Fatalf("unexpected bytes: %q. Expecting %q", bb, "foo")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
// Attempt to read next row while the current row isnt read till the end
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false, because the previous row has unread columns")
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS := err.Error()
if !strings.Contains(errS, "unread columns") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "unread columns")
}
}
}
func TestReaderUnreadColsAll(t *testing.T) {
b := bytes.NewBufferString("foo\tbar\n")
r := NewTSV(b)
if !r.Next() {
t.Fatalf("Next must return true")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
// Attempt to read next row while the current row isnt read till the end
for i := 0; i < 10; i++ {
if r.Next() {
t.Fatalf("Next must return false, because the previous row has unread columns")
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-nil error")
}
errS := err.Error()
if !strings.Contains(errS, "unread columns") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "unread columns")
}
}
}
func TestReaderMultiRowsBytesCol(t *testing.T) {
testReaderMultiRowsBytesCol(t, 2)
testReaderMultiRowsBytesCol(t, 10)
testReaderMultiRowsBytesCol(t, 100)
testReaderMultiRowsBytesCol(t, 1000)
testReaderMultiRowsBytesCol(t, 10000)
}
func testReaderMultiRowsBytesCol(t *testing.T, rows int) {
t.Helper()
var expected []string
var ss []string
for i := 0; i < rows; i++ {
s := fmt.Sprintf("foo%d bar", rand.Int())
expected = append(expected, s)
ss = append(ss, fmt.Sprintf("%s\n", s))
}
b := bytes.NewBufferString(strings.Join(ss, ""))
r := NewTSV(b)
for i, expectedS := range expected {
if !r.Next() {
t.Fatalf("Next must return true when reading %q at row #%d", expectedS, i+1)
}
if r.Error() != nil {
t.Fatalf("unexpected error when reading %q at row #%d: %s", expectedS, i+1, r.Error())
}
bb := r.Bytes()
if string(bb) != expectedS {
t.Fatalf("unexpected bytes at row #%d: %q. Expecting %q", i+1, bb, expectedS)
}
if r.Error() != nil {
t.Fatalf("unexpected error after reading %q at row #%d: %s", expectedS, i+1, r.Error())
}
}
if r.Next() {
t.Fatalf("Next must return false")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderMultiRowsIntCol(t *testing.T) {
testReaderMultiRowsIntCol(t, 2)
testReaderMultiRowsIntCol(t, 10)
testReaderMultiRowsIntCol(t, 100)
testReaderMultiRowsIntCol(t, 1000)
testReaderMultiRowsIntCol(t, 10000)
}
func testReaderMultiRowsIntCol(t *testing.T, rows int) {
t.Helper()
var expected []int
var ss []string
for i := 0; i < rows; i++ {
n := rand.Int()
if rand.Intn(2) == 0 {
n = -n
}
expected = append(expected, n)
ss = append(ss, fmt.Sprintf("%d\n", n))
}
b := bytes.NewBufferString(strings.Join(ss, ""))
r := NewTSV(b)
for i, expectedN := range expected {
if !r.Next() {
t.Fatalf("Next must return true when reading %d at row #%d", expectedN, i+1)
}
if r.Error() != nil {
t.Fatalf("unexpected error when reading %d at row #%d: %s", expectedN, i+1, r.Error())
}
n := r.Int()
if n != expectedN {
t.Fatalf("unexpected int at row #%d: %d. Expecting %d", i+1, n, expectedN)
}
if r.Error() != nil {
t.Fatalf("unexpected error after reading %d at row #%d: %s", expectedN, i+1, r.Error())
}
}
if r.Next() {
t.Fatalf("Next must return false")
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderMultiRowsMultiCols(t *testing.T) {
testReaderMultiRowsMultiCols(t, 2, 2)
testReaderMultiRowsMultiCols(t, 10, 5)
testReaderMultiRowsMultiCols(t, 100, 10)
testReaderMultiRowsMultiCols(t, 1000, 100)
testReaderMultiRowsMultiCols(t, 10000, 3)
testReaderMultiRowsMultiCols(t, 3, 500)
}
func testReaderMultiRowsMultiCols(t *testing.T, rows, cols int) {
t.Helper()
var expected [][]string
var ss []string
for i := 0; i < rows; i++ {
var rowS []string
for j := 0; j < cols; j++ {
s := fmt.Sprintf("foobar%d", j+i*cols)
rowS = append(rowS, s)
}
expected = append(expected, rowS)
ss = append(ss, strings.Join(rowS, "\t")+"\n")
}
b := bytes.NewBufferString(strings.Join(ss, ""))
r := NewTSV(b)
testReaderMultiRowsCols(t, r, expected)
}
func TestReaderSlowSource(t *testing.T) {
testReaderSlowSource(t, 1, 10000)
testReaderSlowSource(t, 10, 1000)
testReaderSlowSource(t, 100, 100)
testReaderSlowSource(t, 1000, 10)
testReaderSlowSource(t, 10000, 1)
}
func testReaderSlowSource(t *testing.T, rows, cols int) {
t.Helper()
var expected [][]string
var ss []string
for i := 0; i < rows; i++ {
var rowS []string
for j := 0; j < cols; j++ {
s := fmt.Sprintf("foo тест %d", j+i*cols)
rowS = append(rowS, s)
}
expected = append(expected, rowS)
ss = append(ss, strings.Join(rowS, "\t")+"\n")
}
b := &slowSource{
s: []byte(strings.Join(ss, "")),
}
r := NewTSV(b)
testReaderMultiRowsCols(t, r, expected)
}
func testReaderMultiRowsCols(t *testing.T, r *Reader, expected [][]string) {
t.Helper()
for i, rowS := range expected {
if !r.Next() {
t.Fatalf("Next must return true when reading row #%d", i+1)
}
if r.Error() != nil {
t.Fatalf("unexpected error when reading row #%d: %s", i+1, r.Error())
}
if r.row != i+1 {
t.Fatalf("unexpected row number: %d. Expecting %d", r.row, i+1)
}
for j, expectedS := range rowS {
bb := r.Bytes()
if string(bb) != expectedS {
t.Fatalf("unexpected bytes at col #%d, row #%d: %q. Expecting %q", j+1, i+1, bb, expectedS)
}
if r.Error() != nil {
t.Fatalf("unexpected error after reading col #%d, row #%d: %s", j+1, i+1, r.Error())
}
if r.row != i+1 {
t.Fatalf("unexpected row number: %d. Expecting %d", r.row, i+1)
}
if r.col != j+1 {
t.Fatalf("unexpected col number on row #%d: %d. Expecting %d", i+1, r.col, j+1)
}
}
}
}
// slowSource returns data by small chunks.
type slowSource struct {
s []byte
}
func (ss *slowSource) Read(p []byte) (int, error) {
if len(ss.s) == 0 {
return 0, io.EOF
}
chunkSize := rand.Intn(10) + 1
if chunkSize > len(ss.s) {
chunkSize = len(ss.s)
}
n := copy(p, ss.s[:chunkSize])
ss.s = ss.s[n:]
return n, nil
}
func TestReaderUintSuccess(t *testing.T) {
testReaderUintSuccess(t, (1<<strconv.IntSize)-1)
testReaderUintSuccess(t, 0)
testReaderUintSuccess(t, 10)
}
func testReaderUintSuccess(t *testing.T, expectedN uint) {
t.Helper()
b := bytes.NewBufferString(fmt.Sprintf("%d\n", expectedN))
r := NewTSV(b)
r.Next()
n := r.Uint()
if n != expectedN {
t.Fatalf("unexpected uint: %d. Expecting %d", n, expectedN)
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderUintNegative(t *testing.T) {
b := bytes.NewBufferString("-123\n")
r := NewTSV(b)
r.Next()
n := r.Uint()
if n != 0 {
t.Fatalf("unexpected non-zero uint: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}
errS := err.Error()
if !strings.Contains(errS, "invalid syntax") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "invalid syntax")
}
}
func TestReaderInt32Success(t *testing.T) {
testReaderInt32Success(t, math.MaxInt32)
testReaderInt32Success(t, math.MinInt32)
testReaderInt32Success(t, 0)
testReaderInt32Success(t, 10)
testReaderInt32Success(t, -10)
}
func testReaderInt32Success(t *testing.T, expectedN int32) {
t.Helper()
b := bytes.NewBufferString(fmt.Sprintf("%d\n", expectedN))
r := NewTSV(b)
r.Next()
n := r.Int32()
if n != expectedN {
t.Fatalf("unexpected int32: %d. Expecting %d", n, expectedN)
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderInt32TooBig(t *testing.T) {
testReaderInt32TooBig(t, fmt.Sprintf("123%d", math.MaxInt32))
testReaderInt32TooBig(t, fmt.Sprintf("-123%d", math.MaxInt32))
}
func testReaderInt32TooBig(t *testing.T, s string) {
t.Helper()
b := bytes.NewBufferString(s + "\n")
r := NewTSV(b)
r.Next()
n := r.Int32()
if n != 0 {
t.Fatalf("unexpected non-zero int32: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}
errS := err.Error()
if !strings.Contains(errS, "out of range") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "out of range")
}
}
func TestReaderUint32Success(t *testing.T) {
testReaderUint32Success(t, math.MaxUint32)
testReaderUint32Success(t, 0)
testReaderUint32Success(t, 10)
}
func testReaderUint32Success(t *testing.T, expectedN uint32) {
t.Helper()
b := bytes.NewBufferString(fmt.Sprintf("%d\n", expectedN))
r := NewTSV(b)
r.Next()
n := r.Uint32()
if n != expectedN {
t.Fatalf("unexpected uint32: %d. Expecting %d", n, expectedN)
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderUint32Negative(t *testing.T) {
b := bytes.NewBufferString("-123\n")
r := NewTSV(b)
r.Next()
n := r.Uint32()
if n != 0 {
t.Fatalf("unexpected non-zero uint32: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}
errS := err.Error()
if !strings.Contains(errS, "invalid syntax") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "invalid syntax")
}
}
func TestReaderUint32TooBig(t *testing.T) {
b := bytes.NewBufferString(fmt.Sprintf("123%d\n", math.MaxUint32))
r := NewTSV(b)
r.Next()
n := r.Uint32()
if n != 0 {
t.Fatalf("unexpected non-zero uint32: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}
errS := err.Error()
if !strings.Contains(errS, "out of range") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "out of range")
}
}
func TestReaderInt16Success(t *testing.T) {
testReaderInt16Success(t, math.MaxInt16)
testReaderInt16Success(t, math.MinInt16)
testReaderInt16Success(t, 0)
testReaderInt16Success(t, 10)
testReaderInt16Success(t, -10)
}
func testReaderInt16Success(t *testing.T, expectedN int16) {
t.Helper()
b := bytes.NewBufferString(fmt.Sprintf("%d\n", expectedN))
r := NewTSV(b)
r.Next()
n := r.Int16()
if n != expectedN {
t.Fatalf("unexpected int16: %d. Expecting %d", n, expectedN)
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderInt16TooBig(t *testing.T) {
testReaderInt16TooBig(t, fmt.Sprintf("1%d", math.MaxInt16))
testReaderInt16TooBig(t, fmt.Sprintf("-1%d", math.MaxInt16))
}
func testReaderInt16TooBig(t *testing.T, s string) {
t.Helper()
b := bytes.NewBufferString(s + "\n")
r := NewTSV(b)
r.Next()
n := r.Int16()
if n != 0 {
t.Fatalf("unexpected non-zero int16: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}
errS := err.Error()
if !strings.Contains(errS, "out of range") {
t.Fatalf("unexpected error: %s. Must contain %q", err, "out of range")
}
}
func TestReaderUint16Success(t *testing.T) {
testReaderUint16Success(t, math.MaxUint16)
testReaderUint16Success(t, 0)
testReaderUint16Success(t, 10)
}
func testReaderUint16Success(t *testing.T, expectedN uint16) {
t.Helper()
b := bytes.NewBufferString(fmt.Sprintf("%d\n", expectedN))
r := NewTSV(b)
r.Next()
n := r.Uint16()
if n != expectedN {
t.Fatalf("unexpected uint16: %d. Expecting %d", n, expectedN)
}
if r.Error() != nil {
t.Fatalf("unexpected error: %s", r.Error())
}
}
func TestReaderUint16Negative(t *testing.T) {
b := bytes.NewBufferString("-123\n")
r := NewTSV(b)
r.Next()
n := r.Uint16()
if n != 0 {
t.Fatalf("unexpected non-zero uint16: %d", n)
}
err := r.Error()
if err == nil {
t.Fatalf("expecting non-zero error")
}