]> git.proxmox.com Git - ceph.git/blame - ceph/src/googletest/googletest/test/gtest-typed-test_test.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / googletest / googletest / test / gtest-typed-test_test.cc
CommitLineData
7c673cae
FG
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.
9f95a23c 29
7c673cae
FG
30
31#include "test/gtest-typed-test_test.h"
32
33#include <set>
9f95a23c 34#include <type_traits>
7c673cae
FG
35#include <vector>
36
37#include "gtest/gtest.h"
38
9f95a23c
TL
39#if _MSC_VER
40GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
41#endif // _MSC_VER
42
7c673cae
FG
43using testing::Test;
44
9f95a23c 45// Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture
7c673cae
FG
46// ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
47// type-parameterized test.
48template <typename T>
49class CommonTest : public Test {
9f95a23c 50 // For some technical reason, SetUpTestSuite() and TearDownTestSuite()
7c673cae
FG
51 // must be public.
52 public:
9f95a23c 53 static void SetUpTestSuite() {
7c673cae
FG
54 shared_ = new T(5);
55 }
56
9f95a23c 57 static void TearDownTestSuite() {
7c673cae 58 delete shared_;
9f95a23c 59 shared_ = nullptr;
7c673cae
FG
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
9f95a23c 72 ~CommonTest() override { EXPECT_EQ(3, value_); }
7c673cae 73
9f95a23c 74 void SetUp() override {
7c673cae
FG
75 EXPECT_EQ(1, value_);
76 value_++;
77 }
78
9f95a23c 79 void TearDown() override {
7c673cae
FG
80 EXPECT_EQ(2, value_);
81 value_++;
82 }
83
84 T value_;
85 static T* shared_;
86};
87
88template <typename T>
9f95a23c 89T* CommonTest<T>::shared_ = nullptr;
7c673cae
FG
90
91// This #ifdef block tests typed tests.
92#if GTEST_HAS_TYPED_TEST
93
94using testing::Types;
95
9f95a23c 96// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
7c673cae
FG
97// and SetUp()/TearDown() work correctly in typed tests
98
99typedef Types<char, int> TwoTypes;
9f95a23c 100TYPED_TEST_SUITE(CommonTest, TwoTypes);
7c673cae
FG
101
102TYPED_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.
122TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
123 // Static members of the fixture class template can also be visited
124 // via 'this'.
9f95a23c 125 ASSERT_TRUE(this->shared_ != nullptr);
7c673cae
FG
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
9f95a23c 132// Tests that multiple TYPED_TEST_SUITE's can be defined in the same
7c673cae
FG
133// translation unit.
134
135template <typename T>
136class TypedTest1 : public Test {
137};
138
9f95a23c 139// Verifies that the second argument of TYPED_TEST_SUITE can be a
7c673cae 140// single type.
9f95a23c 141TYPED_TEST_SUITE(TypedTest1, int);
7c673cae
FG
142TYPED_TEST(TypedTest1, A) {}
143
144template <typename T>
145class TypedTest2 : public Test {
146};
147
9f95a23c 148// Verifies that the second argument of TYPED_TEST_SUITE can be a
7c673cae 149// Types<...> type list.
9f95a23c 150TYPED_TEST_SUITE(TypedTest2, Types<int>);
7c673cae
FG
151
152// This also verifies that tests from different typed test cases can
153// share the same name.
154TYPED_TEST(TypedTest2, A) {}
155
156// Tests that a typed test case can be defined in a namespace.
157
158namespace library1 {
159
160template <typename T>
161class NumericTest : public Test {
162};
163
164typedef Types<int, long> NumericTypes;
9f95a23c 165TYPED_TEST_SUITE(NumericTest, NumericTypes);
7c673cae
FG
166
167TYPED_TEST(NumericTest, DefaultIsZero) {
168 EXPECT_EQ(0, TypeParam());
169}
170
171} // namespace library1
172
9f95a23c
TL
173// Tests that custom names work.
174template <typename T>
175class TypedTestWithNames : public Test {};
176
177class 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
190TYPED_TEST_SUITE(TypedTestWithNames, TwoTypes, TypedTestNames);
191
192TYPED_TEST(TypedTestWithNames, TestSuiteName) {
193 if (std::is_same<TypeParam, char>::value) {
194 EXPECT_STREQ(::testing::UnitTest::GetInstance()
195 ->current_test_info()
196 ->test_case_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_case_name(),
203 "TypedTestWithNames/int1");
204 }
205}
206
7c673cae
FG
207#endif // GTEST_HAS_TYPED_TEST
208
209// This #ifdef block tests type-parameterized tests.
210#if GTEST_HAS_TYPED_TEST_P
211
212using testing::Types;
9f95a23c 213using testing::internal::TypedTestSuitePState;
7c673cae 214
9f95a23c 215// Tests TypedTestSuitePState.
7c673cae 216
9f95a23c 217class TypedTestSuitePStateTest : public Test {
7c673cae 218 protected:
9f95a23c 219 void SetUp() override {
7c673cae
FG
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
9f95a23c 225 TypedTestSuitePState state_;
7c673cae
FG
226};
227
9f95a23c 228TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
7c673cae
FG
229 const char* tests = "A, B, C";
230 EXPECT_EQ(tests,
9f95a23c 231 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
7c673cae
FG
232}
233
234// Makes sure that the order of the tests and spaces around the names
235// don't matter.
9f95a23c 236TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
7c673cae
FG
237 const char* tests = "A,C, B";
238 EXPECT_EQ(tests,
9f95a23c 239 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
7c673cae
FG
240}
241
9f95a23c 242using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
7c673cae 243
9f95a23c 244TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
7c673cae 245 EXPECT_DEATH_IF_SUPPORTED(
9f95a23c 246 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
7c673cae
FG
247 "foo\\.cc.1.?: Test A is listed more than once\\.");
248}
249
9f95a23c 250TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
7c673cae 251 EXPECT_DEATH_IF_SUPPORTED(
9f95a23c
TL
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\\.");
7c673cae
FG
254}
255
9f95a23c 256TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
7c673cae 257 EXPECT_DEATH_IF_SUPPORTED(
9f95a23c 258 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, C"),
7c673cae
FG
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.
9f95a23c
TL
264TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
265 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
7c673cae
FG
266 EXPECT_DEATH_IF_SUPPORTED(
267 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
9f95a23c 268 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
7c673cae
FG
269 "\\(FooTest, \\.\\.\\.\\)\\.");
270}
271
9f95a23c 272// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
7c673cae
FG
273// and SetUp()/TearDown() work correctly in type-parameterized tests.
274
275template <typename T>
276class DerivedTest : public CommonTest<T> {
277};
278
9f95a23c 279TYPED_TEST_SUITE_P(DerivedTest);
7c673cae
FG
280
281TYPED_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.
293TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
294 // Static members of the fixture class template can also be visited
295 // via 'this'.
9f95a23c 296 ASSERT_TRUE(this->shared_ != nullptr);
7c673cae
FG
297 EXPECT_EQ(5, *this->shared_);
298 EXPECT_EQ(2, this->value_);
299}
300
9f95a23c 301REGISTER_TYPED_TEST_SUITE_P(DerivedTest,
7c673cae
FG
302 ValuesAreCorrect, ValuesAreStillCorrect);
303
304typedef Types<short, long> MyTwoTypes;
9f95a23c 305INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes);
7c673cae 306
9f95a23c
TL
307// Tests that custom names work with type parametrized tests. We reuse the
308// TwoTypes from above here.
309template <typename T>
310class TypeParametrizedTestWithNames : public Test {};
311
312TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames);
313
314TYPED_TEST_P(TypeParametrizedTestWithNames, TestSuiteName) {
315 if (std::is_same<TypeParam, char>::value) {
316 EXPECT_STREQ(::testing::UnitTest::GetInstance()
317 ->current_test_info()
318 ->test_case_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_case_name(),
325 "CustomName/TypeParametrizedTestWithNames/parInt1");
326 }
327}
328
329REGISTER_TYPED_TEST_SUITE_P(TypeParametrizedTestWithNames, TestSuiteName);
330
331class 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
344INSTANTIATE_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
7c673cae
FG
348// translation unit.
349
350template <typename T>
351class TypedTestP1 : public Test {
352};
353
9f95a23c 354TYPED_TEST_SUITE_P(TypedTestP1);
7c673cae 355
9f95a23c 356// For testing that the code between TYPED_TEST_SUITE_P() and
7c673cae 357// TYPED_TEST_P() is not enclosed in a namespace.
9f95a23c 358using IntAfterTypedTestSuiteP = int;
7c673cae
FG
359
360TYPED_TEST_P(TypedTestP1, A) {}
361TYPED_TEST_P(TypedTestP1, B) {}
362
363// For testing that the code between TYPED_TEST_P() and
9f95a23c
TL
364// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
365using IntBeforeRegisterTypedTestSuiteP = int;
7c673cae 366
9f95a23c 367REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
7c673cae
FG
368
369template <typename T>
370class TypedTestP2 : public Test {
371};
372
9f95a23c 373TYPED_TEST_SUITE_P(TypedTestP2);
7c673cae
FG
374
375// This also verifies that tests from different type-parameterized
376// test cases can share the same name.
377TYPED_TEST_P(TypedTestP2, A) {}
378
9f95a23c 379REGISTER_TYPED_TEST_SUITE_P(TypedTestP2, A);
7c673cae 380
9f95a23c
TL
381// Verifies that the code between TYPED_TEST_SUITE_P() and
382// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
383IntAfterTypedTestSuiteP after = 0;
384IntBeforeRegisterTypedTestSuiteP before = 0;
7c673cae 385
9f95a23c 386// Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
7c673cae 387// can be either a single type or a Types<...> type list.
9f95a23c
TL
388INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
389INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP2, Types<int>);
7c673cae
FG
390
391// Tests that the same type-parameterized test case can be
392// instantiated more than once in the same translation unit.
9f95a23c 393INSTANTIATE_TYPED_TEST_SUITE_P(Double, TypedTestP2, Types<double>);
7c673cae
FG
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.)
398typedef Types<std::vector<double>, std::set<char> > MyContainers;
9f95a23c 399INSTANTIATE_TYPED_TEST_SUITE_P(My, ContainerTest, MyContainers);
7c673cae
FG
400
401// Tests that a type-parameterized test case can be defined and
402// instantiated in a namespace.
403
404namespace library2 {
405
406template <typename T>
407class NumericTest : public Test {
408};
409
9f95a23c 410TYPED_TEST_SUITE_P(NumericTest);
7c673cae
FG
411
412TYPED_TEST_P(NumericTest, DefaultIsZero) {
413 EXPECT_EQ(0, TypeParam());
414}
415
416TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
417 EXPECT_LT(TypeParam(0), TypeParam(1));
418}
419
9f95a23c 420REGISTER_TYPED_TEST_SUITE_P(NumericTest,
7c673cae
FG
421 DefaultIsZero, ZeroIsLessThanOne);
422typedef Types<int, double> NumericTypes;
9f95a23c 423INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
7c673cae
FG
424
425static const char* GetTestName() {
426 return testing::UnitTest::GetInstance()->current_test_info()->name();
427}
428// Test the stripping of space from test names
429template <typename T> class TrimmedTest : public Test { };
9f95a23c 430TYPED_TEST_SUITE_P(TrimmedTest);
7c673cae
FG
431TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
432TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
433TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
434TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
435TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
9f95a23c 436REGISTER_TYPED_TEST_SUITE_P(
7c673cae
FG
437 TrimmedTest,
438 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
439template <typename T1, typename T2> struct MyPair {};
440// Be sure to try a type with a comma in its name just in case it matters.
441typedef Types<int, double, MyPair<int, int> > TrimTypes;
9f95a23c 442INSTANTIATE_TYPED_TEST_SUITE_P(My, TrimmedTest, TrimTypes);
7c673cae
FG
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.
456TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
457
9f95a23c
TL
458#if _MSC_VER
459GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
460#endif // _MSC_VER
461
7c673cae 462#endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)