Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / SquaredDifference.test.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 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/SquaredDifference.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 TEST(SquaredDifferenceTest, Float)
32 {
33   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
34
35   Shape input_shape{3, 1, 2};
36   std::vector<float> input_data1{1.0, 0.0, -1.0, 11.0, -2.0, -1.44};
37   std::vector<float> input_data2{-1.0, 0.0, 1.0, 12.0, -3.0, -1.43};
38   Tensor input_tensor1 =
39     makeInputTensor<DataType::FLOAT32>(input_shape, input_data1, memory_manager.get());
40   Tensor input_tensor2 =
41     makeInputTensor<DataType::FLOAT32>(input_shape, input_data2, memory_manager.get());
42   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
43
44   SquaredDifference kernel(&input_tensor1, &input_tensor2, &output_tensor);
45   kernel.configure();
46   memory_manager->allocate_memory(output_tensor);
47   kernel.execute();
48
49   std::vector<float> ref_output_data{4.0, 0.0, 4.0, 1.0, 1.0, 0.0001};
50   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
51 }
52
53 TEST(SquaredDifferenceTest, FloatBroadcast)
54 {
55   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
56
57   Shape input_shape1{3, 1, 2};
58   Shape input_shape2{1};
59   std::vector<float> input_data1{1.0, 0.0, -1.0, 11.0, -2.0, -1.44};
60   std::vector<float> input_data2{1.0};
61   Tensor input_tensor1 =
62     makeInputTensor<DataType::FLOAT32>(input_shape1, input_data1, memory_manager.get());
63   Tensor input_tensor2 =
64     makeInputTensor<DataType::FLOAT32>(input_shape2, input_data2, memory_manager.get());
65   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
66
67   SquaredDifference kernel(&input_tensor1, &input_tensor2, &output_tensor);
68   kernel.configure();
69   memory_manager->allocate_memory(output_tensor);
70   kernel.execute();
71
72   std::vector<float> ref_output_data{0.0, 1.0, 4.0, 100.0, 9.0, 5.9536};
73   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
74 }
75
76 } // namespace
77 } // namespace kernels
78 } // namespace luci_interpreter