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