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