• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include "chre/util/conditional_lock_guard.h"
20 
21 using chre::ConditionalLockGuard;
22 
23 class MockMutex {
24  public:
MockMutex()25   MockMutex() : mLockCount(0) {}
26 
lock()27   void lock() {
28     mLockCount++;
29   }
30 
unlock()31   void unlock() {
32     mLockCount--;
33   }
34 
getLockCount()35   int getLockCount() {
36     return mLockCount;
37   }
38 
39  private:
40   int mLockCount;
41 };
42 
TEST(ConditionalLockGuard,LockUnlock)43 TEST(ConditionalLockGuard, LockUnlock) {
44   MockMutex mutex;
45   ASSERT_EQ(mutex.getLockCount(), 0);
46 
47   {
48     ConditionalLockGuard<MockMutex> lock(mutex, true);
49     EXPECT_EQ(mutex.getLockCount(), 1);
50   }
51 
52   EXPECT_EQ(mutex.getLockCount(), 0);
53 }
54 
TEST(ConditionalLockGuard,DontLock)55 TEST(ConditionalLockGuard, DontLock) {
56   MockMutex mutex;
57   ASSERT_EQ(mutex.getLockCount(), 0);
58 
59   {
60     ConditionalLockGuard<MockMutex> lock(mutex, false);
61     EXPECT_EQ(mutex.getLockCount(), 0);
62   }
63 
64   EXPECT_EQ(mutex.getLockCount(), 0);
65 }
66