1 /*
2  * Copyright (c) 2023 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/broadcast_to_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 BroadcastToBuilderTest : 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 
36 protected:
37     BroadcastToBuilder m_builder;
38     std::vector<uint32_t> m_inputs {0};
39     std::vector<uint32_t> m_outputs {1};
40     std::vector<uint32_t> m_params {2};
41     std::vector<int32_t> m_inputDim {1, 3};
42     std::vector<int32_t> m_outputDim {2, 3};
43     std::vector<int32_t> m_paramDim {2};
44 };
45 
SetUp()46 void BroadcastToBuilderTest::SetUp() {}
47 
TearDown()48 void BroadcastToBuilderTest::TearDown() {}
49 
SaveParamsTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)50 void BroadcastToBuilderTest::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> shapeTensor = TransToNNTensor(dataType, dim, quantParam, type);
54     int64_t* shapeValue = new (std::nothrow) int64_t[2] {2, 3};
55     int32_t shapeSize = 2;
56     EXPECT_NE(nullptr, shapeValue);
57     shapeTensor->SetBuffer(shapeValue, sizeof(int64_t) * shapeSize);
58     m_allTensors.emplace_back(shapeTensor);
59 }
60 
61 /**
62  * @tc.name: broadcast_to_build_001
63  * @tc.desc: Verify that the build function returns a successful message.
64  * @tc.type: FUNC
65  */
66 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_001, TestSize.Level1)
67 {
68     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
69     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
70     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_BROADCAST_TO_SHAPE);
71 
72     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
73     EXPECT_EQ(OH_NN_SUCCESS, ret);
74 }
75 
76 /**
77  * @tc.name: broadcast_to_build_002
78  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_002, TestSize.Level1)
82 {
83     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
84     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
85     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_BROADCAST_TO_SHAPE);
86 
87     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
88     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
89     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
90 }
91 
92 /**
93  * @tc.name: broadcast_to_build_003
94  * @tc.desc: Verify that the build function returns a failed message with invalided input.
95  * @tc.type: FUNC
96  */
97 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_003, TestSize.Level1)
98 {
99     m_inputs = {0, 1};
100     m_outputs = {2};
101     m_params = {3};
102 
103     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
104     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
105     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_BROADCAST_TO_SHAPE);
106 
107     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
108     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
109 }
110 
111 /**
112  * @tc.name: broadcast_to_build_004
113  * @tc.desc: Verify that the build function returns a failed message with invalided output.
114  * @tc.type: FUNC
115  */
116 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_004, TestSize.Level1)
117 {
118     m_outputs = {1, 2};
119     m_params = {3};
120 
121     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
122     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
123     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_BROADCAST_TO_SHAPE);
124 
125     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
126     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127 }
128 
129 /**
130  * @tc.name: broadcast_to_build_005
131  * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_005, TestSize.Level1)
135 {
136     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
137     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
138 }
139 
140 /**
141  * @tc.name: broadcast_to_build_006
142  * @tc.desc: Verify that the build function returns a failed message without output tensor.
143  * @tc.type: FUNC
144  */
145 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_006, TestSize.Level1)
146 {
147     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
148 
149     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
150     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
151 }
152 
153 /**
154  * @tc.name: broadcast_to_build_007
155  * @tc.desc: Verify that the build function returns a failed message with invalid shape's dataType.
156  * @tc.type: FUNC
157  */
158 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_007, TestSize.Level1)
159 {
160     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
161     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
162 
163     std::shared_ptr<NNTensor> shapeTensor = TransToNNTensor(OH_NN_FLOAT32, m_paramDim,
164         nullptr, OH_NN_BROADCAST_TO_SHAPE);
__anon6f26c7460102null165     float* shapeValue = new (std::nothrow) float[2] {2.0f, 3.0f};
166     int32_t shapeSize = 2;
167     shapeTensor->SetBuffer(shapeValue, sizeof(float) * shapeSize);
168     m_allTensors.emplace_back(shapeTensor);
169 
170     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
171     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
172 }
173 
174 /**
175  * @tc.name: broadcast_to_build_008
176  * @tc.desc: Verify that the build function returns a failed message with passing invalid param.
177  * @tc.type: FUNC
178  */
179 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_008, TestSize.Level1)
180 {
181     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
182     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
183     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
184 
185     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
186     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
187 }
188 
189 /**
190  * @tc.name: broadcast_to_build_009
191  * @tc.desc: Verify that the build function returns a failed message without set buffer for shape.
192  * @tc.type: FUNC
193  */
194 HWTEST_F(BroadcastToBuilderTest, broadcast_to_build_009, TestSize.Level1)
195 {
196     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
197     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
198 
199     std::shared_ptr<NNTensor> shapeTensor = TransToNNTensor(OH_NN_INT64, m_paramDim,
200         nullptr, OH_NN_BROADCAST_TO_SHAPE);
201     m_allTensors.emplace_back(shapeTensor);
202 
203     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
204     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
205 }
206 
207 /**
208  * @tc.name: broadcast_to_getprimitive_001
209  * @tc.desc: Verify that the getPrimitive function returns a successful message
210  * @tc.type: FUNC
211  */
212 HWTEST_F(BroadcastToBuilderTest, broadcast_to_getprimitive_001, TestSize.Level1)
213 {
214     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
215     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
216     SaveParamsTensor(OH_NN_INT64, m_paramDim, nullptr, OH_NN_BROADCAST_TO_SHAPE);
217 
218     std::vector<int64_t> shapeValue = {2, 3};
219     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
220     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
221     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
222     EXPECT_NE(expectPrimitive, primitive);
223 
224     auto returnShape = mindspore::lite::MindIR_BroadcastTo_GetShape(primitive.get());
225     auto returnShapeSize = returnShape.size();
226     for (size_t i = 0; i < returnShapeSize; ++i) {
227         EXPECT_EQ(returnShape[i], shapeValue[i]);
228     }
229 }
230 
231 /**
232  * @tc.name: broadcast_to_getprimitive_002
233  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
234  * @tc.type: FUNC
235  */
236 HWTEST_F(BroadcastToBuilderTest, broadcast_to_getprimitive_002, TestSize.Level1)
237 {
238     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
239     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
240     EXPECT_EQ(expectPrimitive, primitive);
241 }
242 }
243 }
244 }