]> git.proxmox.com Git - ceph.git/blob - ceph/src/googletest/googletest/test/gtest-typed-test_test.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / googletest / googletest / test / gtest-typed-test_test.cc
1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 #include "test/gtest-typed-test_test.h"
32
33 #include <set>
34 #include <type_traits>
35 #include <vector>
36
37 #include "gtest/gtest.h"
38
39 #if _MSC_VER
40 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
41 #endif // _MSC_VER
42
43 using testing::Test;
44
45 // Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture
46 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
47 // type-parameterized test.
48 template <typename T>
49 class CommonTest : public Test {
50 // For some technical reason, SetUpTestSuite() and TearDownTestSuite()
51 // must be public.
52 public:
53 static void SetUpTestSuite() {
54 shared_ = new T(5);
55 }
56
57 static void TearDownTestSuite() {
58 delete shared_;
59 shared_ = nullptr;
60 }
61
62 // This 'protected:' is optional. There's no harm in making all
63 // members of this fixture class template public.
64 protected:
65 // We used to use std::list here, but switched to std::vector since
66 // MSVC's <list> doesn't compile cleanly with /W4.
67 typedef std::vector<T> Vector;
68 typedef std::set<int> IntSet;
69
70 CommonTest() : value_(1) {}
71
72 ~CommonTest() override { EXPECT_EQ(3, value_); }
73
74 void SetUp() override {
75 EXPECT_EQ(1, value_);
76 value_++;
77 }
78
79 void TearDown() override {
80 EXPECT_EQ(2, value_);
81 value_++;
82 }
83
84 T value_;
85 static T* shared_;
86 };
87
88 template <typename T>
89 T* CommonTest<T>::shared_ = nullptr;
90
91 // This #ifdef block tests typed tests.
92 #if GTEST_HAS_TYPED_TEST
93
94 using testing::Types;
95
96 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
97 // and SetUp()/TearDown() work correctly in typed tests
98
99 typedef Types<char, int> TwoTypes;
100 TYPED_TEST_SUITE(CommonTest, TwoTypes);
101
102 TYPED_TEST(CommonTest, ValuesAreCorrect) {
103 // Static members of the fixture class template can be visited via
104 // the TestFixture:: prefix.
105 EXPECT_EQ(5, *TestFixture::shared_);
106
107 // Typedefs in the fixture class template can be visited via the
108 // "typename TestFixture::" prefix.
109 typename TestFixture::Vector empty;
110 EXPECT_EQ(0U, empty.size());
111
112 typename TestFixture::IntSet empty2;
113 EXPECT_EQ(0U, empty2.size());
114
115 // Non-static members of the fixture class must be visited via
116 // 'this', as required by C++ for class templates.
117 EXPECT_EQ(2, this->value_);
118 }
119
120 // The second test makes sure shared_ is not deleted after the first
121 // test.
122 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
123 // Static members of the fixture class template can also be visited
124 // via 'this'.
125 ASSERT_TRUE(this->shared_ != nullptr);
126 EXPECT_EQ(5, *this->shared_);
127
128 // TypeParam can be used to refer to the type parameter.
129 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
130 }
131
132 // Tests that multiple TYPED_TEST_SUITE's can be defined in the same
133 // translation unit.
134
135 template <typename T>
136 class TypedTest1 : public Test {
137 };
138
139 // Verifies that the second argument of TYPED_TEST_SUITE can be a
140 // single type.
141 TYPED_TEST_SUITE(TypedTest1, int);
142 TYPED_TEST(TypedTest1, A) {}
143
144 template <typename T>
145 class TypedTest2 : public Test {
146 };
147
148 // Verifies that the second argument of TYPED_TEST_SUITE can be a
149 // Types<...> type list.
150 TYPED_TEST_SUITE(TypedTest2, Types<int>);
151
152 // This also verifies that tests from different typed test cases can
153 // share the same name.
154 TYPED_TEST(TypedTest2, A) {}
155
156 // Tests that a typed test case can be defined in a namespace.
157
158 namespace library1 {
159
160 template <typename T>
161 class NumericTest : public Test {
162 };
163
164 typedef Types<int, long> NumericTypes;
165 TYPED_TEST_SUITE(NumericTest, NumericTypes);
166
167 TYPED_TEST(NumericTest, DefaultIsZero) {
168 EXPECT_EQ(0, TypeParam());
169 }
170
171 } // namespace library1
172
173 // Tests that custom names work.
174 template <typename T>
175 class TypedTestWithNames : public Test {};
176
177 class TypedTestNames {
178 public:
179 template <typename T>
180 static std::string GetName(int i) {
181 if (std::is_same<T, char>::value) {
182 return std::string("char") + ::testing::PrintToString(i);
183 }
184 if (std::is_same<T, int>::value) {
185 return std::string("int") + ::testing::PrintToString(i);
186 }
187 }
188 };
189
190 TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
191
192 TYPED_TEST(TypedTestWithNames, TestSuiteName) {
193 if (std::is_same<TypeParam, char>::value) {
194 EXPECT_STREQ(::testing::UnitTest::GetInstance()
195 ->current_test_info()
196 ->test_suite_name(),
197 "TypedTestWithNames/char0");
198 }
199 if (std::is_same<TypeParam, int>::value) {
200 EXPECT_STREQ(::testing::UnitTest::GetInstance()
201 ->current_test_info()
202 ->test_suite_name(),
203 "TypedTestWithNames/int1");
204 }
205 }
206
207 #endif // GTEST_HAS_TYPED_TEST
208
209 // This #ifdef block tests type-parameterized tests.
210 #if GTEST_HAS_TYPED_TEST_P
211
212 using testing::Types;
213 using testing::internal::TypedTestSuitePState;
214
215 // Tests TypedTestSuitePState.
216
217 class TypedTestSuitePStateTest : public Test {
218 protected:
219 void SetUp() override {
220 state_.AddTestName("foo.cc", 0, "FooTest", "A");
221 state_.AddTestName("foo.cc", 0, "FooTest", "B");
222 state_.AddTestName("foo.cc", 0, "FooTest", "C");
223 }
224
225 TypedTestSuitePState state_;
226 };
227
228 TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
229 const char* tests = "A, B, C";
230 EXPECT_EQ(tests,
231 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
232 }
233
234 // Makes sure that the order of the tests and spaces around the names
235 // don't matter.
236 TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
237 const char* tests = "A,C, B";
238 EXPECT_EQ(tests,
239 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
240 }
241
242 using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
243
244 TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
245 EXPECT_DEATH_IF_SUPPORTED(
246 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
247 "foo\\.cc.1.?: Test A is listed more than once\\.");
248 }
249
250 TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
251 EXPECT_DEATH_IF_SUPPORTED(
252 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C, D"),
253 "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
254 }
255
256 TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
257 EXPECT_DEATH_IF_SUPPORTED(
258 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, C"),
259 "foo\\.cc.1.?: You forgot to list test B\\.");
260 }
261
262 // Tests that defining a test for a parameterized test case generates
263 // a run-time error if the test case has been registered.
264 TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
265 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
266 EXPECT_DEATH_IF_SUPPORTED(
267 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
268 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
269 "\\(FooTest, \\.\\.\\.\\)\\.");
270 }
271
272 // Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
273 // and SetUp()/TearDown() work correctly in type-parameterized tests.
274
275 template <typename T>
276 class DerivedTest : public CommonTest<T> {
277 };
278
279 TYPED_TEST_SUITE_P(DerivedTest);
280
281 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
282 // Static members of the fixture class template can be visited via
283 // the TestFixture:: prefix.
284 EXPECT_EQ(5, *TestFixture::shared_);
285
286 // Non-static members of the fixture class must be visited via
287 // 'this', as required by C++ for class templates.
288 EXPECT_EQ(2, this->value_);
289 }
290
291 // The second test makes sure shared_ is not deleted after the first
292 // test.
293 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
294 // Static members of the fixture class template can also be visited
295 // via 'this'.
296 ASSERT_TRUE(this->shared_ != nullptr);
297 EXPECT_EQ(5, *this->shared_);
298 EXPECT_EQ(2, this->value_);
299 }
300
301 REGISTER_TYPED_TEST_SUITE_P(DerivedTest,
302 ValuesAreCorrect, ValuesAreStillCorrect);
303
304 typedef Types<short, long> MyTwoTypes;
305 INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
306
307 // Tests that custom names work with type parametrized tests. We reuse the
308 // TwoTypes from above here.
309 template <typename T>
310 class TypeParametrizedTestWithNames : public Test {};
311
312 TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
313
314 TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
315 if (std::is_same<TypeParam, char>::value) {
316 EXPECT_STREQ(::testing::UnitTest::GetInstance()
317 ->current_test_info()
318 ->test_suite_name(),
319 "CustomName/TypeParametrizedTestWithNames/parChar0");
320 }
321 if (std::is_same<TypeParam, int>::value) {
322 EXPECT_STREQ(::testing::UnitTest::GetInstance()
323 ->current_test_info()
324 ->test_suite_name(),
325 "CustomName/TypeParametrizedTestWithNames/parInt1");
326 }
327 }
328
329 REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
330
331 class TypeParametrizedTestNames {
332 public:
333 template <typename T>
334 static std::string GetName(int i) {
335 if (std::is_same<T, char>::value) {
336 return std::string("parChar") + ::testing::PrintToString(i);
337 }
338 if (std::is_same<T, int>::value) {
339 return std::string("parInt") + ::testing::PrintToString(i);
340 }
341 }
342 };
343
344 INSTANTIATE_TYPED_TEST_SUITE_P(CustomName, TypeParametrizedTestWithNames,
345 TwoTypes, TypeParametrizedTestNames);
346
347 // Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
348 // translation unit.
349
350 template <typename T>
351 class TypedTestP1 : public Test {
352 };
353
354 TYPED_TEST_SUITE_P(TypedTestP1);
355
356 // For testing that the code between TYPED_TEST_SUITE_P() and
357 // TYPED_TEST_P() is not enclosed in a namespace.
358 using IntAfterTypedTestSuiteP = int;
359
360 TYPED_TEST_P(TypedTestP1, A) {}
361 TYPED_TEST_P(TypedTestP1, B) {}
362
363 // For testing that the code between TYPED_TEST_P() and
364 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
365 using IntBeforeRegisterTypedTestSuiteP = int;
366
367 REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
368
369 template <typename T>
370 class TypedTestP2 : public Test {
371 };
372
373 TYPED_TEST_SUITE_P(TypedTestP2);
374
375 // This also verifies that tests from different type-parameterized
376 // test cases can share the same name.
377 TYPED_TEST_P(TypedTestP2, A) {}
378
379 REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
380
381 // Verifies that the code between TYPED_TEST_SUITE_P() and
382 // REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
383 IntAfterTypedTestSuiteP after = 0;
384 IntBeforeRegisterTypedTestSuiteP before = 0;
385
386 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
387 // can be either a single type or a Types<...> type list.
388 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
389 INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP2, Types<int>);
390
391 // Tests that the same type-parameterized test case can be
392 // instantiated more than once in the same translation unit.
393 INSTANTIATE_TYPED_TEST_SUITE_P(Double, TypedTestP2, Types<double>);
394
395 // Tests that the same type-parameterized test case can be
396 // instantiated in different translation units linked together.
397 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
398 typedef Types<std::vector<double>, std::set<char> > MyContainers;
399 INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers);
400
401 // Tests that a type-parameterized test case can be defined and
402 // instantiated in a namespace.
403
404 namespace library2 {
405
406 template <typename T>
407 class NumericTest : public Test {
408 };
409
410 TYPED_TEST_SUITE_P(NumericTest);
411
412 TYPED_TEST_P(NumericTest, DefaultIsZero) {
413 EXPECT_EQ(0, TypeParam());
414 }
415
416 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
417 EXPECT_LT(TypeParam(0), TypeParam(1));
418 }
419
420 REGISTER_TYPED_TEST_SUITE_P(NumericTest,
421 DefaultIsZero, ZeroIsLessThanOne);
422 typedef Types<int, double> NumericTypes;
423 INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
424
425 static const char* GetTestName() {
426 return testing::UnitTest::GetInstance()->current_test_info()->name();
427 }
428 // Test the stripping of space from test names
429 template <typename T> class TrimmedTest : public Test { };
430 TYPED_TEST_SUITE_P(TrimmedTest);
431 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
432 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
433 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
434 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
435 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
436 REGISTER_TYPED_TEST_SUITE_P(
437 TrimmedTest,
438 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
439 template <typename T1, typename T2> struct MyPair {};
440 // Be sure to try a type with a comma in its name just in case it matters.
441 typedef Types<int, double, MyPair<int, int> > TrimTypes;
442 INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes);
443
444 } // namespace library2
445
446 #endif // GTEST_HAS_TYPED_TEST_P
447
448 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
449
450 // Google Test may not support type-parameterized tests with some
451 // compilers. If we use conditional compilation to compile out all
452 // code referring to the gtest_main library, MSVC linker will not link
453 // that library at all and consequently complain about missing entry
454 // point defined in that library (fatal error LNK1561: entry point
455 // must be defined). This dummy test keeps gtest_main linked in.
456 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
457
458 #if _MSC_VER
459 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
460 #endif // _MSC_VER
461
462 #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)