Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Elu.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/Elu.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 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
31            std::initializer_list<float> input_data, std::initializer_list<float> output_data)
32 {
33   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
34   Tensor input_tensor =
35     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
36   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
37
38   Elu kernel(&input_tensor, &output_tensor);
39   kernel.configure();
40   memory_manager->allocate_memory(output_tensor);
41   kernel.execute();
42
43   (void)output_shape;
44   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(output_data));
45 }
46
47 TEST(EluTest, SimpleElu)
48 {
49   Check(
50     /*input_shape=*/{1, 2, 4, 1}, /*output_shape=*/{1, 2, 4, 1},
51     /*input_data=*/
52     {
53       0, -6, 2, -4,    //
54       3, -2, 10, -0.1, //
55     },
56     /*output_data=*/
57     {
58       0.0, -0.997521, 2.0, -0.981684,   //
59       3.0, -0.864665, 10.0, -0.0951626, //
60     });
61 }
62
63 TEST(EluTest, InOutTypeMismatch_NEG)
64 {
65   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
66   Shape input_shape{1, 2, 4, 1};
67   std::vector<float> input_data{
68     0, -6, 2,  -4,   //
69     3, -2, 10, -0.1, //
70   };
71   Tensor input_tensor =
72     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
73   Tensor output_tensor = makeOutputTensor(DataType::U8);
74
75   Elu kernel(&input_tensor, &output_tensor);
76   EXPECT_ANY_THROW(kernel.configure());
77 }
78
79 } // namespace
80 } // namespace kernels
81 } // namespace luci_interpreter