c2ff3ea1b646921da472bf90d6013e2c587c1299
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Reshape.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/Reshape.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 class ReshapeTest : public ::testing::Test
31 {
32 protected:
33   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
34
35   std::unique_ptr<IMemoryManager> _memory_manager;
36 };
37
38 // TODO Test types other than FLOAT32.
39
40 TEST_F(ReshapeTest, Regular)
41 {
42   Shape input_shape{1, 2, 2, 3};
43   std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
44   Shape shape_shape{2};
45   std::vector<int32_t> shape_data{3, 4};
46   Tensor input_tensor =
47     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
48   Tensor shape_tensor =
49     makeInputTensor<DataType::S32>(shape_shape, shape_data, _memory_manager.get());
50   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
51
52   Reshape kernel(&input_tensor, &shape_tensor, &output_tensor);
53   kernel.configure();
54   _memory_manager->allocate_memory(output_tensor);
55   kernel.execute();
56
57   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(input_data));
58 }
59
60 TEST_F(ReshapeTest, UnknownDimension)
61 {
62   Shape input_shape{2, 1, 2, 3};
63   std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
64   Shape shape_shape{3};
65   std::vector<int32_t> shape_data{2, -1, 2};
66   Tensor input_tensor =
67     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
68   Tensor shape_tensor =
69     makeInputTensor<DataType::S32>(shape_shape, shape_data, _memory_manager.get());
70   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
71
72   Reshape kernel(&input_tensor, &shape_tensor, &output_tensor);
73   kernel.configure();
74   _memory_manager->allocate_memory(output_tensor);
75   kernel.execute();
76
77   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(input_data));
78 }
79
80 } // namespace
81 } // namespace kernels
82 } // namespace luci_interpreter