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/bias_add_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 BiasAddBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32     void SetBiasAddToallTensors();
33 public:
34     BiasAddBuilder m_builder;
35     std::vector<uint32_t> m_inputs{0, 1};
36     std::vector<uint32_t> m_outputs{2};
37     std::vector<uint32_t> m_params{};
38     std::vector<int32_t> m_output_dim{2, 3};
39 };
40 
SetUp()41 void BiasAddBuilderTest::SetUp() {}
42 
TearDown()43 void BiasAddBuilderTest::TearDown() {}
44 
SetBiasAddToallTensors()45 void BiasAddBuilderTest::SetBiasAddToallTensors()
46 {
47     std::vector<int32_t> m_input_dim{2, 3};
48     std::vector<int32_t> biasDim{3};
49     std::shared_ptr<NNTensor> inputTensor;
50     inputTensor = TransToNNTensor(OH_NN_FLOAT32, m_input_dim, nullptr, OH_NN_TENSOR);
51     m_allTensors.emplace_back(inputTensor);
52     inputTensor = TransToNNTensor(OH_NN_FLOAT32, biasDim, nullptr, OH_NN_TENSOR);
53     m_allTensors.emplace_back(inputTensor);
54 }
55 
56 /**
57  * @tc.name: biasadd_build_001
58  * @tc.desc: Verify the success of the build function
59  * @tc.type: FUNC
60  */
61 HWTEST_F(BiasAddBuilderTest, biasadd_build_001, TestSize.Level1)
62 {
63     m_paramsIndex = m_params;
64     m_inputsIndex = m_inputs;
65     SetBiasAddToallTensors();
66     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
67     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
68 }
69 
70 /**
71  * @tc.name: biasadd_build_002
72  * @tc.desc: Verify the forbidden of the build function
73  * @tc.type: FUNC
74  */
75 HWTEST_F(BiasAddBuilderTest, biasadd_build_002, TestSize.Level1)
76 {
77     m_paramsIndex = m_params;
78     m_inputsIndex = m_inputs;
79 
80     SetBiasAddToallTensors();
81     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
82     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
83     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
84 }
85 
86 /**
87  * @tc.name: biasadd_build_003
88  * @tc.desc: Verify the missing input of the build function
89  * @tc.type: FUNC
90  */
91 HWTEST_F(BiasAddBuilderTest, biasadd_build_003, TestSize.Level1)
92 {
93     m_inputs = {0};
94     m_outputs = {1};
95     m_params = {};
96     m_paramsIndex = m_params;
97     m_inputsIndex = m_inputs;
98 
99     SetBiasAddToallTensors();
100     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
101     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
102 }
103 
104 /**
105  * @tc.name: biasadd_build_004
106  * @tc.desc: Verify the missing output of the build function
107  * @tc.type: FUNC
108  */
109 HWTEST_F(BiasAddBuilderTest, biasadd_build_004, TestSize.Level1)
110 {
111     m_inputs = {0, 1};
112     m_outputs = {};
113     m_params = {};
114     m_paramsIndex = m_params;
115     m_inputsIndex = m_inputs;
116     SetBiasAddToallTensors();
117 
118     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
119     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
120 }
121 
122 /**
123  * @tc.name: biasadd_build_005
124  * @tc.desc: Verify the inputIndex out of bounds of the build function
125  * @tc.type: FUNC
126  */
127 HWTEST_F(BiasAddBuilderTest, biasadd_build_005, TestSize.Level1)
128 {
129     m_inputs = {0, 6};
130     m_outputs = {2};
131     m_params = {};
132     m_paramsIndex = m_params;
133     m_inputsIndex = m_inputs;
134     SetBiasAddToallTensors();
135 
136     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
137     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
138 }
139 
140 /**
141  * @tc.name: biasadd_build_006
142  * @tc.desc: Verify the outputIndex out of bounds of the build function
143  * @tc.type: FUNC
144  */
145 HWTEST_F(BiasAddBuilderTest, biasadd_build_006, TestSize.Level1)
146 {
147     m_inputs = {0, 1};
148     m_outputs = {6};
149     m_params = {};
150     m_paramsIndex = m_params;
151     m_inputsIndex = m_inputs;
152     SetBiasAddToallTensors();
153 
154     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
155     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
156 }
157 
158 /**
159  * @tc.name: biasadd_build_007
160  * @tc.desc: Verify the paramIndex not empty of the build function
161  * @tc.type: FUNC
162  */
163 HWTEST_F(BiasAddBuilderTest, biasadd_build_007, TestSize.Level1)
164 {
165     m_params = {1};
166     m_paramsIndex = m_params;
167     m_inputsIndex = m_inputs;
168     SetBiasAddToallTensors();
169 
170     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
171     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
172 }
173 
174 /**
175  * @tc.name: biasadd_getprimitive_001
176  * @tc.desc: Verify the success of the GetPrimitive function
177  * @tc.type: FUNC
178  */
179 HWTEST_F(BiasAddBuilderTest, biasadd_getprimitive_001, TestSize.Level1)
180 {
181     m_paramsIndex = m_params;
182     m_inputsIndex = m_inputs;
183 
184     SetBiasAddToallTensors();
185     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
186     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
187 
188     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
189     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
190     EXPECT_NE(expectPrimitive, primitive);
191 }
192 
193 /**
194  * @tc.name: biasadd_getprimitive_002
195  * @tc.desc: Verify the nullptr return of the GetPrimitive function
196  * @tc.type: FUNC
197  */
198 HWTEST_F(BiasAddBuilderTest, biasadd_getprimitive_002, TestSize.Level1)
199 {
200     m_paramsIndex = m_params;
201     m_inputsIndex = m_inputs;
202 
203     SetBiasAddToallTensors();
204     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
205 
206     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
207     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
208     EXPECT_EQ(expectPrimitive, primitive);
209 }
210 } // namespace UnitTest
211 } // namespace NeuralNetworkRuntime
212 } // namespace OHOS