@@ -182,6 +182,65 @@ true;1;1;1;1;2;2;2;2;0.2;0.2;str-2
182182 }
183183}
184184
185+ func TestCSVWriterWithHeader (t * testing.T ) {
186+ f := new (bytes.Buffer )
187+
188+ pool := memory .NewCheckedAllocator (memory .NewGoAllocator ())
189+ defer pool .AssertSize (t , 0 )
190+ schema := arrow .NewSchema (
191+ []arrow.Field {
192+ {Name : "bool" , Type : arrow .FixedWidthTypes .Boolean },
193+ {Name : "i8" , Type : arrow .PrimitiveTypes .Int8 },
194+ {Name : "i16" , Type : arrow .PrimitiveTypes .Int16 },
195+ {Name : "i32" , Type : arrow .PrimitiveTypes .Int32 },
196+ {Name : "i64" , Type : arrow .PrimitiveTypes .Int64 },
197+ {Name : "u8" , Type : arrow .PrimitiveTypes .Uint8 },
198+ {Name : "u16" , Type : arrow .PrimitiveTypes .Uint16 },
199+ {Name : "u32" , Type : arrow .PrimitiveTypes .Uint32 },
200+ {Name : "u64" , Type : arrow .PrimitiveTypes .Uint64 },
201+ {Name : "f32" , Type : arrow .PrimitiveTypes .Float32 },
202+ {Name : "f64" , Type : arrow .PrimitiveTypes .Float64 },
203+ {Name : "str" , Type : arrow .BinaryTypes .String },
204+ },
205+ nil ,
206+ )
207+
208+ b := array .NewRecordBuilder (pool , schema )
209+ defer b .Release ()
210+
211+ b .Field (0 ).(* array.BooleanBuilder ).AppendValues ([]bool {true , false , true }, nil )
212+ b .Field (1 ).(* array.Int8Builder ).AppendValues ([]int8 {- 1 , 0 , 1 }, nil )
213+ b .Field (2 ).(* array.Int16Builder ).AppendValues ([]int16 {- 1 , 0 , 1 }, nil )
214+ b .Field (3 ).(* array.Int32Builder ).AppendValues ([]int32 {- 1 , 0 , 1 }, nil )
215+ b .Field (4 ).(* array.Int64Builder ).AppendValues ([]int64 {- 1 , 0 , 1 }, nil )
216+ b .Field (5 ).(* array.Uint8Builder ).AppendValues ([]uint8 {0 , 1 , 2 }, nil )
217+ b .Field (6 ).(* array.Uint16Builder ).AppendValues ([]uint16 {0 , 1 , 2 }, nil )
218+ b .Field (7 ).(* array.Uint32Builder ).AppendValues ([]uint32 {0 , 1 , 2 }, nil )
219+ b .Field (8 ).(* array.Uint64Builder ).AppendValues ([]uint64 {0 , 1 , 2 }, nil )
220+ b .Field (9 ).(* array.Float32Builder ).AppendValues ([]float32 {0.0 , 0.1 , 0.2 }, nil )
221+ b .Field (10 ).(* array.Float64Builder ).AppendValues ([]float64 {0.0 , 0.1 , 0.2 }, nil )
222+ b .Field (11 ).(* array.StringBuilder ).AppendValues ([]string {"str-0" , "str-1" , "str-2" }, nil )
223+
224+ rec := b .NewRecord ()
225+ defer rec .Release ()
226+
227+ w := csv .NewWriter (f , schema , csv .WithComma (';' ), csv .WithCRLF (false ), csv .WithHeader ())
228+ err := w .Write (rec )
229+ if err != nil {
230+ t .Fatal (err )
231+ }
232+
233+ want := `bool;i8;i16;i32;i64;u8;u16;u32;u64;f32;f64;str
234+ true;-1;-1;-1;-1;0;0;0;0;0;0;str-0
235+ false;0;0;0;0;1;1;1;1;0.1;0.1;str-1
236+ true;1;1;1;1;2;2;2;2;0.2;0.2;str-2
237+ `
238+
239+ if got , want := f .String (), want ; strings .Compare (got , want ) != 0 {
240+ t .Fatalf ("invalid output:\n got=%s\n want=%s\n " , got , want )
241+ }
242+ }
243+
185244func BenchmarkWrite (b * testing.B ) {
186245 pool := memory .NewCheckedAllocator (memory .NewGoAllocator ())
187246 defer pool .AssertSize (b , 0 )
0 commit comments