Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Split.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2018 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/split/FloatSplitKernel.h"
20 #include "luci_interpreter/test_models/split/IntSplitKernel.h"
21
22 #include "loader/ModuleLoader.h"
23
24 namespace luci_interpreter
25 {
26 namespace
27 {
28
29 using namespace testing;
30
31 class SplitTest : public ::testing::Test
32 {
33   // Do nothing
34 };
35
36 template <typename T>
37 std::vector<std::vector<T>> checkSplitKernel(test_kernel::TestDataBase<T> *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() == 1);
49
50   // Set 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   runtime_module.execute();
58
59   assert(main_runtime_graph->getNumOfOutputTensors() == 2);
60
61   std::vector<std::vector<T>> result;
62
63   for (int i = 0; i < 2; ++i)
64   {
65     T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(i));
66     const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(i) / sizeof(T));
67     std::vector<T> output_data_vector(output_data, output_data + num_elements);
68     result.push_back(output_data_vector);
69   }
70
71   return result;
72 }
73
74 TEST_F(SplitTest, Float_P)
75 {
76   test_kernel::TestDataFloatSplit test_data_kernel;
77   const auto output_data_vector = checkSplitKernel(&test_data_kernel);
78
79   for (int i = 0; i < 2; ++i)
80   {
81     EXPECT_THAT(output_data_vector[i], test_data_kernel.get_output_data_by_index(i));
82   }
83 }
84
85 TEST_F(SplitTest, Int_P)
86 {
87   test_kernel::TestDataIntSplit test_data_kernel;
88   const auto output_data_vector = checkSplitKernel(&test_data_kernel);
89
90   for (int i = 0; i < 2; ++i)
91   {
92     EXPECT_THAT(output_data_vector[i], test_data_kernel.get_output_data_by_index(i));
93   }
94 }
95
96 // TODO: add negative tests?
97
98 } // namespace
99 } // namespace luci_interpreter