brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame^] | 1 | #include "aos/common/mutex.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | namespace aos { |
| 6 | namespace testing { |
| 7 | |
| 8 | class MutexTest : public ::testing::Test { |
| 9 | public: |
| 10 | Mutex test_mutex; |
| 11 | }; |
| 12 | |
| 13 | typedef MutexTest MutexDeathTest; |
| 14 | |
| 15 | TEST_F(MutexTest, TryLock) { |
| 16 | EXPECT_TRUE(test_mutex.TryLock()); |
| 17 | EXPECT_FALSE(test_mutex.TryLock()); |
| 18 | } |
| 19 | |
| 20 | TEST_F(MutexTest, Lock) { |
| 21 | test_mutex.Lock(); |
| 22 | EXPECT_FALSE(test_mutex.TryLock()); |
| 23 | } |
| 24 | |
| 25 | TEST_F(MutexTest, Unlock) { |
| 26 | test_mutex.Lock(); |
| 27 | EXPECT_FALSE(test_mutex.TryLock()); |
| 28 | test_mutex.Unlock(); |
| 29 | EXPECT_TRUE(test_mutex.TryLock()); |
| 30 | } |
| 31 | |
| 32 | #ifndef __VXWORKS__ |
| 33 | // Sees what happens with multiple unlocks. |
| 34 | TEST_F(MutexDeathTest, RepeatUnlock) { |
| 35 | test_mutex.Lock(); |
| 36 | test_mutex.Unlock(); |
| 37 | EXPECT_DEATH(test_mutex.Unlock(), ".*multiple unlock.*"); |
| 38 | } |
| 39 | |
| 40 | // Sees what happens if you unlock without ever locking (or unlocking) it. |
| 41 | TEST_F(MutexDeathTest, NeverLock) { |
| 42 | EXPECT_DEATH(test_mutex.Unlock(), ".*multiple unlock.*"); |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | TEST_F(MutexTest, MutexLocker) { |
| 47 | { |
| 48 | aos::MutexLocker locker(&test_mutex); |
| 49 | EXPECT_FALSE(test_mutex.TryLock()); |
| 50 | } |
| 51 | EXPECT_TRUE(test_mutex.TryLock()); |
| 52 | } |
| 53 | |
| 54 | } // namespace testing |
| 55 | } // namespace aos |