• 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 #include <cmath>
16 #include <cstdio>
17 #include <vector>
18 #include <thread>
19 #include <cstdlib>
20 
21 #include "nnrt_utils.h"
22 #include "model.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::NeuralNetworkRuntime;
26 using namespace OHOS::NeuralNetworkRuntime::Test;
27 using namespace OHOS::HDI::Nnrt::V1_0;
28 
29 namespace {
30 
31 class MemoryTest : public testing::Test {
32 protected:
33     AddModel addModel;
34     OHNNGraphArgs graphArgs = addModel.graphArgs;
35     OHNNCompileParam compileParam;
36 };
37 
CheckCreateInputMemory(OH_NNExecutor * executor,uint32_t inputIndex,size_t length)38 void CheckCreateInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, size_t length)
39 {
40     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, inputIndex, length);
41     ASSERT_NE(nullptr, OHNNMemory);
42 }
43 
CheckCreateOutputMemory(OH_NNExecutor * executor,uint32_t outputIndex,size_t length)44 void CheckCreateOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, size_t length)
45 {
46     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, outputIndex, length);
47     ASSERT_NE(nullptr, OHNNMemory);
48 }
49 
50 } // namespace
51 
52 /**
53  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0100
54  * @tc.name   : 创建输入共享内存,executor为nullptr
55  * @tc.desc   : [C- SOFTWARE -0200]
56  */
57 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0100, Function | MediumTest | Level3)
58 {
59     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(nullptr, 0, 4);
60     ASSERT_EQ(nullptr, OHNNMemory);
61 }
62 
63 /**
64  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0200
65  * @tc.name   : 创建输入共享内存,inputIndex不存在
66  * @tc.desc   : [C- SOFTWARE -0200]
67  */
68 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0200, Function | MediumTest | Level3)
69 {
70     OH_NNModel *model = OH_NNModel_Construct();
71     ASSERT_NE(nullptr, model);
72     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
73     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
74     ASSERT_NE(nullptr, compilation);
75     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
76     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
77     ASSERT_NE(nullptr, executor);
78     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 2, graphArgs.operands[0].length);
79     ASSERT_EQ(nullptr, OHNNMemory);
80     Free(model, compilation, executor);
81 }
82 
83 /**
84  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0300
85  * @tc.name   : 创建输入共享内存,length为0
86  * @tc.desc   : [C- SOFTWARE -0200]
87  */
88 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0300, Function | MediumTest | Level3)
89 {
90     OH_NNModel *model = OH_NNModel_Construct();
91     ASSERT_NE(nullptr, model);
92     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
93     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
94     ASSERT_NE(nullptr, compilation);
95     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
96     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
97     ASSERT_NE(nullptr, executor);
98     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, 0);
99     ASSERT_EQ(nullptr, OHNNMemory);
100     Free(model, compilation, executor);
101 }
102 
103 /**
104  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0400
105  * @tc.name   :创建输入共享内存,length为最大限制2G
106  * @tc.desc   : [C- SOFTWARE -0200]
107  */
108 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0400, Function | MediumTest | Level3)
109 {
110     OH_NNModel *model = OH_NNModel_Construct();
111     ASSERT_NE(nullptr, model);
112     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
113     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
114     ASSERT_NE(nullptr, compilation);
115     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
116     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
117     ASSERT_NE(nullptr, executor);
118     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, 1024 * 1024 * 1024 + 1);
119     ASSERT_EQ(nullptr, OHNNMemory);
120     Free(model, compilation, executor);
121 }
122 
123 /**
124  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0500
125  * @tc.name   : 创建输入共享内存,inputIndex重复创建
126  * @tc.desc   : [C- SOFTWARE -0200]
127  */
128 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0500, Function | MediumTest | Level2)
129 {
130     OH_NNModel *model = OH_NNModel_Construct();
131     ASSERT_NE(nullptr, model);
132     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
133     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
134     ASSERT_NE(nullptr, compilation);
135     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
136     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
137     ASSERT_NE(nullptr, executor);
138     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
139     ASSERT_NE(nullptr, OHNNMemory);
140     OH_NN_Memory *OHNNMemory2 = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
141     ASSERT_NE(nullptr, OHNNMemory2);
142     Free(model, compilation, executor);
143 }
144 
145 /**
146  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0600
147  * @tc.name   : 多线程创建不同index输入的共享内存
148  * @tc.desc   : [C- SOFTWARE -0200]
149  */
150 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateInputMemory_0600, Function | MediumTest | Level2)
151 {
152     OH_NNModel *model = OH_NNModel_Construct();
153     ASSERT_NE(nullptr, model);
154     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
155     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
156     ASSERT_NE(nullptr, compilation);
157     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
158     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
159     ASSERT_NE(nullptr, executor);
160     std::thread th1(CheckCreateInputMemory, executor, 0, graphArgs.operands[0].length);
161     std::thread th2(CheckCreateInputMemory, executor, 1, graphArgs.operands[1].length);
162     th1.join();
163     th2.join();
164     Free(model, compilation, executor);
165 }
166 
167 /**
168  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0100
169  * @tc.name   : 创建输出共享内存,executor为nullptr
170  * @tc.desc   : [C- SOFTWARE -0200]
171  */
172 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0100, Function | MediumTest | Level3)
173 {
174     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(nullptr, 0, 4);
175     ASSERT_EQ(nullptr, OHNNMemory);
176 }
177 
178 /**
179  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0200
180  * @tc.name   : 创建输出共享内存,inputIndex不存在
181  * @tc.desc   : [C- SOFTWARE -0200]
182  */
183 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0200, Function | MediumTest | Level3)
184 {
185     OH_NNModel *model = OH_NNModel_Construct();
186     ASSERT_NE(nullptr, model);
187     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
188     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
189     ASSERT_NE(nullptr, compilation);
190     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
191     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
192     ASSERT_NE(nullptr, executor);
193     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 2, graphArgs.operands[0].length);
194     ASSERT_EQ(nullptr, OHNNMemory);
195     Free(model, compilation, executor);
196 }
197 
198 /**
199  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0300
200  * @tc.name   : 创建输出共享内存,length为0
201  * @tc.desc   : [C- SOFTWARE -0200]
202  */
203 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0300, Function | MediumTest | Level3)
204 {
205     OH_NNModel *model = OH_NNModel_Construct();
206     ASSERT_NE(nullptr, model);
207     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
208     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
209     ASSERT_NE(nullptr, compilation);
210     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
211     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
212     ASSERT_NE(nullptr, executor);
213     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, 0);
214     ASSERT_EQ(nullptr, OHNNMemory);
215     Free(model, compilation, executor);
216 }
217 
218 /**
219  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0400
220  * @tc.name   :创建输出共享内存,length为最大限制2G
221  * @tc.desc   : [C- SOFTWARE -0200]
222  */
223 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0400, Function | MediumTest | Level3)
224 {
225     OH_NNModel *model = OH_NNModel_Construct();
226     ASSERT_NE(nullptr, model);
227     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
228     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
229     ASSERT_NE(nullptr, compilation);
230     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
231     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
232     ASSERT_NE(nullptr, executor);
233     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, 1024 * 1024 * 1024 + 1);
234     ASSERT_EQ(nullptr, OHNNMemory);
235     Free(model, compilation, executor);
236 }
237 
238 /**
239  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0500
240  * @tc.name   : 创建输出共享内存,outputIndex重复创建
241  * @tc.desc   : [C- SOFTWARE -0200]
242  */
243 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0500, Function | MediumTest | Level2)
244 {
245     OH_NNModel *model = OH_NNModel_Construct();
246     ASSERT_NE(nullptr, model);
247     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
248     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
249     ASSERT_NE(nullptr, compilation);
250     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
251     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
252     ASSERT_NE(nullptr, executor);
253     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
254     ASSERT_NE(nullptr, OHNNMemory);
255     OH_NN_Memory *OHNNMemory2 = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
256     ASSERT_NE(nullptr, OHNNMemory2);
257     Free(model, compilation, executor);
258 }
259 
260 /**
261  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0600
262  * @tc.name   : 多线程创建不同index输出的共享内存
263  * @tc.desc   : [C- SOFTWARE -0200]
264  */
265 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_CreateOutputMemory_0600, Function | MediumTest | Level2)
266 {
267     OH_NNModel *model = OH_NNModel_Construct();
268     ASSERT_NE(nullptr, model);
269     TopKModel topKModel;
270     graphArgs = topKModel.graphArgs;
271     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
272 
273     OHNNCompileParam compileParam;
274     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
275     ASSERT_NE(nullptr, compilation);
276     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
277     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
278     ASSERT_NE(nullptr, executor);
279     std::thread th1(CheckCreateOutputMemory, executor, 0, graphArgs.operands[3].length);
280     std::thread th2(CheckCreateOutputMemory, executor, 1, graphArgs.operands[4].length);
281     th1.join();
282     th2.join();
283     Free(model, compilation, executor);
284 }
285 /**
286  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0100
287  * @tc.name   : 销毁输入共享内存,executor为nullptr
288  * @tc.desc   : [C- SOFTWARE -0200]
289  */
290 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0100, Function | MediumTest | Level3)
291 {
292     OH_NNModel *model = OH_NNModel_Construct();
293     ASSERT_NE(nullptr, model);
294     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
295     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
296     ASSERT_NE(nullptr, compilation);
297     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
298     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
299     ASSERT_NE(nullptr, executor);
300     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
301     ASSERT_NE(nullptr, OHNNMemory);
302     OH_NNExecutor_DestroyInputMemory(nullptr, 0, &OHNNMemory);
303     ASSERT_NE(nullptr, OHNNMemory);
304     Free(model, compilation, executor);
305 }
306 
307 /**
308  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0200
309  * @tc.name   : 销毁输入共享内存,inputIndex不存在
310  * @tc.desc   : [C- SOFTWARE -0200]
311  */
312 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0200, Function | MediumTest | Level3)
313 {
314     OH_NNModel *model = OH_NNModel_Construct();
315     ASSERT_NE(nullptr, model);
316     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
317     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
318     ASSERT_NE(nullptr, compilation);
319     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
320     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
321     ASSERT_NE(nullptr, executor);
322     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
323     ASSERT_NE(nullptr, OHNNMemory);
324     OH_NNExecutor_DestroyInputMemory(executor, 1, &OHNNMemory);
325     ASSERT_NE(nullptr, OHNNMemory);
326     Free(model, compilation, executor);
327 }
328 
329 /**
330  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0300
331  * @tc.name   : 销毁输出共享内存,*memory为nullptr
332  * @tc.desc   : [C- SOFTWARE -0200]
333  */
334 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0300, Function | MediumTest | Level3)
335 {
336     OH_NNModel *model = OH_NNModel_Construct();
337     ASSERT_NE(nullptr, model);
338     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
339     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
340     ASSERT_NE(nullptr, compilation);
341     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
342     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
343     ASSERT_NE(nullptr, executor);
344     OH_NN_Memory *OHNNMemory = nullptr;
345     ASSERT_NO_THROW(OH_NNExecutor_DestroyInputMemory(executor, 0, &OHNNMemory));
346 }
347 
348 /**
349  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0400
350  * @tc.name   : 销毁输出共享内存,inputIndex不同memory重复销毁
351  * @tc.desc   : [C- SOFTWARE -0200]
352  */
353 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0400, Function | MediumTest | Level3)
354 {
355     OH_NNModel *model = OH_NNModel_Construct();
356     ASSERT_NE(nullptr, model);
357     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
358     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
359     ASSERT_NE(nullptr, compilation);
360     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
361     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
362     ASSERT_NE(nullptr, executor);
363     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
364     ASSERT_NE(nullptr, OHNNMemory);
365     OH_NN_Memory *OHNNMemory2 = OH_NNExecutor_AllocateInputMemory(executor, 1, graphArgs.operands[1].length);
366     ASSERT_NE(nullptr, OHNNMemory2);
367     OH_NNExecutor_DestroyInputMemory(executor, 0, &OHNNMemory);
368     ASSERT_EQ(nullptr, OHNNMemory);
369     OH_NNExecutor_DestroyInputMemory(executor, 1, &OHNNMemory2);
370     ASSERT_EQ(nullptr, OHNNMemory2);
371     Free(model, compilation, executor);
372 }
373 
374 /**
375  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0500
376  * @tc.name   : 多线销毁不同index输入的共享内存
377  * @tc.desc   : [C- SOFTWARE -0200]
378  */
379 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyInputMemory_0500, Function | MediumTest | Level3)
380 {
381     OH_NNModel *model = OH_NNModel_Construct();
382     ASSERT_NE(nullptr, model);
383     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
384     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
385     ASSERT_NE(nullptr, compilation);
386     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
387     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
388     ASSERT_NE(nullptr, executor);
389     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
390     ASSERT_NE(nullptr, OHNNMemory);
391     OH_NN_Memory *OHNNMemory2 = OH_NNExecutor_AllocateInputMemory(executor, 1, graphArgs.operands[1].length);
392     ASSERT_NE(nullptr, OHNNMemory2);
393     std::thread th1(OH_NNExecutor_DestroyInputMemory, executor, 0, &OHNNMemory);
394     std::thread th2(OH_NNExecutor_DestroyInputMemory, executor, 1, &OHNNMemory2);
395     th1.join();
396     th2.join();
397     ASSERT_EQ(nullptr, OHNNMemory);
398     ASSERT_EQ(nullptr, OHNNMemory2);
399     Free(model, compilation, executor);
400 }
401 
402 /**
403  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0100
404  * @tc.name   : 销毁输出共享内存,executor为nullptr
405  * @tc.desc   : [C- SOFTWARE -0200]
406  */
407 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0100, Function | MediumTest | Level3)
408 {
409     OH_NNModel *model = OH_NNModel_Construct();
410     ASSERT_NE(nullptr, model);
411     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
412     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
413     ASSERT_NE(nullptr, compilation);
414     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
415     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
416     ASSERT_NE(nullptr, executor);
417     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
418     ASSERT_NE(nullptr, OHNNMemory);
419     OH_NNExecutor_DestroyOutputMemory(nullptr, 0, &OHNNMemory);
420     ASSERT_NE(nullptr, OHNNMemory);
421     Free(model, compilation, executor);
422 }
423 
424 /**
425  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0200
426  * @tc.name   : 销毁输出共享内存,inputIndex不存在
427  * @tc.desc   : [C- SOFTWARE -0200]
428  */
429 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0200, Function | MediumTest | Level3)
430 {
431     OH_NNModel *model = OH_NNModel_Construct();
432     ASSERT_NE(nullptr, model);
433     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
434     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
435     ASSERT_NE(nullptr, compilation);
436     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
437     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
438     ASSERT_NE(nullptr, executor);
439     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
440     ASSERT_NE(nullptr, OHNNMemory);
441     OH_NNExecutor_DestroyOutputMemory(executor, 1, &OHNNMemory);
442     ASSERT_NE(nullptr, OHNNMemory);
443     Free(model, compilation, executor);
444 }
445 
446 /**
447  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0300
448  * @tc.name   : 销毁输出共享内存,*memory为nullptr
449  * @tc.desc   : [C- SOFTWARE -0200]
450  */
451 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0300, Function | MediumTest | Level3)
452 {
453     OH_NNModel *model = OH_NNModel_Construct();
454     ASSERT_NE(nullptr, model);
455     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
456     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
457     ASSERT_NE(nullptr, compilation);
458     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
459     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
460     ASSERT_NE(nullptr, executor);
461     ASSERT_NO_THROW(OH_NNExecutor_DestroyOutputMemory(executor, 0, nullptr));
462 }
463 
464 /**
465  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0400
466  * @tc.name   : 销毁输出共享内存,inputIndex不同memory重复销毁
467  * @tc.desc   : [C- SOFTWARE -0200]
468  */
469 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0400, Function | MediumTest | Level3)
470 {
471     OH_NNModel *model = OH_NNModel_Construct();
472     ASSERT_NE(nullptr, model);
473     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
474     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
475     ASSERT_NE(nullptr, compilation);
476     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
477     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
478     ASSERT_NE(nullptr, executor);
479     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
480     ASSERT_NE(nullptr, OHNNMemory);
481     OH_NN_Memory *OHNNMemory2 = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
482     ASSERT_NE(nullptr, OHNNMemory2);
483     OH_NNExecutor_DestroyOutputMemory(executor, 0, &OHNNMemory);
484     ASSERT_EQ(nullptr, OHNNMemory);
485     OH_NNExecutor_DestroyOutputMemory(executor, 0, &OHNNMemory2);
486     ASSERT_EQ(nullptr, OHNNMemory2);
487     Free(model, compilation, executor);
488 }
489 
490 /**
491  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0500
492  * @tc.name   : 多线销毁不同index输出的共享内存
493  * @tc.desc   : [C- SOFTWARE -0200]
494  */
495 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_DestroyOutputMemory_0500, Function | MediumTest | Level3)
496 {
497     OH_NNModel *model = OH_NNModel_Construct();
498     ASSERT_NE(nullptr, model);
499     TopKModel topKModel;
500     graphArgs = topKModel.graphArgs;
501     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
502     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
503     ASSERT_NE(nullptr, compilation);
504     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
505     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
506     ASSERT_NE(nullptr, executor);
507     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
508     ASSERT_NE(nullptr, OHNNMemory);
509     OH_NN_Memory *OHNNMemory2 = OH_NNExecutor_AllocateOutputMemory(executor, 1, graphArgs.operands[1].length);
510     ASSERT_NE(nullptr, OHNNMemory2);
511     std::thread th1(OH_NNExecutor_DestroyOutputMemory, executor, 0, &OHNNMemory);
512     std::thread th2(OH_NNExecutor_DestroyOutputMemory, executor, 1, &OHNNMemory2);
513     th1.join();
514     th2.join();
515     ASSERT_EQ(nullptr, OHNNMemory);
516     ASSERT_EQ(nullptr, OHNNMemory2);
517     Free(model, compilation, executor);
518 }
519 
520 /**
521  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0100
522  * @tc.name   : 设置输入共享内存,executor为nullptr
523  * @tc.desc   : [C- SOFTWARE -0200]
524  */
525 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0100, Function | MediumTest | Level3)
526 {
527     OH_NNModel *model = OH_NNModel_Construct();
528     ASSERT_NE(nullptr, model);
529     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
530     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
531     ASSERT_NE(nullptr, compilation);
532     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
533     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
534     ASSERT_NE(nullptr, executor);
535     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
536     ASSERT_NE(nullptr, OHNNMemory);
537     const OHNNOperandTest &operandTem = graphArgs.operands[0];
538     auto quantParam = operandTem.quantParam;
539     OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
540                              quantParam, operandTem.type};
541     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetInputWithMemory(nullptr, 0, &operand, OHNNMemory));
542     Free(model, compilation, executor);
543 }
544 
545 /**
546  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0200
547  * @tc.name   : 设置输入共享内存,inputIndex不存在
548  * @tc.desc   : [C- SOFTWARE -0200]
549  */
550 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0200, Function | MediumTest | Level3)
551 {
552     OH_NNModel *model = OH_NNModel_Construct();
553     ASSERT_NE(nullptr, model);
554     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
555     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
556     ASSERT_NE(nullptr, compilation);
557     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
558     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
559     ASSERT_NE(nullptr, executor);
560     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
561     ASSERT_NE(nullptr, OHNNMemory);
562     const OHNNOperandTest &operandTem = graphArgs.operands[0];
563     auto quantParam = operandTem.quantParam;
564     OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
565                              quantParam, operandTem.type};
566     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetInputWithMemory(executor, 2, &operand, OHNNMemory));
567     Free(model, compilation, executor);
568 }
569 
570 /**
571  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0300
572  * @tc.name   : 设置输入共享内存,operand为nullptr
573  * @tc.desc   : [C- SOFTWARE -0200]
574  */
575 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0300, Function | MediumTest | Level3)
576 {
577     OH_NNModel *model = OH_NNModel_Construct();
578     ASSERT_NE(nullptr, model);
579     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
580     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
581     ASSERT_NE(nullptr, compilation);
582     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
583     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
584     ASSERT_NE(nullptr, executor);
585     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
586     ASSERT_NE(nullptr, OHNNMemory);
587     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetInputWithMemory(executor, 0, nullptr, OHNNMemory));
588     Free(model, compilation, executor);
589 }
590 
591 /**
592  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0400
593  * @tc.name   : 设置输入共享内存,operand与输入不匹配
594  * @tc.desc   : [C- SOFTWARE -0200]
595  */
596 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0400, Function | MediumTest | Level2)
597 {
598     OH_NNModel *model = OH_NNModel_Construct();
599     ASSERT_NE(nullptr, model);
600     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
601     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
602     ASSERT_NE(nullptr, compilation);
603     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
604     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
605     ASSERT_NE(nullptr, executor);
606     OH_NN_Memory *OHNNMemory1 = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
607     ASSERT_NE(nullptr, OHNNMemory1);
608     const OHNNOperandTest &operandTem = graphArgs.operands[2];
609     auto quantParam = operandTem.quantParam;
610     OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
611                              quantParam, operandTem.type};
612     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetInputWithMemory(executor, 0, &operand, OHNNMemory1));
613     Free(model, compilation, executor);
614 }
615 
616 /**
617  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0500
618  * @tc.name   : 设置输入共享内存,memory为nullptr
619  * @tc.desc   : [C- SOFTWARE -0200]
620  */
621 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0500, Function | MediumTest | Level3)
622 {
623     OH_NNModel *model = OH_NNModel_Construct();
624     ASSERT_NE(nullptr, model);
625     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
626     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
627     ASSERT_NE(nullptr, compilation);
628     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
629     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
630     ASSERT_NE(nullptr, executor);
631     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
632     ASSERT_NE(nullptr, OHNNMemory);
633     const OHNNOperandTest &operandTem = graphArgs.operands[0];
634     auto quantParam = operandTem.quantParam;
635     OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
636                              quantParam, operandTem.type};
637     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetInputWithMemory(executor, 0, &operand, nullptr));
638     Free(model, compilation, executor);
639 }
640 
641 /**
642  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0600
643  * @tc.name   : 设置输入共享内存,重复设置相同inputIndex
644  * @tc.desc   : [C- SOFTWARE -0200]
645  */
646 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetInputFromMemory_0600, Function | MediumTest | Level2)
647 {
648     OH_NNModel *model = OH_NNModel_Construct();
649     ASSERT_NE(nullptr, model);
650     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
651     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
652     ASSERT_NE(nullptr, compilation);
653     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
654     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
655     ASSERT_NE(nullptr, executor);
656     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateInputMemory(executor, 0, graphArgs.operands[0].length);
657     ASSERT_NE(nullptr, OHNNMemory);
658     const OHNNOperandTest &operandTem = graphArgs.operands[0];
659     auto quantParam = operandTem.quantParam;
660     OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
661                              quantParam, operandTem.type};
662     ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetInputWithMemory(executor, 0, &operand, OHNNMemory));
663     ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetInputWithMemory(executor, 0, &operand, OHNNMemory));
664     Free(model, compilation, executor);
665 }
666 
667 /**
668  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0100
669  * @tc.name   : 设置输出共享内存,executor为nullptr
670  * @tc.desc   : [C- SOFTWARE -0200]
671  */
672 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0100, Function | MediumTest | Level3)
673 {
674     OH_NNModel *model = OH_NNModel_Construct();
675     ASSERT_NE(nullptr, model);
676     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
677     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
678     ASSERT_NE(nullptr, compilation);
679     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
680     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
681     ASSERT_NE(nullptr, executor);
682     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
683     ASSERT_NE(nullptr, OHNNMemory);
684     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetOutputWithMemory(nullptr, 0, OHNNMemory));
685     Free(model, compilation, executor);
686 }
687 
688 /**
689  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0200
690  * @tc.name   : 设置输出共享内存,outputIndex不存在
691  * @tc.desc   : [C- SOFTWARE -0200]
692  */
693 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0200, Function | MediumTest | Level3)
694 {
695     OH_NNModel *model = OH_NNModel_Construct();
696     ASSERT_NE(nullptr, model);
697     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
698     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
699     ASSERT_NE(nullptr, compilation);
700     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
701     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
702     ASSERT_NE(nullptr, executor);
703     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
704     ASSERT_NE(nullptr, OHNNMemory);
705     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetOutputWithMemory(executor, 1, OHNNMemory));
706     Free(model, compilation, executor);
707 }
708 
709 /**
710  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0300
711  * @tc.name   : 设置输出共享内存,memory为nullptr
712  * @tc.desc   : [C- SOFTWARE -0200]
713  */
714 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0300, Function | MediumTest | Level3)
715 {
716     OH_NNModel *model = OH_NNModel_Construct();
717     ASSERT_NE(nullptr, model);
718     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
719     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
720     ASSERT_NE(nullptr, compilation);
721     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
722     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
723     ASSERT_NE(nullptr, executor);
724     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
725     ASSERT_NE(nullptr, OHNNMemory);
726     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetOutputWithMemory(executor, 0, nullptr));
727     Free(model, compilation, executor);
728 }
729 
730 /**
731  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0400
732  * @tc.name   : 设置输出共享内存,重复设置相同outputIndex
733  * @tc.desc   : [C- SOFTWARE -0200]
734  */
735 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_SetOutputFromMemory_0400, Function | MediumTest | Level2)
736 {
737     OH_NNModel *model = OH_NNModel_Construct();
738     ASSERT_NE(nullptr, model);
739     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
740     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
741     ASSERT_NE(nullptr, compilation);
742     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
743     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
744     ASSERT_NE(nullptr, executor);
745     OH_NN_Memory *OHNNMemory = OH_NNExecutor_AllocateOutputMemory(executor, 0, graphArgs.operands[0].length);
746     ASSERT_NE(nullptr, OHNNMemory);
747     ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetOutputWithMemory(executor, 0, OHNNMemory));
748     ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetOutputWithMemory(executor, 0, OHNNMemory));
749     Free(model, compilation, executor);
750 }
751 
752 /**
753  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_Run_0100
754  * @tc.name   : 共享内存模型推理,executor设置输入个数不足
755  * @tc.desc   : [C- SOFTWARE -0200]
756  */
757 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_Run_0100, Function | MediumTest | Level3)
758 {
759     OH_NNModel *model = OH_NNModel_Construct();
760     ASSERT_NE(nullptr, model);
761     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
762     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
763     ASSERT_NE(nullptr, compilation);
764     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
765     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
766     ASSERT_NE(nullptr, executor);
767     uint32_t inputIndex = 0;
768     uint32_t outputIndex = 0;
769     for (auto i = 0; i < graphArgs.operands.size(); i++) {
770         const OHNNOperandTest &operandTem = graphArgs.operands[i];
771         auto quantParam = operandTem.quantParam;
772         OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
773                                  quantParam, operandTem.type};
774         if (i == 0) {
775             OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, inputIndex, operandTem.length);
776             ASSERT_NE(nullptr, inputMemory);
777             ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetInputWithMemory(executor, inputIndex, &operand, inputMemory));
778 
779             ASSERT_EQ(EOK, memcpy_s(inputMemory->data, operandTem.length, (void *)operandTem.data, operandTem.length));
780 
781         } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
782                    graphArgs.outputIndices.end()) {
783             OH_NN_Memory *outputMemory = OH_NNExecutor_AllocateOutputMemory(executor, outputIndex, operandTem.length);
784             ASSERT_NE(nullptr, outputMemory);
785             ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetOutputWithMemory(executor, outputIndex, outputMemory));
786             outputIndex += 1;
787         }
788     }
789     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_Run(executor));
790 }
791 
792 /**
793  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_Run_0200
794  * @tc.name   : 共享内存模型推理,executor设置输出个数不足
795  * @tc.desc   : [C- SOFTWARE -0200]
796  */
797 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_Run_0200, Function | MediumTest | Level3)
798 {
799     OH_NNModel *model = OH_NNModel_Construct();
800     ASSERT_NE(nullptr, model);
801     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
802     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
803     ASSERT_NE(nullptr, compilation);
804     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
805     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
806     ASSERT_NE(nullptr, executor);
807     uint32_t inputIndex = 0;
808     for (auto i = 0; i < graphArgs.operands.size(); i++) {
809         const OHNNOperandTest &operandTem = graphArgs.operands[i];
810         auto quantParam = operandTem.quantParam;
811         OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
812                                  quantParam, operandTem.type};
813         if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
814             graphArgs.inputIndices.end()) {
815             OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, inputIndex, operandTem.length);
816             ASSERT_NE(nullptr, inputMemory);
817             ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetInputWithMemory(executor, inputIndex, &operand, inputMemory));
818             ASSERT_EQ(EOK, memcpy_s(inputMemory->data, operandTem.length, (void *)operandTem.data, operandTem.length));
819         }
820     }
821     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_Run(executor));
822 }
823 
824 /**
825  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_Run_0300
826  * @tc.name   : 共享内存,定长模型推理测试
827  * @tc.desc   : [C- SOFTWARE -0200]
828  */
829 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_Run_0300, Function | MediumTest | Level1)
830 {
831     OH_NNModel *model = OH_NNModel_Construct();
832     ASSERT_NE(nullptr, model);
833     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
834     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
835     ASSERT_NE(nullptr, compilation);
836     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
837     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
838     ASSERT_NE(nullptr, executor);
839     size_t ioSize = graphArgs.inputIndices.size() + graphArgs.outputIndices.size();
840     OH_NN_Memory *OHNNMemory[ioSize];
841     ASSERT_EQ(OH_NN_SUCCESS, ExecutorWithMemory(executor, graphArgs, OHNNMemory, addModel.expectValue));
842     for (auto i = 0; i < graphArgs.inputIndices.size(); i++) {
843         OH_NNExecutor_DestroyInputMemory(executor, i, &OHNNMemory[i]);
844         ASSERT_EQ(OHNNMemory[i], nullptr);
845     }
846     for (auto j = 0; j < graphArgs.outputIndices.size(); j++) {
847         auto outputIndex = graphArgs.inputIndices.size() + j;
848         // check memory output
849         EXPECT_TRUE(CheckOutput(static_cast<float*>(const_cast<void*>(OHNNMemory[outputIndex]->data)),
850         (float*) addModel.expectValue));
851         OH_NNExecutor_DestroyOutputMemory(executor, j, &OHNNMemory[outputIndex]);
852         ASSERT_EQ(OHNNMemory[outputIndex], nullptr);
853     }
854     Free(model, compilation, executor);
855 }
856 
857 /**
858  * @tc.number : SUB_AI_NNRt_Func_North_Executor_Memory_Run_0400
859  * @tc.name   : 共享内存,变长模型推理测试
860  * @tc.desc   : [C- SOFTWARE -0200]
861  */
862 HWTEST_F(MemoryTest, SUB_AI_NNRt_Func_North_Executor_Memory_Run_0400, Function | MediumTest | Level1)
863 {
864     OH_NNModel *model = OH_NNModel_Construct();
865     ASSERT_NE(nullptr, model);
866     AvgPoolDynamicModel avgModel;
867     graphArgs = avgModel.graphArgs;
868     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
869 
870     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
871     ASSERT_NE(nullptr, compilation);
872 
873     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
874 
875     OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
876     ASSERT_NE(nullptr, executor);
877     avgModel.dynamicInput.shape = {1, 3, 3, 1};
878     avgModel.output.shape = {1, 2, 2, 1};
879     graphArgs.operands = {avgModel.dynamicInput, avgModel.kernel,     avgModel.strides,
880                           avgModel.padMode,      avgModel.activation, avgModel.output};
881     size_t ioSize = graphArgs.inputIndices.size() + graphArgs.outputIndices.size();
882     OH_NN_Memory *OHNNMemory[ioSize];
883 
884     ASSERT_EQ(OH_NN_SUCCESS, ExecutorWithMemory(executor, graphArgs, OHNNMemory, avgModel.expectValue));
885 
886     for (auto i = 0; i < graphArgs.inputIndices.size(); i++) {
887         OH_NNExecutor_DestroyInputMemory(executor, i, &OHNNMemory[i]);
888         ASSERT_EQ(OHNNMemory[i], nullptr);
889     }
890     for (auto j = 0; j < graphArgs.outputIndices.size(); j++) {
891         auto outputIndex = graphArgs.inputIndices.size() + j;
892         // check memory output
893         EXPECT_TRUE(CheckOutput(static_cast<float*>(const_cast<void*>(OHNNMemory[outputIndex]->data)),
894         (float*) avgModel.expectValue));
895         OH_NNExecutor_DestroyOutputMemory(executor, j, &OHNNMemory[outputIndex]);
896         ASSERT_EQ(OHNNMemory[outputIndex], nullptr);
897     }
898     Free(model, compilation, executor);
899 }