d71248d904e3528118c73346453d1a57a587b49e
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Pow.test.cpp
1 /*
2  * Copyright (c) 2020 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/Pow.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 PowTest : 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 TEST_F(PowTest, SimplePow)
40 {
41   std::initializer_list<int32_t> base_shape = {1, 1, 3, 2};
42
43   std::vector<float> input1_data{0.3f, 2.3f, 0.9f, 0.5f, 0.8f, 1.1f};
44   std::vector<float> input2_data{0.2f, 0.3f, -0.4f, 0.5f, 1.0f, 0.9f};
45   std::vector<float> test_outputs{0.786f, 1.2838f, 1.043f, 0.7071f, 0.8f, 1.08956f};
46
47   Tensor input1_tensor =
48     makeInputTensor<DataType::FLOAT32>(base_shape, input1_data, _memory_manager.get());
49   Tensor input2_tensor =
50     makeInputTensor<DataType::FLOAT32>(base_shape, input2_data, _memory_manager.get());
51   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
52
53   Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
54   kernel.configure();
55   _memory_manager->allocate_memory(output_tensor);
56   kernel.execute();
57
58   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(test_outputs, 0.0001f));
59   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(base_shape));
60 }
61
62 TEST_F(PowTest, FloatBroadcastPow)
63 {
64   std::initializer_list<int32_t> input1_shape = {1, 3};
65   std::initializer_list<int32_t> input2_shape = {3, 1};
66
67   std::vector<float> input1_data{0.3f, 2.3f, 0.9f};
68   std::vector<float> input2_data{0.2f, 0.3f, 0.4f};
69   std::vector<float> test_outputs{0.786f,   1.18126f, 0.9791f, 0.6968f, 1.28386f,
70                                   0.96888f, 0.6178f,  1.3953f, 0.9587f};
71
72   Tensor input1_tensor =
73     makeInputTensor<DataType::FLOAT32>(input1_shape, input1_data, _memory_manager.get());
74   Tensor input2_tensor =
75     makeInputTensor<DataType::FLOAT32>(input2_shape, input2_data, _memory_manager.get());
76   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
77
78   Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
79   kernel.configure();
80   _memory_manager->allocate_memory(output_tensor);
81   kernel.execute();
82
83   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(test_outputs, 0.0001f));
84 }
85
86 TEST_F(PowTest, IntPow)
87 {
88   std::initializer_list<int32_t> base_shape = {1, 3};
89
90   std::vector<int32_t> input_data{2, 3, 4};
91   std::vector<int32_t> test_outputs{4, 27, 256};
92
93   Tensor input1_tensor =
94     makeInputTensor<DataType::S32>(base_shape, input_data, _memory_manager.get());
95   Tensor input2_tensor =
96     makeInputTensor<DataType::S32>(base_shape, input_data, _memory_manager.get());
97   Tensor output_tensor = makeOutputTensor(DataType::S32);
98
99   Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
100   kernel.configure();
101   _memory_manager->allocate_memory(output_tensor);
102   kernel.execute();
103
104   EXPECT_THAT(extractTensorData<int32_t>(output_tensor), ::testing::ElementsAreArray(test_outputs));
105   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(base_shape));
106 }
107
108 TEST_F(PowTest, Input_Output_Type_NEG)
109 {
110   Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.0f}, _memory_manager.get());
111   Tensor input2_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.0f}, _memory_manager.get());
112   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
113
114   Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
115   EXPECT_ANY_THROW(kernel.configure());
116 }
117
118 TEST_F(PowTest, Input_Type_Mismatch_NEG)
119 {
120   Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.0f}, _memory_manager.get());
121   Tensor input2_tensor = makeInputTensor<DataType::S32>({1}, {4}, _memory_manager.get());
122   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
123
124   Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
125   EXPECT_ANY_THROW(kernel.configure());
126 }
127
128 TEST_F(PowTest, Invalid_Input_Type_NEG)
129 {
130   Tensor input1_tensor = makeInputTensor<DataType::S64>({1}, {1}, _memory_manager.get());
131   Tensor input2_tensor = makeInputTensor<DataType::S64>({1}, {1}, _memory_manager.get());
132   Tensor output_tensor = makeOutputTensor(DataType::S64);
133
134   Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
135   kernel.configure();
136   EXPECT_ANY_THROW(kernel.execute());
137 }
138
139 } // namespace
140 } // namespace kernels
141 } // namespace luci_interpreter