@@ -32,10 +32,15 @@ namespace test
3232
3333namespace
3434{
35+ template <typename Lock>
36+ using LPtr2int = LockedPtr<int , Lock>;
37+
3538struct IntWrapper
3639{
3740 int value;
3841};
42+
43+ using LPtr2IntW = LockedPtr<IntWrapper, std::unique_lock<MockMutex>>;
3944} // namespace
4045
4146TEST (LockedPtrTest, ConstructionWithTypes)
@@ -72,53 +77,161 @@ TEST(LockedPtrTest, SwappingWithTypes)
7277 << " LockedPtr with shared_lock should be swappable" ;
7378}
7479
75- TEST (LockedPtrTest, TFunctionsWithTypes )
80+ TEST (LockedPtrTest, DereferenceTests )
7681{
77- // Functions working with T
78- EXPECT_TRUE ((std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<int , BasicLockableArchetype>::operator *),
79- LockedPtr<int , BasicLockableArchetype>*>,
80- int &>))
81- << " LockedPtr::operator*() should return int&" ;
82- EXPECT_TRUE ((std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<const int , BasicLockableArchetype>::operator *),
83- LockedPtr<const int , BasicLockableArchetype>*>,
84- const int &>))
85- << " LockedPtr::operator*() should return const int&" ;
86- EXPECT_TRUE ((std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<int , BasicLockableArchetype>::operator ->),
87- LockedPtr<int , BasicLockableArchetype>*>,
88- int *>))
89- << " LockedPtr::operator->() should return int*" ;
90- EXPECT_TRUE (
91- (std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<const int , BasicLockableArchetype>::operator ->),
92- LockedPtr<const int , BasicLockableArchetype>*>,
93- const int *>))
94- << " LockedPtr::operator->() should return const int*" ;
95- EXPECT_TRUE ((std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<int , BasicLockableArchetype>::get),
96- LockedPtr<int , BasicLockableArchetype>*>,
97- int *>))
98- << " LockedPtr::get() should return int*" ;
99- EXPECT_TRUE ((std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<const int , BasicLockableArchetype>::get),
100- LockedPtr<const int , BasicLockableArchetype>*>,
101- const int *>))
102- << " LockedPtr::get() should return const int*" ;
82+ int x = 10 ;
83+ LPtr2int<BasicLockableArchetype> lp_basic{&x, BasicLockableArchetype{}};
84+
85+ EXPECT_EQ (*lp_basic, 10 );
86+ EXPECT_EQ (*std::as_const (lp_basic), 10 );
87+
88+ *lp_basic = 20 ;
89+ EXPECT_EQ (*lp_basic, 20 );
90+ EXPECT_EQ (*std::as_const (lp_basic), 20 );
91+
92+ decltype (auto ) x_ref = *lp_basic;
93+ EXPECT_TRUE ((std::is_same_v<decltype (x_ref), int &>)) << " LockedPtr::operator* should return int&" ;
94+
95+ decltype (auto ) cx_ref = *std::as_const (lp_basic);
96+ EXPECT_TRUE ((std::is_same_v<decltype (cx_ref), const int &>)) << " LockedPtr::operator* should return const int&" ;
10397}
10498
105- TEST (LockedPtrTest, LockFunctionsWithTypes )
99+ TEST (LockedPtrTest, PtrUsageTests )
106100{
107- // Functions working with Lock
108- EXPECT_TRUE ((std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<int , BasicLockableArchetype>::unlock_guard),
109- LockedPtr<int , BasicLockableArchetype>*>,
110- UnlockGuard<BasicLockableArchetype>>))
111- << " LockedPtr::unlock_guard() should return UnlockGuard<BasicLockableArchetype>" ;
112- EXPECT_TRUE (
113- (std::is_same_v<std::invoke_result_t <decltype (&LockedPtr<int , std::unique_lock<std::mutex>>::unlock_guard),
114- LockedPtr<int , std::unique_lock<std::mutex>>*>,
115- UnlockGuard<std::unique_lock<std::mutex>>>))
116- << " LockedPtr::unlock_guard() should return UnlockGuard<unique_lock>" ;
117- EXPECT_TRUE ((std::is_same_v<
118- std::invoke_result_t <decltype (&LockedPtr<int , std::shared_lock<std::shared_mutex>>::unlock_guard),
119- LockedPtr<int , std::shared_lock<std::shared_mutex>>*>,
120- UnlockGuard<std::shared_lock<std::shared_mutex>>>))
121- << " LockedPtr::unlock_guard() should return UnlockGuard<shared_lock>" ;
101+ IntWrapper obj{42 };
102+ MockMutex mut{};
103+
104+ LPtr2IntW lp_obj{&obj, std::unique_lock<MockMutex>{mut}};
105+ EXPECT_EQ (lp_obj->value , 42 );
106+ EXPECT_EQ (std::as_const (lp_obj)->value , 42 );
107+
108+ lp_obj->value = 100 ;
109+ EXPECT_EQ (lp_obj->value , 100 );
110+ EXPECT_EQ (std::as_const (lp_obj)->value , 100 );
111+ }
112+
113+ TEST (LockedPtrTest, GetTests)
114+ {
115+ IntWrapper obj{42 };
116+ MockMutex mut{};
117+
118+ LPtr2IntW lp_obj{&obj, std::unique_lock<MockMutex>{mut}};
119+ EXPECT_EQ (lp_obj.get (), &obj);
120+ EXPECT_EQ (std::as_const (lp_obj).get (), &obj);
121+
122+ decltype (auto ) x_ptr = lp_obj.get ();
123+ EXPECT_TRUE ((std::is_same_v<decltype (x_ptr), IntWrapper*>)) << " LockedPtr::get() should return IntWrapper*" ;
124+
125+ decltype (auto ) cx_ptr = std::as_const (lp_obj).get ();
126+ EXPECT_TRUE ((std::is_same_v<decltype (cx_ptr), const IntWrapper*>))
127+ << " LockedPtr::get() should return const IntWrapper*" ;
128+ }
129+
130+ TEST (LockedPtrTest, UnlockGuardBasicLockableArchetypeTests)
131+ {
132+ int x = 42 ;
133+
134+ LPtr2int<BasicLockableArchetype> lp_basic{&x, BasicLockableArchetype{}};
135+
136+ {
137+ auto ug_basic = lp_basic.unlock_guard ();
138+ EXPECT_TRUE ((std::is_same_v<decltype (ug_basic), UnlockGuard<BasicLockableArchetype>>))
139+ << " unlock_guard() should return UnlockGuard<BasicLockableArchetype>" ;
140+ }
141+
142+ {
143+ auto cug_basic = std::as_const (lp_basic).unlock_guard ();
144+ EXPECT_TRUE ((std::is_same_v<decltype (cug_basic), UnlockGuard<BasicLockableArchetype>>))
145+ << " unlock_guard() should return UnlockGuard<BasicLockableArchetype>" ;
146+ }
147+ }
148+
149+ TEST (LockedPtrTest, UnlockGuardUniqueLockTests)
150+ {
151+ int x = 42 ;
152+
153+ std::mutex mut{};
154+ LPtr2int<std::unique_lock<std::mutex>> lp_mut{&x, std::unique_lock{mut}};
155+
156+ {
157+ auto ug_mut = lp_mut.unlock_guard ();
158+ EXPECT_TRUE ((std::is_same_v<decltype (ug_mut), UnlockGuard<std::unique_lock<std::mutex>>>))
159+ << " unlock_guard() should return UnlockGuard<std::unique_lock<std::mutex>>" ;
160+ }
161+
162+ {
163+ auto cug_mut = std::as_const (lp_mut).unlock_guard ();
164+ EXPECT_TRUE ((std::is_same_v<decltype (cug_mut), UnlockGuard<std::unique_lock<std::mutex>>>))
165+ << " unlock_guard() should return UnlockGuard<std::unique_lock<std::mutex>>" ;
166+ }
167+
168+ {
169+ IntWrapper obj{42 };
170+ MockMutex mut{};
171+ LPtr2IntW lp_obj{&obj, std::unique_lock<MockMutex>{mut}};
172+ EXPECT_TRUE (mut.is_locked ());
173+ {
174+ auto ug = lp_obj.unlock_guard ();
175+ EXPECT_FALSE (mut.is_locked ());
176+ }
177+ EXPECT_TRUE (mut.is_locked ());
178+ }
179+
180+ {
181+ IntWrapper obj{42 };
182+ MockMutex mut{};
183+ LPtr2IntW lp_obj{&obj, std::unique_lock<MockMutex>{mut}};
184+ EXPECT_TRUE (mut.is_locked ());
185+ {
186+ auto cug = std::as_const (lp_obj).unlock_guard ();
187+ EXPECT_FALSE (mut.is_locked ());
188+ }
189+ EXPECT_TRUE (mut.is_locked ());
190+ }
191+ }
192+
193+ TEST (LockedPtrTest, UnlockGuardSharedLockTests)
194+ {
195+ int x = 42 ;
196+
197+ std::shared_mutex sh_mut{};
198+ LPtr2int<std::shared_lock<std::shared_mutex>> lp_sh{&x, std::shared_lock{sh_mut}};
199+
200+ {
201+ auto ug_sh = lp_sh.unlock_guard ();
202+ EXPECT_TRUE ((std::is_same_v<decltype (ug_sh), UnlockGuard<std::shared_lock<std::shared_mutex>>>))
203+ << " unlock_guard() should return UnlockGuard<std::shared_lock<std::shared_mutex>>" ;
204+ }
205+
206+ {
207+ auto cug_sh = std::as_const (lp_sh).unlock_guard ();
208+ EXPECT_TRUE ((std::is_same_v<decltype (cug_sh), UnlockGuard<std::shared_lock<std::shared_mutex>>>))
209+ << " unlock_guard() should return UnlockGuard<std::shared_lock<std::shared_mutex>>" ;
210+ }
211+
212+ {
213+ IntWrapper obj{42 };
214+ MockSharedMutex mut{};
215+ LockedPtr lp_obj{&obj, std::shared_lock<MockSharedMutex>{mut}};
216+ EXPECT_TRUE (mut.is_shared_locked ());
217+ {
218+ auto ug = lp_obj.unlock_guard ();
219+ EXPECT_FALSE (mut.is_shared_locked ());
220+ }
221+ EXPECT_TRUE (mut.is_shared_locked ());
222+ }
223+
224+ {
225+ IntWrapper obj{42 };
226+ MockSharedMutex mut{};
227+ LockedPtr lp_obj{&obj, std::shared_lock<MockSharedMutex>{mut}};
228+ EXPECT_TRUE (mut.is_shared_locked ());
229+ {
230+ auto cug = std::as_const (lp_obj).unlock_guard ();
231+ EXPECT_FALSE (mut.is_shared_locked ());
232+ }
233+ EXPECT_TRUE (mut.is_shared_locked ());
234+ }
122235}
123236
124237TEST (LockedPtrTest, NonConstWithUniqueLock)
0 commit comments