Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / LogicalAnd.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/LogicalAnd.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
21
22 namespace luci_interpreter
23 {
24 namespace kernels
25 {
26 namespace
27 {
28
29 using namespace testing;
30
31 class LogicalAndTest : public ::testing::Test
32 {
33 protected:
34   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
35
36   std::unique_ptr<IMemoryManager> _memory_manager;
37 };
38
39 TEST_F(LogicalAndTest, Basic)
40 {
41   Shape input_shape{1, 1, 1, 4};
42   Tensor input_tensor1 =
43     makeInputTensor<DataType::BOOL>(input_shape, {true, false, false, true}, _memory_manager.get());
44   Tensor input_tensor2 =
45     makeInputTensor<DataType::BOOL>(input_shape, {true, false, true, false}, _memory_manager.get());
46   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
47
48   LogicalAnd kernel(&input_tensor1, &input_tensor2, &output_tensor);
49   kernel.configure();
50   _memory_manager->allocate_memory(output_tensor);
51   kernel.execute();
52
53   EXPECT_THAT(extractTensorData<bool>(output_tensor),
54               ::testing::ElementsAre(true, false, false, false));
55   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAre(1, 1, 1, 4));
56 }
57
58 TEST_F(LogicalAndTest, Broadcast)
59 {
60   Tensor input_tensor1 = makeInputTensor<DataType::BOOL>({1, 1, 1, 4}, {true, false, false, true},
61                                                          _memory_manager.get());
62   Tensor input_tensor2 =
63     makeInputTensor<DataType::BOOL>({1, 1, 1, 1}, {true}, _memory_manager.get());
64   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
65
66   LogicalAnd kernel(&input_tensor1, &input_tensor2, &output_tensor);
67   kernel.configure();
68   _memory_manager->allocate_memory(output_tensor);
69   kernel.execute();
70
71   EXPECT_THAT(extractTensorData<bool>(output_tensor),
72               ::testing::ElementsAre(true, false, false, true));
73   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAre(1, 1, 1, 4));
74 }
75
76 TEST_F(LogicalAndTest, MismatchInputType_NEG)
77 {
78   Tensor input1_tensor =
79     makeInputTensor<DataType::S32>({1, 1, 1, 4}, {1, 0, 0, 1}, _memory_manager.get());
80   Tensor input2_tensor =
81     makeInputTensor<DataType::BOOL>({1, 1, 1, 1}, {false}, _memory_manager.get());
82   Tensor output_tensor = makeOutputTensor(DataType::S32);
83
84   LogicalAnd kernel(&input1_tensor, &input2_tensor, &output_tensor);
85   EXPECT_ANY_THROW(kernel.configure());
86 }
87
88 TEST_F(LogicalAndTest, InputTypeInvalid_NEG)
89 {
90   Tensor input1_tensor =
91     makeInputTensor<DataType::S32>({1, 1, 1, 4}, {1, 0, 0, 1}, _memory_manager.get());
92   Tensor input2_tensor = makeInputTensor<DataType::S32>({1, 1, 1, 1}, {0}, _memory_manager.get());
93   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
94
95   LogicalAnd kernel(&input1_tensor, &input2_tensor, &output_tensor);
96   EXPECT_ANY_THROW(kernel.configure());
97 }
98
99 } // namespace
100 } // namespace kernels
101 } // namespace luci_interpreter