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/expandims_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 ExpandDimsBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 public:
33     ExpandDimsBuilder m_builder;
34     std::vector<uint32_t> m_inputs {0, 1};
35     std::vector<uint32_t> m_outputs {2};
36     std::vector<uint32_t> m_params {};
37     std::vector<int32_t> m_input_dim {3, 3};
38     std::vector<int32_t> m_output_dim {3, 3};
39     std::vector<int32_t> m_param_dim {};
40 };
41 
SetUp()42 void ExpandDimsBuilderTest::SetUp() {}
43 
TearDown()44 void ExpandDimsBuilderTest::TearDown() {}
45 
46 /**
47  * @tc.name: expandims_build_001
48  * @tc.desc: Verify the success of the build function
49  * @tc.type: FUNC
50  */
51 HWTEST_F(ExpandDimsBuilderTest, expandims_build_001, TestSize.Level1)
52 {
53     m_paramsIndex = m_params;
54     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
55     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
56     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
57 }
58 
59 /**
60  * @tc.name: expandims_build_002
61  * @tc.desc: Verify the forbidden of the build function
62  * @tc.type: FUNC
63  */
64 HWTEST_F(ExpandDimsBuilderTest, expandims_build_002, TestSize.Level1)
65 {
66     m_paramsIndex = m_params;
67     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
68     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
69 
70     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
71     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
72 }
73 
74 /**
75  * @tc.name: expandims_build_003
76  * @tc.desc: Verify the misssing input of the build function
77  * @tc.type: FUNC
78  */
79 HWTEST_F(ExpandDimsBuilderTest, expandims_build_003, TestSize.Level1)
80 {
81     m_inputs = {0};
82     m_outputs = {1};
83     m_paramsIndex = m_params;
84 
85     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
86     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
87     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
88 }
89 
90 /**
91  * @tc.name: expandims_build_004
92  * @tc.desc: Verify the missing output of the build function
93  * @tc.type: FUNC
94  */
95 HWTEST_F(ExpandDimsBuilderTest, expandims_build_004, TestSize.Level1)
96 {
97     m_outputs = {};
98     m_paramsIndex = m_params;
99 
100     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
101     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
102     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
103 }
104 
105 /**
106  * @tc.name: expandims_build_005
107  * @tc.desc: Verify the inputIndex out of bounds of the build function
108  * @tc.type: FUNC
109  */
110 HWTEST_F(ExpandDimsBuilderTest, expandims_build_005, TestSize.Level1)
111 {
112     m_inputs = {0, 6};
113     m_paramsIndex = m_params;
114 
115     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
116     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
117     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
118 }
119 
120 /**
121  * @tc.name: expandims_build_006
122  * @tc.desc: Verify the outputIndex out of bounds of the build function
123  * @tc.type: FUNC
124  */
125 HWTEST_F(ExpandDimsBuilderTest, expandims_build_006, TestSize.Level1)
126 {
127     m_outputs = {6};
128     m_paramsIndex = m_params;
129 
130     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
131     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
132     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
133 }
134 
135 /**
136  * @tc.name: expandims_build_007
137  * @tc.desc: Verify the paramIndex not empth of the build function
138  * @tc.type: FUNC
139  */
140 
141 HWTEST_F(ExpandDimsBuilderTest, expandims_build_007, TestSize.Level1)
142 {
143     m_params = {1};
144     m_param_dim = {1};
145     m_paramsIndex = m_params;
146 
147     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
148     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
149     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
150 }
151 
152 /**
153  * @tc.name: expandims_getprimitive_001
154  * @tc.desc: Verify the success of the GetPrimitive function
155  * @tc.type: FUNC
156  */
157 HWTEST_F(ExpandDimsBuilderTest, expandims_getprimitive_001, TestSize.Level1)
158 {
159     m_paramsIndex = m_params;
160     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
161     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
162     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
163 
164     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
165     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
166     EXPECT_NE(expectPrimitive, primitive);
167 }
168 
169 /**
170  * @tc.name: expandims_getprimitive_002
171  * @tc.desc: Verify the nullptr return of the GetPrimitive function
172  * @tc.type: FUNC
173  */
174 HWTEST_F(ExpandDimsBuilderTest, expandims_getprimitive_002, TestSize.Level1)
175 {
176     m_paramsIndex = m_params;
177     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_input_dim, nullptr);
178     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
179 
180     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
181     LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
182     EXPECT_EQ(expectPrimitive, primitive);
183 }
184 } // namespace UnitTest
185 } // namespace NeuralNetworkRuntime
186 } // namespace OHOS