Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Pack.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/pack/PackKernel.h"
19
20 #include "loader/ModuleLoader.h"
21
22 namespace luci_interpreter
23 {
24 namespace
25 {
26
27 using namespace testing;
28
29 class PackTest : public ::testing::Test
30 {
31   // Do nothing
32 };
33
34 template <typename T> std::vector<T> checkPackKernel(test_kernel::TestDataBase<T> *test_data_base)
35 {
36   MemoryManager memory_manager{};
37   RuntimeModule runtime_module{};
38   bool dealloc_input = true;
39
40   // Load model with single op
41   auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
42   ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
43
44   auto *main_runtime_graph = runtime_module.getMainGraph();
45   assert(main_runtime_graph->getNumOfInputTensors() == 2);
46
47   // Set input 1 data
48   {
49     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
50     std::copy(test_data_base->get_input_data_by_index(0).begin(),
51               test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
52   }
53
54   // Set input 2 data
55   {
56     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
57     std::copy(test_data_base->get_input_data_by_index(1).begin(),
58               test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
59   }
60
61   runtime_module.execute();
62
63   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
64
65   T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
66   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
67   std::vector<T> output_data_vector(output_data, output_data + num_elements);
68   return output_data_vector;
69 }
70
71 TEST_F(PackTest, Float_P)
72 {
73   test_kernel::TestDataFloatPack test_data_pack_kernel;
74   std::vector<float> output_data_vector = checkPackKernel(&test_data_pack_kernel);
75   EXPECT_THAT(output_data_vector, test_data_pack_kernel.get_output_data_by_index(0));
76 }
77
78 TEST_F(PackTest, Int_P)
79 {
80   test_kernel::TestDataIntPack test_data_pack_kernel;
81   std::vector<int32_t> output_data_vector = checkPackKernel(&test_data_pack_kernel);
82   EXPECT_THAT(output_data_vector, test_data_pack_kernel.get_output_data_by_index(0));
83 }
84
85 TEST_F(PackTest, QuantU8_P)
86 {
87   test_kernel::TestDataQuantU8Pack test_data_pack_kernel;
88   std::vector<uint8_t> output_data_vector = checkPackKernel(&test_data_pack_kernel);
89   EXPECT_THAT(output_data_vector, test_data_pack_kernel.get_output_data_by_index(0));
90 }
91
92 // TODO: add negative tests?
93
94 } // namespace
95 } // namespace luci_interpreter