File: test-array.rb

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (236 lines) | stat: -rw-r--r-- 7,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# 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.

class TestArray < Test::Unit::TestCase
  include Helper::Buildable

  def test_equal
    assert_equal(build_boolean_array([true, false]),
                 build_boolean_array([true, false]))
  end

  def test_equal_approx
    array1 = build_double_array([1.1, 2.2 + Float::EPSILON * 10])
    array2 = build_double_array([1.1, 2.2])
    assert do
      array1.equal_approx(array2)
    end
  end

  def test_equal_range
    array1 = build_int32_array([1, 2, 3, 4, 5])
    array2 = build_int32_array([-2, -1, 0, 1, 2, 3, 4, 999])
    assert do
      array1.equal_range(1, array2, 4, 3)
    end
  end

  def test_is_null
    builder = Arrow::BooleanArrayBuilder.new
    builder.append_null
    builder.append_value(true)
    array = builder.finish
    assert_equal([true, false],
                 array.length.times.collect {|i| array.null?(i)})
  end

  def test_is_valid
    builder = Arrow::BooleanArrayBuilder.new
    builder.append_null
    builder.append_value(true)
    array = builder.finish
    assert_equal([false, true],
                 array.length.times.collect {|i| array.valid?(i)})
  end

  def test_length
    builder = Arrow::BooleanArrayBuilder.new
    builder.append_value(true)
    array = builder.finish
    assert_equal(1, array.length)
  end

  def test_n_nulls
    builder = Arrow::BooleanArrayBuilder.new
    builder.append_null
    builder.append_null
    array = builder.finish
    assert_equal(2, array.n_nulls)
  end

  def test_null_bitmap
    builder = Arrow::BooleanArrayBuilder.new
    builder.append_null
    builder.append_value(true)
    builder.append_value(false)
    builder.append_null
    builder.append_value(false)
    array = builder.finish
    assert_equal(0b10110, array.null_bitmap.data.to_s.unpack("c*")[0])
  end

  def test_value_data_type
    builder = Arrow::BooleanArrayBuilder.new
    array = builder.finish
    assert_equal(Arrow::BooleanDataType.new, array.value_data_type)
  end

  def test_value_type
    builder = Arrow::BooleanArrayBuilder.new
    array = builder.finish
    assert_equal(Arrow::Type::BOOLEAN, array.value_type)
  end

  def test_slice
    builder = Arrow::BooleanArrayBuilder.new
    builder.append_value(true)
    builder.append_value(false)
    builder.append_value(true)
    array = builder.finish
    sub_array = array.slice(1, 2)
    assert_equal([false, true],
                 sub_array.length.times.collect {|i| sub_array.get_value(i)})
  end

  def test_to_s
    assert_equal(<<-CONTENT.chomp, build_boolean_array([true, false, true]).to_s)
[
  true,
  false,
  true
]
    CONTENT
  end

  sub_test_case("#view") do
    def test_valid
      int32_array = build_int32_array([0, 1069547520, -1071644672, nil])
      assert_equal(build_float_array([0.0, 1.5, -2.5, nil]),
                   int32_array.view(Arrow::FloatDataType.new))
    end

    def test_invalid
      message = "[array][view]: Invalid: " +
                "Can't view array of type int16 as int8: incompatible layouts"
      error = assert_raise(Arrow::Error::Invalid) do
        build_int16_array([0, -1, 3]).view(Arrow::Int8DataType.new)
      end
      assert_equal(message, error.message.lines.first.chomp)
    end
  end

  sub_test_case("#diff_unified") do
    def test_no_diff
      array = build_string_array(["Start", "Shutdown", "Reboot"])
      other_array = build_string_array(["Start", "Shutdown", "Reboot"])
      assert_nil(array.diff_unified(other_array))
    end

    def test_diff
      array = build_string_array(["Start", "Shutdown", "Reboot"])
      other_array = build_string_array(["Start", "Running", "Reboot"])
      assert_equal(<<-STRING.chomp, array.diff_unified(other_array))

@@ -1, +1 @@
-"Shutdown"
+"Running"

      STRING
    end

    def test_different_type
      array = build_string_array(["Start", "Shutdown", "Reboot"])
      other_array = build_int8_array([2, 3, 6, 10])
      assert_equal("# Array types differed: string vs int8\n",
                   array.diff_unified(other_array))
    end
  end

  sub_test_case("#concatenate") do
    def test_no_other_arrays
      assert_equal(build_int32_array([1, 2, 3]),
                   build_int32_array([1, 2, 3]).concatenate([]))
    end

    def test_multiple_other_arrays
      a = build_int32_array([1, 2, 3])
      b = build_int32_array([4])
      c = build_int32_array([5, 6])
      assert_equal(build_int32_array([1, 2, 3, 4, 5, 6]),
                   a.concatenate([b, c]))
    end

    def test_mixed_type
      int32_array = build_int32_array([1, 2, 3])
      uint32_array = build_uint32_array([4])
      message =
        "[array][concatenate]: Invalid: " +
        "arrays to be concatenated must be identically typed, " +
        "but int32 and uint32 were encountered."
      assert_raise(Arrow::Error::Invalid.new(message)) do
        int32_array.concatenate([uint32_array])
      end
    end
  end

  sub_test_case("#validate") do
    def test_valid
      array = build_int32_array([1, 2, 3, 4, 5])
      assert do
        array.validate
      end
    end

    def test_invalid
      message = "[array][validate]: Invalid: Array length is negative"
      array = Arrow::Int8Array.new(-1, Arrow::Buffer.new(""), Arrow::Buffer.new(""), -1)
      error = assert_raise(Arrow::Error::Invalid) do
        array.validate
      end
      assert_equal(message,
                   error.message.lines.first.chomp)
    end
  end

  sub_test_case("#validate_full") do
    def test_valid
      array = build_int32_array([1, 2, 3, 4, 5])
      assert do
        array.validate_full
      end
    end

    def test_invalid
      message = "[array][validate-full]: Invalid: Invalid UTF8 sequence at string index 0"

      # U+3042 HIRAGANA LETTER A, U+3044 HIRAGANA LETTER I
      data = "\u3042\u3044".b[0..-2]
      value_offsets = Arrow::Buffer.new([0, data.size].pack("l*"))
      array = Arrow::StringArray.new(1,
                                     value_offsets,
                                     Arrow::Buffer.new(data),
                                     Arrow::Buffer.new([0b01].pack("C*")),
                                     -1)

      error = assert_raise(Arrow::Error::Invalid) do
        array.validate_full
      end
      assert_equal(message,
                   error.message.lines.first.chomp)
    end
  end
end