1 /*
2  * Copyright (c) 2024 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/reduceL2_builder.h"
17 
18 #include "ops_test.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime::Ops;
23 
24 namespace OHOS {
25 namespace NeuralNetworkRuntime {
26 namespace UnitTest {
27 class ReduceL2BuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SetKeepDims(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35     void SetCoeff(OH_NN_DataType dataType,
36         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
37     void SetReduceToEnd(OH_NN_DataType dataType,
38         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
39 
40 protected:
41     ReduceL2Builder m_builder;
42     std::vector<uint32_t> m_inputs {0, 1};
43     std::vector<uint32_t> m_outputs {2};
44     std::vector<uint32_t> m_params {3, 4, 5};
45     std::vector<int32_t> m_inputDim {1, 1, 2, 2};
46     std::vector<int32_t> m_outputDim {1, 1, 1, 2};
47     std::vector<int32_t> m_paramDim {1};
48 };
49 
SetUp()50 void ReduceL2BuilderTest::SetUp() {}
51 
TearDown()52 void ReduceL2BuilderTest::TearDown() {}
53 
SetKeepDims(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)54 void ReduceL2BuilderTest::SetKeepDims(OH_NN_DataType dataType,
55     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
56 {
57     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(dataType, dim, quantParam, type);
58     bool *keepDimsValue = new (std::nothrow) bool(true);
59     EXPECT_NE(nullptr, keepDimsValue);
60     keepDimsTensor->SetBuffer(keepDimsValue, sizeof(bool));
61     m_allTensors.emplace_back(keepDimsTensor);
62 }
63 
SetCoeff(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)64 void ReduceL2BuilderTest::SetCoeff(OH_NN_DataType dataType,
65     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
66 {
67     std::shared_ptr<NNTensor> coeffTensor = TransToNNTensor(dataType, dim, quantParam, type);
68     float *coeffValue = new (std::nothrow) float(0.0f);
69     EXPECT_NE(nullptr, coeffValue);
70     coeffTensor->SetBuffer(coeffValue, sizeof(float));
71     m_allTensors.emplace_back(coeffTensor);
72 }
73 
SetReduceToEnd(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)74 void ReduceL2BuilderTest::SetReduceToEnd(OH_NN_DataType dataType,
75     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
76 {
77     std::shared_ptr<NNTensor> reduceToEndTensor = TransToNNTensor(dataType, dim, quantParam, type);
78     bool *reduceToEndValue = new (std::nothrow) bool(true);
79     EXPECT_NE(nullptr, reduceToEndValue);
80     reduceToEndTensor->SetBuffer(reduceToEndValue, sizeof(bool));
81     m_allTensors.emplace_back(reduceToEndTensor);
82 }
83 
84 /**
85  * @tc.name: reduceL2_build_001
86  * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
87  * @tc.type: FUNC
88  */
89 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_001, TestSize.Level0)
90 {
91     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
92     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
93     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
94     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
95     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
96 
97     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
98     EXPECT_EQ(OH_NN_SUCCESS, ret);
99 }
100 
101 /**
102  * @tc.name: reduceL2_build_002
103  * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
104  * @tc.type: FUNC
105  */
106 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_002, TestSize.Level0)
107 {
108     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
109     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
110     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
111     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
112     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
113 
114     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
115     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
116     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
117 }
118 
119 /**
120  * @tc.name: reduceL2_build_003
121  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
122  * @tc.type: FUNC
123  */
124 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_003, TestSize.Level0)
125 {
126     m_inputs = {0, 1, 2};
127     m_outputs = {3};
128     m_params = {4, 5, 6};
129 
130     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
131     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
132     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
133     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
134     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
135 
136     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
137     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
138 }
139 
140 /**
141  * @tc.name: reduceL2_build_004
142  * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
143  * @tc.type: FUNC
144  */
145 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_004, TestSize.Level0)
146 {
147     m_outputs = {2, 3};
148     m_params = {4, 5, 6};
149 
150     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
151     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
152     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
153     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
154     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
155 
156     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
157     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
158 }
159 
160 /**
161  * @tc.name: reduceL2_build_005
162  * @tc.desc: Verify that the build function return a failed message with null allTensor
163  * @tc.type: FUNC
164  */
165 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_005, TestSize.Level0)
166 {
167     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
168     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
169 }
170 
171 /**
172  * @tc.name: reduceL2_build_006
173  * @tc.desc: Verify that the build function return a failed message without output tensor
174  * @tc.type: FUNC
175  */
176 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_006, TestSize.Level0)
177 {
178     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
179 
180     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
181     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
182 }
183 
184 /**
185  * @tc.name: reduceL2_build_007
186  * @tc.desc: Verify that the build function return a failed message with invalided keepdims's dataType
187  * @tc.type: FUNC
188  */
189 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_007, TestSize.Level0)
190 {
191     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
192     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
193 
194     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_INT64,
195         m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
196     int64_t keepDimsValue = 1;
197     keepDimsTensor->SetBuffer(&keepDimsValue, sizeof(keepDimsValue));
198     m_allTensors.emplace_back(keepDimsTensor);
199     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
200     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
201 
202     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
203     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
204     keepDimsTensor->SetBuffer(nullptr, 0);
205 }
206 
207 /**
208  * @tc.name: reduceL2_build_008
209  * @tc.desc: Verify that the build function return a failed message with invalided coeff's dataType
210  * @tc.type: FUNC
211  */
212 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_008, TestSize.Level0)
213 {
214     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
215     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
216 
217     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
218     std::shared_ptr<NNTensor> coeffTensor = TransToNNTensor(OH_NN_INT64,
219         m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
220     int64_t coeffValue = 1;
221     coeffTensor->SetBuffer(&coeffValue, sizeof(int64_t));
222     m_allTensors.emplace_back(coeffTensor);
223     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
224 
225     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
226     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
227     coeffTensor->SetBuffer(nullptr, 0);
228 }
229 
230 /**
231  * @tc.name: reduceL2_build_009
232  * @tc.desc: Verify that the build function return a failed message with invalided reduceToEnd's dataType
233  * @tc.type: FUNC
234  */
235 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_009, TestSize.Level0)
236 {
237     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
238     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
239 
240     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
241     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
242     std::shared_ptr<NNTensor> reduceToEndTensor = TransToNNTensor(OH_NN_INT64,
243         m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
244     int64_t reduceToEndValue = 1;
245     reduceToEndTensor->SetBuffer(&reduceToEndValue, sizeof(reduceToEndValue));
246     m_allTensors.emplace_back(reduceToEndTensor);
247 
248     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
249     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
250     reduceToEndTensor->SetBuffer(nullptr, 0);
251 }
252 
253 /**
254  * @tc.name: reduceL2_build_010
255  * @tc.desc: Verify that the build function return a failed message with invalided keepdims's dimension
256  * @tc.type: FUNC
257  */
258 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_010, TestSize.Level0)
259 {
260     std::vector<int32_t> m_paramDims = {1, 2};
261 
262     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
263     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
264 
265     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_BOOL, m_paramDims,
266         nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
267     bool keepDimsValue[2] = {true, true};
268     keepDimsTensor->SetBuffer(keepDimsValue, 2 * sizeof(bool));
269     m_allTensors.emplace_back(keepDimsTensor);
270     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
271     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
272 
273     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
274     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
275     keepDimsTensor->SetBuffer(nullptr, 0);
276 }
277 
278 /**
279  * @tc.name: reduceL2_build_011
280  * @tc.desc: Verify that the build function return a failed message with invalided coeff's dimension
281  * @tc.type: FUNC
282  */
283 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_011, TestSize.Level0)
284 {
285     std::vector<int32_t> m_paramDims = {1, 2};
286 
287     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
288     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
289 
290     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
291     std::shared_ptr<NNTensor> coeffTensor = TransToNNTensor(OH_NN_INT64, m_paramDims,
292         nullptr, OH_NN_REDUCE_L2_COEFF);
293     float coeffValue[2] = {1.0f, 1.0f};
294     coeffTensor->SetBuffer(coeffValue, 2 * sizeof(float));
295     m_allTensors.emplace_back(coeffTensor);
296     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
297 
298     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
299     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
300     coeffTensor->SetBuffer(nullptr, 0);
301 }
302 
303 /**
304  * @tc.name: reduceL2_build_012
305  * @tc.desc: Verify that the build function return a failed message with invalided reduceToEnd's dimension
306  * @tc.type: FUNC
307  */
308 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_012, TestSize.Level0)
309 {
310     std::vector<int32_t> m_paramDims = {1, 2};
311 
312     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
313     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
314 
315     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
316     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
317     std::shared_ptr<NNTensor> reduceToEndTensor = TransToNNTensor(OH_NN_BOOL, m_paramDims,
318         nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
319     bool reduceToEndValue[2] = {true, true};
320     reduceToEndTensor->SetBuffer(reduceToEndValue, 2 * sizeof(bool));
321     m_allTensors.emplace_back(reduceToEndTensor);
322 
323     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
324     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
325     reduceToEndTensor->SetBuffer(nullptr, 0);
326 }
327 
328 /**
329  * @tc.name: reduceL2_build_013
330  * @tc.desc: Verify that the build function return a failed message with invalided keepDims parameter
331  * @tc.type: FUNC
332  */
333 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_013, TestSize.Level0)
334 {
335     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
336     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
337     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_QUANT_DTYPE_CAST_SRC_T);
338     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
339     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
340 
341     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
342     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
343 }
344 
345 /**
346  * @tc.name: reduceL2_build_014
347  * @tc.desc: Verify that the build function return a failed message with invalided coeff parameter
348  * @tc.type: FUNC
349  */
350 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_014, TestSize.Level0)
351 {
352     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
353     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
354     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
355     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_QUANT_DTYPE_CAST_SRC_T);
356     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
357 
358     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
359     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
360 }
361 
362 /**
363  * @tc.name: reduceL2_build_015
364  * @tc.desc: Verify that the build function return a failed message with invalided reduceToEnd parameter
365  * @tc.type: FUNC
366  */
367 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_015, TestSize.Level0)
368 {
369     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
370     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
371     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
372     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
373     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_QUANT_DTYPE_CAST_SRC_T);
374 
375     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
376     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
377 }
378 
379 /**
380  * @tc.name: reduceL2_build_016
381  * @tc.desc: Verify that the build function return a failed message with empty keepdims's buffer
382  * @tc.type: FUNC
383  */
384 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_016, TestSize.Level0)
385 {
386     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
387     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
388 
389     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_BOOL,
390         m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
391     m_allTensors.emplace_back(keepDimsTensor);
392     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
393     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
394 
395     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
396     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
397 }
398 
399 /**
400  * @tc.name: reduceL2_build_017
401  * @tc.desc: Verify that the build function return a failed message with empty coeff's buffer
402  * @tc.type: FUNC
403  */
404 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_017, TestSize.Level0)
405 {
406     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
407     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
408 
409     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
410     std::shared_ptr<NNTensor> coeffTensor = TransToNNTensor(OH_NN_FLOAT32,
411         m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
412     m_allTensors.emplace_back(coeffTensor);
413     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
414 
415     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
416     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
417 }
418 
419 /**
420  * @tc.name: reduceL2_build_018
421  * @tc.desc: Verify that the build function return a failed message with empty reduceToEnd's buffer
422  * @tc.type: FUNC
423  */
424 HWTEST_F(ReduceL2BuilderTest, reduceL2_build_018, TestSize.Level0)
425 {
426     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
427     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
428 
429     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
430     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
431     std::shared_ptr<NNTensor> reduceToEndTensor = TransToNNTensor(OH_NN_BOOL,
432         m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
433     m_allTensors.emplace_back(reduceToEndTensor);
434 
435     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
436     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
437 }
438 
439 /**
440  * @tc.name: reduceL2_get_primitive_001
441  * @tc.desc: Verify the GetPrimitive function return nullptr
442  * @tc.type: FUNC
443  */
444 HWTEST_F(ReduceL2BuilderTest, reduceL2_get_primitive_001, TestSize.Level0)
445 {
446     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
447     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
448     EXPECT_EQ(primitive, expectPrimitive);
449 }
450 
451 /**
452  * @tc.name: reduceL2_get_primitive_002
453  * @tc.desc: Verify the normal params return behavior of the getprimitive function
454  * @tc.type: FUNC
455  */
456 HWTEST_F(ReduceL2BuilderTest, reduceL2_get_primitive_002, TestSize.Level0)
457 {
458     SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
459     SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
460     SetKeepDims(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_KEEP_DIMS);
461     SetCoeff(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_REDUCE_L2_COEFF);
462     SetReduceToEnd(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_L2_REDUCE_TO_END);
463 
464     bool keepDimsValue = true;
465     float coeffValue = 0.0f;
466     bool reduceToEndValue = true;
467     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
468     LiteGraphTensorPtr reduceL2Primitive = m_builder.GetPrimitive();
469     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
470 
471     EXPECT_NE(reduceL2Primitive, expectPrimitive);
472     auto returnKeepDimsValue = mindspore::lite::MindIR_ReduceFusion_GetKeepDims(reduceL2Primitive.get());
473     EXPECT_EQ(returnKeepDimsValue, keepDimsValue);
474     auto returnCoeffValue = mindspore::lite::MindIR_ReduceFusion_GetCoeff(reduceL2Primitive.get());
475     EXPECT_EQ(returnCoeffValue, coeffValue);
476     auto returnReduceToEndValue = mindspore::lite::MindIR_ReduceFusion_GetReduceToEnd(reduceL2Primitive.get());
477     EXPECT_EQ(returnReduceToEndValue, reduceToEndValue);
478 }
479 } // namespace UnitTest
480 } // namespace NeuralNetworkRuntime
481 } // namespace OHOS