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