Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / ArgMin.test.cpp
1 /*
2  * Copyright (c) 2020 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/argmin/FloatArgMinKernel.h"
19 #include "luci_interpreter/test_models/argmin/NegArgMinKernel.h"
20
21 #include "loader/ModuleLoader.h"
22
23 namespace luci_interpreter
24 {
25 namespace
26 {
27
28 using namespace testing;
29
30 class ArgMinTest : public ::testing::Test
31 {
32   // Do nothing
33 };
34
35 template <typename T, typename O>
36 std::vector<O> checkKernel(test_kernel::TestDataBase<T, O> *test_data_base)
37 {
38   MemoryManager memory_manager{};
39   RuntimeModule runtime_module{};
40   bool dealloc_input = true;
41
42   // Load model with single op
43   auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
44   ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
45
46   auto *main_runtime_graph = runtime_module.getMainGraph();
47   assert(main_runtime_graph->getNumOfInputTensors() == 1);
48
49   // Set input data
50   {
51     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
52     std::copy(test_data_base->get_input_data_by_index(0).begin(),
53               test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
54   }
55
56   runtime_module.execute();
57
58   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
59
60   O *output_data = reinterpret_cast<O *>(main_runtime_graph->getOutputDataByIndex(0));
61   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(O));
62   std::vector<O> output_data_vector(output_data, output_data + num_elements);
63   return output_data_vector;
64 }
65
66 TEST_F(ArgMinTest, MainTest_P)
67 {
68   test_kernel::TestDataFloatArgMin test_data_kernel;
69   std::vector<int32_t> output_data_vector = checkKernel(&test_data_kernel);
70   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
71 }
72
73 TEST_F(ArgMinTest, Input_output_type_mismatch_NEG)
74 {
75   test_kernel::TestDataOutputWrongOutputArgMin test_data_kernel;
76   MemoryManager memory_manager{};
77   RuntimeModule runtime_module{};
78   bool dealloc_input = true;
79   // Load model with single op
80   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
81   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
82                "");
83 }
84
85 } // namespace
86 } // namespace luci_interpreter