• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "gtest/gtest.h"
17 #include <cstring>
18 #include <unistd.h>
19 
20 #include "client_executor/include/i_aie_client.inl"
21 #include "protocol/struct_definition/aie_info_define.h"
22 #include "utils/aie_client_callback.h"
23 #include "utils/aie_client_common.h"
24 #include "utils/aie_client_const.h"
25 #include "utils/log/aie_log.h"
26 #include "utils/service_dead_cb.h"
27 #include "utils/utils.h"
28 
29 using namespace ::testing;
30 using namespace testing::ext;
31 using namespace OHOS::AI;
32 
33 class AieClientDestroyFunctionTest : public testing::Test {};
34 
35 /**
36  * Tests AieClientDestroy. Invoke sequence: AieClientInit-AieClientPrepare-AieClientSyncProcess/AieClientAsyncProcess
37  * -AieClientRelease-AieClientDestroy.
38  *
39  * isAsync  The value of the input parameter AlgorithmInfo.isAsync of AieClientInit.
40  */
TestAieClientDestroy(bool isAsync)41 static void TestAieClientDestroy(bool isAsync)
42 {
43     // Step 0: Defines variables.
44     ConfigInfo configInfo;
45     GetConfigInfo(configInfo);
46 
47     ClientInfo clientInfo;
48     GetClientInfo(clientInfo);
49 
50     AlgorithmInfo algorithmInfo;
51     int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
52     GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
53 
54     ServiceDeadCb *cb = nullptr;
55     AIE_NEW(cb, ServiceDeadCb());
56 
57     // Step 1: Invokes AieClientInit.
58     int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
59     EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
60     EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
61     EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
62                                               << ") is incorrect!";
63 
64     // Step 2: Invokes AieClientPrepare.
65     bool isPrepareInputInfoNull = GetRandomBool();
66     DataInfo prepareInputInfo = GetDataInfo(isPrepareInputInfoNull, INPUT_INFO_PREPARE);
67     DataInfo prepareOutputInfo = GetDataInfo();
68 
69     IClientCb *callback = nullptr;
70     if (isAsync) {
71         AIE_NEW(callback, ClientCallback());
72     }
73     int prepareReturnCode = AieClientPrepare(clientInfo, algorithmInfo, prepareInputInfo, prepareOutputInfo, callback);
74     EXPECT_EQ(RETCODE_SUCCESS, prepareReturnCode) << "AieClientPrepare is failed!";
75     EXPECT_EQ(true, prepareOutputInfo.data != nullptr) << "Prepare outputInfo is incorrect!";
76 
77     // Step 3: Invokes AieClientSyncProcess/AieClientAsyncProcess.
78     bool isProcessInputInfoNull = GetRandomBool();
79     if (isAsync) {
80         DataInfo asyncProcessInputInfo = GetDataInfo(isProcessInputInfoNull, INPUT_INFO_ASYNC_PROCESS);
81         int asyncProcessReturnCode = AieClientAsyncProcess(clientInfo, algorithmInfo, asyncProcessInputInfo);
82         EXPECT_EQ(RETCODE_SUCCESS, asyncProcessReturnCode) << "AieClientAsyncProcess is failed!";
83     } else {
84         DataInfo syncProcessInputInfo = GetDataInfo(isProcessInputInfoNull, INPUT_INFO_SYNC_PROCESS);
85         DataInfo syncProcessOutputInfo = GetDataInfo();
86         int syncProcessReturnCode = AieClientSyncProcess(clientInfo, algorithmInfo, syncProcessInputInfo,
87                                                          syncProcessOutputInfo);
88         EXPECT_EQ(RETCODE_SUCCESS, syncProcessReturnCode) << "AieClientSyncProcess is failed!";
89         EXPECT_EQ(true, syncProcessOutputInfo.data != nullptr) << "AieClientSyncProcess outputInfo is incorrect!";
90     }
91 
92     // Step 4: Invokes AieClientRelease.
93     bool isReleaseInputInfoNull = GetRandomBool();
94     DataInfo releaseInputInfo = GetDataInfo(isReleaseInputInfoNull, INPUT_INFO_RELEASE);
95     int releaseReturnCode = AieClientRelease(clientInfo, algorithmInfo, releaseInputInfo);
96     EXPECT_EQ(RETCODE_SUCCESS, releaseReturnCode) << "AieClientRelease is failed!";
97 
98     // Step 5: Invokes AieClientDestroy.
99     int destroyReturnCode = AieClientDestroy(clientInfo);
100     EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
101 
102     // Step 6: Deletes callback.
103     AIE_DELETE(cb);
104     if (isAsync) {
105         AIE_DELETE(callback);
106     }
107 }
108 
109 /**
110  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientDestroy_0100
111  * @tc.name   : 01. 初始化之后,调用AieClientDestroy返回成功
112  * @tc.desc   : [C- SOFTWARE -0200]
113  */
114 HWTEST_F(AieClientDestroyFunctionTest, testAieClientDestroyFunction001, Function | MediumTest | Level2)
115 {
116     HILOGI("[Test]testAieClientDestroyFunction001.");
117     bool isAsync = GetRandomBool();
118     // Step 0: Defines variables.
119     ConfigInfo configInfo;
120     GetConfigInfo(configInfo);
121 
122     ClientInfo clientInfo;
123     GetClientInfo(clientInfo);
124 
125     AlgorithmInfo algorithmInfo;
126     int algorithmType = isAsync ? ALGORITHM_TYPE_ASYNC : ALGORITHM_TYPE_SYNC;
127     GetSyncAlgorithmInfo(algorithmInfo, isAsync, algorithmType);
128 
129     ServiceDeadCb *cb = nullptr;
130     AIE_NEW(cb, ServiceDeadCb());
131 
132     // Step 1: Invokes AieClientInit.
133     int initReturnCode = AieClientInit(configInfo, clientInfo, algorithmInfo, cb);
134     EXPECT_EQ(RETCODE_SUCCESS, initReturnCode) << "AieClientInit is failed!";
135     EXPECT_EQ(true, clientInfo.clientId > 0) << "clientId(" << std::to_string(clientInfo.clientId) << ") is incorrect!";
136     EXPECT_EQ(true, clientInfo.sessionId > 0) << "sessionId(" << std::to_string(clientInfo.sessionId)
137         << ") is incorrect!";
138     AIE_DELETE(cb);
139 
140     // Step 2: Invokes AieClientDestroy.
141     int destroyReturnCode = AieClientDestroy(clientInfo);
142     EXPECT_EQ(RETCODE_SUCCESS, destroyReturnCode) << "AieClientDestroy is failed!";
143 }
144 
145 /**
146  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientDestroy_0200
147  * @tc.name   : 02. 同步执行算法之后,调用AieClientDestroy返回成功
148  * @tc.desc   : [C- SOFTWARE -0200]
149  */
150 HWTEST_F(AieClientDestroyFunctionTest, testAieClientDestroyFunction002, Function | MediumTest | Level3)
151 {
152     HILOGI("[Test]testAieClientDestroyFunction002.");
153     TestAieClientDestroy(false);
154 }
155 
156 /**
157  * @tc.number : SUB_AI_AIDistributedAbility_HiAiEngine_Lite_Function_HiAiAPI_AIEClient_AieClientDestroy_0300
158  * @tc.name   : 03. 异步执行算法之后,调用AieClientDestroy返回成功
159  * @tc.desc   : [C- SOFTWARE -0200]
160  */
161 HWTEST_F(AieClientDestroyFunctionTest, testAieClientDestroyFunction003, Function | MediumTest | Level3)
162 {
163     HILOGI("[Test]testAieClientDestroyFunction003.");
164     TestAieClientDestroy(true);
165 }
166