Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Slice.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/Slice.h"
18 #include "kernels/TestUtils.h"
19 #include "luci_interpreter/TestMemoryManager.h"
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25 namespace
26 {
27
28 using namespace testing;
29
30 template <typename T> class SliceTest : public ::testing::Test
31 {
32 };
33
34 using DataTypes = ::testing::Types<float, uint8_t>;
35 TYPED_TEST_CASE(SliceTest, DataTypes);
36
37 TYPED_TEST(SliceTest, SimpleTest)
38 {
39   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
40
41   std::vector<TypeParam> input_data{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
42   Shape input_shape{3, 2, 3, 1};
43   std::vector<int32_t> begin_data{1, 0, 0, 0};
44   Shape begin_shape{4};
45   std::vector<int32_t> size_data{2, 1, -1, 1};
46   Shape size_shape{4};
47   std::vector<TypeParam> output_data{3, 3, 3, 5, 5, 5};
48   std::vector<int32_t> output_shape{2, 1, 3, 1};
49
50   Tensor input_tensor =
51     makeInputTensor<getElementType<TypeParam>()>(input_shape, input_data, memory_manager.get());
52   Tensor begin_tensor =
53     makeInputTensor<DataType::S32>(begin_shape, begin_data, memory_manager.get());
54   Tensor size_tensor = makeInputTensor<DataType::S32>(size_shape, size_data, memory_manager.get());
55
56   Tensor output_tensor = makeOutputTensor(getElementType<TypeParam>());
57
58   Slice kernel(&input_tensor, &begin_tensor, &size_tensor, &output_tensor);
59   kernel.configure();
60   memory_manager->allocate_memory(output_tensor);
61   kernel.execute();
62
63   EXPECT_THAT(extractTensorData<TypeParam>(output_tensor),
64               ::testing::ElementsAreArray(output_data));
65   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
66 }
67
68 } // namespace
69 } // namespace kernels
70 } // namespace luci_interpreter