1 /*
2  * Copyright (C) 2024 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 <meta/api/threading/mutex.h>
18 
19 #include "src/test_runner.h"
20 #include "src/testing_objects.h"
21 
22 META_BEGIN_NAMESPACE()
23 
24 using namespace testing::ext;
25 using CORE_NS::Mutex;
26 using CORE_NS::UniqueLock;
27 
28 class MutexTest : public testing::Test {
29 public:
SetUpTestSuite()30     static void SetUpTestSuite()
31     {
32         SetTest();
33     }
TearDownTestSuite()34     static void TearDownTestSuite()
35     {
36         ResetTest();
37     }
38     void SetUp() override {}
39     void TearDown() override {}
40 };
41 
42 /**
43  * @tc.name: MutexTest
44  * @tc.desc: test UniqueLock function
45  * @tc.type: FUNC
46  * @tc.require: I7DMS1
47  */
HWTEST_F(MutexTest, UniqueLock, TestSize.Level1)48 HWTEST_F(MutexTest, UniqueLock, TestSize.Level1)
49 {
50     Mutex m;
51     {
52         UniqueLock l { m };
53         EXPECT_TRUE(l);
54         l.Unlock();
55         EXPECT_FALSE(l);
56     }
57 
58     {
59         UniqueLock l { m };
60         EXPECT_TRUE(l);
61         l.Unlock();
62         EXPECT_FALSE(l);
63         l.Lock();
64         EXPECT_TRUE(l);
65     }
66 
67     {
68         UniqueLock l { m };
69         EXPECT_TRUE(l);
70         UniqueLock l2 = BASE_NS::move(l);
71         EXPECT_FALSE(l);
72         EXPECT_TRUE(l2);
73         l = std::move(l2);
74         EXPECT_FALSE(l2);
75         EXPECT_TRUE(l);
76     }
77 }
78 META_END_NAMESPACE()