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/sparse_to_dense_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 SparseToDenseBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SetInputTensor();
34     void SetOutputTensor();
35 
36 protected:
37     SparseToDenseBuilder m_builder;
38     std::vector<uint32_t> m_inputs {0, 1, 2};
39     std::vector<uint32_t> m_outputs {3};
40     std::vector<uint32_t> m_params {};
41     std::vector<int32_t> m_indicesDim {2, 2};
42     std::vector<int32_t> m_valueDim {2};
43     std::vector<int32_t> m_sparseShapeDim {2};
44     std::vector<int32_t> m_outputDim {2, 3};
45 };
46 
SetUp()47 void SparseToDenseBuilderTest::SetUp() {}
48 
TearDown()49 void SparseToDenseBuilderTest::TearDown() {}
50 
SetInputTensor()51 void SparseToDenseBuilderTest::SetInputTensor()
52 {
53     m_inputsIndex = m_inputs;
54     std::shared_ptr<NNTensor> indicesTensor;
55     indicesTensor = TransToNNTensor(OH_NN_FLOAT32, m_indicesDim, nullptr, OH_NN_TENSOR);
56     m_allTensors.emplace_back(indicesTensor);
57 
58     std::shared_ptr<NNTensor> valueTensor;
59     valueTensor = TransToNNTensor(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_TENSOR);
60     m_allTensors.emplace_back(valueTensor);
61 
62     std::shared_ptr<NNTensor> sparseShapeTensor;
63     sparseShapeTensor = TransToNNTensor(OH_NN_FLOAT32, m_sparseShapeDim, nullptr, OH_NN_TENSOR);
64     m_allTensors.emplace_back(sparseShapeTensor);
65 }
66 
67 /**
68  * @tc.name: SparseToDense_build_001
69  * @tc.desc: Verify that the build function returns a successful message.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(SparseToDenseBuilderTest, SparseToDense_build_001, TestSize.Level1)
73 {
74     SetInputTensor();
75     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
76 
77     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
78     EXPECT_EQ(OH_NN_SUCCESS, ret);
79 }
80 
81 /**
82  * @tc.name: SparseToDense_build_002
83  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
84  * @tc.type: FUNC
85  */
86 HWTEST_F(SparseToDenseBuilderTest, SparseToDense_build_002, TestSize.Level1)
87 {
88     SetInputTensor();
89     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
90 
91     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
92     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
93     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
94 }
95 
96 /**
97  * @tc.name: SparseToDense_build_003
98  * @tc.desc: Verify that the build function returns a failed message with invalided input.
99  * @tc.type: FUNC
100  */
101 HWTEST_F(SparseToDenseBuilderTest, SparseToDense_build_003, TestSize.Level1)
102 {
103     m_inputs = {0, 1, 2, 3};
104     m_outputs = {4};
105 
106     SetInputTensor();
107     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
108 
109     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
110     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
111 }
112 
113 /**
114  * @tc.name: SparseToDense_build_004
115  * @tc.desc: Verify that the build function returns a failed message with invalided output.
116  * @tc.type: FUNC
117  */
118 HWTEST_F(SparseToDenseBuilderTest, SparseToDense_build_004, TestSize.Level1)
119 {
120     m_outputs = {3, 4};
121 
122     SetInputTensor();
123     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
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: SparseToDense_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(SparseToDenseBuilderTest, SparseToDense_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: SparseToDense_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(SparseToDenseBuilderTest, SparseToDense_build_006, TestSize.Level1)
146 {
147     SetInputTensor();
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: SparseToDense_getprimitive_001
155  * @tc.desc: Verify that the getPrimitive function returns a successful message.
156  * @tc.type: FUNC
157  */
158 HWTEST_F(SparseToDenseBuilderTest, SparseToDense_getprimitive_001, TestSize.Level1)
159 {
160     SetInputTensor();
161     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
162 
163     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
164     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
165     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
166     EXPECT_NE(expectPrimitive, primitive);
167 }
168 
169 /**
170  * @tc.name: SparseToDense_getprimitive_002
171  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
172  * @tc.type: FUNC
173  */
174 HWTEST_F(SparseToDenseBuilderTest, SparseToDense_getprimitive_002, TestSize.Level1)
175 {
176     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
177     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
178     EXPECT_EQ(expectPrimitive, primitive);
179 }
180 }
181 }
182 }