Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / AddN.test.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "kernels/TestUtils.h"
18 #include "luci_interpreter/test_models/add_n/FloatAddNKernel.h"
19 #include "luci_interpreter/test_models/add_n/NegAddNKernel.h"
20
21 #include "loader/ModuleLoader.h"
22
23 namespace luci_interpreter
24 {
25 namespace
26 {
27
28 using namespace testing;
29
30 class AddNTest : public ::testing::Test
31 {
32   // Do nothing
33 };
34
35 template <typename T> std::vector<T> checkAddNKernel(test_kernel::TestDataBase<T> *test_data_base)
36 {
37   MemoryManager memory_manager{};
38   RuntimeModule runtime_module{};
39   bool dealloc_input = true;
40
41   // Load model with single op
42   auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
43   ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
44
45   auto *main_runtime_graph = runtime_module.getMainGraph();
46   assert(main_runtime_graph->getNumOfInputTensors() == 3);
47
48   // set 1 input data
49   {
50     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
51     std::copy(test_data_base->get_input_data_by_index(0).begin(),
52               test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
53   }
54
55   // set 2 input data
56   {
57     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
58     std::copy(test_data_base->get_input_data_by_index(1).begin(),
59               test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
60   }
61
62   // set 3 input data
63   {
64     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(2));
65     std::copy(test_data_base->get_input_data_by_index(2).begin(),
66               test_data_base->get_input_data_by_index(2).end(), input_tensor_data);
67   }
68
69   runtime_module.execute();
70
71   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
72
73   T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
74   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
75   std::vector<T> output_data_vector(output_data, output_data + num_elements);
76   return output_data_vector;
77 }
78
79 TEST_F(AddNTest, Float_P)
80 {
81   test_kernel::TestDataFloatAddN test_data_kernel;
82   std::vector<float> output_data_vector = checkAddNKernel(&test_data_kernel);
83   EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
84                                     test_data_kernel.get_output_data_by_index(0), 0.0001f));
85 }
86
87 TEST_F(AddNTest, InputTypeMismatch_NEG)
88 {
89   test_kernel::TestDataInputTypeMismatchAddN test_data_kernel;
90   MemoryManager memory_manager{};
91   RuntimeModule runtime_module{};
92   bool dealloc_input = true;
93   // Load model with single op
94   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
95   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
96                "");
97 }
98
99 } // namespace
100 } // namespace luci_interpreter