1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
24 "github.com/apache/arrow/go/v6/arrow"
25 "github.com/apache/arrow/go/v6/arrow/decimal128"
26 "github.com/apache/arrow/go/v6/arrow/float16"
29 func TestBooleanTraits(t *testing.T) {
30 for _, tc := range []struct {
34 {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1},
38 t.Run(fmt.Sprintf("nbytes=%d", tc.i), func(t *testing.T) {
39 got := arrow.BooleanTraits.BytesRequired(tc.i)
41 t.Fatalf("got=%v, want=%v", got, tc.want)
47 func TestFloat16Traits(t *testing.T) {
49 nbytes := arrow.Float16Traits.BytesRequired(N)
50 b1 := arrow.Float16Traits.CastToBytes([]float16.Num{
63 b2 := make([]byte, nbytes)
64 for i := 0; i < N; i++ {
65 beg := i * arrow.Float16SizeBytes
66 end := (i + 1) * arrow.Float16SizeBytes
67 arrow.Float16Traits.PutValue(b2[beg:end], float16.New(float32(i)))
70 if !reflect.DeepEqual(b1, b2) {
71 v1 := arrow.Float16Traits.CastFromBytes(b1)
72 v2 := arrow.Float16Traits.CastFromBytes(b2)
73 t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
76 v1 := arrow.Float16Traits.CastFromBytes(b1)
77 for i, v := range v1 {
78 if got, want := v.Float32(), float32(i); got != want {
79 t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
83 v2 := make([]float16.Num, N)
84 arrow.Float16Traits.Copy(v2, v1)
86 if !reflect.DeepEqual(v1, v2) {
87 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
91 func TestDecimal128Traits(t *testing.T) {
93 nbytes := arrow.Decimal128Traits.BytesRequired(N)
94 b1 := arrow.Decimal128Traits.CastToBytes([]decimal128.Num{
95 decimal128.New(0, 10),
96 decimal128.New(1, 10),
97 decimal128.New(2, 10),
98 decimal128.New(3, 10),
99 decimal128.New(4, 10),
100 decimal128.New(5, 10),
101 decimal128.New(6, 10),
102 decimal128.New(7, 10),
103 decimal128.New(8, 10),
104 decimal128.New(9, 10),
107 b2 := make([]byte, nbytes)
108 for i := 0; i < N; i++ {
109 beg := i * arrow.Decimal128SizeBytes
110 end := (i + 1) * arrow.Decimal128SizeBytes
111 arrow.Decimal128Traits.PutValue(b2[beg:end], decimal128.New(int64(i), 10))
114 if !reflect.DeepEqual(b1, b2) {
115 v1 := arrow.Decimal128Traits.CastFromBytes(b1)
116 v2 := arrow.Decimal128Traits.CastFromBytes(b2)
117 t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
120 v1 := arrow.Decimal128Traits.CastFromBytes(b1)
121 for i, v := range v1 {
122 if got, want := v, decimal128.New(int64(i), 10); got != want {
123 t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
127 v2 := make([]decimal128.Num, N)
128 arrow.Decimal128Traits.Copy(v2, v1)
130 if !reflect.DeepEqual(v1, v2) {
131 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
135 func TestMonthIntervalTraits(t *testing.T) {
137 b1 := arrow.MonthIntervalTraits.CastToBytes([]arrow.MonthInterval{
138 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
141 b2 := make([]byte, arrow.MonthIntervalTraits.BytesRequired(N))
142 for i := 0; i < N; i++ {
143 beg := i * arrow.MonthIntervalSizeBytes
144 end := (i + 1) * arrow.MonthIntervalSizeBytes
145 arrow.MonthIntervalTraits.PutValue(b2[beg:end], arrow.MonthInterval(i))
148 if !reflect.DeepEqual(b1, b2) {
149 v1 := arrow.MonthIntervalTraits.CastFromBytes(b1)
150 v2 := arrow.MonthIntervalTraits.CastFromBytes(b2)
151 t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
154 v1 := arrow.MonthIntervalTraits.CastFromBytes(b1)
155 for i, v := range v1 {
156 if got, want := v, arrow.MonthInterval(i); got != want {
157 t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
161 v2 := make([]arrow.MonthInterval, N)
162 arrow.MonthIntervalTraits.Copy(v2, v1)
164 if !reflect.DeepEqual(v1, v2) {
165 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
169 func TestDayTimeIntervalTraits(t *testing.T) {
171 b1 := arrow.DayTimeIntervalTraits.CastToBytes([]arrow.DayTimeInterval{
172 {0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9},
175 b2 := make([]byte, arrow.DayTimeIntervalTraits.BytesRequired(N))
176 for i := 0; i < N; i++ {
177 beg := i * arrow.DayTimeIntervalSizeBytes
178 end := (i + 1) * arrow.DayTimeIntervalSizeBytes
179 arrow.DayTimeIntervalTraits.PutValue(b2[beg:end], arrow.DayTimeInterval{int32(i), int32(i)})
182 if !reflect.DeepEqual(b1, b2) {
183 v1 := arrow.DayTimeIntervalTraits.CastFromBytes(b1)
184 v2 := arrow.DayTimeIntervalTraits.CastFromBytes(b2)
185 t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
188 v1 := arrow.DayTimeIntervalTraits.CastFromBytes(b1)
189 for i, v := range v1 {
190 if got, want := v, (arrow.DayTimeInterval{int32(i), int32(i)}); got != want {
191 t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
195 v2 := make([]arrow.DayTimeInterval, N)
196 arrow.DayTimeIntervalTraits.Copy(v2, v1)
198 if !reflect.DeepEqual(v1, v2) {
199 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
203 func TestMonthDayNanoIntervalTraits(t *testing.T) {
205 b1 := arrow.MonthDayNanoIntervalTraits.CastToBytes([]arrow.MonthDayNanoInterval{
206 {0, 0, 0}, {1, 1, 1000}, {2, 2, 2000}, {3, 3, 3000}, {4, 4, 4000}, {5, 5, 5000}, {6, 6, 6000}, {7, 7, 7000}, {8, 8, 8000}, {9, 9, 9000},
209 b2 := make([]byte, arrow.MonthDayNanoIntervalTraits.BytesRequired(N))
210 for i := 0; i < N; i++ {
211 beg := i * arrow.MonthDayNanoIntervalSizeBytes
212 end := (i + 1) * arrow.MonthDayNanoIntervalSizeBytes
213 arrow.MonthDayNanoIntervalTraits.PutValue(b2[beg:end], arrow.MonthDayNanoInterval{int32(i), int32(i), int64(i) * 1000})
216 if !reflect.DeepEqual(b1, b2) {
217 v1 := arrow.MonthDayNanoIntervalTraits.CastFromBytes(b1)
218 v2 := arrow.MonthDayNanoIntervalTraits.CastFromBytes(b2)
219 t.Fatalf("invalid values:\nb1=%v\nb2=%v\nv1=%v\nv2=%v\n", b1, b2, v1, v2)
222 v1 := arrow.MonthDayNanoIntervalTraits.CastFromBytes(b1)
223 for i, v := range v1 {
224 if got, want := v, (arrow.MonthDayNanoInterval{int32(i), int32(i), int64(i) * 1000}); got != want {
225 t.Fatalf("invalid value[%d]. got=%v, want=%v", i, got, want)
229 v2 := make([]arrow.MonthDayNanoInterval, N)
230 arrow.MonthDayNanoIntervalTraits.Copy(v2, v1)
232 if !reflect.DeepEqual(v1, v2) {
233 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)