@@ -45,6 +45,177 @@ TEST_F(TestArray, Attrs) {
45
45
ASSERT_EQ (values_.size (), array_->length ());
46
46
}
47
47
48
+ template <template <typename > class LEFT_ARRAY_TYPE , typename LEFT_DATA_TYPE,
49
+ template <typename > class RIGHT_ARRAY_TYPE , typename RIGHT_DATA_TYPE,
50
+ std::size_t LENGTH = 10 >
51
+ class OperatorTest {
52
+ public:
53
+ OperatorTest ()
54
+ : left_buffer_(std::make_shared<Buffer>(
55
+ reinterpret_cast <const std::uint8_t *>(Initialize(left_data_)),
56
+ LENGTH * sizeof (LEFT_C_TYPE))),
57
+ right_buffer_ (std::make_shared<Buffer>(
58
+ reinterpret_cast <const std::uint8_t *>(Initialize(right_data_)),
59
+ LENGTH * sizeof(RIGHT_C_TYPE))),
60
+ left_array_(LENGTH, left_buffer_),
61
+ right_array_(LENGTH, right_buffer_) {}
62
+
63
+ template <typename OPERATOR>
64
+ void TestOperator (OPERATOR& operation) {
65
+ auto result = operation (left_array_, right_array_);
66
+ for (auto ii = 0 ; ii < left_array_.length (); ++ii) {
67
+ ASSERT_EQ (result.data ()[ii], operation (left_data_[ii], right_data_[ii]));
68
+ }
69
+ }
70
+
71
+ template <typename OPERATOR, typename INPLACE_OPERATOR>
72
+ void TestInplaceOperator (OPERATOR& operation, INPLACE_OPERATOR& inplace_operation) {
73
+ auto result = operation (left_array_, right_array_);
74
+ for (auto ii = 0 ; ii < left_array_.length (); ++ii) {
75
+ ASSERT_EQ (result.data ()[ii], operation (left_data_[ii], right_data_[ii]));
76
+ }
77
+ inplace_operation (left_array_, right_array_);
78
+ for (auto ii = 0 ; ii < left_array_.length (); ++ii) {
79
+ ASSERT_EQ (left_array_.data ()[ii], operation (left_data_[ii], right_data_[ii]));
80
+ }
81
+ for (auto ii = 0 ; ii < left_array_.length (); ++ii) {
82
+ ASSERT_EQ (left_array_.data ()[ii], result.data ()[ii]);
83
+ }
84
+ }
85
+
86
+ private:
87
+ template <typename C_TYPE>
88
+ static C_TYPE* Initialize (C_TYPE (&value)[LENGTH]) {
89
+ for (auto ii = 0 ; ii < LENGTH; ++ii) {
90
+ // Start at 1 so that we don't get FPE with operator/
91
+ value[ii] = static_cast <C_TYPE>(ii + 1 );
92
+ }
93
+ return value;
94
+ }
95
+
96
+ using LEFT_C_TYPE = typename LEFT_DATA_TYPE::c_type;
97
+
98
+ using RIGHT_C_TYPE = typename RIGHT_DATA_TYPE::c_type;
99
+
100
+ LEFT_C_TYPE left_data_[LENGTH];
101
+
102
+ RIGHT_C_TYPE right_data_[LENGTH];
103
+
104
+ std::shared_ptr<Buffer> left_buffer_;
105
+
106
+ std::shared_ptr<Buffer> right_buffer_;
107
+
108
+ LEFT_ARRAY_TYPE<LEFT_DATA_TYPE> left_array_;
109
+
110
+ RIGHT_ARRAY_TYPE<RIGHT_DATA_TYPE> right_array_;
111
+ };
112
+
113
+ TEST (TestArrayOperators, Addition) {
114
+ auto plus = [](auto const & left, auto const & right) { return left + right; };
115
+ auto plus_inplace = [](auto & left, auto const & right) { left += right; };
116
+
117
+ OperatorTest<IntegerArray, UInt8Type, IntegerArray, UInt16Type>().TestInplaceOperator (
118
+ plus, plus_inplace);
119
+ OperatorTest<IntegerArray, UInt16Type, IntegerArray, UInt8Type>().TestInplaceOperator (
120
+ plus, plus_inplace);
121
+ OperatorTest<IntegerArray, Int8Type, IntegerArray, Int16Type>().TestInplaceOperator (
122
+ plus, plus_inplace);
123
+ OperatorTest<IntegerArray, Int16Type, IntegerArray, Int8Type>().TestInplaceOperator (
124
+ plus, plus_inplace);
125
+ OperatorTest<IntegerArray, UInt32Type, IntegerArray, UInt16Type>().TestInplaceOperator (
126
+ plus, plus_inplace);
127
+ OperatorTest<IntegerArray, UInt64Type, IntegerArray, UInt32Type>().TestInplaceOperator (
128
+ plus, plus_inplace);
129
+ OperatorTest<IntegerArray, Int32Type, IntegerArray, Int64Type>().TestInplaceOperator (
130
+ plus, plus_inplace);
131
+ OperatorTest<IntegerArray, Int64Type, IntegerArray, Int32Type>().TestInplaceOperator (
132
+ plus, plus_inplace);
133
+
134
+ OperatorTest<IntegerArray, Int8Type, IntegerArray, Int8Type>().TestInplaceOperator (
135
+ plus, plus_inplace);
136
+ OperatorTest<IntegerArray, Int16Type, IntegerArray, Int16Type>().TestInplaceOperator (
137
+ plus, plus_inplace);
138
+ OperatorTest<IntegerArray, Int32Type, IntegerArray, Int32Type>().TestInplaceOperator (
139
+ plus, plus_inplace);
140
+ OperatorTest<IntegerArray, Int64Type, IntegerArray, Int64Type>().TestInplaceOperator (
141
+ plus, plus_inplace);
142
+ OperatorTest<IntegerArray, UInt8Type, IntegerArray, UInt8Type>().TestInplaceOperator (
143
+ plus, plus_inplace);
144
+ OperatorTest<IntegerArray, UInt16Type, IntegerArray, UInt16Type>().TestInplaceOperator (
145
+ plus, plus_inplace);
146
+ OperatorTest<IntegerArray, UInt32Type, IntegerArray, UInt32Type>().TestInplaceOperator (
147
+ plus, plus_inplace);
148
+ OperatorTest<IntegerArray, UInt64Type, IntegerArray, UInt64Type>().TestInplaceOperator (
149
+ plus, plus_inplace);
150
+
151
+ OperatorTest<FloatingArray, FloatType, IntegerArray, UInt8Type>().TestInplaceOperator (
152
+ plus, plus_inplace);
153
+ OperatorTest<FloatingArray, FloatType, IntegerArray, UInt64Type>().TestInplaceOperator (
154
+ plus, plus_inplace);
155
+ OperatorTest<FloatingArray, FloatType, IntegerArray, Int8Type>().TestInplaceOperator (
156
+ plus, plus_inplace);
157
+ OperatorTest<FloatingArray, FloatType, IntegerArray, Int64Type>().TestInplaceOperator (
158
+ plus, plus_inplace);
159
+
160
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, UInt16Type>().TestInplaceOperator (
161
+ plus, plus_inplace);
162
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, UInt64Type>().TestInplaceOperator (
163
+ plus, plus_inplace);
164
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, Int16Type>().TestInplaceOperator (
165
+ plus, plus_inplace);
166
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, Int64Type>().TestInplaceOperator (
167
+ plus, plus_inplace);
168
+ }
169
+
170
+ TEST (TestArrayOperators, Division) {
171
+ auto divide = [](auto const & left, auto const & right) { return left / right; };
172
+ auto divide_inplace = [](auto & left, auto const & right) { left /= right; };
173
+
174
+ OperatorTest<IntegerArray, UInt8Type, IntegerArray, UInt16Type>().TestOperator (divide);
175
+ OperatorTest<IntegerArray, UInt16Type, IntegerArray, UInt8Type>().TestOperator (divide);
176
+ OperatorTest<IntegerArray, Int8Type, IntegerArray, Int16Type>().TestOperator (divide);
177
+ OperatorTest<IntegerArray, Int16Type, IntegerArray, Int8Type>().TestOperator (divide);
178
+ OperatorTest<IntegerArray, UInt32Type, IntegerArray, UInt16Type>().TestOperator (divide);
179
+ OperatorTest<IntegerArray, UInt64Type, IntegerArray, UInt32Type>().TestOperator (divide);
180
+ OperatorTest<IntegerArray, Int32Type, IntegerArray, Int64Type>().TestOperator (divide);
181
+ OperatorTest<IntegerArray, Int64Type, IntegerArray, Int32Type>().TestOperator (divide);
182
+
183
+ OperatorTest<IntegerArray, Int8Type, IntegerArray, Int8Type>().TestOperator (divide);
184
+ OperatorTest<IntegerArray, Int16Type, IntegerArray, Int16Type>().TestOperator (divide);
185
+ OperatorTest<IntegerArray, Int32Type, IntegerArray, Int32Type>().TestOperator (divide);
186
+ OperatorTest<IntegerArray, Int64Type, IntegerArray, Int64Type>().TestOperator (divide);
187
+ OperatorTest<IntegerArray, UInt8Type, IntegerArray, UInt8Type>().TestOperator (divide);
188
+ OperatorTest<IntegerArray, UInt16Type, IntegerArray, UInt16Type>().TestOperator (divide);
189
+ OperatorTest<IntegerArray, UInt32Type, IntegerArray, UInt32Type>().TestOperator (divide);
190
+ OperatorTest<IntegerArray, UInt64Type, IntegerArray, UInt64Type>().TestOperator (divide);
191
+
192
+ OperatorTest<FloatingArray, FloatType, IntegerArray, UInt8Type>().TestInplaceOperator (
193
+ divide, divide_inplace);
194
+ OperatorTest<FloatingArray, FloatType, IntegerArray, UInt64Type>().TestInplaceOperator (
195
+ divide, divide_inplace);
196
+ OperatorTest<FloatingArray, FloatType, IntegerArray, Int8Type>().TestInplaceOperator (
197
+ divide, divide_inplace);
198
+ OperatorTest<FloatingArray, FloatType, IntegerArray, Int64Type>().TestInplaceOperator (
199
+ divide, divide_inplace);
200
+ OperatorTest<FloatingArray, FloatType, FloatingArray, FloatType>().TestInplaceOperator (
201
+ divide, divide_inplace);
202
+ OperatorTest<FloatingArray, FloatType, FloatingArray, DoubleType>().TestInplaceOperator (
203
+ divide, divide_inplace);
204
+
205
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, UInt16Type>().TestInplaceOperator (
206
+ divide, divide_inplace);
207
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, UInt64Type>().TestInplaceOperator (
208
+ divide, divide_inplace);
209
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, Int16Type>().TestInplaceOperator (
210
+ divide, divide_inplace);
211
+ OperatorTest<FloatingArray, DoubleType, IntegerArray, Int64Type>().TestInplaceOperator (
212
+ divide, divide_inplace);
213
+ OperatorTest<FloatingArray, DoubleType, FloatingArray, FloatType>().TestInplaceOperator (
214
+ divide, divide_inplace);
215
+ OperatorTest<FloatingArray, DoubleType, FloatingArray, DoubleType>()
216
+ .TestInplaceOperator (divide, divide_inplace);
217
+ }
218
+
48
219
// ----------------------------------------------------------------------
49
220
// Array view object
50
221
0 commit comments