Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
gtest-typed-test_test.cc
Go to the documentation of this file.
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
32
33#include <set>
34#include <type_traits>
35#include <vector>
36
37#include "gtest/gtest.h"
38
39#if _MSC_VER
40GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
41#endif // _MSC_VER
42
43using 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.
48template <typename T>
49class 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
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
85 static T* shared_;
86};
87
88template <typename T>
89T* CommonTest<T>::shared_ = nullptr;
90
91// This #ifdef block tests typed tests.
92#if GTEST_HAS_TYPED_TEST
93
94using testing::Types;
95
96// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
97// and SetUp()/TearDown() work correctly in typed tests
98
99typedef Types<char, int> TwoTypes;
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'.
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
135template <typename T>
136class TypedTest1 : public Test {
137};
138
139// Verifies that the second argument of TYPED_TEST_SUITE can be a
140// single type.
141TYPED_TEST_SUITE(TypedTest1, int);
142TYPED_TEST(TypedTest1, A) {}
143
144template <typename T>
145class TypedTest2 : public Test {
146};
147
148// Verifies that the second argument of TYPED_TEST_SUITE can be a
149// Types<...> type list.
150TYPED_TEST_SUITE(TypedTest2, Types<int>);
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;
165TYPED_TEST_SUITE(NumericTest, NumericTypes);
166
167TYPED_TEST(NumericTest, DefaultIsZero) {
168 EXPECT_EQ(0, TypeParam());
169}
170
171} // namespace library1
172
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) {
195 ->current_test_info()
196 ->test_case_name(),
197 "TypedTestWithNames/char0");
198 }
199 if (std::is_same<TypeParam, int>::value) {
201 ->current_test_info()
202 ->test_case_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
212using testing::Types;
213using testing::internal::TypedTestSuitePState;
214
215// Tests TypedTestSuitePState.
216
217class 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
228TEST_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.
236TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
237 const char* tests = "A,C, B";
238 EXPECT_EQ(tests,
239 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
240}
241
242using TypedTestSuitePStateDeathTest = TypedTestSuitePStateTest;
243
244TEST_F(TypedTestSuitePStateDeathTest, DetectsDuplicates) {
246 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
247 "foo\\.cc.1.?: Test A is listed more than once\\.");
248}
249
250TEST_F(TypedTestSuitePStateDeathTest, DetectsExtraTest) {
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
256TEST_F(TypedTestSuitePStateDeathTest, DetectsMissedTest) {
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.
264TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
265 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
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
275template <typename T>
276class DerivedTest : public CommonTest<T> {
277};
278
279TYPED_TEST_SUITE_P(DerivedTest);
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'.
296 ASSERT_TRUE(this->shared_ != nullptr);
297 EXPECT_EQ(5, *this->shared_);
298 EXPECT_EQ(2, this->value_);
299}
300
302 ValuesAreCorrect, ValuesAreStillCorrect);
303
304typedef Types<short, long> MyTwoTypes;
305INSTANTIATE_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.
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) {
317 ->current_test_info()
318 ->test_case_name(),
319 "CustomName/TypeParametrizedTestWithNames/parChar0");
320 }
321 if (std::is_same<TypeParam, int>::value) {
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
348// translation unit.
349
350template <typename T>
351class TypedTestP1 : public Test {
352};
353
354TYPED_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.
358using IntAfterTypedTestSuiteP = int;
359
360TYPED_TEST_P(TypedTestP1, A) {}
361TYPED_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.
365using IntBeforeRegisterTypedTestSuiteP = int;
366
367REGISTER_TYPED_TEST_SUITE_P(TypedTestP1, A, B);
368
369template <typename T>
370class TypedTestP2 : public Test {
371};
372
373TYPED_TEST_SUITE_P(TypedTestP2);
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
379REGISTER_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.
383IntAfterTypedTestSuiteP after = 0;
384IntBeforeRegisterTypedTestSuiteP 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.
388INSTANTIATE_TYPED_TEST_SUITE_P(Int, TypedTestP1, int);
389INSTANTIATE_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.
393INSTANTIATE_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.)
398typedef Types<std::vector<double>, std::set<char> > MyContainers;
399INSTANTIATE_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
404namespace library2 {
405
406template <typename T>
407class NumericTest : public Test {
408};
409
410TYPED_TEST_SUITE_P(NumericTest);
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
421 DefaultIsZero, ZeroIsLessThanOne);
422typedef Types<int, double> NumericTypes;
423INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes);
424
425static const char* GetTestName() {
427}
428// Test the stripping of space from test names
429template <typename T> class TrimmedTest : public Test { };
430TYPED_TEST_SUITE_P(TrimmedTest);
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()); }
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;
442INSTANTIATE_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.
456TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
457
458#if _MSC_VER
460#endif // _MSC_VER
461
462#endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
INSTANTIATE_TYPED_TEST_SUITE_P(CacheFad, FadOpsUnitTest, FadTypes)
TYPED_TEST_P(FadBLASUnitTests, testSCAL1)
TYPED_TEST_SUITE_P(FadBLASUnitTests)
REGISTER_TYPED_TEST_SUITE_P(FadBLASUnitTests, testSCAL1, testSCAL2, testSCAL3, testSCAL4, testCOPY1, testCOPY2, testCOPY3, testCOPY4, testAXPY1, testAXPY2, testAXPY3, testAXPY4, testDOT1, testDOT2, testDOT3, testDOT4, testNRM21, testNRM22, testGEMV1, testGEMV2, testGEMV3, testGEMV4, testGEMV5, testGEMV6, testGEMV7, testGEMV8, testGEMV9, testTRMV1, testTRMV2, testTRMV3, testTRMV4, testGER1, testGER2, testGER3, testGER4, testGER5, testGER6, testGER7, testGEMM1, testGEMM2, testGEMM3, testGEMM4, testGEMM5, testGEMM6, testGEMM7, testGEMM8, testGEMM9, testGEMM10, testSYMM1, testSYMM2, testSYMM3, testSYMM4, testSYMM5, testSYMM6, testSYMM7, testSYMM8, testSYMM9, testTRMM1, testTRMM2, testTRMM3, testTRMM4, testTRMM5, testTRMM6, testTRMM7, testTRSM1, testTRSM2, testTRSM3, testTRSM4, testTRSM5, testTRSM6, testTRSM7)
#define T
Definition: Sacado_rad.hpp:573
#define A
Definition: Sacado_rad.hpp:572
static void TearDownTestSuite()
static void SetUpTestSuite()
void SetUp() override
void TearDown() override
std::set< int > IntSet
std::vector< T > Vector
~CommonTest() override
const char * name() const
Definition: gtest.h:718
virtual void SetUp()
Definition: gtest.cc:2439
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
Definition: gtest.cc:5344
static UnitTest * GetInstance()
Definition: gtest.cc:4998
int value_
TYPED_TEST_SUITE(TypedTest, MyTypes)
TYPED_TEST(TypedTest, TestA)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:323
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:324
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2379
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2038
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2348
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2107
#define ASSERT_TRUE(condition)
Definition: gtest.h:1985
#define EXPECT_LT(val1, val2)
Definition: gtest.h:2044
internal::ProxyTypeList< Ts... > Types
::std::string PrintToString(const T &value)