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 <unistd.h>
17 
18 #include "gtest/gtest.h"
19 
20 #include "platform/semaphore/include/i_semaphore.h"
21 #include "platform/semaphore/include/simple_event_notifier.h"
22 #include "utils/log/aie_log.h"
23 
24 using namespace OHOS::AI;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace AI {
29 const int WAIT_TIME_MS = 1000;
30 const int INTERVAL_TIME_S = 2;
31 const int TIME_OUT = 20;
32 const int CONST_VALUE = 123;
33 class ISemaphore;
34 class VectorSimpleEventNotifier;
35 } // namespace AI
36 } // namespace OHOS
37 
38 class SemaphoreTest : public testing::Test {
39 public:
40     // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()41     static void SetUpTestCase() {};
42 
43     // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()44     static void TearDownTestCase() {};
45 
46     // SetUp:Execute before each test case
SetUp()47     void SetUp() {};
48 
49     // TearDown:Execute after each test case
TearDown()50     void TearDown() {};
51 };
52 
53 /**
54  * @tc.name: SemaphoreTest001
55  * @tc.desc: Test semaphore wait function.
56  * @tc.type: FUNC
57  * @tc.require: AR000F77TL
58  */
59 HWTEST_F(SemaphoreTest, SemaphoreTest001, TestSize.Level1)
60 {
61     HILOGD("[Test]Begin to main. test ISemaphore.");
62     std::shared_ptr<ISemaphore> semaphoreExample = ISemaphore::MakeShared(0);
63     semaphoreExample->Signal();
64     sleep(INTERVAL_TIME_S);
65     bool ret = semaphoreExample->Wait(WAIT_TIME_MS);
66     ASSERT_TRUE(ret);
67 }
68 
69 /**
70  * @tc.name: SimpleNotifierTest001
71  * @tc.desc: Test simpleNotifier AddToBack and GetFromFront functions.
72  * @tc.type: FUNC
73  * @tc.require: AR000F77TL
74  */
75 HWTEST_F(SemaphoreTest, SimpleNotifierTest001, TestSize.Level1)
76 {
77     HILOGD("[Test]Test simple notifier.");
78     SimpleEventNotifier<int> simpleNotifier = SimpleEventNotifier<int>(nullptr);
79     int* itemIn = nullptr;
80     AIE_NEW(itemIn, int(CONST_VALUE));
81     int *itemOut = nullptr;
82     simpleNotifier.AddToBack(itemIn);
83     sleep(INTERVAL_TIME_S);
84     int ret = simpleNotifier.GetFromFront(TIME_OUT, itemOut);
85     if (itemOut != nullptr) {
86         ASSERT_EQ(*itemOut, CONST_VALUE);
87     }
88     ASSERT_TRUE(ret);
89     AIE_DELETE(itemIn);
90 }
91