|
| 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 <gtest/gtest.h> |
| 19 | + |
| 20 | +#include "arrow/testing/generator.h" |
| 21 | + |
| 22 | +namespace arrow::gen { |
| 23 | + |
| 24 | +template <typename CType> |
| 25 | +void CheckStep(const Array& result, CType start, CType step, int64_t length) { |
| 26 | + using ArrowType = typename CTypeTraits<CType>::ArrowType; |
| 27 | + |
| 28 | + ASSERT_OK(result.ValidateFull()); |
| 29 | + ASSERT_EQ(result.type_id(), TypeTraits<ArrowType>::type_singleton()->id()); |
| 30 | + ASSERT_EQ(result.length(), length); |
| 31 | + ASSERT_EQ(result.null_bitmap(), nullptr); |
| 32 | + auto data = result.data()->GetValues<CType>(1); |
| 33 | + CType current = start; |
| 34 | + for (int64_t i = 0; i < length; ++i) { |
| 35 | + ASSERT_EQ(data[i], current); |
| 36 | + current += step; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +TEST(StepTest, Default) { |
| 41 | + for (auto length : {0, 1, 1024}) { |
| 42 | + ARROW_SCOPED_TRACE("length=" + std::to_string(length)); |
| 43 | + ASSERT_OK_AND_ASSIGN(auto array, Step()->Generate(length)); |
| 44 | + CheckStep<uint32_t>(*array, 0, 1, length); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +using NumericCTypes = ::testing::Types<int8_t, uint8_t, int16_t, uint16_t, int32_t, |
| 49 | + uint32_t, int64_t, uint64_t, float, double>; |
| 50 | + |
| 51 | +template <typename CType> |
| 52 | +class TypedStepTest : public ::testing::Test {}; |
| 53 | + |
| 54 | +TYPED_TEST_SUITE(TypedStepTest, NumericCTypes); |
| 55 | + |
| 56 | +TYPED_TEST(TypedStepTest, Basic) { |
| 57 | + for (auto length : {0, 1, 1024}) { |
| 58 | + ARROW_SCOPED_TRACE("length=" + std::to_string(length)); |
| 59 | + for (TypeParam start : |
| 60 | + {std::numeric_limits<TypeParam>::min(), static_cast<TypeParam>(0)}) { |
| 61 | + ARROW_SCOPED_TRACE("start=" + std::to_string(start)); |
| 62 | + for (TypeParam step : |
| 63 | + {static_cast<TypeParam>(0), std::numeric_limits<TypeParam>::epsilon(), |
| 64 | + static_cast<TypeParam>(std::numeric_limits<TypeParam>::max() / |
| 65 | + (length + 1))}) { |
| 66 | + ARROW_SCOPED_TRACE("step=" + std::to_string(step)); |
| 67 | + ASSERT_OK_AND_ASSIGN(auto array, Step(start, step)->Generate(length)); |
| 68 | + CheckStep(*array, start, step, length); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace arrow::gen |
0 commit comments