|
12 | 12 | #include "pandas/array.h"
|
13 | 13 | #include "pandas/common.h"
|
14 | 14 | #include "pandas/memory.h"
|
| 15 | +#include "pandas/meta/cartesian_product.h" |
15 | 16 | #include "pandas/test-util.h"
|
16 | 17 | #include "pandas/type.h"
|
17 | 18 | #include "pandas/types/numeric.h"
|
@@ -45,6 +46,133 @@ TEST_F(TestArray, Attrs) {
|
45 | 46 | ASSERT_EQ(values_.size(), array_->length());
|
46 | 47 | }
|
47 | 48 |
|
| 49 | +template <typename LEFT_ARRAY_TYPE, typename RIGHT_ARRAY_TYPE, std::size_t LENGTH = 10> |
| 50 | +class OperatorTestData { |
| 51 | + public: |
| 52 | + OperatorTestData() |
| 53 | + : left_buffer_(std::make_shared<Buffer>( |
| 54 | + reinterpret_cast<const std::uint8_t*>(Initialize(left_data_)), |
| 55 | + LENGTH * sizeof(LEFT_C_TYPE))), |
| 56 | + right_buffer_(std::make_shared<Buffer>( |
| 57 | + reinterpret_cast<const std::uint8_t*>(Initialize(right_data_)), |
| 58 | + LENGTH * sizeof(RIGHT_C_TYPE))), |
| 59 | + left_array_(LENGTH, left_buffer_), |
| 60 | + right_array_(LENGTH, right_buffer_) {} |
| 61 | + |
| 62 | + template <typename C_TYPE> |
| 63 | + static C_TYPE* Initialize(C_TYPE (&value)[LENGTH]) { |
| 64 | + for (auto ii = 0; ii < LENGTH; ++ii) { |
| 65 | + // Start at 1 so that we don't get FPE with operator/ |
| 66 | + value[ii] = static_cast<C_TYPE>(ii + 1); |
| 67 | + } |
| 68 | + return value; |
| 69 | + } |
| 70 | + |
| 71 | + using LEFT_C_TYPE = typename LEFT_ARRAY_TYPE::T; |
| 72 | + |
| 73 | + using RIGHT_C_TYPE = typename RIGHT_ARRAY_TYPE::T; |
| 74 | + |
| 75 | + LEFT_C_TYPE left_data_[LENGTH]; |
| 76 | + |
| 77 | + RIGHT_C_TYPE right_data_[LENGTH]; |
| 78 | + |
| 79 | + std::shared_ptr<Buffer> left_buffer_; |
| 80 | + |
| 81 | + std::shared_ptr<Buffer> right_buffer_; |
| 82 | + |
| 83 | + LEFT_ARRAY_TYPE left_array_; |
| 84 | + |
| 85 | + RIGHT_ARRAY_TYPE right_array_; |
| 86 | +}; |
| 87 | + |
| 88 | +template <typename OPERATOR, typename INPLACE_OPERATOR, std::size_t LENGTH = 10> |
| 89 | +class TestInplaceOperator { |
| 90 | + public: |
| 91 | + TestInplaceOperator(OPERATOR& operation, INPLACE_OPERATOR& inplace_operation) |
| 92 | + : operation_(operation), inplace_operation_(inplace_operation) {} |
| 93 | + |
| 94 | + template <typename PAIR> |
| 95 | + void operator()() { |
| 96 | + using FIRST = typename std::tuple_element<0, PAIR>::type; |
| 97 | + using SECOND = typename std::tuple_element<1, PAIR>::type; |
| 98 | + OperatorTestData<FIRST, SECOND> test_data; |
| 99 | + auto result = operation_(test_data.left_array_, test_data.right_array_); |
| 100 | + for (auto ii = 0; ii < test_data.left_array_.length(); ++ii) { |
| 101 | + ASSERT_EQ(result.data()[ii], |
| 102 | + operation_(test_data.left_data_[ii], test_data.right_data_[ii])); |
| 103 | + } |
| 104 | + inplace_operation_(test_data.left_array_, test_data.right_array_); |
| 105 | + for (auto ii = 0; ii < test_data.left_array_.length(); ++ii) { |
| 106 | + ASSERT_EQ(test_data.left_array_.data()[ii], |
| 107 | + operation_(test_data.left_data_[ii], test_data.right_data_[ii])); |
| 108 | + } |
| 109 | + for (auto ii = 0; ii < test_data.left_array_.length(); ++ii) { |
| 110 | + ASSERT_EQ(test_data.left_array_.data()[ii], result.data()[ii]); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private: |
| 115 | + OPERATOR& operation_; |
| 116 | + |
| 117 | + INPLACE_OPERATOR inplace_operation_; |
| 118 | +}; |
| 119 | + |
| 120 | +template <typename OPERATOR, std::size_t LENGTH = 10> |
| 121 | +class TestOperator { |
| 122 | + public: |
| 123 | + TestOperator(OPERATOR& operation) : operation_(operation) {} |
| 124 | + |
| 125 | + template <typename PAIR> |
| 126 | + void operator()() { |
| 127 | + using FIRST = typename std::tuple_element<0, PAIR>::type; |
| 128 | + using SECOND = typename std::tuple_element<1, PAIR>::type; |
| 129 | + OperatorTestData<FIRST, SECOND> test_data; |
| 130 | + auto result = operation_(test_data.left_array_, test_data.right_array_); |
| 131 | + for (auto ii = 0; ii < test_data.left_array_.length(); ++ii) { |
| 132 | + ASSERT_EQ(result.data()[ii], |
| 133 | + operation_(test_data.left_data_[ii], test_data.right_data_[ii])); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private: |
| 138 | + OPERATOR& operation_; |
| 139 | +}; |
| 140 | + |
| 141 | +using IntegerTypes = TypeList<IntegerArray<UInt8Type>, IntegerArray<UInt16Type>, |
| 142 | + IntegerArray<UInt32Type>, IntegerArray<UInt64Type>, IntegerArray<Int8Type>, |
| 143 | + IntegerArray<Int16Type>, IntegerArray<Int32Type>, IntegerArray<Int64Type>>; |
| 144 | + |
| 145 | +using FloatingPointTypes = TypeList<FloatingArray<FloatType>, FloatingArray<DoubleType>>; |
| 146 | + |
| 147 | +using NumericTypes = decltype(IntegerTypes() + FloatingPointTypes()); |
| 148 | + |
| 149 | +TEST(TestArrayOperators, Addition) { |
| 150 | + auto plus = [](auto const& left, auto const& right) { return left + right; }; |
| 151 | + auto plus_inplace = [](auto& left, auto const& right) { left += right; }; |
| 152 | + |
| 153 | + using AdditionTests = |
| 154 | + decltype(typename CartesianProduct<FloatingPointTypes, NumericTypes>::type() + |
| 155 | + CartesianProduct<IntegerTypes, IntegerTypes>::type()); |
| 156 | + |
| 157 | + TestInplaceOperator<decltype(plus), decltype(plus_inplace)> tester(plus, plus_inplace); |
| 158 | + AdditionTests::iterate(tester); |
| 159 | +} |
| 160 | + |
| 161 | +TEST(TestArrayOperators, Division) { |
| 162 | + auto divide = [](auto const& left, auto const& right) { return left / right; }; |
| 163 | + auto divide_inplace = [](auto& left, auto const& right) { left /= right; }; |
| 164 | + |
| 165 | + using DivisionTests = typename CartesianProduct<IntegerTypes>::type; |
| 166 | + TestOperator<decltype(divide)> test(divide); |
| 167 | + DivisionTests::iterate(test); |
| 168 | + |
| 169 | + using InplaceDivisionTests = |
| 170 | + typename CartesianProduct<FloatingPointTypes, NumericTypes>::type; |
| 171 | + TestInplaceOperator<decltype(divide), decltype(divide_inplace)> inplace_test( |
| 172 | + divide, divide_inplace); |
| 173 | + InplaceDivisionTests::iterate(inplace_test); |
| 174 | +} |
| 175 | + |
48 | 176 | // ----------------------------------------------------------------------
|
49 | 177 | // Array view object
|
50 | 178 |
|
|
0 commit comments