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/all_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 AllBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SaveParamsTensor(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35     void SetInputTensor();
36 
37 protected:
38     AllBuilder m_builder;
39     std::vector<uint32_t> m_inputs {0, 1};
40     std::vector<uint32_t> m_outputs {2};
41     std::vector<uint32_t> m_params {3};
42     std::vector<int32_t> m_dim {2, 2};
43     std::vector<int32_t> m_paramDim {};
44 };
45 
SetUp()46 void AllBuilderTest::SetUp() {}
47 
TearDown()48 void AllBuilderTest::TearDown() {}
49 
SaveParamsTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)50 void AllBuilderTest::SaveParamsTensor(OH_NN_DataType dataType,
51     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
52 {
53     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(dataType, dim, quantParam, type);
54     int64_t* keepDimsValue = new (std::nothrow) int64_t[1] {0};
55     EXPECT_NE(nullptr, keepDimsValue);
56     keepDimsTensor->SetBuffer(keepDimsValue, sizeof(int64_t));
57     m_allTensors.emplace_back(keepDimsTensor);
58 }
59 
SetInputTensor()60 void AllBuilderTest::SetInputTensor()
61 {
62     m_inputsIndex = m_inputs;
63     std::shared_ptr<NNTensor> inputTensor;
64     inputTensor = TransToNNTensor(OH_NN_INT32, m_dim, nullptr, OH_NN_TENSOR);
65     m_allTensors.emplace_back(inputTensor);
66 
67     std::shared_ptr<NNTensor> axisTensor;
68     axisTensor = TransToNNTensor(OH_NN_INT32, m_dim, nullptr, OH_NN_TENSOR);
69     m_allTensors.emplace_back(axisTensor);
70 }
71 
72 /**
73  * @tc.name: all_build_001
74  * @tc.desc: Verify that the build function returns a successful message.
75  * @tc.type: FUNC
76  */
77 HWTEST_F(AllBuilderTest, all_build_001, TestSize.Level1)
78 {
79     SetInputTensor();
80     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
81     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_ALL_KEEP_DIMS);
82 
83     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
84     EXPECT_EQ(OH_NN_SUCCESS, ret);
85 }
86 
87 /**
88  * @tc.name: all_build_002
89  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
90  * @tc.type: FUNC
91  */
92 HWTEST_F(AllBuilderTest, all_build_002, TestSize.Level1)
93 {
94     SetInputTensor();
95     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
96     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_ALL_KEEP_DIMS);
97 
98     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
99     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
100     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
101 }
102 
103 /**
104  * @tc.name: all_build_003
105  * @tc.desc: Verify that the build function returns a failed message with invalided input.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(AllBuilderTest, all_build_003, TestSize.Level1)
109 {
110     m_inputs = {0, 1, 2};
111     m_outputs = {3};
112     m_params = {4};
113 
114     SetInputTensor();
115     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
116     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_ALL_KEEP_DIMS);
117 
118     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
119     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
120 }
121 
122 /**
123  * @tc.name: all_build_004
124  * @tc.desc: Verify that the build function returns a failed message with invalided output.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(AllBuilderTest, all_build_004, TestSize.Level1)
128 {
129     m_outputs = {2, 3};
130     m_params = {4};
131 
132     SetInputTensor();
133     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
134     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_ALL_KEEP_DIMS);
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: all_build_005
142  * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
143  * @tc.type: FUNC
144  */
145 HWTEST_F(AllBuilderTest, all_build_005, TestSize.Level1)
146 {
147     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
148     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
149 }
150 
151 /**
152  * @tc.name: all_build_006
153  * @tc.desc: Verify that the build function returns a failed message without output tensor.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(AllBuilderTest, all_build_006, TestSize.Level1)
157 {
158     SetInputTensor();
159 
160     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
161     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
162 }
163 
164 /**
165  * @tc.name: all_build_007
166  * @tc.desc: Verify that the build function returns a failed message with invalid keep_dims's dataType.
167  * @tc.type: FUNC
168  */
169 HWTEST_F(AllBuilderTest, all_build_007, TestSize.Level1)
170 {
171     SetInputTensor();
172     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
173 
174     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_FLOAT32, m_paramDim,
175         nullptr, OH_NN_ALL_KEEP_DIMS);
__anonbc7e07ea0102null176     float* keepDimsValue = new (std::nothrow) float[1] {0.0f};
177     keepDimsTensor->SetBuffer(keepDimsValue, sizeof(float));
178     m_allTensors.emplace_back(keepDimsTensor);
179 
180     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
181     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
182 }
183 
184 /**
185  * @tc.name: all_build_008
186  * @tc.desc: Verify that the build function returns a failed message with passing invalid keep_dims param.
187  * @tc.type: FUNC
188  */
189 HWTEST_F(AllBuilderTest, all_build_008, TestSize.Level1)
190 {
191     SetInputTensor();
192     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
193     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
194 
195     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
196     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
197 }
198 
199 /**
200  * @tc.name: all_build_009
201  * @tc.desc: Verify that the build function returns a failed message without set buffer for keep_dims.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(AllBuilderTest, all_build_009, TestSize.Level1)
205 {
206     SetInputTensor();
207     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
208 
209     std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_INT64, m_paramDim,
210         nullptr, OH_NN_ALL_KEEP_DIMS);
211     m_allTensors.emplace_back(keepDimsTensor);
212 
213     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
214     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
215 }
216 
217 /**
218  * @tc.name: all_getprimitive_001
219  * @tc.desc: Verify that the getPrimitive function returns a successful message
220  * @tc.type: FUNC
221  */
222 HWTEST_F(AllBuilderTest, all_getprimitive_001, TestSize.Level1)
223 {
224     SetInputTensor();
225     SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
226     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_ALL_KEEP_DIMS);
227 
228     int64_t keepDimsValue = 0;
229     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
230     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
231     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
232     EXPECT_NE(expectPrimitive, primitive);
233 
234     auto returnValue = mindspore::lite::MindIR_All_GetKeepDims(primitive.get());
235     EXPECT_EQ(returnValue, keepDimsValue);
236 }
237 
238 /**
239  * @tc.name: all_getprimitive_002
240  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
241  * @tc.type: FUNC
242  */
243 HWTEST_F(AllBuilderTest, all_getprimitive_002, TestSize.Level1)
244 {
245     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
246     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
247     EXPECT_EQ(expectPrimitive, primitive);
248 }
249 }
250 }
251 }