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/rsqrt_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 RsqrtBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     RsqrtBuilder m_builder;
34     std::vector<uint32_t> m_inputs {0};
35     std::vector<uint32_t> m_outputs {1};
36     std::vector<int32_t> m_dim {1, 8, 1, 1};
37 };
38 
SetUp()39 void RsqrtBuilderTest::SetUp() {}
40 
TearDown()41 void RsqrtBuilderTest::TearDown() {}
42 
43 /**
44  * @tc.name: rsqrt_build_001
45  * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
46  * @tc.type: FUNC
47  */
48 HWTEST_F(RsqrtBuilderTest, rsqrt_build_001, TestSize.Level0)
49 {
50     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
51     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
52 
53     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputsIndex, m_allTensors);
54     EXPECT_EQ(OH_NN_SUCCESS, ret);
55 }
56 
57 /**
58  * @tc.name: rsqrt_build_002
59  * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
60  * @tc.type: FUNC
61  */
62 HWTEST_F(RsqrtBuilderTest, rsqrt_build_002, TestSize.Level0)
63 {
64     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
65     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
66 
67     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputs, m_outputsIndex, m_allTensors));
68     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputsIndex, m_allTensors);
69     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
70 }
71 
72 /**
73  * @tc.name: rsqrt_build_003
74  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
75  * @tc.type: FUNC
76  */
77 HWTEST_F(RsqrtBuilderTest, rsqrt_build_003, TestSize.Level0)
78 {
79     m_inputs = {0, 1};
80     m_outputs = {2};
81 
82     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
83     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
84 
85     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputsIndex, m_allTensors);
86     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
87 }
88 
89 /**
90  * @tc.name: rsqrt_build_004
91  * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
92  * @tc.type: FUNC
93  */
94 HWTEST_F(RsqrtBuilderTest, rsqrt_build_004, TestSize.Level0)
95 {
96     m_outputs = {1, 2};
97 
98     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
99     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
100 
101     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputsIndex, m_allTensors);
102     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
103 }
104 
105 /**
106  * @tc.name: rsqrt_build_005
107  * @tc.desc: Verify that the build function return a failed message with null allTensor
108  * @tc.type: FUNC
109  */
110 HWTEST_F(RsqrtBuilderTest, rsqrt_build_005, TestSize.Level0)
111 {
112     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputs, m_allTensors);
113     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
114 }
115 
116 /**
117  * @tc.name: rsqrt_build_006
118  * @tc.desc: Verify that the build function return a failed message without output tensor
119  * @tc.type: FUNC
120  */
121 HWTEST_F(RsqrtBuilderTest, rsqrt_build_006, TestSize.Level0)
122 {
123     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
124 
125     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputs, m_allTensors);
126     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127 }
128 
129 /**
130  * @tc.name: rsqrt_build_007
131  * @tc.desc: Verify that the build function return a failed message with a virtual parameter
132  * @tc.type: FUNC
133  */
134 HWTEST_F(RsqrtBuilderTest, rsqrt_build_007, TestSize.Level0)
135 {
136     std::vector<uint32_t> paramsIndex = {2};
137     std::vector<int32_t> paramDim = {};
138 
139     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
140     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
141     std::shared_ptr<NNTensor> paramTensor = TransToNNTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_TENSOR);
142     m_allTensors.emplace_back(paramTensor);
143 
144     OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, m_inputs, m_outputsIndex, m_allTensors);
145     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
146 }
147 
148 /**
149  * @tc.name: rsqrt_get_primitive_001
150  * @tc.desc: Verify the GetPrimitive function return nullptr
151  * @tc.type: FUNC
152  */
153 HWTEST_F(RsqrtBuilderTest, rsqrt_get_primitive_001, TestSize.Level0)
154 {
155     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
156     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
157     EXPECT_EQ(primitive, expectPrimitive);
158 }
159 
160 /**
161  * @tc.name: rsqrt_get_primitive_002
162  * @tc.desc: Verify the normal params return behavior of the getprimitive function
163  * @tc.type: FUNC
164  */
165 HWTEST_F(RsqrtBuilderTest, rsqrt_get_primitive_002, TestSize.Level0)
166 {
167     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_dim, nullptr);
168     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_dim, nullptr);
169 
170     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputs, m_outputsIndex, m_allTensors));
171     LiteGraphTensorPtr rsqrtPrimitive = m_builder.GetPrimitive();
172     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
173     EXPECT_NE(rsqrtPrimitive, expectPrimitive);
174 }
175 } // namespace UnitTest
176 } // namespace NeuralNetworkRuntime
177 } // namespace OHOS