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/cast_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 CastBuilderTest : public OpsTest {
28 public:
29 void SetUp() override;
30 void TearDown() override;
31
32 void SetCastAddToallTensors();
33
34 public:
35 CastBuilder m_builder;
36 std::vector<uint32_t> m_inputs{0, 1};
37 std::vector<uint32_t> m_outputs{2};
38 std::vector<uint32_t> m_params{};
39 std::vector<int32_t> m_output_dim{1, 2, 2, 1};
40 };
41
SetUp()42 void CastBuilderTest::SetUp() {}
43
TearDown()44 void CastBuilderTest::TearDown() {}
45
SetCastAddToallTensors()46 void CastBuilderTest::SetCastAddToallTensors()
47 {
48 std::vector<int32_t> m_input_dim{1, 2, 2, 1};
49 std::vector<int32_t> typeDim = {};
50 std::shared_ptr<NNTensor> inputTensor;
51 inputTensor = TransToNNTensor(OH_NN_FLOAT32, m_input_dim, nullptr, OH_NN_TENSOR);
52 m_allTensors.emplace_back(inputTensor);
53
54 inputTensor = TransToNNTensor(OH_NN_INT32, typeDim, nullptr, OH_NN_TENSOR);
55 int32_t* typeValue = new (std::nothrow) int32_t(4);
56 EXPECT_NE(nullptr, typeValue);
57 inputTensor->SetBuffer(typeValue, sizeof(int32_t));
58 m_allTensors.emplace_back(inputTensor);
59 }
60
61 /**
62 * @tc.name: cast_build_001
63 * @tc.desc: Verify the success of the build function
64 * @tc.type: FUNC
65 */
66 HWTEST_F(CastBuilderTest, cast_build_001, TestSize.Level1)
67 {
68 m_paramsIndex = m_params;
69 m_inputsIndex = m_inputs;
70
71 SetCastAddToallTensors();
72 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
73 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
74 }
75
76 /**
77 * @tc.name: cast_build_002
78 * @tc.desc: Verify the forbidden of the build function
79 * @tc.type: FUNC
80 */
81 HWTEST_F(CastBuilderTest, cast_build_002, TestSize.Level1)
82 {
83 m_paramsIndex = m_params;
84 m_inputsIndex = m_inputs;
85
86 SetCastAddToallTensors();
87 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
88 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
89 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
90 }
91
92 /**
93 * @tc.name: cast_build_003
94 * @tc.desc: Verify the missing input of the build function
95 * @tc.type: FUNC
96 */
97 HWTEST_F(CastBuilderTest, cast_build_003, TestSize.Level1)
98 {
99 m_inputs = {0};
100 m_outputs = {1};
101 m_paramsIndex = m_params;
102 m_inputsIndex = m_inputs;
103
104 SetCastAddToallTensors();
105 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
106 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
107 }
108
109 /**
110 * @tc.name: cast_build_004
111 * @tc.desc: Verify the missing output of the build function
112 * @tc.type: FUNC
113 */
114 HWTEST_F(CastBuilderTest, cast_build_004, TestSize.Level1)
115 {
116 m_inputs = {0, 1};
117 m_outputs = {};
118 m_paramsIndex = m_params;
119 m_inputsIndex = m_inputs;
120
121 SetCastAddToallTensors();
122 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
123 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
124 }
125
126 /**
127 * @tc.name: cast_build_005
128 * @tc.desc: Verify the inputIndex out of bounds of the build function
129 * @tc.type: FUNC
130 */
131 HWTEST_F(CastBuilderTest, cast_build_005, TestSize.Level1)
132 {
133 m_inputs = {0, 6};
134 m_outputs = {2};
135 m_paramsIndex = m_params;
136 m_inputsIndex = m_inputs;
137
138 SetCastAddToallTensors();
139 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
140 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
141 }
142
143 /**
144 * @tc.name: cast_build_006
145 * @tc.desc: Verify the outputIndex out of bounds of the build function
146 * @tc.type: FUNC
147 */
148 HWTEST_F(CastBuilderTest, cast_build_006, TestSize.Level1)
149 {
150 m_inputs = {0, 1};
151 m_outputs = {6};
152 m_paramsIndex = m_params;
153 m_inputsIndex = m_inputs;
154
155 SetCastAddToallTensors();
156 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
157 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
158 }
159
160 /**
161 * @tc.name: cast_build_007
162 * @tc.desc: Verify the paramIndex not empty of the build function
163 * @tc.type: FUNC
164 */
165 HWTEST_F(CastBuilderTest, cast_build_007, TestSize.Level1)
166 {
167 m_params = {1};
168 m_paramsIndex = m_params;
169 m_inputsIndex = m_inputs;
170
171 SetCastAddToallTensors();
172 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
173 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
174 }
175
176 /**
177 * @tc.name: cast_build_008
178 * @tc.desc: Verify the paramIndex not empty of the build function
179 * @tc.type: FUNC
180 */
181 HWTEST_F(CastBuilderTest, cast_build_008, TestSize.Level1)
182 {
183 m_paramsIndex = m_params;
184 m_inputsIndex = m_inputs;
185
186 std::vector<int32_t> m_input_dim{1, 2, 2, 1};
187 std::vector<int32_t> typeDim = {};
188 std::shared_ptr<NNTensor> inputTensor;
189 inputTensor = TransToNNTensor(OH_NN_FLOAT32, m_input_dim, nullptr, OH_NN_TENSOR);
190 m_allTensors.emplace_back(inputTensor);
191
192 inputTensor = TransToNNTensor(OH_NN_INT32, typeDim, nullptr, OH_NN_TENSOR);
193 int32_t* typeValue = new (std::nothrow) int32_t(40);
194 EXPECT_NE(nullptr, typeValue);
195
196 inputTensor->SetBuffer(typeValue, sizeof(int32_t));
197 m_allTensors.emplace_back(inputTensor);
198 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
199 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
200 }
201
202 /**
203 * @tc.name: cast_build_009
204 * @tc.desc: Verify the cast without set types of the build function
205 * @tc.type: FUNC
206 */
207 HWTEST_F(CastBuilderTest, cast_build_009, TestSize.Level1)
208 {
209 m_paramsIndex = m_params;
210 m_inputsIndex = m_inputs;
211
212 std::vector<int32_t> m_input_dim{1, 2, 2, 1};
213 std::vector<int32_t> typeDim = {};
214 std::shared_ptr<NNTensor> inputTensor;
215 inputTensor = TransToNNTensor(OH_NN_FLOAT32, m_input_dim, nullptr, OH_NN_TENSOR);
216 m_allTensors.emplace_back(inputTensor);
217
218 inputTensor = TransToNNTensor(OH_NN_INT32, typeDim, nullptr, OH_NN_TENSOR);
219 m_allTensors.emplace_back(inputTensor);
220 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
221 EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
222 }
223
224 /**
225 * @tc.name: cast_getprimitive_001
226 * @tc.desc: Verify the behavior of the GetPrimitive function
227 * @tc.type: FUNC
228 */
229 HWTEST_F(CastBuilderTest, cast_getprimitive_001, TestSize.Level1)
230 {
231 m_paramsIndex = m_params;
232 m_inputsIndex = m_inputs;
233 SetCastAddToallTensors();
234 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
235 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
236
237 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
238 LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
239 EXPECT_NE(expectPrimitive, primitive);
240 }
241
242 /**
243 * @tc.name: cast_getprimitive_002
244 * @tc.desc: Verify the behavior of the GetPrimitive function
245 * @tc.type: FUNC
246 */
247 HWTEST_F(CastBuilderTest, cast_getprimitive_002, TestSize.Level1)
248 {
249 m_paramsIndex = m_params;
250 m_inputsIndex = m_inputs;
251 SetCastAddToallTensors();
252 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
253
254 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
255 LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
256 EXPECT_EQ(expectPrimitive, primitive);
257 }
258 } // namespace UnitTest
259 } // namespace NeuralNetworkRuntime
260 } // namespace OHOS