-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader_test.go
More file actions
1140 lines (1039 loc) · 27 KB
/
reader_test.go
File metadata and controls
1140 lines (1039 loc) · 27 KB
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
//go:build goexperiment.simd && amd64
package simdcsv
import (
"encoding/csv"
"io"
"reflect"
"strings"
"testing"
)
// =============================================================================
// TestRead Tests - Basic CSV Parsing
// =============================================================================
// TestRead_Simple tests basic CSV parsing with simple unquoted fields.
func TestRead_Simple(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
wantErr bool
}{
{
name: "single row single field",
input: "hello\n",
want: [][]string{{"hello"}},
},
{
name: "single row multiple fields",
input: "a,b,c\n",
want: [][]string{{"a", "b", "c"}},
},
{
name: "multiple rows",
input: "a,b,c\n1,2,3\nx,y,z\n",
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}, {"x", "y", "z"}},
},
{
name: "numeric fields",
input: "1,2,3\n4,5,6\n",
want: [][]string{{"1", "2", "3"}, {"4", "5", "6"}},
},
{
name: "mixed content",
input: "name,age,city\nAlice,30,Tokyo\nBob,25,Osaka\n",
want: [][]string{{"name", "age", "city"}, {"Alice", "30", "Tokyo"}, {"Bob", "25", "Osaka"}},
},
{
name: "single column",
input: "a\nb\nc\n",
want: [][]string{{"a"}, {"b"}, {"c"}},
},
{
name: "whitespace in fields",
input: "hello world,foo bar\n",
want: [][]string{{"hello world", "foo bar"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_Quoted tests parsing of quoted fields.
func TestRead_Quoted(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "simple quoted field",
input: `"hello",world` + "\n",
want: [][]string{{"hello", "world"}},
},
{
name: "all fields quoted",
input: `"a","b","c"` + "\n",
want: [][]string{{"a", "b", "c"}},
},
{
name: "quoted field with comma",
input: `"hello,world",foo` + "\n",
want: [][]string{{"hello,world", "foo"}},
},
{
name: "quoted empty field",
input: `"",b,c` + "\n",
want: [][]string{{"", "b", "c"}},
},
{
name: "mixed quoted and unquoted",
input: `a,"b",c,"d"` + "\n",
want: [][]string{{"a", "b", "c", "d"}},
},
{
name: "quoted field with spaces",
input: `" spaces ",normal` + "\n",
want: [][]string{{" spaces ", "normal"}},
},
{
name: "quoted field at end",
input: `a,b,"c"` + "\n",
want: [][]string{{"a", "b", "c"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_DoubleQuote tests parsing of escaped double quotes ("").
func TestRead_DoubleQuote(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "simple double quote escape",
input: `"he said ""hello"""` + "\n",
want: [][]string{{`he said "hello"`}},
},
{
name: "double quote at start",
input: `"""hello"""` + "\n",
want: [][]string{{`"hello"`}},
},
{
name: "double quote at end",
input: `"hello"""` + "\n",
want: [][]string{{`hello"`}},
},
{
name: "multiple double quotes",
input: `"a""b""c"` + "\n",
want: [][]string{{`a"b"c`}},
},
{
name: "just double quote",
input: `""""` + "\n",
want: [][]string{{`"`}},
},
{
name: "double quote with other fields",
input: `a,"b""c",d` + "\n",
want: [][]string{{"a", `b"c`, "d"}},
},
{
name: "consecutive double quotes",
input: `""""""` + "\n",
want: [][]string{{`""`}},
},
{
name: "double quote in middle of text",
input: `"foo""bar"` + "\n",
want: [][]string{{`foo"bar`}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_Multiline tests parsing of fields containing newlines.
func TestRead_Multiline(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "newline in quoted field",
input: "\"hello\nworld\",foo\n",
want: [][]string{{"hello\nworld", "foo"}},
},
{
name: "multiple newlines in field",
input: "\"line1\nline2\nline3\",b\n",
want: [][]string{{"line1\nline2\nline3", "b"}},
},
{
name: "newline at start of field",
input: "\"\nhello\",b\n",
want: [][]string{{"\nhello", "b"}},
},
{
name: "newline at end of field",
input: "\"hello\n\",b\n",
want: [][]string{{"hello\n", "b"}},
},
{
name: "multiple fields with newlines",
input: "\"a\nb\",\"c\nd\"\n",
want: [][]string{{"a\nb", "c\nd"}},
},
{
name: "newline and comma in field",
input: "\"hello,\nworld\",foo\n",
want: [][]string{{"hello,\nworld", "foo"}},
},
{
name: "newline and double quote in field",
input: "\"hello\n\"\"world\"\"\",foo\n",
want: [][]string{{"hello\n\"world\"", "foo"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_CRLF tests parsing with Windows-style line endings.
func TestRead_CRLF(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "simple CRLF",
input: "a,b,c\r\n1,2,3\r\n",
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "CRLF at end only",
input: "a,b,c\r\n",
want: [][]string{{"a", "b", "c"}},
},
{
name: "mixed LF and CRLF",
input: "a,b,c\n1,2,3\r\n",
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "CRLF in quoted field",
input: "\"hello\r\nworld\",foo\r\n",
want: [][]string{{"hello\r\nworld", "foo"}},
},
{
name: "standalone CR in quoted field",
input: "\"hello\rworld\",foo\n",
want: [][]string{{"hello\rworld", "foo"}},
},
{
name: "multiple CRLF rows",
input: "a,b\r\nc,d\r\ne,f\r\n",
want: [][]string{{"a", "b"}, {"c", "d"}, {"e", "f"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_Empty tests parsing of empty input.
func TestRead_Empty(t *testing.T) {
tests := []struct {
name string
input string
wantEOF bool
}{
{
name: "completely empty",
input: "",
wantEOF: true,
},
{
name: "just whitespace newlines",
input: "\n\n\n",
wantEOF: false, // encoding/csv returns empty records for blank lines with FieldsPerRecord=-1
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test with encoding/csv first
stdReader := csv.NewReader(strings.NewReader(tt.input))
stdReader.FieldsPerRecord = -1
_, stdErr := stdReader.Read()
if tt.wantEOF {
if stdErr != io.EOF {
t.Errorf("encoding/csv: expected io.EOF, got %v", stdErr)
}
}
// Our implementation should match
simdReader := NewReader(strings.NewReader(tt.input))
simdReader.FieldsPerRecord = -1
_, simdErr := simdReader.Read()
if tt.wantEOF {
if simdErr != io.EOF {
t.Errorf("simdcsv: expected io.EOF, got %v", simdErr)
}
}
if (stdErr == io.EOF) != (simdErr == io.EOF) {
t.Errorf("EOF behavior mismatch: encoding/csv=%v, simdcsv=%v", stdErr, simdErr)
}
})
}
}
// TestRead_EmptyFields tests parsing of empty fields between commas.
func TestRead_EmptyFields(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "empty field at start",
input: ",b,c\n",
want: [][]string{{"", "b", "c"}},
},
{
name: "empty field in middle",
input: "a,,c\n",
want: [][]string{{"a", "", "c"}},
},
{
name: "empty field at end",
input: "a,b,\n",
want: [][]string{{"a", "b", ""}},
},
{
name: "all empty fields",
input: ",,\n",
want: [][]string{{"", "", ""}},
},
{
name: "single empty field",
input: "\n",
want: [][]string{{""}},
},
{
name: "multiple consecutive empty fields",
input: "a,,,b\n",
want: [][]string{{"a", "", "", "b"}},
},
{
name: "empty quoted field",
input: `"",b,c` + "\n",
want: [][]string{{"", "b", "c"}},
},
{
name: "multiple rows with empty fields",
input: ",b\na,\n",
want: [][]string{{"", "b"}, {"a", ""}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_TrailingNewline tests parsing of files ending with newline.
func TestRead_TrailingNewline(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "single row with trailing newline",
input: "a,b,c\n",
want: [][]string{{"a", "b", "c"}},
},
{
name: "multiple rows with trailing newline",
input: "a,b\nc,d\n",
want: [][]string{{"a", "b"}, {"c", "d"}},
},
{
name: "trailing CRLF",
input: "a,b,c\r\n",
want: [][]string{{"a", "b", "c"}},
},
{
name: "multiple trailing newlines",
input: "a,b\n\n",
want: [][]string{{"a", "b"}, {""}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_NoTrailingNewline tests parsing of files without trailing newline.
func TestRead_NoTrailingNewline(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "single row no newline",
input: "a,b,c",
want: [][]string{{"a", "b", "c"}},
},
{
name: "multiple rows no trailing newline",
input: "a,b\nc,d",
want: [][]string{{"a", "b"}, {"c", "d"}},
},
{
name: "quoted field no trailing newline",
input: `"hello","world"`,
want: [][]string{{"hello", "world"}},
},
{
name: "single field no newline",
input: "hello",
want: [][]string{{"hello"}},
},
{
name: "empty field at end no newline",
input: "a,b,",
want: [][]string{{"a", "b", ""}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// =============================================================================
// TestReadAll Tests
// =============================================================================
// TestReadAll_Basic tests ReadAll with basic inputs.
func TestReadAll_Basic(t *testing.T) {
tests := []struct {
name string
input string
want [][]string
}{
{
name: "simple multiple rows",
input: "a,b,c\n1,2,3\nx,y,z\n",
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}, {"x", "y", "z"}},
},
{
name: "single row",
input: "a,b,c\n",
want: [][]string{{"a", "b", "c"}},
},
{
name: "empty input",
input: "",
want: nil,
},
{
name: "quoted and unquoted mixed",
input: `a,"b,c",d` + "\n" + `"e",f,"g"` + "\n",
want: [][]string{{"a", "b,c", "d"}, {"e", "f", "g"}},
},
{
name: "multiline fields",
input: "\"hello\nworld\",b\nc,d\n",
want: [][]string{{"hello\nworld", "b"}, {"c", "d"}},
},
{
name: "no trailing newline",
input: "a,b\nc,d",
want: [][]string{{"a", "b"}, {"c", "d"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test with encoding/csv
stdReader := csv.NewReader(strings.NewReader(tt.input))
stdReader.FieldsPerRecord = -1
stdRecords, stdErr := stdReader.ReadAll()
// Test with simdcsv
simdReader := NewReader(strings.NewReader(tt.input))
simdReader.FieldsPerRecord = -1
simdRecords, simdErr := simdReader.ReadAll()
if stdErr != simdErr {
// Allow nil vs nil comparison
if stdErr != nil || simdErr != nil {
t.Errorf("error mismatch: encoding/csv=%v, simdcsv=%v", stdErr, simdErr)
}
}
if !reflect.DeepEqual(stdRecords, simdRecords) {
t.Errorf("records mismatch:\nencoding/csv=%v\nsimdcsv=%v", stdRecords, simdRecords)
}
})
}
}
// TestReadAll_LargeFile tests ReadAll with larger inputs.
func TestReadAll_LargeFile(t *testing.T) {
// Generate a large CSV
var buf strings.Builder
numRows := 10000
numCols := 10
for i := 0; i < numRows; i++ {
for j := 0; j < numCols; j++ {
if j > 0 {
buf.WriteByte(',')
}
buf.WriteString("field")
}
buf.WriteByte('\n')
}
input := buf.String()
// Test with encoding/csv
stdReader := csv.NewReader(strings.NewReader(input))
stdReader.FieldsPerRecord = -1
stdRecords, stdErr := stdReader.ReadAll()
if stdErr != nil {
t.Fatalf("encoding/csv ReadAll error: %v", stdErr)
}
// Test with simdcsv
simdReader := NewReader(strings.NewReader(input))
simdReader.FieldsPerRecord = -1
simdRecords, simdErr := simdReader.ReadAll()
if simdErr != nil {
t.Fatalf("simdcsv ReadAll error: %v", simdErr)
}
if len(stdRecords) != len(simdRecords) {
t.Errorf("record count mismatch: encoding/csv=%d, simdcsv=%d", len(stdRecords), len(simdRecords))
}
// Verify first and last few records
for i := 0; i < 5; i++ {
if !reflect.DeepEqual(stdRecords[i], simdRecords[i]) {
t.Errorf("row %d mismatch:\nencoding/csv=%v\nsimdcsv=%v", i, stdRecords[i], simdRecords[i])
}
}
for i := numRows - 5; i < numRows; i++ {
if !reflect.DeepEqual(stdRecords[i], simdRecords[i]) {
t.Errorf("row %d mismatch:\nencoding/csv=%v\nsimdcsv=%v", i, stdRecords[i], simdRecords[i])
}
}
}
// TestReadAll_LargeFileWithQuotes tests ReadAll with large files containing quotes.
func TestReadAll_LargeFileWithQuotes(t *testing.T) {
var buf strings.Builder
numRows := 1000
for i := 0; i < numRows; i++ {
buf.WriteString(`"field with ""quotes""",normal,"has,comma"`)
buf.WriteByte('\n')
}
input := buf.String()
// Test with encoding/csv
stdReader := csv.NewReader(strings.NewReader(input))
stdReader.FieldsPerRecord = -1
stdRecords, stdErr := stdReader.ReadAll()
if stdErr != nil {
t.Fatalf("encoding/csv ReadAll error: %v", stdErr)
}
// Test with simdcsv
simdReader := NewReader(strings.NewReader(input))
simdReader.FieldsPerRecord = -1
simdRecords, simdErr := simdReader.ReadAll()
if simdErr != nil {
t.Fatalf("simdcsv ReadAll error: %v", simdErr)
}
if len(stdRecords) != len(simdRecords) {
t.Errorf("record count mismatch: encoding/csv=%d, simdcsv=%d", len(stdRecords), len(simdRecords))
}
for i := range stdRecords {
if !reflect.DeepEqual(stdRecords[i], simdRecords[i]) {
t.Errorf("row %d mismatch:\nencoding/csv=%v\nsimdcsv=%v", i, stdRecords[i], simdRecords[i])
}
}
}
// =============================================================================
// Reader Options Tests
// =============================================================================
// TestRead_CustomSeparator tests parsing with custom field separator.
func TestRead_CustomSeparator(t *testing.T) {
tests := []struct {
name string
input string
comma rune
want [][]string
}{
{
name: "tab separator",
input: "a\tb\tc\n1\t2\t3\n",
comma: '\t',
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "semicolon separator",
input: "a;b;c\n1;2;3\n",
comma: ';',
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "pipe separator",
input: "a|b|c\n1|2|3\n",
comma: '|',
want: [][]string{{"a", "b", "c"}, {"1", "2", "3"}},
},
{
name: "tab with quoted comma",
input: "\"a,b\"\tc\n",
comma: '\t',
want: [][]string{{"a,b", "c"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &readerOptions{comma: tt.comma}
compareWithStdlib(t, tt.input, opts)
})
}
}
// TestRead_Comment tests comment handling.
func TestRead_Comment(t *testing.T) {
tests := []struct {
name string
input string
comment rune
want [][]string
}{
{
name: "hash comment",
input: "a,b\n#comment\nc,d\n",
comment: '#',
want: [][]string{{"a", "b"}, {"c", "d"}},
},
{
name: "semicolon comment",
input: "a,b\n;comment\nc,d\n",
comment: ';',
want: [][]string{{"a", "b"}, {"c", "d"}},
},
{
name: "comment at start",
input: "#header\na,b\n",
comment: '#',
want: [][]string{{"a", "b"}},
},
{
name: "comment at end",
input: "a,b\n#footer\n",
comment: '#',
want: [][]string{{"a", "b"}},
},
{
name: "no comment char set",
input: "a,b\n#not a comment\n",
comment: 0,
want: [][]string{{"a", "b"}, {"#not a comment"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &readerOptions{comment: tt.comment}
compareWithStdlib(t, tt.input, opts)
})
}
}
// TestRead_TrimLeadingSpace tests trimming of leading whitespace.
func TestRead_TrimLeadingSpace(t *testing.T) {
tests := []struct {
name string
input string
trimLeadingSpace bool
want [][]string
}{
{
name: "trim spaces",
input: " a, b, c\n",
trimLeadingSpace: true,
want: [][]string{{"a", "b", "c"}},
},
{
name: "no trim",
input: " a, b, c\n",
trimLeadingSpace: false,
want: [][]string{{" a", " b", " c"}},
},
{
name: "trim tabs",
input: "\ta,\tb\n",
trimLeadingSpace: true,
want: [][]string{{"a", "b"}},
},
{
name: "trim before quote",
input: ` "a", "b"` + "\n",
trimLeadingSpace: true,
want: [][]string{{"a", "b"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &readerOptions{trimLeadingSpace: tt.trimLeadingSpace}
compareWithStdlib(t, tt.input, opts)
})
}
}
// TestRead_ReuseRecord tests the ReuseRecord optimization.
func TestRead_ReuseRecord(t *testing.T) {
input := "a,b,c\n1,2,3\nx,y,z\n"
// Test with encoding/csv
stdReader := csv.NewReader(strings.NewReader(input))
stdReader.ReuseRecord = true
var stdRecords [][]string
for {
record, err := stdReader.Read()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("encoding/csv error: %v", err)
}
// Must copy when ReuseRecord is true
recordCopy := make([]string, len(record))
copy(recordCopy, record)
stdRecords = append(stdRecords, recordCopy)
}
// Test with simdcsv
simdReader := NewReader(strings.NewReader(input))
simdReader.ReuseRecord = true
var simdRecords [][]string
for {
record, err := simdReader.Read()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("simdcsv error: %v", err)
}
recordCopy := make([]string, len(record))
copy(recordCopy, record)
simdRecords = append(simdRecords, recordCopy)
}
if !reflect.DeepEqual(stdRecords, simdRecords) {
t.Errorf("records mismatch:\nencoding/csv=%v\nsimdcsv=%v", stdRecords, simdRecords)
}
}
// =============================================================================
// Chunk Boundary Tests (SIMD specific edge cases)
// =============================================================================
// TestRead_ChunkBoundary tests parsing when special characters are at 64-byte boundaries.
func TestRead_ChunkBoundary(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "comma at byte 63",
input: strings.Repeat("a", 63) + "," + "b\n",
},
{
name: "comma at byte 64",
input: strings.Repeat("a", 64) + "," + "b\n",
},
{
name: "newline at byte 63",
input: strings.Repeat("a", 63) + "\nb,c\n",
},
{
name: "newline at byte 64",
input: strings.Repeat("a", 64) + "\nb,c\n",
},
{
name: "quote at byte 63",
input: strings.Repeat("a", 62) + ",\"b\"\n",
},
{
name: "quote at byte 64",
input: strings.Repeat("a", 63) + ",\"b\"\n",
},
{
name: "double quote spanning boundary",
input: strings.Repeat("a", 62) + ",\"" + strings.Repeat("b", 60) + "\"\"c\"\n",
},
{
name: "CRLF spanning boundary",
input: strings.Repeat("a", 63) + "\r\nb,c\n",
},
{
name: "CRLF at byte 64-65",
input: strings.Repeat("a", 64) + "\r\nb,c\n",
},
{
name: "field spanning multiple chunks",
input: strings.Repeat("a", 200) + ",b\n",
},
{
name: "quoted field spanning multiple chunks",
input: "\"" + strings.Repeat("a", 200) + "\",b\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWithStdlib(t, tt.input, nil)
})
}
}
// TestRead_LongFields tests parsing of very long fields.
func TestRead_LongFields(t *testing.T) {
tests := []struct {
name string
length int
}{
{name: "64 bytes", length: 64},
{name: "128 bytes", length: 128},
{name: "1KB", length: 1024},
{name: "64KB", length: 64 * 1024},
{name: "1MB", length: 1024 * 1024},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
field := strings.Repeat("x", tt.length)
input := field + ",b\n"
compareWithStdlib(t, input, nil)
})
}
}
// TestRead_LongQuotedFields tests parsing of very long quoted fields.
func TestRead_LongQuotedFields(t *testing.T) {
tests := []struct {
name string
length int
}{
{name: "64 bytes quoted", length: 64},
{name: "128 bytes quoted", length: 128},
{name: "1KB quoted", length: 1024},
{name: "64KB quoted", length: 64 * 1024},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
field := strings.Repeat("x", tt.length)
input := "\"" + field + "\",b\n"
compareWithStdlib(t, input, nil)
})
}
}
// =============================================================================
// FieldPos and InputOffset Tests
// =============================================================================
// TestFieldPos_Basic tests the FieldPos function.
func TestFieldPos_Basic(t *testing.T) {
input := "a,b,c\n1,2,3\n"
reader := NewReader(strings.NewReader(input))
reader.FieldsPerRecord = -1
// Read first record
record, err := reader.Read()
if err != nil {
t.Fatalf("Read error: %v", err)
}
if len(record) != 3 {
t.Fatalf("Expected 3 fields, got %d", len(record))
}
// Check field positions
tests := []struct {
fieldIdx int
wantLine int
wantColumn int
}{
{0, 1, 1}, // 'a' at line 1, column 1
{1, 1, 3}, // 'b' at line 1, column 3
{2, 1, 5}, // 'c' at line 1, column 5
}
for _, tt := range tests {
line, col := reader.FieldPos(tt.fieldIdx)
if line != tt.wantLine || col != tt.wantColumn {
t.Errorf("FieldPos(%d): got (%d, %d), want (%d, %d)",
tt.fieldIdx, line, col, tt.wantLine, tt.wantColumn)
}
}
}
// TestFieldPos_QuotedFields tests FieldPos with quoted fields.
func TestFieldPos_QuotedFields(t *testing.T) {
input := `"a","b,c","d"` + "\n"
reader := NewReader(strings.NewReader(input))
reader.FieldsPerRecord = -1
record, err := reader.Read()
if err != nil {
t.Fatalf("Read error: %v", err)
}
if len(record) != 3 {
t.Fatalf("Expected 3 fields, got %d", len(record))
}
// First field starts at column 2 (after opening quote)
line, col := reader.FieldPos(0)
if line != 1 {
t.Errorf("FieldPos(0) line: got %d, want 1", line)
}
// Column depends on implementation - just check it's reasonable
if col < 1 {
t.Errorf("FieldPos(0) column: got %d, want >= 1", col)
}
}
// TestFieldPos_Panic tests that FieldPos panics with out-of-range index.
func TestFieldPos_Panic(t *testing.T) {
input := "a,b,c\n"
reader := NewReader(strings.NewReader(input))
reader.FieldsPerRecord = -1
_, err := reader.Read()
if err != nil {
t.Fatalf("Read error: %v", err)
}
// Test panic for negative index
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for negative index")
}
}()
reader.FieldPos(-1)
}
// TestFieldPos_PanicOutOfRange tests panic for index >= field count.
func TestFieldPos_PanicOutOfRange(t *testing.T) {
input := "a,b,c\n"
reader := NewReader(strings.NewReader(input))
reader.FieldsPerRecord = -1
_, err := reader.Read()
if err != nil {
t.Fatalf("Read error: %v", err)