]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 array_test | |
18 | ||
19 | import ( | |
20 | "reflect" | |
21 | "testing" | |
22 | ||
23 | "github.com/apache/arrow/go/v6/arrow" | |
24 | "github.com/apache/arrow/go/v6/arrow/array" | |
25 | "github.com/apache/arrow/go/v6/arrow/memory" | |
26 | ) | |
27 | ||
28 | func TestFixedSizeListArray(t *testing.T) { | |
29 | pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) | |
30 | defer pool.AssertSize(t, 0) | |
31 | ||
32 | var ( | |
33 | vs = []int32{0, 1, 2, 3, 4, 5, 6} | |
34 | lengths = []int{3, 0, 4} | |
35 | isValid = []bool{true, false, true} | |
36 | ) | |
37 | ||
38 | lb := array.NewFixedSizeListBuilder(pool, int32(len(vs)), arrow.PrimitiveTypes.Int32) | |
39 | defer lb.Release() | |
40 | ||
41 | for i := 0; i < 10; i++ { | |
42 | vb := lb.ValueBuilder().(*array.Int32Builder) | |
43 | vb.Reserve(len(vs)) | |
44 | ||
45 | pos := 0 | |
46 | for i, length := range lengths { | |
47 | lb.Append(isValid[i]) | |
48 | for j := 0; j < length; j++ { | |
49 | vb.Append(vs[pos]) | |
50 | pos++ | |
51 | } | |
52 | } | |
53 | ||
54 | arr := lb.NewArray().(*array.FixedSizeList) | |
55 | defer arr.Release() | |
56 | ||
57 | arr.Retain() | |
58 | arr.Release() | |
59 | ||
60 | if got, want := arr.DataType().ID(), arrow.FIXED_SIZE_LIST; got != want { | |
61 | t.Fatalf("got=%v, want=%v", got, want) | |
62 | } | |
63 | ||
64 | if got, want := arr.Len(), len(isValid); got != want { | |
65 | t.Fatalf("got=%d, want=%d", got, want) | |
66 | } | |
67 | ||
68 | for i := range lengths { | |
69 | if got, want := arr.IsValid(i), isValid[i]; got != want { | |
70 | t.Fatalf("got[%d]=%v, want[%d]=%v", i, got, i, want) | |
71 | } | |
72 | if got, want := arr.IsNull(i), lengths[i] == 0; got != want { | |
73 | t.Fatalf("got[%d]=%v, want[%d]=%v", i, got, i, want) | |
74 | } | |
75 | } | |
76 | ||
77 | varr := arr.ListValues().(*array.Int32) | |
78 | if got, want := varr.Int32Values(), vs; !reflect.DeepEqual(got, want) { | |
79 | t.Fatalf("got=%v, want=%v", got, want) | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | func TestFixedSizeListArrayEmpty(t *testing.T) { | |
85 | pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) | |
86 | defer pool.AssertSize(t, 0) | |
87 | ||
88 | lb := array.NewFixedSizeListBuilder(pool, 3, arrow.PrimitiveTypes.Int32) | |
89 | defer lb.Release() | |
90 | arr := lb.NewArray().(*array.FixedSizeList) | |
91 | defer arr.Release() | |
92 | if got, want := arr.Len(), 0; got != want { | |
93 | t.Fatalf("got=%d, want=%d", got, want) | |
94 | } | |
95 | } | |
96 | ||
97 | func TestFixedSizeListArrayBulkAppend(t *testing.T) { | |
98 | pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) | |
99 | defer pool.AssertSize(t, 0) | |
100 | ||
101 | var ( | |
102 | vs = []int32{0, 1, 2, 3, 4, 5, 6} | |
103 | lengths = []int{3, 0, 4} | |
104 | isValid = []bool{true, false, true} | |
105 | ) | |
106 | ||
107 | lb := array.NewFixedSizeListBuilder(pool, int32(len(vs)), arrow.PrimitiveTypes.Int32) | |
108 | defer lb.Release() | |
109 | vb := lb.ValueBuilder().(*array.Int32Builder) | |
110 | vb.Reserve(len(vs)) | |
111 | ||
112 | lb.AppendValues(isValid) | |
113 | for _, v := range vs { | |
114 | vb.Append(v) | |
115 | } | |
116 | ||
117 | arr := lb.NewArray().(*array.FixedSizeList) | |
118 | defer arr.Release() | |
119 | ||
120 | if got, want := arr.DataType().ID(), arrow.FIXED_SIZE_LIST; got != want { | |
121 | t.Fatalf("got=%v, want=%v", got, want) | |
122 | } | |
123 | ||
124 | if got, want := arr.Len(), len(isValid); got != want { | |
125 | t.Fatalf("got=%d, want=%d", got, want) | |
126 | } | |
127 | ||
128 | for i := range lengths { | |
129 | if got, want := arr.IsValid(i), isValid[i]; got != want { | |
130 | t.Fatalf("got[%d]=%v, want[%d]=%v", i, got, i, want) | |
131 | } | |
132 | if got, want := arr.IsNull(i), lengths[i] == 0; got != want { | |
133 | t.Fatalf("got[%d]=%v, want[%d]=%v", i, got, i, want) | |
134 | } | |
135 | } | |
136 | ||
137 | varr := arr.ListValues().(*array.Int32) | |
138 | if got, want := varr.Int32Values(), vs; !reflect.DeepEqual(got, want) { | |
139 | t.Fatalf("got=%v, want=%v", got, want) | |
140 | } | |
141 | } | |
142 | ||
143 | func TestFixedSizeListArrayStringer(t *testing.T) { | |
144 | pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) | |
145 | defer pool.AssertSize(t, 0) | |
146 | ||
147 | const N = 3 | |
148 | var ( | |
149 | vs = [][N]int32{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, -9, -8}} | |
150 | isValid = []bool{true, false, true, true} | |
151 | ) | |
152 | ||
153 | lb := array.NewFixedSizeListBuilder(pool, N, arrow.PrimitiveTypes.Int32) | |
154 | defer lb.Release() | |
155 | ||
156 | vb := lb.ValueBuilder().(*array.Int32Builder) | |
157 | vb.Reserve(len(vs)) | |
158 | ||
159 | for i, v := range vs { | |
160 | lb.Append(isValid[i]) | |
161 | vb.AppendValues(v[:], nil) | |
162 | } | |
163 | ||
164 | arr := lb.NewArray().(*array.FixedSizeList) | |
165 | defer arr.Release() | |
166 | ||
167 | arr.Retain() | |
168 | arr.Release() | |
169 | ||
170 | want := `[[0 1 2] (null) [6 7 8] [9 -9 -8]]` | |
171 | if got, want := arr.String(), want; got != want { | |
172 | t.Fatalf("got=%q, want=%q", got, want) | |
173 | } | |
174 | } | |
175 | ||
176 | func TestFixedSizeListArraySlice(t *testing.T) { | |
177 | pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) | |
178 | defer pool.AssertSize(t, 0) | |
179 | ||
180 | const N = 3 | |
181 | var ( | |
182 | vs = [][N]int32{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, -9, -8}} | |
183 | isValid = []bool{true, false, true, true} | |
184 | ) | |
185 | ||
186 | lb := array.NewFixedSizeListBuilder(pool, N, arrow.PrimitiveTypes.Int32) | |
187 | defer lb.Release() | |
188 | ||
189 | vb := lb.ValueBuilder().(*array.Int32Builder) | |
190 | vb.Reserve(len(vs)) | |
191 | ||
192 | for i, v := range vs { | |
193 | lb.Append(isValid[i]) | |
194 | vb.AppendValues(v[:], nil) | |
195 | } | |
196 | ||
197 | arr := lb.NewArray().(*array.FixedSizeList) | |
198 | defer arr.Release() | |
199 | ||
200 | arr.Retain() | |
201 | arr.Release() | |
202 | ||
203 | want := `[[0 1 2] (null) [6 7 8] [9 -9 -8]]` | |
204 | if got, want := arr.String(), want; got != want { | |
205 | t.Fatalf("got=%q, want=%q", got, want) | |
206 | } | |
207 | ||
208 | sub := array.NewSlice(arr, 1, 3).(*array.FixedSizeList) | |
209 | defer sub.Release() | |
210 | ||
211 | want = `[(null) [6 7 8]]` | |
212 | if got, want := sub.String(), want; got != want { | |
213 | t.Fatalf("got=%q, want=%q", got, want) | |
214 | } | |
215 | } |