Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Sqrt.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/Sqrt.h"
18 #include "kernels/TestUtils.h"
19
20 namespace luci_interpreter
21 {
22 namespace kernels
23 {
24 namespace
25 {
26
27 using namespace testing;
28
29 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
30            std::initializer_list<float> input_data, std::initializer_list<float> output_data)
31 {
32   Tensor input_tensor{DataType::FLOAT32, input_shape, {}, ""};
33   input_tensor.writeData(input_data.begin(), input_data.size() * sizeof(float));
34
35   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
36
37   Sqrt kernel(&input_tensor, &output_tensor);
38   kernel.configure();
39   kernel.execute();
40
41   EXPECT_THAT(extractTensorData<float>(output_tensor),
42               ::testing::ElementsAreArray(ArrayFloatNear(output_data)));
43   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
44 }
45
46 TEST(SqrtTest, SimpleSqrt)
47 {
48   Check(
49       /*input_shape=*/{1, 2, 4, 1}, /*output_shape=*/{1, 2, 4, 1},
50       /*input_data=*/
51       {
52           0, 8, 2, 4,    //
53           3, 7, 10, 0.3, //
54       },
55       /*output_data=*/
56       {
57           0.0, 2.8284271, 1.4142136, 2,                //
58           1.7320508, 2.6457513, 3.1622777, 0.54772256, //
59       });
60 }
61
62 TEST(SqrtTest, Input_Output_Type_NEG)
63 {
64   Tensor input_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f});
65   Tensor output_tensor = makeOutputTensor(DataType::S32);
66
67   Sqrt kernel(&input_tensor, &output_tensor);
68   EXPECT_ANY_THROW(kernel.configure());
69 }
70
71 TEST(AddTest, Invalid_Input_Type_NEG)
72 {
73   Tensor input_tensor = makeInputTensor<DataType::S64>({1}, {1});
74   Tensor output_tensor = makeOutputTensor(DataType::S64);
75
76   Sqrt kernel(&input_tensor, &output_tensor);
77   kernel.configure();
78   EXPECT_ANY_THROW(kernel.execute());
79 }
80
81 } // namespace
82 } // namespace kernels
83 } // namespace luci_interpreter