You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by sb...@apache.org on 2019/06/14 16:31:41 UTC
[arrow] branch master updated: ARROW-5604: [Go] improve coverage of
TypeTraits
This is an automated email from the ASF dual-hosted git repository.
sbinet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new a5fa7bb ARROW-5604: [Go] improve coverage of TypeTraits
a5fa7bb is described below
commit a5fa7bb4a5785db627cd5f4d8996fd8afc3f2e42
Author: Sebastien Binet <bi...@cern.ch>
AuthorDate: Fri Jun 14 18:31:21 2019 +0200
ARROW-5604: [Go] improve coverage of TypeTraits
Author: Sebastien Binet <bi...@cern.ch>
Closes #4571 from sbinet/issue-5604 and squashes the following commits:
2252eec70 <Sebastien Binet> ARROW-5604: improve coverage of TypeTraits
---
go/arrow/Makefile | 2 +-
go/arrow/doc.go | 2 +-
go/arrow/type_traits_interval.go | 4 +-
go/arrow/type_traits_numeric.gen.go | 7 +-
go/arrow/type_traits_numeric.gen.go.tmpl | 11 +-
go/arrow/type_traits_numeric.gen_test.go | 570 ++++++++++++++++++++++++++
go/arrow/type_traits_numeric.gen_test.go.tmpl | 61 +++
go/arrow/type_traits_test.go | 156 +++++++
8 files changed, 803 insertions(+), 10 deletions(-)
diff --git a/go/arrow/Makefile b/go/arrow/Makefile
index bd77836..9c4a232 100644
--- a/go/arrow/Makefile
+++ b/go/arrow/Makefile
@@ -30,7 +30,7 @@ assembly:
@$(MAKE) -C math assembly
generate: bin/tmpl
- bin/tmpl -i -data=numeric.tmpldata type_traits_numeric.gen.go.tmpl array/numeric.gen.go.tmpl array/numericbuilder.gen_test.go.tmpl array/numericbuilder.gen.go.tmpl array/bufferbuilder_numeric.gen.go.tmpl
+ bin/tmpl -i -data=numeric.tmpldata type_traits_numeric.gen.go.tmpl type_traits_numeric.gen_test.go.tmpl array/numeric.gen.go.tmpl array/numericbuilder.gen_test.go.tmpl array/numericbuilder.gen.go.tmpl array/bufferbuilder_numeric.gen.go.tmpl
bin/tmpl -i -data=datatype_numeric.gen.go.tmpldata datatype_numeric.gen.go.tmpl
@$(MAKE) -C math generate
diff --git a/go/arrow/doc.go b/go/arrow/doc.go
index a91e62c..10ddda9 100644
--- a/go/arrow/doc.go
+++ b/go/arrow/doc.go
@@ -31,7 +31,7 @@ array is valid (not null). If the array has no null entries, it is possible to o
*/
package arrow
-//go:generate go run _tools/tmpl/main.go -i -data=numeric.tmpldata type_traits_numeric.gen.go.tmpl array/numeric.gen.go.tmpl array/numericbuilder.gen.go.tmpl array/bufferbuilder_numeric.gen.go.tmpl
+//go:generate go run _tools/tmpl/main.go -i -data=numeric.tmpldata type_traits_numeric.gen.go.tmpl type_traits_numeric.gen_test.go.tmpl array/numeric.gen.go.tmpl array/numericbuilder.gen.go.tmpl array/bufferbuilder_numeric.gen.go.tmpl
//go:generate go run _tools/tmpl/main.go -i -data=datatype_numeric.gen.go.tmpldata datatype_numeric.gen.go.tmpl tensor/numeric.gen.go.tmpl tensor/numeric.gen_test.go.tmpl
//go:generate go run ./gen-flatbuffers.go
diff --git a/go/arrow/type_traits_interval.go b/go/arrow/type_traits_interval.go
index 8ddaa51..fcff1e6 100644
--- a/go/arrow/type_traits_interval.go
+++ b/go/arrow/type_traits_interval.go
@@ -89,8 +89,8 @@ func (daytimeTraits) BytesRequired(n int) int { return DayTimeIntervalSizeBytes
// PutValue
func (daytimeTraits) PutValue(b []byte, v DayTimeInterval) {
- binary.LittleEndian.PutUint32(b, uint32(v.Days))
- binary.LittleEndian.PutUint32(b, uint32(v.Milliseconds))
+ binary.LittleEndian.PutUint32(b[0:4], uint32(v.Days))
+ binary.LittleEndian.PutUint32(b[4:8], uint32(v.Milliseconds))
}
// CastFromBytes reinterprets the slice b to a slice of type DayTimeInterval.
diff --git a/go/arrow/type_traits_numeric.gen.go b/go/arrow/type_traits_numeric.gen.go
index c8c063a..f98f494 100644
--- a/go/arrow/type_traits_numeric.gen.go
+++ b/go/arrow/type_traits_numeric.gen.go
@@ -16,10 +16,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package arrow
+package arrow // import "github.com/apache/arrow/go/arrow"
import (
"encoding/binary"
+ "math"
"reflect"
"unsafe"
)
@@ -153,7 +154,7 @@ func (float64Traits) BytesRequired(n int) int { return Float64SizeBytes * n }
// PutValue
func (float64Traits) PutValue(b []byte, v float64) {
- binary.LittleEndian.PutUint64(b, uint64(v))
+ binary.LittleEndian.PutUint64(b, math.Float64bits(v))
}
// CastFromBytes reinterprets the slice b to a slice of type float64.
@@ -297,7 +298,7 @@ func (float32Traits) BytesRequired(n int) int { return Float32SizeBytes * n }
// PutValue
func (float32Traits) PutValue(b []byte, v float32) {
- binary.LittleEndian.PutUint32(b, uint32(v))
+ binary.LittleEndian.PutUint32(b, math.Float32bits(v))
}
// CastFromBytes reinterprets the slice b to a slice of type float32.
diff --git a/go/arrow/type_traits_numeric.gen.go.tmpl b/go/arrow/type_traits_numeric.gen.go.tmpl
index 362d2d8..c4a25ee 100644
--- a/go/arrow/type_traits_numeric.gen.go.tmpl
+++ b/go/arrow/type_traits_numeric.gen.go.tmpl
@@ -14,10 +14,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package arrow
+package arrow // import "github.com/apache/arrow/go/arrow"
import (
"encoding/binary"
+ "math"
"reflect"
"unsafe"
)
@@ -43,7 +44,11 @@ func ({{.name}}Traits) BytesRequired(n int) int { return {{.Name}}SizeBytes * n
// PutValue
func ({{.name}}Traits) PutValue(b []byte, v {{.Type}}) {
-{{- if eq .Size "1" -}}
+{{- if eq .Type "float32" -}}
+ binary.LittleEndian.PutUint32(b, math.Float32bits(v))
+{{- else if eq .Type "float64" -}}
+ binary.LittleEndian.PutUint64(b, math.Float64bits(v))
+{{- else if eq .Size "1" -}}
b[0] = byte(v)
{{- else if eq .Size "2" -}}
binary.LittleEndian.PutUint16(b, uint16(v))
@@ -86,4 +91,4 @@ func ({{.name}}Traits) CastToBytes(b []{{.Type}}) []byte {
// Copy copies src to dst.
func ({{.name}}Traits) Copy(dst, src []{{.Type}}) { copy(dst, src) }
-{{end}}
\ No newline at end of file
+{{end}}
diff --git a/go/arrow/type_traits_numeric.gen_test.go b/go/arrow/type_traits_numeric.gen_test.go
new file mode 100644
index 0000000..7347de3
--- /dev/null
+++ b/go/arrow/type_traits_numeric.gen_test.go
@@ -0,0 +1,570 @@
+// Code generated by type_traits_numeric.gen_test.go.tmpl. DO NOT EDIT.
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package arrow_test
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/apache/arrow/go/arrow"
+)
+
+func TestInt64Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Int64Traits.CastToBytes([]int64{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Int64Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Int64SizeBytes
+ end := (i + 1) * arrow.Int64SizeBytes
+ arrow.Int64Traits.PutValue(b2[beg:end], int64(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Int64Traits.CastFromBytes(b1)
+ v2 := arrow.Int64Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Int64Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, int64(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]int64, N)
+ arrow.Int64Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestUint64Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Uint64Traits.CastToBytes([]uint64{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Uint64Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Uint64SizeBytes
+ end := (i + 1) * arrow.Uint64SizeBytes
+ arrow.Uint64Traits.PutValue(b2[beg:end], uint64(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Uint64Traits.CastFromBytes(b1)
+ v2 := arrow.Uint64Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Uint64Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, uint64(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]uint64, N)
+ arrow.Uint64Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestFloat64Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Float64Traits.CastToBytes([]float64{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Float64Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Float64SizeBytes
+ end := (i + 1) * arrow.Float64SizeBytes
+ arrow.Float64Traits.PutValue(b2[beg:end], float64(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Float64Traits.CastFromBytes(b1)
+ v2 := arrow.Float64Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Float64Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, float64(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]float64, N)
+ arrow.Float64Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestInt32Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Int32Traits.CastToBytes([]int32{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Int32Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Int32SizeBytes
+ end := (i + 1) * arrow.Int32SizeBytes
+ arrow.Int32Traits.PutValue(b2[beg:end], int32(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Int32Traits.CastFromBytes(b1)
+ v2 := arrow.Int32Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Int32Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, int32(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]int32, N)
+ arrow.Int32Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestUint32Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Uint32Traits.CastToBytes([]uint32{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Uint32Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Uint32SizeBytes
+ end := (i + 1) * arrow.Uint32SizeBytes
+ arrow.Uint32Traits.PutValue(b2[beg:end], uint32(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Uint32Traits.CastFromBytes(b1)
+ v2 := arrow.Uint32Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Uint32Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, uint32(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]uint32, N)
+ arrow.Uint32Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestFloat32Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Float32Traits.CastToBytes([]float32{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Float32Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Float32SizeBytes
+ end := (i + 1) * arrow.Float32SizeBytes
+ arrow.Float32Traits.PutValue(b2[beg:end], float32(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Float32Traits.CastFromBytes(b1)
+ v2 := arrow.Float32Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Float32Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, float32(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]float32, N)
+ arrow.Float32Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestInt16Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Int16Traits.CastToBytes([]int16{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Int16Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Int16SizeBytes
+ end := (i + 1) * arrow.Int16SizeBytes
+ arrow.Int16Traits.PutValue(b2[beg:end], int16(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Int16Traits.CastFromBytes(b1)
+ v2 := arrow.Int16Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Int16Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, int16(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]int16, N)
+ arrow.Int16Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestUint16Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Uint16Traits.CastToBytes([]uint16{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Uint16Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Uint16SizeBytes
+ end := (i + 1) * arrow.Uint16SizeBytes
+ arrow.Uint16Traits.PutValue(b2[beg:end], uint16(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Uint16Traits.CastFromBytes(b1)
+ v2 := arrow.Uint16Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Uint16Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, uint16(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]uint16, N)
+ arrow.Uint16Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestInt8Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Int8Traits.CastToBytes([]int8{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Int8Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Int8SizeBytes
+ end := (i + 1) * arrow.Int8SizeBytes
+ arrow.Int8Traits.PutValue(b2[beg:end], int8(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Int8Traits.CastFromBytes(b1)
+ v2 := arrow.Int8Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Int8Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, int8(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]int8, N)
+ arrow.Int8Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestUint8Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Uint8Traits.CastToBytes([]uint8{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Uint8Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Uint8SizeBytes
+ end := (i + 1) * arrow.Uint8SizeBytes
+ arrow.Uint8Traits.PutValue(b2[beg:end], uint8(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Uint8Traits.CastFromBytes(b1)
+ v2 := arrow.Uint8Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Uint8Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, uint8(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]uint8, N)
+ arrow.Uint8Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestTimestampTraits(t *testing.T) {
+ const N = 10
+ b1 := arrow.TimestampTraits.CastToBytes([]arrow.Timestamp{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.TimestampTraits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.TimestampSizeBytes
+ end := (i + 1) * arrow.TimestampSizeBytes
+ arrow.TimestampTraits.PutValue(b2[beg:end], arrow.Timestamp(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.TimestampTraits.CastFromBytes(b1)
+ v2 := arrow.TimestampTraits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.TimestampTraits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.Timestamp(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.Timestamp, N)
+ arrow.TimestampTraits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestTime32Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Time32Traits.CastToBytes([]arrow.Time32{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Time32Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Time32SizeBytes
+ end := (i + 1) * arrow.Time32SizeBytes
+ arrow.Time32Traits.PutValue(b2[beg:end], arrow.Time32(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Time32Traits.CastFromBytes(b1)
+ v2 := arrow.Time32Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Time32Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.Time32(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.Time32, N)
+ arrow.Time32Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestTime64Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Time64Traits.CastToBytes([]arrow.Time64{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Time64Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Time64SizeBytes
+ end := (i + 1) * arrow.Time64SizeBytes
+ arrow.Time64Traits.PutValue(b2[beg:end], arrow.Time64(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Time64Traits.CastFromBytes(b1)
+ v2 := arrow.Time64Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Time64Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.Time64(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.Time64, N)
+ arrow.Time64Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestDate32Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Date32Traits.CastToBytes([]arrow.Date32{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Date32Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Date32SizeBytes
+ end := (i + 1) * arrow.Date32SizeBytes
+ arrow.Date32Traits.PutValue(b2[beg:end], arrow.Date32(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Date32Traits.CastFromBytes(b1)
+ v2 := arrow.Date32Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Date32Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.Date32(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.Date32, N)
+ arrow.Date32Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestDate64Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.Date64Traits.CastToBytes([]arrow.Date64{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.Date64Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Date64SizeBytes
+ end := (i + 1) * arrow.Date64SizeBytes
+ arrow.Date64Traits.PutValue(b2[beg:end], arrow.Date64(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Date64Traits.CastFromBytes(b1)
+ v2 := arrow.Date64Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Date64Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.Date64(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.Date64, N)
+ arrow.Date64Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestDurationTraits(t *testing.T) {
+ const N = 10
+ b1 := arrow.DurationTraits.CastToBytes([]arrow.Duration{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.DurationTraits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.DurationSizeBytes
+ end := (i + 1) * arrow.DurationSizeBytes
+ arrow.DurationTraits.PutValue(b2[beg:end], arrow.Duration(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.DurationTraits.CastFromBytes(b1)
+ v2 := arrow.DurationTraits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.DurationTraits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.Duration(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.Duration, N)
+ arrow.DurationTraits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
diff --git a/go/arrow/type_traits_numeric.gen_test.go.tmpl b/go/arrow/type_traits_numeric.gen_test.go.tmpl
new file mode 100644
index 0000000..5a0e269
--- /dev/null
+++ b/go/arrow/type_traits_numeric.gen_test.go.tmpl
@@ -0,0 +1,61 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package arrow_test
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/apache/arrow/go/arrow"
+)
+
+{{- range .In}}
+
+func Test{{.Name}}Traits(t *testing.T) {
+ const N = 10
+ b1 := arrow.{{.Name}}Traits.CastToBytes([]{{or .QualifiedType .Type}}{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.{{.Name}}Traits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.{{.Name}}SizeBytes
+ end := (i + 1) * arrow.{{.Name}}SizeBytes
+ arrow.{{.Name}}Traits.PutValue(b2[beg:end], {{or .QualifiedType .Type}}(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.{{.Name}}Traits.CastFromBytes(b1)
+ v2 := arrow.{{.Name}}Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.{{.Name}}Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, {{or .QualifiedType .Type}}(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]{{or .QualifiedType .Type}}, N)
+ arrow.{{.Name}}Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+{{end}}
diff --git a/go/arrow/type_traits_test.go b/go/arrow/type_traits_test.go
new file mode 100644
index 0000000..f2f1d9a
--- /dev/null
+++ b/go/arrow/type_traits_test.go
@@ -0,0 +1,156 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package arrow_test
+
+import (
+ "fmt"
+ "reflect"
+ "testing"
+
+ "github.com/apache/arrow/go/arrow"
+ "github.com/apache/arrow/go/arrow/float16"
+)
+
+func TestBooleanTraits(t *testing.T) {
+ for _, tc := range []struct {
+ i, want int
+ }{
+ {0, 0},
+ {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1},
+ {9, 2},
+ {17, 3},
+ } {
+ t.Run(fmt.Sprintf("nbytes=%d", tc.i), func(t *testing.T) {
+ got := arrow.BooleanTraits.BytesRequired(tc.i)
+ if got != tc.want {
+ t.Fatalf("got=%v, want=%v", got, tc.want)
+ }
+ })
+ }
+}
+
+func TestFloat16Traits(t *testing.T) {
+ const N = 10
+ nbytes := arrow.Float16Traits.BytesRequired(N)
+ b1 := arrow.Float16Traits.CastToBytes([]float16.Num{
+ float16.New(0),
+ float16.New(1),
+ float16.New(2),
+ float16.New(3),
+ float16.New(4),
+ float16.New(5),
+ float16.New(6),
+ float16.New(7),
+ float16.New(8),
+ float16.New(9),
+ })
+
+ b2 := make([]byte, nbytes)
+ for i := 0; i < N; i++ {
+ beg := i * arrow.Float16SizeBytes
+ end := (i + 1) * arrow.Float16SizeBytes
+ arrow.Float16Traits.PutValue(b2[beg:end], float16.New(float32(i)))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.Float16Traits.CastFromBytes(b1)
+ v2 := arrow.Float16Traits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.Float16Traits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v.Float32(), float32(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]float16.Num, N)
+ arrow.Float16Traits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestMonthIntervalTraits(t *testing.T) {
+ const N = 10
+ b1 := arrow.MonthIntervalTraits.CastToBytes([]arrow.MonthInterval{
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ })
+
+ b2 := make([]byte, arrow.MonthIntervalTraits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.MonthIntervalSizeBytes
+ end := (i + 1) * arrow.MonthIntervalSizeBytes
+ arrow.MonthIntervalTraits.PutValue(b2[beg:end], arrow.MonthInterval(i))
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.MonthIntervalTraits.CastFromBytes(b1)
+ v2 := arrow.MonthIntervalTraits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.MonthIntervalTraits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, arrow.MonthInterval(i); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.MonthInterval, N)
+ arrow.MonthIntervalTraits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}
+
+func TestDayTimeIntervalTraits(t *testing.T) {
+ const N = 10
+ b1 := arrow.DayTimeIntervalTraits.CastToBytes([]arrow.DayTimeInterval{
+ {0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9},
+ })
+
+ b2 := make([]byte, arrow.DayTimeIntervalTraits.BytesRequired(N))
+ for i := 0; i < N; i++ {
+ beg := i * arrow.DayTimeIntervalSizeBytes
+ end := (i + 1) * arrow.DayTimeIntervalSizeBytes
+ arrow.DayTimeIntervalTraits.PutValue(b2[beg:end], arrow.DayTimeInterval{int32(i), int32(i)})
+ }
+
+ if !reflect.DeepEqual(b1, b2) {
+ v1 := arrow.DayTimeIntervalTraits.CastFromBytes(b1)
+ v2 := arrow.DayTimeIntervalTraits.CastFromBytes(b2)
+ t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
+ }
+
+ v1 := arrow.DayTimeIntervalTraits.CastFromBytes(b1)
+ for i, v := range v1 {
+ if got, want := v, (arrow.DayTimeInterval{int32(i), int32(i)}); got != want {
+ t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
+ }
+ }
+
+ v2 := make([]arrow.DayTimeInterval, N)
+ arrow.DayTimeIntervalTraits.Copy(v2, v1)
+
+ if !reflect.DeepEqual(v1, v2) {
+ t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
+ }
+}