OLD | NEW |
| (Empty) |
1 // Copyright 2003-2009 Google Inc. | |
2 // | |
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 // lock_ptr_unittest.cpp | |
17 | |
18 #include "omaha/base/lock_ptr.h" | |
19 #include "omaha/base/synchronized.h" | |
20 #include "omaha/testing/unit_test.h" | |
21 | |
22 namespace omaha { | |
23 | |
24 // Test type. | |
25 class X { | |
26 public: | |
27 X() : i_(0) {} | |
28 void f() { i_ = 10; } | |
29 | |
30 private: | |
31 int i_; | |
32 | |
33 friend class LockTest; | |
34 FRIEND_TEST(LockTest, X); | |
35 }; | |
36 | |
37 // A dummy lock just to test that locking and unlocking methods get called. | |
38 class DummyLock { | |
39 public: | |
40 DummyLock() : cnt_(0) {} | |
41 | |
42 void FakeLock() { ++cnt_; } | |
43 void FakeUnlock() { --cnt_; } | |
44 | |
45 private: | |
46 int cnt_; | |
47 | |
48 friend class LockTest; | |
49 FRIEND_TEST(LockTest, DummyLock); | |
50 }; | |
51 | |
52 // Specialization of the policy for the DummyLock. | |
53 template<> void AcquireLock(DummyLock& lock) { lock.FakeLock(); } | |
54 template<> void ReleaseLock(DummyLock& lock) { lock.FakeUnlock(); } | |
55 | |
56 // Empty test fixture. | |
57 class LockTest : public testing::Test {}; | |
58 | |
59 TEST_F(LockTest, X) { | |
60 // Create a few synchronization objects. | |
61 CriticalSection cs_lock; | |
62 LLock local_lock; | |
63 GLock global_lock; | |
64 ASSERT_TRUE(global_lock.Initialize(_T("test"))); | |
65 | |
66 // The instance to lock. | |
67 X x; | |
68 | |
69 // Lock the instance and call a method. | |
70 LockPtr<X>(x, local_lock)->f(); | |
71 ASSERT_EQ((*LockPtr<X>(x, cs_lock)).i_, 10); | |
72 | |
73 // Lock the instance and access a data member. | |
74 LockPtr<X>(x, cs_lock)->i_ = 0; | |
75 ASSERT_EQ((*LockPtr<X>(x, cs_lock)).i_, 0); | |
76 | |
77 // Lock the instance and call a method. | |
78 LockPtr<X>(x, global_lock)->f(); | |
79 ASSERT_EQ((*LockPtr<X>(x, cs_lock)).i_, 10); | |
80 } | |
81 | |
82 TEST_F(LockTest, DummyLock) { | |
83 DummyLock dummy_lock; | |
84 ASSERT_EQ(dummy_lock.cnt_, 0); | |
85 | |
86 // The instance to lock. | |
87 X x; | |
88 | |
89 { | |
90 LockPtr<X> p(x, dummy_lock); | |
91 ASSERT_EQ(dummy_lock.cnt_, 1); | |
92 } | |
93 | |
94 ASSERT_EQ(dummy_lock.cnt_, 0); | |
95 } | |
96 | |
97 } // namespace omaha | |
98 | |
OLD | NEW |