nirandaperera commented on a change in pull request #10410:
URL: https://github.com/apache/arrow/pull/10410#discussion_r642728158



##########
File path: cpp/src/arrow/compute/kernels/scalar_if_else_test.cc
##########
@@ -0,0 +1,474 @@
+// 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.
+
+#include <arrow/array.h>
+#include <arrow/compute/api_scalar.h>
+#include <arrow/compute/kernels/test_util.h>
+#include <arrow/testing/gtest_util.h>
+#include <gtest/gtest.h>
+
+namespace arrow {
+namespace compute {
+
+void CheckIfElseOutput(const Datum& cond, const Datum& left, const Datum& 
right,
+                       const Datum& expected) {
+  ASSERT_OK_AND_ASSIGN(Datum datum_out, IfElse(cond, left, right));
+  if (datum_out.is_array()) {
+    std::shared_ptr<Array> result = datum_out.make_array();
+    ASSERT_OK(result->ValidateFull());
+    std::shared_ptr<Array> expected_ = expected.make_array();
+    AssertArraysEqual(*expected_, *result, /*verbose=*/true);
+  } else {  // expecting scalar
+    const std::shared_ptr<Scalar>& result = datum_out.scalar();
+    const std::shared_ptr<Scalar>& expected_ = expected.scalar();
+    AssertScalarsEqual(*expected_, *result, /*verbose=*/true);
+  }
+}
+
+void CheckIfElseOutputAAA(const std::shared_ptr<DataType>& type, const 
std::string& cond,
+                          const std::string& left, const std::string& right,
+                          const std::string& expected) {
+  const std::shared_ptr<Array>& cond_ = ArrayFromJSON(boolean(), cond);
+  const std::shared_ptr<Array>& left_ = ArrayFromJSON(type, left);
+  const std::shared_ptr<Array>& right_ = ArrayFromJSON(type, right);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond_, left_, right_, expected_);
+}
+
+void CheckIfElseOutputAAS(const std::shared_ptr<DataType>& type, const 
std::string& cond,
+                          const std::string& left, const 
std::shared_ptr<Scalar>& right,
+                          const std::string& expected) {
+  const std::shared_ptr<Array>& cond_ = ArrayFromJSON(boolean(), cond);
+  const std::shared_ptr<Array>& left_ = ArrayFromJSON(type, left);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond_, left_, right, expected_);
+}
+
+void CheckIfElseOutputASA(const std::shared_ptr<DataType>& type, const 
std::string& cond,
+                          const std::shared_ptr<Scalar>& left, const 
std::string& right,
+                          const std::string& expected) {
+  const std::shared_ptr<Array>& cond_ = ArrayFromJSON(boolean(), cond);
+  const std::shared_ptr<Array>& right_ = ArrayFromJSON(type, right);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond_, left, right_, expected_);
+}
+
+void CheckIfElseOutputASS(const std::shared_ptr<DataType>& type, const 
std::string& cond,
+                          const std::shared_ptr<Scalar>& left,
+                          const std::shared_ptr<Scalar>& right,
+                          const std::string& expected) {
+  const std::shared_ptr<Array>& cond_ = ArrayFromJSON(boolean(), cond);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond_, left, right, expected_);
+}
+
+void CheckIfElseOutputSAA(const std::shared_ptr<DataType>& type,
+                          const std::shared_ptr<Scalar>& cond, const 
std::string& left,
+                          const std::string& right, const std::string& 
expected) {
+  const std::shared_ptr<Array>& left_ = ArrayFromJSON(type, left);
+  const std::shared_ptr<Array>& right_ = ArrayFromJSON(type, right);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond, left_, right_, expected_);
+}
+
+void CheckIfElseOutputSAS(const std::shared_ptr<DataType>& type,
+                          const std::shared_ptr<Scalar>& cond, const 
std::string& left,
+                          const std::shared_ptr<Scalar>& right,
+                          const std::string& expected) {
+  const std::shared_ptr<Array>& left_ = ArrayFromJSON(type, left);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond, left_, right, expected_);
+}
+
+void CheckIfElseOutputSSA(const std::shared_ptr<DataType>& type,
+                          const std::shared_ptr<Scalar>& cond,
+                          const std::shared_ptr<Scalar>& left, const 
std::string& right,
+                          const std::string& expected) {
+  const std::shared_ptr<Array>& right_ = ArrayFromJSON(type, right);
+  const std::shared_ptr<Array>& expected_ = ArrayFromJSON(type, expected);
+  CheckIfElseOutput(cond, left, right_, expected_);
+}
+
+class TestIfElseKernel : public ::testing::Test {};
+
+template <typename Type>
+class TestIfElsePrimitive : public ::testing::Test {};
+
+using PrimitiveTypes = ::testing::Types<Int8Type, UInt8Type, Int16Type, 
UInt16Type,
+                                        Int32Type, UInt32Type, Int64Type, 
UInt64Type,
+                                        FloatType, DoubleType, Date32Type, 
Date64Type>;
+
+TYPED_TEST_SUITE(TestIfElsePrimitive, PrimitiveTypes);
+
+TYPED_TEST(TestIfElsePrimitive, IfElseFixedSizeRand) {
+  using ArrayType = typename TypeTraits<TypeParam>::ArrayType;
+  auto type = TypeTraits<TypeParam>::type_singleton();
+
+  random::RandomArrayGenerator rand(/*seed=*/0);
+  int64_t len = 1000;
+  auto cond = std::static_pointer_cast<BooleanArray>(
+      rand.ArrayOf(boolean(), len, /*null_probability=*/0.01));
+  auto left = std::static_pointer_cast<ArrayType>(
+      rand.ArrayOf(type, len, /*null_probability=*/0.01));
+  auto right = std::static_pointer_cast<ArrayType>(
+      rand.ArrayOf(type, len, /*null_probability=*/0.01));
+
+  typename TypeTraits<TypeParam>::BuilderType builder;
+
+  for (int64_t i = 0; i < len; ++i) {
+    if (!cond->IsValid(i) || (cond->Value(i) && !left->IsValid(i)) ||
+        (!cond->Value(i) && !right->IsValid(i))) {
+      ASSERT_OK(builder.AppendNull());
+      continue;
+    }
+
+    if (cond->Value(i)) {
+      ASSERT_OK(builder.Append(left->Value(i)));
+    } else {
+      ASSERT_OK(builder.Append(right->Value(i)));
+    }
+  }
+  ASSERT_OK_AND_ASSIGN(auto expected_data, builder.Finish());
+
+  CheckIfElseOutput(cond, left, right, expected_data);
+}
+
+#define IF_ELSE_TEST_GEN(type, l0, l1, l2, l3, r0, r1, r2, r3, valid, valid1)  
          \

Review comment:
       I rewrote the test cases in a more generic way. Could you please take a 
look at it?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to