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