1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ops/squared_difference_builder.h"
17
18 #include <gtest/gtest.h>
19 #include "nn_tensor.h"
20 #include "ops_test.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::NeuralNetworkRuntime::Ops;
25
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace UnitTest {
29 class SquaredDifferenceBuilderTest : public OpsTest {
30 protected:
31 void InitTensor(const std::vector<uint32_t>& inputsIndex,
32 const std::vector<uint32_t>& outputsIndex) override;
33 void CheckResult();
34
35 protected:
36 SquaredDifferenceBuilder m_builder;
37 };
38
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)39 void SquaredDifferenceBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
40 const std::vector<uint32_t>& outputsIndex)
41 {
42 std::vector<int32_t> inputDim = {3};
43 std::vector<int32_t> OutputDim = {3};
44
45 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
46 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
47 }
48
CheckResult()49 void SquaredDifferenceBuilderTest::CheckResult()
50 {
51 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
52 LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
53 EXPECT_NE(primitive, expectPrimitive);
54 }
55
56 /**
57 * @tc.name: squareddifference_build_001
58 * @tc.desc: Provide normal input, output to verify the normal behavior of the Build function
59 * @tc.type: FUNC
60 */
61 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_001, TestSize.Level0)
62 {
63 std::vector<uint32_t> inputsIndex = { 0, 1 };
64 std::vector<uint32_t> outputsIndex = { 2 };
65 std::vector<int32_t> inputDim = {3};
66 std::vector<int32_t> OutputDim = {3};
67
68 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
69 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
70 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
71 EXPECT_EQ(OH_NN_SUCCESS, ret);
72 }
73
74 /**
75 * @tc.name: squareddifference_build_002
76 * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
77 * @tc.type: FUNC
78 */
79 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_002, TestSize.Level0)
80 {
81 std::vector<uint32_t> inputsIndex = { 0, 1 };
82 std::vector<uint32_t> outputsIndex = { 2 };
83 std::vector<int32_t> inputDim = {3};
84 std::vector<int32_t> OutputDim = {3};
85
86 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
87 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
88 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
89 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
90 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
91 }
92
93 /**
94 * @tc.name: squareddifference_build_003
95 * @tc.desc: rovide one more than normal input to verify the abnormal behavior of the Build function
96 * @tc.type: FUNC
97 */
98 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_003, TestSize.Level0)
99 {
100 std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
101 std::vector<uint32_t> outputsIndex = { 3 };
102 std::vector<int32_t> inputDim = {3};
103 std::vector<int32_t> OutputDim = {3};
104
105 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
106 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
107 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
108 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
109 }
110
111 /**
112 * @tc.name: squareddifference_build_004
113 * @tc.desc: rovide one more than normal output to verify the abnormal behavior of the Build function
114 * @tc.type: FUNC
115 */
116 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_004, TestSize.Level0)
117 {
118 std::vector<uint32_t> inputsIndex = { 0, 1 };
119 std::vector<uint32_t> outputsIndex = { 2, 3 };
120 std::vector<int32_t> inputDim = {3};
121 std::vector<int32_t> OutputDim = {3};
122
123 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
124 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
125 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
126 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127 }
128
129 /**
130 * @tc.name: squareddifference_build_005
131 * @tc.desc: Provide empty input, output, and parameters to verify the abnormal behavior of the Build function
132 * @tc.type: FUNC
133 */
134 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_005, TestSize.Level0)
135 {
136 std::vector<uint32_t> inputsIndex = { 0, 1 };
137 std::vector<uint32_t> outputsIndex = { 2 };
138 std::vector<uint32_t> paramsIndex = {};
139 OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, inputsIndex, outputsIndex, m_allTensors);
140 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
141 }
142
143 /**
144 * @tc.name: squareddifference_build_006
145 * @tc.desc: Provide empty output to verify the abnormal behavior of the Build function
146 * @tc.type: FUNC
147 */
148 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_006, TestSize.Level0)
149 {
150 std::vector<uint32_t> inputsIndex = { 0, 1 };
151 std::vector<uint32_t> outputsIndex = { 2 };
152 std::vector<int32_t> inputDim = {3};
153
154 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
155
156 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, outputsIndex, m_allTensors);
157 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
158 }
159
160 /**
161 * @tc.name: squareddifference_build_007
162 * @tc.desc: Provide a param to verify the abnormal behavior of the Build function
163 * @tc.type: FUNC
164 */
165 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_build_007, TestSize.Level0)
166 {
167 std::vector<uint32_t> inputsIndex = { 0, 1 };
168 std::vector<uint32_t> outputsIndex = { 2 };
169 std::vector<uint32_t> paramsIndex = { 4 };
170
171 m_paramsIndex = paramsIndex;
172 InitTensor(inputsIndex, outputsIndex);
173 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
174 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
175 }
176
177 /**
178 * @tc.name: squareddifference_getprimitive_001
179 * @tc.desc: Verify the GetPrimitive function return nullptr
180 * @tc.type: FUNC
181 */
182 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_getprimitive_001, TestSize.Level0)
183 {
184 auto primitive = m_builder.GetPrimitive();
185 LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
186 EXPECT_EQ(primitive, expectPrimitive);
187 }
188
189 /**
190 * @tc.name: squareddifference_getprimitive_002
191 * @tc.desc: Verify the normal return behavior of the getprimitive function
192 * @tc.type: FUNC
193 */
194 HWTEST_F(SquaredDifferenceBuilderTest, squareddifference_getprimitive_002, TestSize.Level0)
195 {
196 std::vector<uint32_t> inputsIndex = { 0, 1 };
197 std::vector<uint32_t> outputsIndex = { 2 };
198 InitTensor(inputsIndex, outputsIndex);
199 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
200 CheckResult();
201 }
202 } // namespace UnitTest
203 } // namespace NeuralNetworkRuntime
204 } // namespace OHOS