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/transpose_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 TransposeBuilderTest : 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     TransposeBuilder m_builder;
36 };
37 
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)38 void TransposeBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
39     const std::vector<uint32_t>& outputsIndex)
40 {
41     std::vector<int32_t> inputDim = {2, 3};
42     std::vector<int32_t> OutputDim = {3, 2};
43 
44     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
45     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
46 }
47 
48 /**
49  * @tc.name: transpose_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(TransposeBuilderTest, transpose_build_001, TestSize.Level0)
54 {
55     std::vector<uint32_t> inputsIndex = { 0, 1 };
56     std::vector<uint32_t> outputsIndex = { 2 };
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: transpose_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(TransposeBuilderTest, transpose_build_002, TestSize.Level0)
70 {
71     std::vector<uint32_t> inputsIndex = { 0, 1 };
72     std::vector<uint32_t> outputsIndex = { 2 };
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: transpose_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(TransposeBuilderTest, transpose_build_003, TestSize.Level0)
87 {
88     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
89     std::vector<uint32_t> outputsIndex = { 3 };
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: transpose_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(TransposeBuilderTest, transpose_build_004, TestSize.Level0)
103 {
104     std::vector<uint32_t> inputsIndex = { 0, 1 };
105     std::vector<uint32_t> outputsIndex = { 2, 3 };
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: transpose_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(TransposeBuilderTest, transpose_build_005, TestSize.Level0)
119 {
120     std::vector<uint32_t> inputsIndex = { 0, 1 };
121     std::vector<uint32_t> outputsIndex = { 2 };
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: transpose_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(TransposeBuilderTest, transpose_build_006, TestSize.Level0)
134 {
135     std::vector<uint32_t> inputsIndex = { 0, 1 };
136     std::vector<uint32_t> outputsIndex = {};
137 
138     InitTensor(inputsIndex, outputsIndex);
139 
140     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
141     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
142 }
143 
144 /**
145  * @tc.name: transpose_build_007
146  * @tc.desc: Provide a param to verify the abnormal behavior of the Build function
147  * @tc.type: FUNC
148  */
149 HWTEST_F(TransposeBuilderTest, transpose_build_007, TestSize.Level0)
150 {
151     std::vector<uint32_t> inputsIndex = { 0, 1 };
152     std::vector<uint32_t> outputsIndex = { 2 };
153     std::vector<uint32_t> paramsIndex = { 4 };
154 
155     m_paramsIndex = paramsIndex;
156     InitTensor(inputsIndex, outputsIndex);
157 
158     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
159     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
160 }
161 
162 /**
163  * @tc.name: transpose_get_primitive_001
164  * @tc.desc: Verify the GetPrimitive function return nullptr
165  * @tc.type: FUNC
166  */
167 HWTEST_F(TransposeBuilderTest, transpose_get_primitive_001, TestSize.Level0)
168 {
169     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
170     LiteGraphTensorPtr expectPrimitive = { nullptr, DestroyLiteGraphPrimitive };
171     EXPECT_EQ(primitive, expectPrimitive);
172 }
173 
174 /**
175  * @tc.name: transpose_getprimitive_002
176  * @tc.desc: Verify the normal return behavior of the getprimitive function
177  * @tc.type: FUNC
178  */
179 HWTEST_F(TransposeBuilderTest, transpose_getprimitive_002, TestSize.Level0)
180 {
181     std::vector<uint32_t> inputsIndex = { 0, 1 };
182     std::vector<uint32_t> outputsIndex = { 2 };
183 
184     InitTensor(inputsIndex, outputsIndex);
185 
186     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
187     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
188     LiteGraphTensorPtr expectPrimitive = { nullptr, DestroyLiteGraphPrimitive };
189     EXPECT_NE(primitive, expectPrimitive);
190 }
191 } // namespace UnitTest
192 } // namespace NeuralNetworkRuntime
193 } // namespace OHOS
194