1 /*
2 * Copyright (c) 2023 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/mod_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 ModBuilderTest : public OpsTest {
28 public:
29 void SetUp() override;
30 void TearDown() override;
31
32 protected:
33 void SetInputTensor();
34
35 protected:
36 ModBuilder m_builder;
37 std::vector<uint32_t> m_inputs {0, 1};
38 std::vector<uint32_t> m_outputs {2};
39 std::vector<int32_t> m_dim {1, 2, 2, 1};
40 };
41
SetUp()42 void ModBuilderTest::SetUp() {}
43
TearDown()44 void ModBuilderTest::TearDown() {}
45
SetInputTensor()46 void ModBuilderTest::SetInputTensor()
47 {
48 m_inputsIndex = m_inputs;
49 std::shared_ptr<NNTensor> input1Tensor;
50 input1Tensor = TransToNNTensor(OH_NN_INT32, m_dim, nullptr, OH_NN_TENSOR);
51 m_allTensors.emplace_back(input1Tensor);
52
53 std::shared_ptr<NNTensor> input2Tensor;
54 input2Tensor = TransToNNTensor(OH_NN_INT32, m_dim, nullptr, OH_NN_TENSOR);
55 m_allTensors.emplace_back(input2Tensor);
56 }
57
58 /**
59 * @tc.name: mod_build_001
60 * @tc.desc: Verify that the build function returns a successful message.
61 * @tc.type: FUNC
62 */
63 HWTEST_F(ModBuilderTest, mod_build_001, TestSize.Level1)
64 {
65 SetInputTensor();
66 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
67
68 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
69 EXPECT_EQ(OH_NN_SUCCESS, ret);
70 }
71
72 /**
73 * @tc.name: mod_build_002
74 * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
75 * @tc.type: FUNC
76 */
77 HWTEST_F(ModBuilderTest, mod_build_002, TestSize.Level1)
78 {
79 SetInputTensor();
80 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
81
82 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
83 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
84 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
85 }
86
87 /**
88 * @tc.name: mod_build_003
89 * @tc.desc: Verify that the build function returns a failed message with invalided input.
90 * @tc.type: FUNC
91 */
92 HWTEST_F(ModBuilderTest, mod_build_003, TestSize.Level1)
93 {
94 m_inputs = {0, 1, 2};
95 m_outputs = {3};
96
97 SetInputTensor();
98 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
99
100 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
101 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
102 }
103
104 /**
105 * @tc.name: mod_build_004
106 * @tc.desc: Verify that the build function returns a failed message with invalided output.
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ModBuilderTest, mod_build_004, TestSize.Level1)
110 {
111 m_outputs = {2, 3};
112
113 SetInputTensor();
114 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
115
116 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
117 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
118 }
119
120 /**
121 * @tc.name: mod_build_005
122 * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(ModBuilderTest, mod_build_005, TestSize.Level1)
126 {
127 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputs, m_outputs, m_allTensors);
128 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
129 }
130
131 /**
132 * @tc.name: mod_build_006
133 * @tc.desc: Verify that the build function returns a failed message without output tensor.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(ModBuilderTest, mod_build_006, TestSize.Level1)
137 {
138 SetInputTensor();
139
140 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputs, m_allTensors);
141 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
142 }
143
144 /**
145 * @tc.name: mod_getprimitive_001
146 * @tc.desc: Verify that the getPrimitive function returns a successful message
147 * @tc.type: FUNC
148 */
149 HWTEST_F(ModBuilderTest, mod_getprimitive_001, TestSize.Level1)
150 {
151 SetInputTensor();
152 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
153
154 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
155 LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
156 LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
157 EXPECT_NE(expectPrimitive, primitive);
158 }
159
160 /**
161 * @tc.name: mod_getprimitive_002
162 * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
163 * @tc.type: FUNC
164 */
165 HWTEST_F(ModBuilderTest, mod_getprimitive_002, TestSize.Level1)
166 {
167 LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
168 LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
169 EXPECT_EQ(expectPrimitive, primitive);
170 }
171 }
172 }
173 }