]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/ruby/red-arrow/test/test-struct-array-builder.rb
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / ruby / red-arrow / test / test-struct-array-builder.rb
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,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18 class StructArrayBuilderTest < Test::Unit::TestCase
19 def setup
20 @data_type = Arrow::StructDataType.new(visible: {type: :boolean},
21 count: {type: :uint64})
22 @builder = Arrow::StructArrayBuilder.new(@data_type)
23 end
24
25 sub_test_case("#append_value") do
26 test("nil") do
27 @builder.append_value(nil)
28 array = @builder.finish
29 assert_equal([
30 [false],
31 [0],
32 ],
33 [
34 array.find_field(0).to_a,
35 array.find_field(1).to_a,
36 ])
37 end
38
39 test("Array") do
40 @builder.append_value([true, 1])
41 @builder.append_value([])
42 @builder.append_value([false])
43 array = @builder.finish
44 assert_equal([
45 [true, nil, false],
46 [1, nil, nil],
47 ],
48 [
49 array.find_field(0).to_a,
50 array.find_field(1).to_a,
51 ])
52 end
53
54 test("Arrow::Struct") do
55 source_array = Arrow::StructArray.new(@data_type, [[true, 1]])
56 struct = source_array.get_value(0)
57 @builder.append_value(struct)
58 array = @builder.finish
59 assert_equal([
60 [true],
61 [1],
62 ],
63 [
64 array.find_field(0).to_a,
65 array.find_field(1).to_a,
66 ])
67 end
68
69 test("Hash") do
70 @builder.append_value(count: 1, visible: true)
71 @builder.append_value(visible: false)
72 @builder.append_value(count: 2)
73 array = @builder.finish
74 assert_equal([
75 [true, false, nil],
76 [1, nil, 2],
77 ],
78 [
79 array.find_field(0).to_a,
80 array.find_field(1).to_a,
81 ])
82 end
83 end
84
85 sub_test_case("#append_values") do
86 test("[nil]") do
87 @builder.append_values([nil])
88 array = @builder.finish
89 assert_equal([
90 [false],
91 [0],
92 ],
93 [
94 array.find_field(0).to_a,
95 array.find_field(1).to_a,
96 ])
97 end
98
99 test("[Array]") do
100 @builder.append_values([[true, 1]])
101 array = @builder.finish
102 assert_equal([
103 [true],
104 [1],
105 ],
106 [
107 array.find_field(0).to_a,
108 array.find_field(1).to_a,
109 ])
110 end
111
112 test("[Hash]") do
113 @builder.append_values([{count: 1, visible: true}])
114 array = @builder.finish
115 assert_equal([
116 [true],
117 [1],
118 ],
119 [
120 array.find_field(0).to_a,
121 array.find_field(1).to_a,
122 ])
123 end
124
125 test("[nil, Array, Hash]") do
126 @builder.append_values([
127 nil,
128 [true, 1],
129 {count: 2, visible: false},
130 ])
131 array = @builder.finish
132 assert_equal([
133 [false, true, false],
134 [0, 1, 2],
135 ],
136 [
137 array.find_field(0).to_a,
138 array.find_field(1).to_a,
139 ])
140 end
141
142 test("is_valids") do
143 @builder.append_values([
144 [true, 1],
145 [false, 2],
146 [true, 3],
147 ],
148 [
149 true,
150 false,
151 true,
152 ])
153 array = @builder.finish
154 assert_equal([
155 [true, false, true],
156 [1, 0, 3],
157 ],
158 [
159 array.find_field(0).to_a,
160 array.find_field(1).to_a,
161 ])
162 end
163 end
164
165 sub_test_case("#append") do
166 test("backward compatibility") do
167 @builder.append
168 @builder.get_field_builder(0).append(true)
169 @builder.get_field_builder(1).append(1)
170 @builder.append
171 @builder.get_field_builder(0).append(false)
172 @builder.get_field_builder(1).append(2)
173 array = @builder.finish
174 assert_equal([
175 {"visible" => true, "count" => 1},
176 {"visible" => false, "count" => 2},
177 ],
178 [
179 array.get_value(0),
180 array.get_value(1),
181 ])
182 end
183 end
184 end