Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / compiler / luci-micro / luci-interpreter / src / kernels / DepthToSpace.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/DepthToSpace.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 DepthToSpaceTest : public ::testing::Test
31 {
32 };
33
34 using DataTypes = ::testing::Types<float, uint8_t>;
35 TYPED_TEST_SUITE(DepthToSpaceTest, DataTypes);
36
37 TYPED_TEST(DepthToSpaceTest, SimpleCase)
38 {
39   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
40   std::vector<TypeParam> input_data{1, 2, 3, 4, 5, 6, 7, 8};
41   Shape input_shape{1, 1, 2, 4};
42   std::vector<TypeParam> output_data{1, 2, 5, 6, 3, 4, 7, 8};
43   std::vector<int32_t> output_shape{1, 2, 4, 1};
44
45   Tensor input_tensor =
46     makeInputTensor<getElementType<TypeParam>()>(input_shape, input_data, memory_manager.get());
47   Tensor output_tensor = makeOutputTensor(getElementType<TypeParam>());
48
49   DepthToSpaceParams params{};
50   params.block_size = 2;
51
52   DepthToSpace kernel = DepthToSpace(&input_tensor, &output_tensor, params);
53   kernel.configure();
54   memory_manager->allocate_memory(output_tensor);
55   kernel.execute();
56
57   EXPECT_THAT(extractTensorData<TypeParam>(output_tensor),
58               ::testing::ElementsAreArray(output_data));
59   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
60 }
61
62 TEST(DepthToSpaceTest, InvalidInputShape_NEG)
63 {
64   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
65   std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8};
66   Shape input_shape{1, 2, 4};
67
68   Tensor input_tensor =
69     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
70   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
71
72   DepthToSpaceParams params{};
73   params.block_size = 2;
74
75   DepthToSpace kernel = DepthToSpace(&input_tensor, &output_tensor, params);
76   EXPECT_ANY_THROW(kernel.configure());
77 }
78
79 TEST(DepthToSpaceTest, InOutTypeMismatch_NEG)
80 {
81   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
82   std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8};
83   Shape input_shape{1, 1, 2, 4};
84
85   Tensor input_tensor =
86     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
87   Tensor output_tensor = makeOutputTensor(DataType::U8);
88
89   DepthToSpaceParams params{};
90   params.block_size = 2;
91
92   DepthToSpace kernel = DepthToSpace(&input_tensor, &output_tensor, params);
93   EXPECT_ANY_THROW(kernel.configure());
94 }
95
96 TEST(DepthToSpaceTest, InvalidBlockSize_NEG)
97 {
98   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
99   std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8};
100   Shape input_shape{1, 1, 2, 4};
101
102   Tensor input_tensor =
103     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
104   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
105
106   DepthToSpaceParams params{};
107   params.block_size = 3;
108
109   DepthToSpace kernel = DepthToSpace(&input_tensor, &output_tensor, params);
110   EXPECT_ANY_THROW(kernel.configure());
111 }
112
113 } // namespace
114 } // namespace kernels
115 } // namespace luci_interpreter