]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/go/arrow/type_traits_test.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / type_traits_test.go
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 package arrow_test
18
19 import (
20 "fmt"
21 "reflect"
22 "testing"
23
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"
27 )
28
29 func TestBooleanTraits(t *testing.T) {
30 for _, tc := range []struct {
31 i, want int
32 }{
33 {0, 0},
34 {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1},
35 {9, 2},
36 {17, 3},
37 } {
38 t.Run(fmt.Sprintf("nbytes=%d", tc.i), func(t *testing.T) {
39 got := arrow.BooleanTraits.BytesRequired(tc.i)
40 if got != tc.want {
41 t.Fatalf("got=%v, want=%v", got, tc.want)
42 }
43 })
44 }
45 }
46
47 func TestFloat16Traits(t *testing.T) {
48 const N = 10
49 nbytes := arrow.Float16Traits.BytesRequired(N)
50 b1 := arrow.Float16Traits.CastToBytes([]float16.Num{
51 float16.New(0),
52 float16.New(1),
53 float16.New(2),
54 float16.New(3),
55 float16.New(4),
56 float16.New(5),
57 float16.New(6),
58 float16.New(7),
59 float16.New(8),
60 float16.New(9),
61 })
62
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)))
68 }
69
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)
74 }
75
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)
80 }
81 }
82
83 v2 := make([]float16.Num, N)
84 arrow.Float16Traits.Copy(v2, v1)
85
86 if !reflect.DeepEqual(v1, v2) {
87 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
88 }
89 }
90
91 func TestDecimal128Traits(t *testing.T) {
92 const N = 10
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),
105 })
106
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))
112 }
113
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)
118 }
119
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)
124 }
125 }
126
127 v2 := make([]decimal128.Num, N)
128 arrow.Decimal128Traits.Copy(v2, v1)
129
130 if !reflect.DeepEqual(v1, v2) {
131 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
132 }
133 }
134
135 func TestMonthIntervalTraits(t *testing.T) {
136 const N = 10
137 b1 := arrow.MonthIntervalTraits.CastToBytes([]arrow.MonthInterval{
138 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
139 })
140
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))
146 }
147
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)
152 }
153
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)
158 }
159 }
160
161 v2 := make([]arrow.MonthInterval, N)
162 arrow.MonthIntervalTraits.Copy(v2, v1)
163
164 if !reflect.DeepEqual(v1, v2) {
165 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
166 }
167 }
168
169 func TestDayTimeIntervalTraits(t *testing.T) {
170 const N = 10
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},
173 })
174
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)})
180 }
181
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)
186 }
187
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)
192 }
193 }
194
195 v2 := make([]arrow.DayTimeInterval, N)
196 arrow.DayTimeIntervalTraits.Copy(v2, v1)
197
198 if !reflect.DeepEqual(v1, v2) {
199 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
200 }
201 }
202
203 func TestMonthDayNanoIntervalTraits(t *testing.T) {
204 const N = 10
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},
207 })
208
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})
214 }
215
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)
220 }
221
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)
226 }
227 }
228
229 v2 := make([]arrow.MonthDayNanoInterval, N)
230 arrow.MonthDayNanoIntervalTraits.Copy(v2, v1)
231
232 if !reflect.DeepEqual(v1, v2) {
233 t.Fatalf("invalid values:\nv1=%v\nv2=%v\n", v1, v2)
234 }
235 }