Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Equal.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/TestUtils.h"
19 #include "luci_interpreter/test_models/equal/FloatEqualKernel.h"
20 #include "luci_interpreter/test_models/equal/IntEqualKernel.h"
21
22 #include "loader/ModuleLoader.h"
23
24 namespace luci_interpreter
25 {
26 namespace
27 {
28
29 using namespace testing;
30
31 class EqualTest : public ::testing::Test
32 {
33   // Do nothing
34 };
35
36 template <typename T, typename U>
37 std::vector<U> checkEqualKernel(test_kernel::TestDataBase<T, U> *test_data_base)
38 {
39   MemoryManager memory_manager{};
40   RuntimeModule runtime_module{};
41   bool dealloc_input = true;
42
43   // Load model with single op
44   auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
45   ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
46
47   auto *main_runtime_graph = runtime_module.getMainGraph();
48   assert(main_runtime_graph->getNumOfInputTensors() == 2);
49
50   // set left input data
51   {
52     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
53     std::copy(test_data_base->get_input_data_by_index(0).begin(),
54               test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
55   }
56
57   // set right input data
58   {
59     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
60     std::copy(test_data_base->get_input_data_by_index(1).begin(),
61               test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
62   }
63
64   runtime_module.execute();
65
66   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
67
68   U *output_data = reinterpret_cast<U *>(main_runtime_graph->getOutputDataByIndex(0));
69   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(U));
70   std::vector<U> output_data_vector(output_data, output_data + num_elements);
71   return output_data_vector;
72 }
73
74 TEST_F(EqualTest, FloatNoBroadcast_P)
75 {
76   const bool is_with_broadcast = false;
77   test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, false);
78   std::vector<bool> output_data_vector = checkEqualKernel<float, bool>(&test_data_kernel);
79   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
80 }
81
82 TEST_F(EqualTest, FloatWithBroadcast_P)
83 {
84   const bool is_with_broadcast = true;
85   test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, false);
86   std::vector<bool> output_data_vector = checkEqualKernel<float, bool>(&test_data_kernel);
87   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
88 }
89
90 TEST_F(EqualTest, FloatNoBroadcast_NEG)
91 {
92   const bool is_with_broadcast = false;
93   test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, true);
94   EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
95 }
96
97 TEST_F(EqualTest, FloatWithBroadcast_NEG)
98 {
99   const bool is_with_broadcast = true;
100   test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, true);
101   EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
102 }
103
104 TEST_F(EqualTest, IntWithBroadcast_P)
105 {
106   const bool is_with_broadcast = true;
107   test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, false);
108   std::vector<bool> output_data_vector = checkEqualKernel<int32_t, bool>(&test_data_kernel);
109   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
110 }
111
112 TEST_F(EqualTest, IntNoBroadcast_P)
113 {
114   const bool is_with_broadcast = false;
115   test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, false);
116   std::vector<bool> output_data_vector = checkEqualKernel<int32_t, bool>(&test_data_kernel);
117   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
118 }
119
120 TEST_F(EqualTest, IntWithBroadcast_NEG)
121 {
122   const bool is_with_broadcast = true;
123   test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, true);
124   EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
125 }
126
127 TEST_F(EqualTest, IntNoBroadcast_NEG)
128 {
129   const bool is_with_broadcast = false;
130   test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, true);
131   EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
132 }
133
134 } // namespace
135 } // namespace luci_interpreter