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/reshape_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 ReshapeBuilderTest : public OpsTest {
28 public:
29 void SetUp() override;
30 void TearDown() override;
31
32 protected:
33 ReshapeBuilder m_builder;
34 std::vector<uint32_t> m_inputs {0, 1};
35 std::vector<uint32_t> m_outputs {2};
36 std::vector<int32_t> m_inputDim {1, 2, 4, 1};
37 std::vector<int32_t> m_outputDim {1, 4, 2, 4};
38 };
39
SetUp()40 void ReshapeBuilderTest::SetUp() {}
41
TearDown()42 void ReshapeBuilderTest::TearDown() {}
43
44 /**
45 * @tc.name: reshape_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(ReshapeBuilderTest, reshape_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: reshape_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(ReshapeBuilderTest, reshape_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: reshape_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(ReshapeBuilderTest, reshape_build_003, TestSize.Level0)
79 {
80 m_inputs = {0, 1, 2};
81 m_outputs = {3};
82 SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
83 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
84
85 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
86 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
87 }
88
89 /**
90 * @tc.name: reshape_build_004
91 * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
92 * @tc.type: FUNC
93 */
94 HWTEST_F(ReshapeBuilderTest, reshape_build_004, TestSize.Level0)
95 {
96 m_outputs = {2, 3};
97
98 SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
99 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
100
101 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
102 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
103 }
104
105 /**
106 * @tc.name: reshape_build_005
107 * @tc.desc: Verify that the build function return a failed message with null allTensor
108 * @tc.type: FUNC
109 */
110 HWTEST_F(ReshapeBuilderTest, reshape_build_005, TestSize.Level0)
111 {
112 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputs, m_allTensors);
113 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
114 }
115
116 /**
117 * @tc.name: reshape_build_006
118 * @tc.desc: Verify that the build function return a failed message without output tensor
119 * @tc.type: FUNC
120 */
121 HWTEST_F(ReshapeBuilderTest, reshape_build_006, TestSize.Level0)
122 {
123 SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
124
125 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputs, m_allTensors);
126 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
127 }
128
129 /**
130 * @tc.name: reshape_build_007
131 * @tc.desc: Verify that the build function return a failed message with a virtual parameter
132 * @tc.type: FUNC
133 */
134 HWTEST_F(ReshapeBuilderTest, reshape_build_007, TestSize.Level0)
135 {
136 std::vector<uint32_t> paramsIndex = {3};
137 std::vector<int32_t> paramDim = {};
138
139 SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
140 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
141 std::shared_ptr<NNTensor> paramTensor = TransToNNTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_TENSOR);
142 m_allTensors.emplace_back(paramTensor);
143
144 OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
145 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
146 }
147
148 /**
149 * @tc.name: reshape_get_primitive_001
150 * @tc.desc: Verify the GetPrimitive function return nullptr
151 * @tc.type: FUNC
152 */
153 HWTEST_F(ReshapeBuilderTest, reshape_get_primitive_001, TestSize.Level0)
154 {
155 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
156 LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
157 EXPECT_EQ(primitive, expectPrimitive);
158 }
159
160 /**
161 * @tc.name: reshape_get_primitive_002
162 * @tc.desc: Verify the normal params return behavior of the getprimitive function
163 * @tc.type: FUNC
164 */
165 HWTEST_F(ReshapeBuilderTest, reshape_get_primitive_002, TestSize.Level0)
166 {
167 SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
168 SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
169
170 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
171 LiteGraphTensorPtr reshapePrimitive = m_builder.GetPrimitive();
172 LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
173 EXPECT_NE(reshapePrimitive, expectPrimitive);
174 }
175 } // namespace UnitTest
176 } // namespace NeuralNetworkRuntime
177 } // namespace OHOS