COMPMID-3961: Add Logical OR/AND/NOT operator on CL

Change-Id: I612aeed6affa17624fb9044964dd59c41a5c9888
Signed-off-by: Sang-Hoon Park <sang-hoon.park@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4448
Reviewed-by: Pablo Marquez Tello <pablo.tello@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
diff --git a/tests/validation/CL/Logical.cpp b/tests/validation/CL/Logical.cpp
new file mode 100644
index 0000000..ecdb7c8
--- /dev/null
+++ b/tests/validation/CL/Logical.cpp
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2020 Arm Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/CL/functions/CLLogicalAnd.h"
+#include "arm_compute/runtime/CL/functions/CLLogicalNot.h"
+#include "arm_compute/runtime/CL/functions/CLLogicalOr.h"
+#include "arm_compute/runtime/Tensor.h"
+#include "arm_compute/runtime/TensorAllocator.h"
+#include "tests/CL/CLAccessor.h"
+#include "tests/PaddingCalculator.h"
+#include "tests/datasets/ShapeDatasets.h"
+#include "tests/framework/Asserts.h"
+#include "tests/framework/Macros.h"
+#include "tests/framework/datasets/Datasets.h"
+#include "tests/validation/Validation.h"
+#include "tests/validation/fixtures/LogicalFixture.h"
+
+namespace
+{
+using namespace arm_compute;
+
+const auto correct_shape = TensorShape(1, 2, 3, 4); // target shape to check against
+const auto wrong_shape   = TensorShape(1, 2, 2, 4); // wrong shape to check validate logic
+const auto correct_dt    = DataType::U8;            // correct data type to check against
+const auto wrong_dt      = DataType::F32;           // wrong data type to check validate logic
+}
+
+namespace arm_compute
+{
+namespace test
+{
+namespace validation
+{
+TEST_SUITE(CL)
+TEST_SUITE(LogicalOr)
+TEST_SUITE(Validate)
+TEST_CASE(NullPtr, framework::DatasetMode::ALL)
+{
+    Status s = CLLogicalOr::validate(nullptr, nullptr, nullptr);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+
+TEST_CASE(WrongDataType, framework::DatasetMode::ALL)
+{
+    TensorInfo in1{ correct_shape, 1, correct_dt };
+    TensorInfo in2{ correct_shape, 1, wrong_dt };
+    TensorInfo out{ correct_shape, 1, correct_dt };
+
+    Status s = CLLogicalOr::validate(&in1, &in2, &out);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+TEST_SUITE_END() // Validate
+template <typename T>
+using CLLogicalOrFixture = LogicalOrValidationFixture<CLTensor, CLAccessor, CLLogicalOr, T>;
+
+FIXTURE_DATA_TEST_CASE(RunSmall, CLLogicalOrFixture<uint8_t>, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes()))
+{
+    // Validate output
+    validate(CLAccessor(_target), _reference);
+}
+
+FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLLogicalOrFixture<uint8_t>, framework::DatasetMode::ALL, datasets::SmallShapesBroadcast())
+{
+    // Validate output
+    validate(CLAccessor(_target), _reference);
+}
+TEST_SUITE_END() // LogicalOr
+
+TEST_SUITE(LogicalAnd)
+TEST_SUITE(Validate)
+TEST_CASE(NullPtr, framework::DatasetMode::ALL)
+{
+    Status s = CLLogicalAnd::validate(nullptr, nullptr, nullptr);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+
+TEST_CASE(WrongDataType, framework::DatasetMode::ALL)
+{
+    TensorInfo in1{ correct_shape, 1, correct_dt };
+    TensorInfo in2{ correct_shape, 1, wrong_dt };
+    TensorInfo out{ correct_shape, 1, correct_dt };
+
+    Status s = CLLogicalAnd::validate(&in1, &in2, &out);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+TEST_SUITE_END() // Validate
+template <typename T>
+using CLLogicalAndFixture = LogicalAndValidationFixture<CLTensor, CLAccessor, CLLogicalAnd, T>;
+
+FIXTURE_DATA_TEST_CASE(RunSmall, CLLogicalAndFixture<uint8_t>, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes()))
+{
+    // Validate output
+    validate(CLAccessor(_target), _reference);
+}
+
+FIXTURE_DATA_TEST_CASE(RunSmallBroadcast, CLLogicalAndFixture<uint8_t>, framework::DatasetMode::ALL, datasets::SmallShapesBroadcast())
+{
+    // Validate output
+    validate(CLAccessor(_target), _reference);
+}
+TEST_SUITE_END() // LogicalAnd
+TEST_SUITE(LogicalNot)
+
+TEST_SUITE(Validate)
+TEST_CASE(NullPtr, framework::DatasetMode::ALL)
+{
+    Status s = CLLogicalNot::validate(nullptr, nullptr);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+
+TEST_CASE(WrongDataType, framework::DatasetMode::ALL)
+{
+    TensorInfo in{ correct_shape, 1, correct_dt };
+    TensorInfo out{ correct_shape, 1, wrong_dt };
+
+    Status s = CLLogicalNot::validate(&in, &out);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+
+    in  = TensorInfo{ correct_shape, 1, wrong_dt };
+    out = TensorInfo{ correct_shape, 1, correct_dt };
+
+    s = CLLogicalNot::validate(&in, &out);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+
+    in  = TensorInfo{ correct_shape, 1, wrong_dt };
+    out = TensorInfo{ correct_shape, 1, wrong_dt };
+
+    s = CLLogicalNot::validate(&in, &out);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+
+TEST_CASE(WrongShape, framework::DatasetMode::ALL)
+{
+    TensorInfo in{ correct_shape, 1, correct_dt };
+    TensorInfo out{ wrong_shape, 1, correct_dt };
+
+    Status s = CLLogicalNot::validate(&in, &out);
+    ARM_COMPUTE_EXPECT((bool)s == false, framework::LogLevel::ERRORS);
+}
+TEST_SUITE_END() // Validate
+
+template <typename T>
+using CLLogicalNotFixture = LogicalNotValidationFixture<CLTensor, CLAccessor, CLLogicalNot, T>;
+
+FIXTURE_DATA_TEST_CASE(RunSmall, CLLogicalNotFixture<uint8_t>, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("DataType",
+                                                                                                    DataType::U8)))
+{
+    // Validate output
+    validate(CLAccessor(_target), _reference);
+}
+TEST_SUITE_END() // LogicalNot
+TEST_SUITE_END() // CL
+} // namespace validation
+} // namespace test
+} // namespace arm_compute
diff --git a/tests/validation/NEON/Logical.cpp b/tests/validation/NEON/Logical.cpp
index f721e3c..6f1c55b 100644
--- a/tests/validation/NEON/Logical.cpp
+++ b/tests/validation/NEON/Logical.cpp
@@ -41,7 +41,7 @@
 
 TEST_SUITE(LogicalAnd)
 template <typename T>
-using NELogicalAndFixture = LogicalBinaryOperationValidationFixture<Tensor, Accessor, NELogicalAnd, reference::LogicalBinaryOperation::AND, T>;
+using NELogicalAndFixture = LogicalAndValidationFixture<Tensor, Accessor, NELogicalAnd, T>;
 
 FIXTURE_DATA_TEST_CASE(RunSmall, NELogicalAndFixture<uint8_t>, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes()))
 {
@@ -58,7 +58,7 @@
 
 TEST_SUITE(LogicalOr)
 template <typename T>
-using NELogicalOrFixture = LogicalBinaryOperationValidationFixture<Tensor, Accessor, NELogicalOr, reference::LogicalBinaryOperation::OR, T>;
+using NELogicalOrFixture = LogicalOrValidationFixture<Tensor, Accessor, NELogicalOr, T>;
 
 FIXTURE_DATA_TEST_CASE(RunSmall, NELogicalOrFixture<uint8_t>, framework::DatasetMode::ALL, zip(datasets::SmallShapes(), datasets::SmallShapes()))
 {
diff --git a/tests/validation/fixtures/LogicalFixture.h b/tests/validation/fixtures/LogicalFixture.h
index a4817cf..4bedb37 100644
--- a/tests/validation/fixtures/LogicalFixture.h
+++ b/tests/validation/fixtures/LogicalFixture.h
@@ -46,10 +46,10 @@
     template <typename U>
     void fill(U &&tensor, int i)
     {
-        constexpr uint8_t zero              = 0;
-        constexpr uint8_t one               = 0x1;
-        constexpr uint8_t mixed             = 0xAA;
-        constexpr uint8_t mixed_bitwise_not = ~(0xAA);
+        constexpr auto zero              = (uint8_t)0;
+        constexpr auto one               = (uint8_t)0x1;
+        constexpr auto mixed             = (uint8_t)0xAA;
+        constexpr auto mixed_bitwise_not = (uint8_t) ~(0xAA);
 
         library->fill_static_values(tensor, i == 0 ?
                                     std::vector<uint8_t> { zero, one, zero, one, mixed, zero, mixed } :
@@ -70,7 +70,10 @@
     SimpleTensor<T> _reference{};
 };
 
-template <typename TensorType, typename AccessorType, typename FunctionType, reference::LogicalBinaryOperation Op, typename T>
+template <typename T>
+using LogicalBinaryRefFunctionPtrType = SimpleTensor<T>(const SimpleTensor<T> &, const SimpleTensor<T> &);
+
+template <typename TensorType, typename AccessorType, typename FunctionType, typename T, LogicalBinaryRefFunctionPtrType<T> RefFunction>
 class LogicalBinaryOperationValidationFixture : public LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>
 {
     using Parent = LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>;
@@ -114,25 +117,19 @@
         Parent::fill(src0, 0);
         Parent::fill(src1, 1);
 
-        switch(Op)
-        {
-            case reference::LogicalBinaryOperation::OR:
-                return reference::logical_or<T>(src0, src1);
-            case reference::LogicalBinaryOperation::AND:
-                return reference::logical_and<T>(src0, src1);
-            case reference::LogicalBinaryOperation::UNKNOWN:
-            /* fall-through */
-            default:
-                ARM_COMPUTE_ASSERT_FAIL("unknown logical binary operator is given");
-        }
-
-        return SimpleTensor<T> {};
+        return RefFunction(src0, src1);
     }
 
-    static constexpr auto _data_type{ DataType::U8 };
+    static constexpr auto _data_type = DataType::U8;
 };
 
 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
+using LogicalOrValidationFixture = LogicalBinaryOperationValidationFixture<TensorType, AccessorType, FunctionType, T, &reference::logical_or<T>>;
+
+template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
+using LogicalAndValidationFixture = LogicalBinaryOperationValidationFixture<TensorType, AccessorType, FunctionType, T, &reference::logical_and<T>>;
+
+template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
 class LogicalNotValidationFixture : public LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>
 {
     using Parent = LogicalOperationValidationFixtureBase<TensorType, AccessorType, FunctionType, T>;
@@ -178,4 +175,4 @@
 } // namespace validation
 } // namespace test
 } // namespace arm_compute
-#endif /* ARM_COMPUTE_TEST_LOGICAL_FIXTURE */
\ No newline at end of file
+#endif /* ARM_COMPUTE_TEST_LOGICAL_FIXTURE */
diff --git a/tests/validation/reference/Logical.cpp b/tests/validation/reference/Logical.cpp
index 394525c..9989ec8 100644
--- a/tests/validation/reference/Logical.cpp
+++ b/tests/validation/reference/Logical.cpp
@@ -22,6 +22,8 @@
  * SOFTWARE.
  */
 #include "tests/validation/reference/Logical.h"
+#include "src/core/KernelTypes.h"
+#include "tests/framework/Asserts.h"
 
 namespace arm_compute
 {
@@ -32,27 +34,30 @@
 namespace reference
 {
 template <typename T>
-T logical_op(LogicalBinaryOperation op, T src1, T src2)
+T logical_binary_op(arm_compute::kernels::LogicalOperation op, T src1, T src2)
 {
     switch(op)
     {
-        case LogicalBinaryOperation::AND:
+        case arm_compute::kernels::LogicalOperation::And:
             return src1 && src2;
-        case LogicalBinaryOperation::OR:
+        case arm_compute::kernels::LogicalOperation::Or:
             return src1 || src2;
-        case LogicalBinaryOperation::UNKNOWN:
+        // The following operators are either invalid or not binary operator
+        case arm_compute::kernels::LogicalOperation::Not:
+        /* fall through */
+        case arm_compute::kernels::LogicalOperation::Unknown:
+        /* fall through */
         default:
-            ARM_COMPUTE_ERROR_ON_MSG(true, "unknown logical binary operation is given");
+            ARM_COMPUTE_ASSERT(true);
     }
-    return false;
+    return T{};
 }
 
 template <size_t dim>
 struct BroadcastUnroll
 {
     template <typename T>
-    static void unroll(LogicalBinaryOperation op,
-                       const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
+    static void unroll(arm_compute::kernels::LogicalOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
                        Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
     {
         const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
@@ -79,10 +84,10 @@
 struct BroadcastUnroll<0>
 {
     template <typename T>
-    static void unroll(LogicalBinaryOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
+    static void unroll(arm_compute::kernels::LogicalOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
                        Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
     {
-        dst[coord2index(dst.shape(), id_dst)] = logical_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]);
+        dst[coord2index(dst.shape(), id_dst)] = logical_binary_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]);
     }
 };
 
@@ -94,7 +99,7 @@
     Coordinates     id_dst{};
     SimpleTensor<T> dst{ TensorShape::broadcast_shape(src1.shape(), src2.shape()), src1.data_type() };
 
-    BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(LogicalBinaryOperation::OR, src1, src2, dst, id_src1, id_src2, id_dst);
+    BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(arm_compute::kernels::LogicalOperation::Or, src1, src2, dst, id_src1, id_src2, id_dst);
 
     return dst;
 }
@@ -107,7 +112,7 @@
     Coordinates     id_dst{};
     SimpleTensor<T> dst{ TensorShape::broadcast_shape(src1.shape(), src2.shape()), src1.data_type() };
 
-    BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(LogicalBinaryOperation::AND, src1, src2, dst, id_src1, id_src2, id_dst);
+    BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(arm_compute::kernels::LogicalOperation::And, src1, src2, dst, id_src1, id_src2, id_dst);
 
     return dst;
 }
@@ -133,4 +138,4 @@
 } // namespace reference
 } // namespace validation
 } // namespace test
-} // namespace arm_compute
\ No newline at end of file
+} // namespace arm_compute
diff --git a/tests/validation/reference/Logical.h b/tests/validation/reference/Logical.h
index fb906b7..0d2bef9 100644
--- a/tests/validation/reference/Logical.h
+++ b/tests/validation/reference/Logical.h
@@ -34,13 +34,6 @@
 {
 namespace reference
 {
-enum class LogicalBinaryOperation
-{
-    UNKNOWN = 0,
-    AND     = 1,
-    OR      = 2
-};
-
 template <typename T>
 SimpleTensor<T> logical_or(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2);
 template <typename T>
@@ -51,4 +44,4 @@
 } // namespace validation
 } // namespace test
 } // namespace arm_compute
-#endif /* ARM_COMPUTE_TEST_LOGICAL_H */
\ No newline at end of file
+#endif /* ARM_COMPUTE_TEST_LOGICAL_H */