-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwriter_test.go
More file actions
290 lines (268 loc) · 6.79 KB
/
writer_test.go
File metadata and controls
290 lines (268 loc) · 6.79 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
//go:build goexperiment.simd && amd64
package simdcsv
import (
"bytes"
"strings"
"testing"
)
// =============================================================================
// TestWriter Tests
// =============================================================================
// TestWrite_Simple tests basic CSV writing.
func TestWrite_Simple(t *testing.T) {
tests := []struct {
name string
records [][]string
want string
}{
{
name: "single row single field",
records: [][]string{{"hello"}},
want: "hello\n",
},
{
name: "single row multiple fields",
records: [][]string{{"a", "b", "c"}},
want: "a,b,c\n",
},
{
name: "multiple rows",
records: [][]string{{"a", "b"}, {"c", "d"}},
want: "a,b\nc,d\n",
},
{
name: "empty string field",
records: [][]string{{"", "b", ""}},
want: ",b,\n",
},
{
name: "numeric strings",
records: [][]string{{"1", "2", "3"}},
want: "1,2,3\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWriterWithStdlib(t, tt.records, false)
})
}
}
// TestWrite_QuoteRequired tests writing fields that require quoting.
func TestWrite_QuoteRequired(t *testing.T) {
tests := []struct {
name string
records [][]string
want string
}{
{
name: "field with comma",
records: [][]string{{"hello,world", "foo"}},
want: "\"hello,world\",foo\n",
},
{
name: "field with newline",
records: [][]string{{"hello\nworld", "foo"}},
want: "\"hello\nworld\",foo\n",
},
{
name: "field with quote",
records: [][]string{{"he said \"hello\"", "foo"}},
want: "\"he said \"\"hello\"\"\",foo\n",
},
{
name: "field with CRLF",
records: [][]string{{"hello\r\nworld", "foo"}},
want: "\"hello\r\nworld\",foo\n",
},
{
name: "field starting with space",
records: [][]string{{" hello", "foo"}},
want: "\" hello\",foo\n",
},
{
name: "field starting with tab",
records: [][]string{{"\thello", "foo"}},
want: "\"\thello\",foo\n",
},
{
name: "field with multiple special chars",
records: [][]string{{"hello,\n\"world\"", "foo"}},
want: "\"hello,\n\"\"world\"\"\",foo\n",
},
{
name: "just a quote",
records: [][]string{{"\""}},
want: "\"\"\"\"\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWriterWithStdlib(t, tt.records, false)
})
}
}
// TestWriteAll tests WriteAll functionality.
func TestWriteAll(t *testing.T) {
tests := []struct {
name string
records [][]string
}{
{
name: "multiple simple rows",
records: [][]string{{"a", "b", "c"}, {"1", "2", "3"}, {"x", "y", "z"}},
},
{
name: "mixed quoted and unquoted",
records: [][]string{{"hello", "world,foo"}, {"bar", "baz"}},
},
{
name: "empty records",
records: [][]string{},
},
{
name: "single empty row",
records: [][]string{{""}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWriterWithStdlib(t, tt.records, false)
})
}
}
// TestWrite_CRLF tests writing with CRLF line endings.
func TestWrite_CRLF(t *testing.T) {
tests := []struct {
name string
records [][]string
}{
{
name: "simple with CRLF",
records: [][]string{{"a", "b"}, {"c", "d"}},
},
{
name: "quoted fields with CRLF",
records: [][]string{{"hello,world", "foo"}, {"bar", "baz"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compareWriterWithStdlib(t, tt.records, true)
})
}
}
// =============================================================================
// fieldNeedsQuotes SIMD Tests
// =============================================================================
func TestFieldNeedsQuotes_SIMDvsScalar(t *testing.T) {
if !useAVX512 {
t.Skip("AVX-512 not available, skipping SIMD test")
}
w := NewWriter(nil)
tests := []struct {
name string
field string
}{
{"empty", ""},
{"simple", "hello"},
{"with comma", "hello,world"},
{"with newline", "hello\nworld"},
{"with CR", "hello\rworld"},
{"with quote", `hello"world`},
{"leading space", " hello"},
{"leading tab", "\thello"},
{"long no special", strings.Repeat("abcdefgh", 10)},
{"long with comma", strings.Repeat("abcdefgh", 10) + ","},
{"long with quote", strings.Repeat("abcdefgh", 10) + `"`},
{"comma at start", "," + strings.Repeat("x", 50)},
{"comma in middle", strings.Repeat("x", 25) + "," + strings.Repeat("y", 25)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scalar := w.fieldNeedsQuotesScalar(tt.field)
// Force SIMD path even for short strings by calling directly
simd := w.fieldNeedsQuotesSIMD(tt.field)
if scalar != simd {
t.Errorf("fieldNeedsQuotes mismatch for %q: scalar=%v, simd=%v",
tt.field, scalar, simd)
}
})
}
}
func TestFieldNeedsQuotes_LongInput(t *testing.T) {
w := NewWriter(nil)
tests := []struct {
name string
field string
want bool
}{
{
name: "100 chars no special",
field: strings.Repeat("abcdefghij", 10),
want: false,
},
{
name: "100 chars with comma at end",
field: strings.Repeat("abcdefghij", 10) + ",",
want: true,
},
{
name: "100 chars with newline at position 50",
field: strings.Repeat("a", 50) + "\n" + strings.Repeat("b", 50),
want: true,
},
{
name: "100 chars with quote at position 80",
field: strings.Repeat("x", 80) + `"` + strings.Repeat("y", 19),
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := w.fieldNeedsQuotes(tt.field)
if got != tt.want {
t.Errorf("fieldNeedsQuotes(%q...) = %v, want %v",
tt.field[:min(20, len(tt.field))], got, tt.want)
}
})
}
}
func TestWriteQuotedField_SIMD(t *testing.T) {
tests := []struct {
name string
field string
want string
}{
{
name: "long with quotes",
field: strings.Repeat("a", 20) + `"` + strings.Repeat("b", 20) + `"` + strings.Repeat("c", 20),
want: `"` + strings.Repeat("a", 20) + `""` + strings.Repeat("b", 20) + `""` + strings.Repeat("c", 20) + `"`,
},
{
name: "long no quotes to escape",
field: strings.Repeat("hello,world ", 10),
want: `"` + strings.Repeat("hello,world ", 10) + `"`,
},
{
name: "quote at chunk boundary",
field: strings.Repeat("x", 31) + `"` + strings.Repeat("y", 31),
want: `"` + strings.Repeat("x", 31) + `""` + strings.Repeat("y", 31) + `"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(&buf)
if err := w.Write([]string{tt.field}); err != nil {
t.Fatalf("Write error: %v", err)
}
if err := w.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
got := strings.TrimSuffix(buf.String(), "\n")
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}