19c585890b4b027c32993ce4f82181b92b48e12b
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Shape.test.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 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/Shape.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
21
22 namespace luci_interpreter
23 {
24 namespace kernels
25 {
26 namespace
27 {
28
29 using namespace testing;
30
31 class ShapeTest : public ::testing::Test
32 {
33 protected:
34   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
35
36   std::unique_ptr<IMemoryManager> _memory_manager;
37 };
38
39 template <typename T> void runShapeKernel(loco::DataType dataType, IMemoryManager *memory_manager)
40 {
41   Shape input_shape{1, 3, 1, 3, 5};
42
43   Tensor input_tensor = Tensor(loco::DataType::FLOAT32, input_shape, {}, "");
44   Tensor output_tensor = makeOutputTensor(dataType);
45
46   ShapeParams params{};
47   params.out_type = dataType;
48
49   ShapeKernel kernel(&input_tensor, &output_tensor, params);
50
51   kernel.configure();
52   memory_manager->allocate_memory(output_tensor);
53   kernel.execute();
54
55   std::vector<T> ref_output_data{1, 3, 1, 3, 5};
56   EXPECT_THAT(extractTensorData<T>(output_tensor), ref_output_data);
57
58   std::vector<int32_t> ref_output_shape{5};
59   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
60 }
61
62 TEST_F(ShapeTest, OutTypeInt)
63 {
64
65   // Run for int32_t output
66   runShapeKernel<int32_t>(loco::DataType::S32, _memory_manager.get());
67   // Run for int64_t output
68   runShapeKernel<int64_t>(loco::DataType::S64, _memory_manager.get());
69
70   SUCCEED();
71 }
72
73 TEST_F(ShapeTest, Invalid_Output_Type_NEG)
74 {
75   Shape input_shape{1, 3};
76
77   Tensor input_tensor = Tensor(loco::DataType::FLOAT32, input_shape, {}, "");
78   Tensor output_tensor = makeOutputTensor(loco::DataType::FLOAT32);
79
80   ShapeParams params{};
81   params.out_type = loco::DataType::FLOAT32;
82
83   ShapeKernel kernel(&input_tensor, &output_tensor, params);
84
85   EXPECT_ANY_THROW(kernel.configure());
86 }
87
88 } // namespace
89 } // namespace kernels
90 } // namespace luci_interpreter