blob: a5d5e86afbfe59c825cd48ff87e7486c5bf838b1 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/mutex.h"
2
3#include "gtest/gtest.h"
4
5namespace aos {
6namespace testing {
7
8class MutexTest : public ::testing::Test {
9 public:
10 Mutex test_mutex;
11};
12
13typedef MutexTest MutexDeathTest;
14
15TEST_F(MutexTest, TryLock) {
16 EXPECT_TRUE(test_mutex.TryLock());
17 EXPECT_FALSE(test_mutex.TryLock());
18}
19
20TEST_F(MutexTest, Lock) {
21 test_mutex.Lock();
22 EXPECT_FALSE(test_mutex.TryLock());
23}
24
25TEST_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.
34TEST_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.
41TEST_F(MutexDeathTest, NeverLock) {
42 EXPECT_DEATH(test_mutex.Unlock(), ".*multiple unlock.*");
43}
44#endif
45
46TEST_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