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/sqrt_builder.h"
17 
18 #include <gtest/gtest.h>
19 #include "nn_tensor.h"
20 #include "ops_test.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::NeuralNetworkRuntime::Ops;
25 
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace UnitTest {
29 class SqrtBuilderTest : public OpsTest {
30 protected:
31     void InitTensor(const std::vector<uint32_t>& inputsIndex,
32         const std::vector<uint32_t>& outputsIndex) override;
33     void CheckResult();
34 
35 protected:
36     SqrtBuilder m_builder;
37 };
38 
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)39 void SqrtBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
40     const std::vector<uint32_t>& outputsIndex)
41 {
42     std::vector<int32_t> inputDim = {3};
43     std::vector<int32_t> OutputDim = {3};
44 
45     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
46     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
47 }
48 
CheckResult()49 void SqrtBuilderTest::CheckResult()
50 {
51     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
52     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
53     EXPECT_NE(primitive, expectPrimitive);
54 }
55 
56 /**
57  * @tc.name: sqrt_build_001
58  * @tc.desc: Provide normal input, output to verify the normal behavior of the Build function
59  * @tc.type: FUNC
60  */
61 HWTEST_F(SqrtBuilderTest, sqrt_build_001, TestSize.Level0)
62 {
63     std::vector<uint32_t> inputsIndex = { 0 };
64     std::vector<uint32_t> outputsIndex = { 1 };
65     std::vector<int32_t> inputDim = {3};
66     std::vector<int32_t> OutputDim = {3};
67 
68     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
69     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
70     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
71     EXPECT_EQ(OH_NN_SUCCESS, ret);
72 }
73 
74 /**
75  * @tc.name: sqrt_build_002
76  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
77  * @tc.type: FUNC
78  */
79 HWTEST_F(SqrtBuilderTest, sqrt_build_002, TestSize.Level0)
80 {
81     std::vector<uint32_t> inputsIndex = { 0 };
82     std::vector<uint32_t> outputsIndex = { 1 };
83     std::vector<int32_t> inputDim = {3};
84     std::vector<int32_t> OutputDim = {3};
85 
86     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
87     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
88     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
89     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
90     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
91 }
92 
93 /**
94  * @tc.name: sqrt_build_003
95  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
96  * @tc.type: FUNC
97  */
98 HWTEST_F(SqrtBuilderTest, sqrt_build_003, TestSize.Level0)
99 {
100     std::vector<uint32_t> inputsIndex = { 0, 1 };
101     std::vector<uint32_t> outputsIndex = { 2 };
102     std::vector<int32_t> inputDim = {3};
103     std::vector<int32_t> OutputDim = {3};
104 
105     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
106     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
107     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
108     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
109 }
110 
111 /**
112  * @tc.name: sqrt_build_004
113  * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
114  * @tc.type: FUNC
115  */
116 HWTEST_F(SqrtBuilderTest, sqrt_build_004, TestSize.Level0)
117 {
118     std::vector<uint32_t> inputsIndex = { 0 };
119     std::vector<uint32_t> outputsIndex = { 1, 2 };
120     std::vector<int32_t> inputDim = {3};
121     std::vector<int32_t> OutputDim = {3};
122 
123     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
124     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
125     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
126     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127 }
128 
129 /**
130  * @tc.name: sqrt_build_005
131  * @tc.desc: Provide empty input, output, and parameters to verify the abnormal behavior of the Build function
132  * @tc.type: FUNC
133  */
134 HWTEST_F(SqrtBuilderTest, sqrt_build_005, TestSize.Level0)
135 {
136     std::vector<uint32_t> inputsIndex = { 0 };
137     std::vector<uint32_t> outputsIndex = { 1 };
138     std::vector<uint32_t> paramsIndex = {};
139     OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, inputsIndex, outputsIndex, m_allTensors);
140     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
141 }
142 
143 /**
144  * @tc.name: sqrt_build_006
145  * @tc.desc: Provide empty output to verify the abnormal behavior of the Build function
146  * @tc.type: FUNC
147  */
148 HWTEST_F(SqrtBuilderTest, sqrt_build_006, TestSize.Level0)
149 {
150     std::vector<uint32_t> inputsIndex = { 0 };
151     std::vector<uint32_t> outputsIndex = {};
152     std::vector<int32_t> inputDim = {3};
153     std::vector<int32_t> OutputDim = {3};
154 
155     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
156     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
157     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
158     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
159 }
160 
161 /**
162  * @tc.name: sqrt_build_007
163  * @tc.desc: Provide a param to verify the abnormal behavior of the Build function
164  * @tc.type: FUNC
165  */
166 HWTEST_F(SqrtBuilderTest, sqrt_build_007, TestSize.Level0)
167 {
168     std::vector<uint32_t> inputsIndex = { 0 };
169     std::vector<uint32_t> outputsIndex = { 1 };
170     std::vector<uint32_t> paramsIndex = { 4 };
171 
172     m_paramsIndex = paramsIndex;
173     InitTensor(inputsIndex, outputsIndex);
174     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
175     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
176 }
177 
178 /**
179  * @tc.name: sqrt_getprimitive_001
180  * @tc.desc: Verify the GetPrimitive function return nullptr
181  * @tc.type: FUNC
182  */
183 HWTEST_F(SqrtBuilderTest, sqrt_getprimitive_001, TestSize.Level0)
184 {
185     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
186     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
187     EXPECT_EQ(primitive, expectPrimitive);
188 }
189 
190 /**
191  * @tc.name: sqrt_getprimitive_002
192  * @tc.desc: Verify the normal params return behavior of the getprimitive function
193  * @tc.type: FUNC
194  */
195 HWTEST_F(SqrtBuilderTest, sqrt_getprimitive_002, TestSize.Level0)
196 {
197     std::vector<uint32_t> inputsIndex = { 0 };
198     std::vector<uint32_t> outputsIndex = { 1 };
199     InitTensor(inputsIndex, outputsIndex);
200     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
201     CheckResult();
202 }
203 } // namespace UnitTest
204 } // namespace NeuralNetworkRuntime
205 } // namespace OHOS
206