2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/Reshape.h"
18 #include "kernels/TestUtils.h"
19 #include "luci_interpreter/TestMemoryManager.h"
21 namespace luci_interpreter
28 using namespace testing;
30 class ReshapeTest : public ::testing::Test
33 void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
35 std::unique_ptr<IMemoryManager> _memory_manager;
38 // TODO Test types other than FLOAT32.
40 TEST_F(ReshapeTest, Regular)
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};
45 std::vector<int32_t> shape_data{3, 4};
47 makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
49 makeInputTensor<DataType::S32>(shape_shape, shape_data, _memory_manager.get());
50 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
52 Reshape kernel(&input_tensor, &shape_tensor, &output_tensor);
54 _memory_manager->allocate_memory(output_tensor);
57 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(input_data));
60 TEST_F(ReshapeTest, UnknownDimension)
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};
65 std::vector<int32_t> shape_data{2, -1, 2};
67 makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
69 makeInputTensor<DataType::S32>(shape_shape, shape_data, _memory_manager.get());
70 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
72 Reshape kernel(&input_tensor, &shape_tensor, &output_tensor);
74 _memory_manager->allocate_memory(output_tensor);
77 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(input_data));
80 TEST_F(ReshapeTest, SupportS64)
82 Shape input_shape{2, 1, 2, 3};
83 std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
85 std::vector<int64_t> shape_data{2, -1, 2};
87 makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
89 makeInputTensor<DataType::S64>(shape_shape, shape_data, _memory_manager.get());
90 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
92 Reshape kernel(&input_tensor, &shape_tensor, &output_tensor);
94 _memory_manager->allocate_memory(output_tensor);
97 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(input_data));
100 TEST_F(ReshapeTest, SupportS16_NEG)
102 Shape input_shape{2, 1, 2, 3};
103 std::vector<float> input_data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
104 Shape shape_shape{3};
105 std::vector<int16_t> shape_data{2, -1, 2};
106 Tensor input_tensor =
107 makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
108 Tensor shape_tensor =
109 makeInputTensor<DataType::S16>(shape_shape, shape_data, _memory_manager.get());
110 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
112 Reshape kernel(&input_tensor, &shape_tensor, &output_tensor);
113 EXPECT_ANY_THROW(kernel.configure());
117 } // namespace kernels
118 } // namespace luci_interpreter