]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/src/arrow/util/int_util_test.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / util / int_util_test.cc
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 #include <algorithm>
19 #include <cstdint>
20 #include <random>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include <gtest/gtest.h>
26
27 #include "arrow/datum.h"
28 #include "arrow/testing/gtest_util.h"
29 #include "arrow/testing/random.h"
30 #include "arrow/type.h"
31 #include "arrow/util/int_util.h"
32 #include "arrow/util/int_util_internal.h"
33
34 namespace arrow {
35 namespace internal {
36
37 static std::vector<uint8_t> all_widths = {1, 2, 4, 8};
38
39 template <typename T>
40 void CheckUIntWidth(const std::vector<T>& values, uint8_t expected_width) {
41 for (const uint8_t min_width : all_widths) {
42 uint8_t width =
43 DetectUIntWidth(values.data(), static_cast<int64_t>(values.size()), min_width);
44 ASSERT_EQ(width, std::max(min_width, expected_width));
45 width = DetectUIntWidth(values.data(), nullptr, static_cast<int64_t>(values.size()),
46 min_width);
47 ASSERT_EQ(width, std::max(min_width, expected_width));
48 }
49 }
50
51 template <typename T>
52 void CheckUIntWidth(const std::vector<T>& values, const std::vector<uint8_t>& valid_bytes,
53 uint8_t expected_width) {
54 for (const uint8_t min_width : all_widths) {
55 uint8_t width = DetectUIntWidth(values.data(), valid_bytes.data(),
56 static_cast<int64_t>(values.size()), min_width);
57 ASSERT_EQ(width, std::max(min_width, expected_width));
58 }
59 }
60
61 template <typename T>
62 void CheckIntWidth(const std::vector<T>& values, uint8_t expected_width) {
63 for (const uint8_t min_width : all_widths) {
64 uint8_t width =
65 DetectIntWidth(values.data(), static_cast<int64_t>(values.size()), min_width);
66 ASSERT_EQ(width, std::max(min_width, expected_width));
67 width = DetectIntWidth(values.data(), nullptr, static_cast<int64_t>(values.size()),
68 min_width);
69 ASSERT_EQ(width, std::max(min_width, expected_width));
70 }
71 }
72
73 template <typename T>
74 void CheckIntWidth(const std::vector<T>& values, const std::vector<uint8_t>& valid_bytes,
75 uint8_t expected_width) {
76 for (const uint8_t min_width : all_widths) {
77 uint8_t width = DetectIntWidth(values.data(), valid_bytes.data(),
78 static_cast<int64_t>(values.size()), min_width);
79 ASSERT_EQ(width, std::max(min_width, expected_width));
80 }
81 }
82
83 template <typename T>
84 std::vector<T> MakeRandomVector(const std::vector<T>& base_values, int n_values) {
85 std::default_random_engine gen(42);
86 std::uniform_int_distribution<int> index_dist(0,
87 static_cast<int>(base_values.size() - 1));
88
89 std::vector<T> values(n_values);
90 for (int i = 0; i < n_values; ++i) {
91 values[i] = base_values[index_dist(gen)];
92 }
93 return values;
94 }
95
96 template <typename T>
97 std::vector<std::pair<std::vector<T>, std::vector<uint8_t>>> AlmostAllNullValues(
98 int n_values, T null_value, T non_null_value) {
99 std::vector<std::pair<std::vector<T>, std::vector<uint8_t>>> vectors;
100 vectors.reserve(n_values);
101 for (int i = 0; i < n_values; ++i) {
102 std::vector<T> values(n_values, null_value);
103 std::vector<uint8_t> valid_bytes(n_values, 0);
104 values[i] = non_null_value;
105 valid_bytes[i] = 1;
106 vectors.push_back({std::move(values), std::move(valid_bytes)});
107 }
108 return vectors;
109 }
110
111 template <typename T>
112 std::vector<std::vector<T>> AlmostAllZeros(int n_values, T nonzero_value) {
113 std::vector<std::vector<T>> vectors;
114 vectors.reserve(n_values);
115 for (int i = 0; i < n_values; ++i) {
116 std::vector<T> values(n_values, 0);
117 values[i] = nonzero_value;
118 vectors.push_back(std::move(values));
119 }
120 return vectors;
121 }
122
123 std::vector<uint64_t> valid_uint8 = {0, 0x7f, 0xff};
124 std::vector<uint64_t> valid_uint16 = {0, 0x7f, 0xff, 0x1000, 0xffff};
125 std::vector<uint64_t> valid_uint32 = {0, 0x7f, 0xff, 0x10000, 0xffffffffULL};
126 std::vector<uint64_t> valid_uint64 = {0, 0x100000000ULL, 0xffffffffffffffffULL};
127
128 TEST(UIntWidth, NoNulls) {
129 std::vector<uint64_t> values{0, 0x7f, 0xff};
130 CheckUIntWidth(values, 1);
131
132 values = {0, 0x100};
133 CheckUIntWidth(values, 2);
134
135 values = {0, 0xffff};
136 CheckUIntWidth(values, 2);
137
138 values = {0, 0x10000};
139 CheckUIntWidth(values, 4);
140
141 values = {0, 0xffffffffULL};
142 CheckUIntWidth(values, 4);
143
144 values = {0, 0x100000000ULL};
145 CheckUIntWidth(values, 8);
146
147 values = {0, 0xffffffffffffffffULL};
148 CheckUIntWidth(values, 8);
149 }
150
151 TEST(UIntWidth, Nulls) {
152 std::vector<uint8_t> valid10{true, false};
153 std::vector<uint8_t> valid01{false, true};
154
155 std::vector<uint64_t> values{0, 0xff};
156 CheckUIntWidth(values, valid01, 1);
157 CheckUIntWidth(values, valid10, 1);
158
159 values = {0, 0x100};
160 CheckUIntWidth(values, valid01, 2);
161 CheckUIntWidth(values, valid10, 1);
162
163 values = {0, 0xffff};
164 CheckUIntWidth(values, valid01, 2);
165 CheckUIntWidth(values, valid10, 1);
166
167 values = {0, 0x10000};
168 CheckUIntWidth(values, valid01, 4);
169 CheckUIntWidth(values, valid10, 1);
170
171 values = {0, 0xffffffffULL};
172 CheckUIntWidth(values, valid01, 4);
173 CheckUIntWidth(values, valid10, 1);
174
175 values = {0, 0x100000000ULL};
176 CheckUIntWidth(values, valid01, 8);
177 CheckUIntWidth(values, valid10, 1);
178
179 values = {0, 0xffffffffffffffffULL};
180 CheckUIntWidth(values, valid01, 8);
181 CheckUIntWidth(values, valid10, 1);
182 }
183
184 TEST(UIntWidth, NoNullsMany) {
185 constexpr int N = 40;
186 for (const auto& values : AlmostAllZeros<uint64_t>(N, 0xff)) {
187 CheckUIntWidth(values, 1);
188 }
189 for (const auto& values : AlmostAllZeros<uint64_t>(N, 0xffff)) {
190 CheckUIntWidth(values, 2);
191 }
192 for (const auto& values : AlmostAllZeros<uint64_t>(N, 0xffffffffULL)) {
193 CheckUIntWidth(values, 4);
194 }
195 for (const auto& values : AlmostAllZeros<uint64_t>(N, 0xffffffffffffffffULL)) {
196 CheckUIntWidth(values, 8);
197 }
198 auto values = MakeRandomVector(valid_uint8, N);
199 CheckUIntWidth(values, 1);
200
201 values = MakeRandomVector(valid_uint16, N);
202 CheckUIntWidth(values, 2);
203
204 values = MakeRandomVector(valid_uint32, N);
205 CheckUIntWidth(values, 4);
206
207 values = MakeRandomVector(valid_uint64, N);
208 CheckUIntWidth(values, 8);
209 }
210
211 TEST(UIntWidth, NullsMany) {
212 constexpr uint64_t huge = 0x123456789abcdefULL;
213 constexpr int N = 40;
214 for (const auto& p : AlmostAllNullValues<uint64_t>(N, 0, 0xff)) {
215 CheckUIntWidth(p.first, p.second, 1);
216 }
217 for (const auto& p : AlmostAllNullValues<uint64_t>(N, huge, 0xff)) {
218 CheckUIntWidth(p.first, p.second, 1);
219 }
220 for (const auto& p : AlmostAllNullValues<uint64_t>(N, 0, 0xffff)) {
221 CheckUIntWidth(p.first, p.second, 2);
222 }
223 for (const auto& p : AlmostAllNullValues<uint64_t>(N, huge, 0xffff)) {
224 CheckUIntWidth(p.first, p.second, 2);
225 }
226 for (const auto& p : AlmostAllNullValues<uint64_t>(N, 0, 0xffffffffULL)) {
227 CheckUIntWidth(p.first, p.second, 4);
228 }
229 for (const auto& p : AlmostAllNullValues<uint64_t>(N, huge, 0xffffffffULL)) {
230 CheckUIntWidth(p.first, p.second, 4);
231 }
232 for (const auto& p : AlmostAllNullValues<uint64_t>(N, 0, 0xffffffffffffffffULL)) {
233 CheckUIntWidth(p.first, p.second, 8);
234 }
235 for (const auto& p : AlmostAllNullValues<uint64_t>(N, huge, 0xffffffffffffffffULL)) {
236 CheckUIntWidth(p.first, p.second, 8);
237 }
238 }
239
240 TEST(IntWidth, NoNulls) {
241 std::vector<int64_t> values{0, 0x7f, -0x80};
242 CheckIntWidth(values, 1);
243
244 values = {0, 0x80};
245 CheckIntWidth(values, 2);
246
247 values = {0, -0x81};
248 CheckIntWidth(values, 2);
249
250 values = {0, 0x7fff, -0x8000};
251 CheckIntWidth(values, 2);
252
253 values = {0, 0x8000};
254 CheckIntWidth(values, 4);
255
256 values = {0, -0x8001};
257 CheckIntWidth(values, 4);
258
259 values = {0, 0x7fffffffLL, -0x80000000LL};
260 CheckIntWidth(values, 4);
261
262 values = {0, 0x80000000LL};
263 CheckIntWidth(values, 8);
264
265 values = {0, -0x80000001LL};
266 CheckIntWidth(values, 8);
267
268 values = {0, 0x7fffffffffffffffLL, -0x7fffffffffffffffLL - 1};
269 CheckIntWidth(values, 8);
270 }
271
272 TEST(IntWidth, Nulls) {
273 std::vector<uint8_t> valid100{true, false, false};
274 std::vector<uint8_t> valid010{false, true, false};
275 std::vector<uint8_t> valid001{false, false, true};
276
277 std::vector<int64_t> values{0, 0x7f, -0x80};
278 CheckIntWidth(values, valid100, 1);
279 CheckIntWidth(values, valid010, 1);
280 CheckIntWidth(values, valid001, 1);
281
282 values = {0, 0x80, -0x81};
283 CheckIntWidth(values, valid100, 1);
284 CheckIntWidth(values, valid010, 2);
285 CheckIntWidth(values, valid001, 2);
286
287 values = {0, 0x7fff, -0x8000};
288 CheckIntWidth(values, valid100, 1);
289 CheckIntWidth(values, valid010, 2);
290 CheckIntWidth(values, valid001, 2);
291
292 values = {0, 0x8000, -0x8001};
293 CheckIntWidth(values, valid100, 1);
294 CheckIntWidth(values, valid010, 4);
295 CheckIntWidth(values, valid001, 4);
296
297 values = {0, 0x7fffffffLL, -0x80000000LL};
298 CheckIntWidth(values, valid100, 1);
299 CheckIntWidth(values, valid010, 4);
300 CheckIntWidth(values, valid001, 4);
301
302 values = {0, 0x80000000LL, -0x80000001LL};
303 CheckIntWidth(values, valid100, 1);
304 CheckIntWidth(values, valid010, 8);
305 CheckIntWidth(values, valid001, 8);
306
307 values = {0, 0x7fffffffffffffffLL, -0x7fffffffffffffffLL - 1};
308 CheckIntWidth(values, valid100, 1);
309 CheckIntWidth(values, valid010, 8);
310 CheckIntWidth(values, valid001, 8);
311 }
312
313 TEST(IntWidth, NoNullsMany) {
314 constexpr int N = 40;
315 // 1 byte wide
316 for (const int64_t value : {0x7f, -0x80}) {
317 for (const auto& values : AlmostAllZeros<int64_t>(N, value)) {
318 CheckIntWidth(values, 1);
319 }
320 }
321 // 2 bytes wide
322 for (const int64_t value : {0x80, -0x81, 0x7fff, -0x8000}) {
323 for (const auto& values : AlmostAllZeros<int64_t>(N, value)) {
324 CheckIntWidth(values, 2);
325 }
326 }
327 // 4 bytes wide
328 for (const int64_t value : {0x8000LL, -0x8001LL, 0x7fffffffLL, -0x80000000LL}) {
329 for (const auto& values : AlmostAllZeros<int64_t>(N, value)) {
330 CheckIntWidth(values, 4);
331 }
332 }
333 // 8 bytes wide
334 for (const int64_t value : {0x80000000LL, -0x80000001LL, 0x7fffffffffffffffLL}) {
335 for (const auto& values : AlmostAllZeros<int64_t>(N, value)) {
336 CheckIntWidth(values, 8);
337 }
338 }
339 }
340
341 TEST(IntWidth, NullsMany) {
342 constexpr int64_t huge = 0x123456789abcdefLL;
343 constexpr int N = 40;
344 // 1 byte wide
345 for (const int64_t value : {0x7f, -0x80}) {
346 for (const auto& p : AlmostAllNullValues<int64_t>(N, 0, value)) {
347 CheckIntWidth(p.first, p.second, 1);
348 }
349 for (const auto& p : AlmostAllNullValues<int64_t>(N, huge, value)) {
350 CheckIntWidth(p.first, p.second, 1);
351 }
352 }
353 // 2 bytes wide
354 for (const int64_t value : {0x80, -0x81, 0x7fff, -0x8000}) {
355 for (const auto& p : AlmostAllNullValues<int64_t>(N, 0, value)) {
356 CheckIntWidth(p.first, p.second, 2);
357 }
358 for (const auto& p : AlmostAllNullValues<int64_t>(N, huge, value)) {
359 CheckIntWidth(p.first, p.second, 2);
360 }
361 }
362 // 4 bytes wide
363 for (const int64_t value : {0x8000LL, -0x8001LL, 0x7fffffffLL, -0x80000000LL}) {
364 for (const auto& p : AlmostAllNullValues<int64_t>(N, 0, value)) {
365 CheckIntWidth(p.first, p.second, 4);
366 }
367 for (const auto& p : AlmostAllNullValues<int64_t>(N, huge, value)) {
368 CheckIntWidth(p.first, p.second, 4);
369 }
370 }
371 // 8 bytes wide
372 for (const int64_t value : {0x80000000LL, -0x80000001LL, 0x7fffffffffffffffLL}) {
373 for (const auto& p : AlmostAllNullValues<int64_t>(N, 0, value)) {
374 CheckIntWidth(p.first, p.second, 8);
375 }
376 for (const auto& p : AlmostAllNullValues<int64_t>(N, huge, value)) {
377 CheckIntWidth(p.first, p.second, 8);
378 }
379 }
380 }
381
382 TEST(TransposeInts, Int8ToInt64) {
383 std::vector<int8_t> src = {1, 3, 5, 0, 3, 2};
384 std::vector<int32_t> transpose_map = {1111, 2222, 3333, 4444, 5555, 6666, 7777};
385 std::vector<int64_t> dest(src.size());
386
387 TransposeInts(src.data(), dest.data(), 6, transpose_map.data());
388 ASSERT_EQ(dest, std::vector<int64_t>({2222, 4444, 6666, 1111, 4444, 3333}));
389 }
390
391 void BoundsCheckPasses(const std::shared_ptr<DataType>& type,
392 const std::string& indices_json, uint64_t upper_limit) {
393 auto indices = ArrayFromJSON(type, indices_json);
394 ASSERT_OK(CheckIndexBounds(*indices->data(), upper_limit));
395 }
396
397 void BoundsCheckFails(const std::shared_ptr<DataType>& type,
398 const std::string& indices_json, uint64_t upper_limit) {
399 auto indices = ArrayFromJSON(type, indices_json);
400 ASSERT_RAISES(IndexError, CheckIndexBounds(*indices->data(), upper_limit));
401 }
402
403 TEST(CheckIndexBounds, Batching) {
404 auto rand = random::RandomArrayGenerator(/*seed=*/0);
405
406 const int64_t length = 200;
407
408 auto indices = rand.Int16(length, 0, 0, /*null_probability=*/0);
409 ArrayData* index_data = indices->data().get();
410 index_data->buffers[0] = *AllocateBitmap(length);
411
412 int16_t* values = index_data->GetMutableValues<int16_t>(1);
413 uint8_t* bitmap = index_data->buffers[0]->mutable_data();
414 BitUtil::SetBitsTo(bitmap, 0, length, true);
415
416 ASSERT_OK(CheckIndexBounds(*index_data, 1));
417
418 // We'll place out of bounds indices at various locations
419 values[99] = 1;
420 ASSERT_RAISES(IndexError, CheckIndexBounds(*index_data, 1));
421
422 // Make that value null
423 BitUtil::ClearBit(bitmap, 99);
424 ASSERT_OK(CheckIndexBounds(*index_data, 1));
425
426 values[199] = 1;
427 ASSERT_RAISES(IndexError, CheckIndexBounds(*index_data, 1));
428
429 // Make that value null
430 BitUtil::ClearBit(bitmap, 199);
431 ASSERT_OK(CheckIndexBounds(*index_data, 1));
432 }
433
434 TEST(CheckIndexBounds, SignedInts) {
435 auto CheckCommon = [&](const std::shared_ptr<DataType>& ty) {
436 BoundsCheckPasses(ty, "[0, 0, 0]", 1);
437 BoundsCheckFails(ty, "[0, 0, 0]", 0);
438 BoundsCheckFails(ty, "[-1]", 1);
439 BoundsCheckFails(ty, "[-128]", 1);
440 BoundsCheckFails(ty, "[0, 100, 127]", 127);
441 BoundsCheckPasses(ty, "[0, 100, 127]", 128);
442 };
443
444 CheckCommon(int8());
445
446 CheckCommon(int16());
447 BoundsCheckPasses(int16(), "[0, 999, 999]", 1000);
448 BoundsCheckFails(int16(), "[0, 1000, 1000]", 1000);
449 BoundsCheckPasses(int16(), "[0, 32767]", 1 << 15);
450
451 CheckCommon(int32());
452 BoundsCheckPasses(int32(), "[0, 999999, 999999]", 1000000);
453 BoundsCheckFails(int32(), "[0, 1000000, 1000000]", 1000000);
454 BoundsCheckPasses(int32(), "[0, 2147483647]", 1LL << 31);
455
456 CheckCommon(int64());
457 BoundsCheckPasses(int64(), "[0, 9999999999, 9999999999]", 10000000000LL);
458 BoundsCheckFails(int64(), "[0, 10000000000, 10000000000]", 10000000000LL);
459 }
460
461 TEST(CheckIndexBounds, UnsignedInts) {
462 auto CheckCommon = [&](const std::shared_ptr<DataType>& ty) {
463 BoundsCheckPasses(ty, "[0, 0, 0]", 1);
464 BoundsCheckFails(ty, "[0, 0, 0]", 0);
465 BoundsCheckFails(ty, "[0, 100, 200]", 200);
466 BoundsCheckPasses(ty, "[0, 100, 200]", 201);
467 };
468
469 CheckCommon(uint8());
470 BoundsCheckPasses(uint8(), "[255, 255, 255]", 1000);
471 BoundsCheckFails(uint8(), "[255, 255, 255]", 255);
472
473 CheckCommon(uint16());
474 BoundsCheckPasses(uint16(), "[0, 999, 999]", 1000);
475 BoundsCheckFails(uint16(), "[0, 1000, 1000]", 1000);
476 BoundsCheckPasses(uint16(), "[0, 65535]", 1 << 16);
477
478 CheckCommon(uint32());
479 BoundsCheckPasses(uint32(), "[0, 999999, 999999]", 1000000);
480 BoundsCheckFails(uint32(), "[0, 1000000, 1000000]", 1000000);
481 BoundsCheckPasses(uint32(), "[0, 4294967295]", 1LL << 32);
482
483 CheckCommon(uint64());
484 BoundsCheckPasses(uint64(), "[0, 9999999999, 9999999999]", 10000000000LL);
485 BoundsCheckFails(uint64(), "[0, 10000000000, 10000000000]", 10000000000LL);
486 }
487
488 void CheckInRangePasses(const std::shared_ptr<DataType>& type,
489 const std::string& values_json, const std::string& limits_json) {
490 auto values = ArrayFromJSON(type, values_json);
491 auto limits = ArrayFromJSON(type, limits_json);
492 ASSERT_OK(CheckIntegersInRange(Datum(values->data()), **limits->GetScalar(0),
493 **limits->GetScalar(1)));
494 }
495
496 void CheckInRangeFails(const std::shared_ptr<DataType>& type,
497 const std::string& values_json, const std::string& limits_json) {
498 auto values = ArrayFromJSON(type, values_json);
499 auto limits = ArrayFromJSON(type, limits_json);
500 ASSERT_RAISES(Invalid,
501 CheckIntegersInRange(Datum(values->data()), **limits->GetScalar(0),
502 **limits->GetScalar(1)));
503 }
504
505 TEST(CheckIntegersInRange, Batching) {
506 auto rand = random::RandomArrayGenerator(/*seed=*/0);
507
508 const int64_t length = 200;
509
510 auto indices = rand.Int16(length, 0, 0, /*null_probability=*/0);
511 ArrayData* index_data = indices->data().get();
512 index_data->buffers[0] = *AllocateBitmap(length);
513
514 int16_t* values = index_data->GetMutableValues<int16_t>(1);
515 uint8_t* bitmap = index_data->buffers[0]->mutable_data();
516 BitUtil::SetBitsTo(bitmap, 0, length, true);
517
518 auto zero = std::make_shared<Int16Scalar>(0);
519 auto one = std::make_shared<Int16Scalar>(1);
520
521 ASSERT_OK(CheckIntegersInRange(*index_data, *zero, *one));
522
523 // 1 is included
524 values[99] = 1;
525 ASSERT_OK(CheckIntegersInRange(*index_data, *zero, *one));
526
527 // We'll place out of bounds indices at various locations
528 values[99] = 2;
529 ASSERT_RAISES(Invalid, CheckIntegersInRange(*index_data, *zero, *one));
530
531 // Make that value null
532 BitUtil::ClearBit(bitmap, 99);
533 ASSERT_OK(CheckIntegersInRange(*index_data, *zero, *one));
534
535 values[199] = 2;
536 ASSERT_RAISES(Invalid, CheckIntegersInRange(*index_data, *zero, *one));
537
538 // Make that value null
539 BitUtil::ClearBit(bitmap, 199);
540 ASSERT_OK(CheckIntegersInRange(*index_data, *zero, *one));
541 }
542
543 TEST(CheckIntegersInRange, SignedInts) {
544 auto CheckCommon = [&](const std::shared_ptr<DataType>& ty) {
545 CheckInRangePasses(ty, "[0, 0, 0]", "[0, 0]");
546 CheckInRangeFails(ty, "[0, 1, 0]", "[0, 0]");
547 CheckInRangeFails(ty, "[1, 1, 1]", "[2, 4]");
548 CheckInRangeFails(ty, "[-1]", "[0, 0]");
549 CheckInRangeFails(ty, "[-128]", "[-127, 0]");
550 CheckInRangeFails(ty, "[0, 100, 127]", "[0, 126]");
551 CheckInRangePasses(ty, "[0, 100, 127]", "[0, 127]");
552 };
553
554 CheckCommon(int8());
555
556 CheckCommon(int16());
557 CheckInRangePasses(int16(), "[0, 999, 999]", "[0, 999]");
558 CheckInRangeFails(int16(), "[0, 1000, 1000]", "[0, 999]");
559
560 CheckCommon(int32());
561 CheckInRangePasses(int32(), "[0, 999999, 999999]", "[0, 999999]");
562 CheckInRangeFails(int32(), "[0, 1000000, 1000000]", "[0, 999999]");
563
564 CheckCommon(int64());
565 CheckInRangePasses(int64(), "[0, 9999999999, 9999999999]", "[0, 9999999999]");
566 CheckInRangeFails(int64(), "[0, 10000000000, 10000000000]", "[0, 9999999999]");
567 }
568
569 TEST(CheckIntegersInRange, UnsignedInts) {
570 auto CheckCommon = [&](const std::shared_ptr<DataType>& ty) {
571 CheckInRangePasses(ty, "[0, 0, 0]", "[0, 0]");
572 CheckInRangeFails(ty, "[0, 1, 0]", "[0, 0]");
573 CheckInRangeFails(ty, "[1, 1, 1]", "[2, 4]");
574 CheckInRangeFails(ty, "[0, 100, 200]", "[0, 199]");
575 CheckInRangePasses(ty, "[0, 100, 200]", "[0, 200]");
576 };
577
578 CheckCommon(uint8());
579 CheckInRangePasses(uint8(), "[255, 255, 255]", "[0, 255]");
580
581 CheckCommon(uint16());
582 CheckInRangePasses(uint16(), "[0, 999, 999]", "[0, 999]");
583 CheckInRangeFails(uint16(), "[0, 1000, 1000]", "[0, 999]");
584 CheckInRangePasses(uint16(), "[0, 65535]", "[0, 65535]");
585
586 CheckCommon(uint32());
587 CheckInRangePasses(uint32(), "[0, 999999, 999999]", "[0, 999999]");
588 CheckInRangeFails(uint32(), "[0, 1000000, 1000000]", "[0, 999999]");
589 CheckInRangePasses(uint32(), "[0, 4294967295]", "[0, 4294967295]");
590
591 CheckCommon(uint64());
592 CheckInRangePasses(uint64(), "[0, 9999999999, 9999999999]", "[0, 9999999999]");
593 CheckInRangeFails(uint64(), "[0, 10000000000, 10000000000]", "[0, 9999999999]");
594 }
595
596 } // namespace internal
597 } // namespace arrow