Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / LogicalOr.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 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/LogicalOr.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 LogicalOrTest : 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(LogicalOrTest, Basic)
40 {
41   Tensor input1_tensor = makeInputTensor<DataType::BOOL>({1, 1, 1, 4}, {true, false, false, true},
42                                                          _memory_manager.get());
43   Tensor input2_tensor = makeInputTensor<DataType::BOOL>({1, 1, 1, 4}, {true, false, true, false},
44                                                          _memory_manager.get());
45
46   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
47
48   LogicalOr kernel(&input1_tensor, &input2_tensor, &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, true, true));
55   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAre(1, 1, 1, 4));
56 }
57
58 TEST_F(LogicalOrTest, Broadcast)
59 {
60   Tensor input1_tensor = makeInputTensor<DataType::BOOL>({1, 1, 1, 4}, {true, false, false, true},
61                                                          _memory_manager.get());
62   Tensor input2_tensor =
63     makeInputTensor<DataType::BOOL>({1, 1, 1, 1}, {false}, _memory_manager.get());
64
65   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
66
67   LogicalOr kernel(&input1_tensor, &input2_tensor, &output_tensor);
68   kernel.configure();
69   _memory_manager->allocate_memory(output_tensor);
70   kernel.execute();
71
72   EXPECT_THAT(extractTensorData<bool>(output_tensor),
73               ::testing::ElementsAre(true, false, false, true));
74   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAre(1, 1, 1, 4));
75 }
76
77 TEST_F(LogicalOrTest, MismatchInputType_NEG)
78 {
79   Tensor input1_tensor =
80     makeInputTensor<DataType::S32>({1, 1, 1, 4}, {1, 0, 0, 1}, _memory_manager.get());
81   Tensor input2_tensor =
82     makeInputTensor<DataType::BOOL>({1, 1, 1, 1}, {false}, _memory_manager.get());
83
84   Tensor output_tensor = makeOutputTensor(DataType::S32);
85
86   LogicalOr kernel(&input1_tensor, &input2_tensor, &output_tensor);
87   EXPECT_ANY_THROW(kernel.configure());
88 }
89
90 TEST_F(LogicalOrTest, InputTypeInvalid_NEG)
91 {
92   Tensor input1_tensor =
93     makeInputTensor<DataType::S32>({1, 1, 1, 4}, {1, 0, 0, 1}, _memory_manager.get());
94   Tensor input2_tensor = makeInputTensor<DataType::S32>({1, 1, 1, 1}, {0}, _memory_manager.get());
95
96   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
97
98   LogicalOr kernel(&input1_tensor, &input2_tensor, &output_tensor);
99   EXPECT_ANY_THROW(kernel.configure());
100 }
101
102 } // namespace
103 } // namespace kernels
104 } // namespace luci_interpreter