• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/native/ops/slice_builder.h"
17 
18 #include <gtest/gtest.h>
19 #include "frameworks/native/nn_tensor.h"
20 #include "ops_test.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::NeuralNetworkRuntime::Ops;
25 
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace UnitTest {
29 class SliceBuilderTest : public OpsTest {
30 protected:
31     void InitTensor(const std::vector<uint32_t>& inputsIndex,
32         const std::vector<uint32_t>& outputsIndex) override;
33     void SaveAxesTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
34         const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35 
36 protected:
37     SliceBuilder m_builder;
38 };
39 
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)40 void SliceBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
41     const std::vector<uint32_t>& outputsIndex)
42 {
43     std::vector<uint32_t> paramsIndex = {};
44     std::vector<int32_t> inputDim = {3, 2, 3};
45     std::vector<int32_t> OutputDim = {1, 1, 3};
46 
47     m_paramsIndex = paramsIndex;
48     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
49     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
50 }
51 
SaveAxesTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)52 void SliceBuilderTest::SaveAxesTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
53     const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
54 {
55     std::shared_ptr<NNTensor> axesTensor = TransToNNTensor(dataType, dim, quantParam, type);
56     int64_t* axesValue = new (std::nothrow) int64_t[1]{0};
57     EXPECT_NE(nullptr, axesValue);
58     axesTensor->SetBuffer(axesValue, sizeof(int64_t));
59     m_allTensors.emplace_back(axesTensor);
60 }
61 
62 /**
63  * @tc.name: slice_build_001
64  * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
65  * @tc.type: FUNC
66  */
67 HWTEST_F(SliceBuilderTest, slice_build_001, TestSize.Level0)
68 {
69     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
70     std::vector<uint32_t> outputsIndex = { 3 };
71 
72     InitTensor(inputsIndex, outputsIndex);
73 
74     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
75     EXPECT_EQ(OH_NN_SUCCESS, ret);
76 }
77 
78 /**
79  * @tc.name: slice_build_002
80  * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
81  * @tc.type: FUNC
82  */
83 HWTEST_F(SliceBuilderTest, slice_build_002, TestSize.Level0)
84 {
85     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
86     std::vector<uint32_t> outputsIndex = { 3 };
87 
88     InitTensor(inputsIndex, outputsIndex);
89 
90     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
91     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
92     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
93 }
94 
95 /**
96  * @tc.name: slice_build_003
97  * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
98  * @tc.type: FUNC
99  */
100 HWTEST_F(SliceBuilderTest, slice_build_003, TestSize.Level0)
101 {
102     std::vector<uint32_t> inputsIndex = { 0, 1, 2, 3 };
103     std::vector<uint32_t> outputsIndex = { 4 };
104 
105     InitTensor(inputsIndex, outputsIndex);
106 
107     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
108     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
109 }
110 
111 /**
112  * @tc.name: slice_build_004
113  * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
114  * @tc.type: FUNC
115  */
116 HWTEST_F(SliceBuilderTest, slice_build_004, TestSize.Level0)
117 {
118     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
119     std::vector<uint32_t> outputsIndex = { 3, 4 };
120 
121     InitTensor(inputsIndex, outputsIndex);
122 
123     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
124     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
125 }
126 
127 /**
128  * @tc.name: slice_build_005
129  * @tc.desc: Provide empty input, output, and parameters to verify the abnormal behavior of the Build function
130  * @tc.type: FUNC
131  */
132 HWTEST_F(SliceBuilderTest, slice_build_005, TestSize.Level0)
133 {
134     std::vector<uint32_t> inputsIndex = {};
135     std::vector<uint32_t> outputsIndex = {};
136 
137     InitTensor(inputsIndex, outputsIndex);
138 
139     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
140     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
141 }
142 
143 /**
144  * @tc.name: slice_build_006
145  * @tc.desc: Provide empty output to verify the abnormal behavior of the Build function
146  * @tc.type: FUNC
147  */
148 HWTEST_F(SliceBuilderTest, slice_build_006, TestSize.Level0)
149 {
150     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
151     std::vector<uint32_t> outputsIndex = {};
152 
153     InitTensor(inputsIndex, outputsIndex);
154 
155     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
156     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
157 }
158 
159 /**
160  * @tc.name: slice_build_007
161  * @tc.desc: Provide a param to verify the abnormal behavior of the Build function
162  * @tc.type: FUNC
163  */
164 HWTEST_F(SliceBuilderTest, slice_build_007, TestSize.Level0)
165 {
166     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
167     std::vector<uint32_t> outputsIndex = {3};
168     std::vector<uint32_t> paramsIndex = { 4 };
169     std::vector<int32_t> inputDim = {3, 2, 3};
170     std::vector<int32_t> OutputDim = {1, 1, 3};
171     std::vector<int32_t> paramDim = {};
172 
173     m_paramsIndex = paramsIndex;
174     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
175     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
176     SaveAxesTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SPLIT_AXIS);
177 
178     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
179     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
180 }
181 
182 /**
183  * @tc.name: slice_getprimitive_001
184  * @tc.desc: Verify the GetPrimitive function return nullptr
185  * @tc.type: FUNC
186  */
187 HWTEST_F(SliceBuilderTest, slice_getprimitive_001, TestSize.Level0)
188 {
189     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
190     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
191     EXPECT_EQ(primitive, expectPrimitive);
192 }
193 
194 /**
195  * @tc.name: slice_getprimitive_002
196  * @tc.desc: Verify the normal params return behavior of the getprimitive function
197  * @tc.type: FUNC
198  */
199 HWTEST_F(SliceBuilderTest, slice_getprimitive_002, TestSize.Level0)
200 {
201     std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
202     std::vector<uint32_t> outputsIndex = { 3 };
203 
204     InitTensor(inputsIndex, outputsIndex);
205 
206     std::vector<int64_t> expectAxesValue = {0};
207     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
208     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
209     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
210     EXPECT_NE(primitive, expectPrimitive);
211 }
212 } // namespace UnitTest
213 } // namespace NeuralNetworkRuntime
214 } // namespace OHOS
215