• 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 <atomic>
17 #include <chrono>
18 #include <hwext/gtest-ext.h>
19 #include <hwext/gtest-tag.h>
20 #include <thread>
21 
22 #include "i_semaphore.h"
23 
24 namespace {
25 using namespace testing::ext;
26 
27 class SemaphoreTest : public testing::Test {
28 protected:
SetUpTestCase()29     static void SetUpTestCase() {}
TearDownTestCase()30     static void TearDownTestCase() {}
31 };
32 
33 /**
34  * @tc.name: SemaphoreTest
35  * @tc.desc: CtorDtor.
36  * @tc.type: FUNC
37  */
38 HWTEST_F(SemaphoreTest, CtorDtor, TestSize.Level1)
39 {
40     auto semaphore = GetSemaphoreFactory().Create(0);
41     EXPECT_NE(semaphore, nullptr);
42 }
43 
44 /**
45  * @tc.name: SemaphoreTest
46  * @tc.desc: Wait.
47  * @tc.type: FUNC
48  */
49 HWTEST_F(SemaphoreTest, Wait, TestSize.Level1)
50 {
51     auto semaphore = GetSemaphoreFactory().Create(1);
52     ASSERT_NE(semaphore, nullptr);
53     EXPECT_TRUE(semaphore->Wait());
54 }
55 
56 /**
57  * @tc.name: SemaphoreTest
58  * @tc.desc: Post.
59  * @tc.type: FUNC
60  */
61 HWTEST_F(SemaphoreTest, Post, TestSize.Level1)
62 {
63     auto semaphore = GetSemaphoreFactory().Create(0);
64     ASSERT_NE(semaphore, nullptr);
65     EXPECT_TRUE(semaphore->Post());
66 }
67 
68 /**
69  * @tc.name: SemaphoreTest
70  * @tc.desc: Post.
71  * @tc.type: FUNC
72  */
73 HWTEST_F(SemaphoreTest, WaitPost, TestSize.Level1)
74 {
75     auto readySem = GetSemaphoreFactory().Create(0);
76     auto finishSem = GetSemaphoreFactory().Create(0);
77     ASSERT_NE(readySem, nullptr);
78     ASSERT_NE(finishSem, nullptr);
79 
80     auto done = std::make_shared<bool>(false);
81     ASSERT_NE(done, nullptr);
82 
__anon92442f0a0202() 83     std::thread bgThread([=]() {
84         readySem->Wait();
85         *done = true;
86         finishSem->Post();
87     });
88 
89     EXPECT_TRUE(readySem->Post());
90     EXPECT_TRUE(finishSem->Wait());
91     EXPECT_TRUE(*done);
92 
93     bgThread.join();
94 }
95 } // namespace