Use type-parameterized test cases in folly/test/SynchronizedTest.cpp
[folly.git] / folly / test / SynchronizedTest.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
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 // @author: Andrei Alexandrescu (aalexandre)
18
19 // Test bed for folly/Synchronized.h
20
21 #include <folly/Synchronized.h>
22 #include <folly/RWSpinLock.h>
23 #include <folly/SharedMutex.h>
24 #include <folly/test/SynchronizedTestLib.h>
25 #include <gtest/gtest.h>
26
27 namespace {
28
29 template <class Mutex>
30 class SynchronizedTest : public testing::Test {};
31
32 using SynchronizedTestTypes = testing::Types
33   < folly::SharedMutexReadPriority
34   , folly::SharedMutexWritePriority
35   , std::mutex
36   , std::recursive_mutex
37 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
38   , std::timed_mutex
39   , std::recursive_timed_mutex
40 #endif
41   , boost::mutex
42   , boost::recursive_mutex
43 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
44   , boost::timed_mutex
45   , boost::recursive_timed_mutex
46 #endif
47   , boost::shared_mutex
48 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
49   , folly::RWTicketSpinLock32
50   , folly::RWTicketSpinLock64
51 #endif
52   >;
53 TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes);
54
55 TYPED_TEST(SynchronizedTest, Basic) {
56   testBasic<TypeParam>();
57 }
58
59 TYPED_TEST(SynchronizedTest, Concurrency) {
60   testConcurrency<TypeParam>();
61 }
62
63 TYPED_TEST(SynchronizedTest, DualLocking) {
64   testDualLocking<TypeParam>();
65 }
66
67 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
68   testDualLockingWithConst<TypeParam>();
69 }
70
71 TYPED_TEST(SynchronizedTest, ConstCopy) {
72   testConstCopy<TypeParam>();
73 }
74
75 template <class Mutex>
76 class SynchronizedTimedTest : public testing::Test {};
77
78 using SynchronizedTimedTestTypes = testing::Types
79   < folly::SharedMutexReadPriority
80   , folly::SharedMutexWritePriority
81 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
82   , std::timed_mutex
83   , std::recursive_timed_mutex
84   , boost::timed_mutex
85   , boost::recursive_timed_mutex
86   , boost::shared_mutex
87 #endif
88 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
89   , folly::RWTicketSpinLock32
90   , folly::RWTicketSpinLock64
91 #endif
92   >;
93 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
94
95 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
96   testTimedSynchronized<TypeParam>();
97 }
98
99 template <class Mutex>
100 class SynchronizedTimedWithConstTest : public testing::Test {};
101
102 using SynchronizedTimedWithConstTestTypes = testing::Types
103   < folly::SharedMutexReadPriority
104   , folly::SharedMutexWritePriority
105 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
106   , boost::shared_mutex
107 #endif
108 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
109   , folly::RWTicketSpinLock32
110   , folly::RWTicketSpinLock64
111 #endif
112   >;
113 TYPED_TEST_CASE(
114     SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
115
116 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
117   testTimedSynchronizedWithConst<TypeParam>();
118 }
119
120 }