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