cMHN 1.2
C++ library for learning MHNs with pRC
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
31
32#include <set>
33#include <string>
34#include <type_traits>
35#include <vector>
36
37#include "gtest/gtest.h"
38
39GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
40
41using testing::Test;
42
43// Used for testing that SetUpTestSuite()/TearDownTestSuite(), fixture
44// ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
45// type-parameterized test.
46template <typename T>
47class CommonTest : public Test {
48 // For some technical reason, SetUpTestSuite() and TearDownTestSuite()
49 // must be public.
50 public:
51 static void SetUpTestSuite() { shared_ = new T(5); }
52
53 static void TearDownTestSuite() {
54 delete shared_;
55 shared_ = nullptr;
56 }
57
58 // This 'protected:' is optional. There's no harm in making all
59 // members of this fixture class template public.
60 protected:
61 // We used to use std::list here, but switched to std::vector since
62 // MSVC's <list> doesn't compile cleanly with /W4.
63 typedef std::vector<T> Vector;
64 typedef std::set<int> IntSet;
65
66 CommonTest() : value_(1) {}
67
68 ~CommonTest() override { EXPECT_EQ(3, value_); }
69
70 void SetUp() override {
71 EXPECT_EQ(1, value_);
72 value_++;
73 }
74
75 void TearDown() override {
76 EXPECT_EQ(2, value_);
77 value_++;
78 }
79
81 static T* shared_;
82};
83
84template <typename T>
85T* CommonTest<T>::shared_ = nullptr;
86
87using testing::Types;
88
89// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
90// and SetUp()/TearDown() work correctly in typed tests
91
92typedef Types<char, int> TwoTypes;
94
95TYPED_TEST(CommonTest, ValuesAreCorrect) {
96 // Static members of the fixture class template can be visited via
97 // the TestFixture:: prefix.
98 EXPECT_EQ(5, *TestFixture::shared_);
99
100 // Typedefs in the fixture class template can be visited via the
101 // "typename TestFixture::" prefix.
102 typename TestFixture::Vector empty;
103 EXPECT_EQ(0U, empty.size());
104
105 typename TestFixture::IntSet empty2;
106 EXPECT_EQ(0U, empty2.size());
107
108 // Non-static members of the fixture class must be visited via
109 // 'this', as required by C++ for class templates.
110 EXPECT_EQ(2, this->value_);
111}
112
113// The second test makes sure shared_ is not deleted after the first
114// test.
115TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
116 // Static members of the fixture class template can also be visited
117 // via 'this'.
118 ASSERT_TRUE(this->shared_ != nullptr);
119 EXPECT_EQ(5, *this->shared_);
120
121 // TypeParam can be used to refer to the type parameter.
122 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
123}
124
125// Tests that multiple TYPED_TEST_SUITE's can be defined in the same
126// translation unit.
127
128template <typename T>
129class TypedTest1 : public Test {};
130
131// Verifies that the second argument of TYPED_TEST_SUITE can be a
132// single type.
135
136template <typename T>
137class TypedTest2 : public Test {};
138
139// Verifies that the second argument of TYPED_TEST_SUITE can be a
140// Types<...> type list.
142
143// This also verifies that tests from different typed test cases can
144// share the same name.
146
147// Tests that a typed test case can be defined in a namespace.
148
149namespace library1 {
150
151template <typename T>
152class NumericTest : public Test {};
153
154typedef Types<int, long> NumericTypes;
156
157TYPED_TEST(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); }
158
159} // namespace library1
160
161// Tests that custom names work.
162template <typename T>
163class TypedTestWithNames : public Test {};
164
165class TypedTestNames {
166 public:
167 template <typename T>
168 static std::string GetName(int i) {
169 if (std::is_same<T, char>::value) {
170 return std::string("char") + ::testing::PrintToString(i);
171 }
172 if (std::is_same<T, int>::value) {
173 return std::string("int") + ::testing::PrintToString(i);
174 }
175 }
176};
177
179
181 if (std::is_same<TypeParam, char>::value) {
183 ->current_test_info()
184 ->test_suite_name(),
185 "TypedTestWithNames/char0");
186 }
187 if (std::is_same<TypeParam, int>::value) {
189 ->current_test_info()
190 ->test_suite_name(),
191 "TypedTestWithNames/int1");
192 }
193}
194
195using testing::Types;
196using testing::internal::TypedTestSuitePState;
197
198// Tests TypedTestSuitePState.
199
201 protected:
202 void SetUp() override {
203 state_.AddTestName("foo.cc", 0, "FooTest", "A");
204 state_.AddTestName("foo.cc", 0, "FooTest", "B");
205 state_.AddTestName("foo.cc", 0, "FooTest", "C");
206 }
207
208 TypedTestSuitePState state_;
209};
210
211TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
212 const char* tests = "A, B, C";
213 EXPECT_EQ(tests,
214 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
215}
216
217// Makes sure that the order of the tests and spaces around the names
218// don't matter.
219TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
220 const char* tests = "A,C, B";
221 EXPECT_EQ(tests,
222 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
223}
224
226
229 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
230 "foo\\.cc.1.?: Test A is listed more than once\\.");
231}
232
235 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C, D"),
236 "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
237}
238
241 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, C"),
242 "foo\\.cc.1.?: You forgot to list test B\\.");
243}
244
245// Tests that defining a test for a parameterized test case generates
246// a run-time error if the test case has been registered.
247TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
248 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
250 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
251 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
252 "\\(FooTest, \\.\\.\\.\\)\\.");
253}
254
255// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
256// and SetUp()/TearDown() work correctly in type-parameterized tests.
257
258template <typename T>
259class DerivedTest : public CommonTest<T> {};
260
262
263TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
264 // Static members of the fixture class template can be visited via
265 // the TestFixture:: prefix.
266 EXPECT_EQ(5, *TestFixture::shared_);
267
268 // Non-static members of the fixture class must be visited via
269 // 'this', as required by C++ for class templates.
270 EXPECT_EQ(2, this->value_);
271}
272
273// The second test makes sure shared_ is not deleted after the first
274// test.
275TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
276 // Static members of the fixture class template can also be visited
277 // via 'this'.
278 ASSERT_TRUE(this->shared_ != nullptr);
279 EXPECT_EQ(5, *this->shared_);
280 EXPECT_EQ(2, this->value_);
281}
282
284 ValuesAreStillCorrect);
285
286typedef Types<short, long> MyTwoTypes;
288
289// Tests that custom names work with type parametrized tests. We reuse the
290// TwoTypes from above here.
291template <typename T>
293
295
297 if (std::is_same<TypeParam, char>::value) {
299 ->current_test_info()
300 ->test_suite_name(),
301 "CustomName/TypeParametrizedTestWithNames/parChar0");
302 }
303 if (std::is_same<TypeParam, int>::value) {
305 ->current_test_info()
306 ->test_suite_name(),
307 "CustomName/TypeParametrizedTestWithNames/parInt1");
308 }
309}
310
312
314 public:
315 template <typename T>
316 static std::string GetName(int i) {
317 if (std::is_same<T, char>::value) {
318 return std::string("parChar") + ::testing::PrintToString(i);
319 }
320 if (std::is_same<T, int>::value) {
321 return std::string("parInt") + ::testing::PrintToString(i);
322 }
323 }
324};
325
328
329// Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
330// translation unit.
331
332template <typename T>
333class TypedTestP1 : public Test {};
334
336
337// For testing that the code between TYPED_TEST_SUITE_P() and
338// TYPED_TEST_P() is not enclosed in a namespace.
340
343
344// For testing that the code between TYPED_TEST_P() and
345// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
347
349
350template <typename T>
351class TypedTestP2 : public Test {};
352
354
355// This also verifies that tests from different type-parameterized
356// test cases can share the same name.
358
360
361// Verifies that the code between TYPED_TEST_SUITE_P() and
362// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
365
366// Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
367// can be either a single type or a Types<...> type list.
370
371// Tests that the same type-parameterized test case can be
372// instantiated more than once in the same translation unit.
374
375// Tests that the same type-parameterized test case can be
376// instantiated in different translation units linked together.
377// (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
378typedef Types<std::vector<double>, std::set<char> > MyContainers;
380
381// Tests that a type-parameterized test case can be defined and
382// instantiated in a namespace.
383
384namespace library2 {
385
386template <typename T>
387class NumericTest : public Test {};
388
390
391TYPED_TEST_P(NumericTest, DefaultIsZero) { EXPECT_EQ(0, TypeParam()); }
392
393TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
394 EXPECT_LT(TypeParam(0), TypeParam(1));
395}
396
397REGISTER_TYPED_TEST_SUITE_P(NumericTest, DefaultIsZero, ZeroIsLessThanOne);
398typedef Types<int, double> NumericTypes;
400
401static const char* GetTestName() {
403}
404// Test the stripping of space from test names
405template <typename T>
406class TrimmedTest : public Test {};
413REGISTER_TYPED_TEST_SUITE_P(TrimmedTest, Test1, Test2, Test3, Test4,
414 Test5); // NOLINT
415template <typename T1, typename T2>
416struct MyPair {};
417// Be sure to try a type with a comma in its name just in case it matters.
418typedef Types<int, double, MyPair<int, int> > TrimTypes;
420
421} // namespace library2
422
Definition gtest-typed-test_test.cc:47
static void TearDownTestSuite()
Definition gtest-typed-test_test.cc:53
static void SetUpTestSuite()
Definition gtest-typed-test_test.cc:51
void SetUp() override
Definition gtest-typed-test_test.cc:70
static T * shared_
Definition gtest-typed-test_test.cc:81
void TearDown() override
Definition gtest-typed-test_test.cc:75
std::set< int > IntSet
Definition gtest-typed-test_test.cc:64
std::vector< T > Vector
Definition gtest-typed-test_test.cc:63
CommonTest()
Definition gtest-typed-test_test.cc:66
T value_
Definition gtest-typed-test_test.cc:80
~CommonTest() override
Definition gtest-typed-test_test.cc:68
Definition gtest-typed-test_test.h:43
Definition gtest-typed-test_test.cc:259
Definition gtest-typed-test_test.cc:313
static std::string GetName(int i)
Definition gtest-typed-test_test.cc:316
Definition gtest-typed-test_test.cc:292
Definition gtest-typed-test_test.cc:129
Definition gtest-typed-test_test.cc:137
Definition googletest-output-test_.cc:715
static std::string GetName(int i)
Definition gtest-typed-test_test.cc:168
Definition gtest-typed-test_test.cc:333
Definition gtest-typed-test_test.cc:351
Definition gtest-typed-test_test.cc:200
void SetUp() override
Definition gtest-typed-test_test.cc:202
TypedTestSuitePState state_
Definition gtest-typed-test_test.cc:208
Definition googletest-output-test_.cc:713
Definition gtest-typed-test_test.cc:152
Definition gtest-typed-test_test.cc:387
Definition gtest-typed-test_test.cc:406
Definition value.hpp:12
const char * name() const
Definition gtest.h:549
Definition gtest.h:243
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
Definition gtest.cc:5518
static UnitTest * GetInstance()
Definition gtest.cc:5156
pRC::Float<> T
Definition externs_nonTT.hpp:1
int i
Definition gmock-matchers-comparisons_test.cc:603
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Definition gtest-death-test.h:337
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition gtest-port.h:360
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition gtest-port.h:361
#define REGISTER_TYPED_TEST_SUITE_P(SuiteName,...)
Definition gtest-typed-test.h:289
#define TYPED_TEST_SUITE_P(SuiteName)
Definition gtest-typed-test.h:259
#define TYPED_TEST_P(SuiteName, TestName)
Definition gtest-typed-test.h:270
#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types,...)
Definition gtest-typed-test.h:306
#define TYPED_TEST(CaseName, TestName)
Definition gtest-typed-test.h:197
#define TYPED_TEST_SUITE(CaseName, Types,...)
Definition gtest-typed-test.h:191
int IntAfterTypedTestSuiteP
Definition gtest-typed-test_test.cc:339
int IntBeforeRegisterTypedTestSuiteP
Definition gtest-typed-test_test.cc:346
Types< std::vector< double >, std::set< char > > MyContainers
Definition gtest-typed-test_test.cc:378
IntAfterTypedTestSuiteP after
Definition gtest-typed-test_test.cc:363
Types< char, int > TwoTypes
Definition gtest-typed-test_test.cc:92
IntBeforeRegisterTypedTestSuiteP before
Definition gtest-typed-test_test.cc:364
Types< short, long > MyTwoTypes
Definition gtest-typed-test_test.cc:286
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2208
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1868
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:1937
#define ASSERT_TRUE(condition)
Definition gtest.h:1815
#define EXPECT_LT(val1, val2)
Definition gtest.h:1874
Definition gtest-typed-test_test.cc:149
Types< int, long > NumericTypes
Definition gtest-typed-test_test.cc:154
Definition gtest-typed-test_test.cc:384
Types< int, double, MyPair< int, int > > TrimTypes
Definition gtest-typed-test_test.cc:418
Types< int, double > NumericTypes
Definition gtest-typed-test_test.cc:398
static const char * GetTestName()
Definition gtest-typed-test_test.cc:401
Definition gmock-actions.h:151
::std::string PrintToString(const T &value)
Definition gtest-printers.h:1148
Definition gtest-typed-test_test.cc:416
Definition gtest-type-util.h:190