tesseract v5.3.3.20231005
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
91using testing::Types;
92
93// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
94// and SetUp()/TearDown() work correctly in typed tests
95
96typedef Types<char, int> TwoTypes;
98
99TYPED_TEST(CommonTest, ValuesAreCorrect) {
100 // Static members of the fixture class template can be visited via
101 // the TestFixture:: prefix.
102 EXPECT_EQ(5, *TestFixture::shared_);
103
104 // Typedefs in the fixture class template can be visited via the
105 // "typename TestFixture::" prefix.
106 typename TestFixture::Vector empty;
107 EXPECT_EQ(0U, empty.size());
108
109 typename TestFixture::IntSet empty2;
110 EXPECT_EQ(0U, empty2.size());
111
112 // Non-static members of the fixture class must be visited via
113 // 'this', as required by C++ for class templates.
114 EXPECT_EQ(2, this->value_);
115}
116
117// The second test makes sure shared_ is not deleted after the first
118// test.
119TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
120 // Static members of the fixture class template can also be visited
121 // via 'this'.
122 ASSERT_TRUE(this->shared_ != nullptr);
123 EXPECT_EQ(5, *this->shared_);
124
125 // TypeParam can be used to refer to the type parameter.
126 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
127}
128
129// Tests that multiple TYPED_TEST_SUITE's can be defined in the same
130// translation unit.
131
132template <typename T>
133class TypedTest1 : public Test {
134};
135
136// Verifies that the second argument of TYPED_TEST_SUITE can be a
137// single type.
140
141template <typename T>
142class TypedTest2 : public Test {
143};
144
145// Verifies that the second argument of TYPED_TEST_SUITE can be a
146// Types<...> type list.
148
149// This also verifies that tests from different typed test cases can
150// share the same name.
152
153// Tests that a typed test case can be defined in a namespace.
154
155namespace library1 {
156
157template <typename T>
158class NumericTest : public Test {
159};
160
161typedef Types<int, long> NumericTypes;
163
164TYPED_TEST(NumericTest, DefaultIsZero) {
165 EXPECT_EQ(0, TypeParam());
166}
167
168} // namespace library1
169
170// Tests that custom names work.
171template <typename T>
172class TypedTestWithNames : public Test {};
173
174class TypedTestNames {
175 public:
176 template <typename T>
177 static std::string GetName(int i) {
179 return std::string("char") + ::testing::PrintToString(i);
180 }
182 return std::string("int") + ::testing::PrintToString(i);
183 }
184 }
185};
186
188
192 ->current_test_info()
193 ->test_suite_name(),
194 "TypedTestWithNames/char0");
195 }
198 ->current_test_info()
199 ->test_suite_name(),
200 "TypedTestWithNames/int1");
201 }
202}
203
204using testing::Types;
205using testing::internal::TypedTestSuitePState;
206
207// Tests TypedTestSuitePState.
208
210 protected:
211 void SetUp() override {
212 state_.AddTestName("foo.cc", 0, "FooTest", "A");
213 state_.AddTestName("foo.cc", 0, "FooTest", "B");
214 state_.AddTestName("foo.cc", 0, "FooTest", "C");
215 }
216
217 TypedTestSuitePState state_;
218};
219
220TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList) {
221 const char* tests = "A, B, C";
222 EXPECT_EQ(tests,
223 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
224}
225
226// Makes sure that the order of the tests and spaces around the names
227// don't matter.
228TEST_F(TypedTestSuitePStateTest, IgnoresOrderAndSpaces) {
229 const char* tests = "A,C, B";
230 EXPECT_EQ(tests,
231 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, tests));
232}
233
235
238 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, A, C"),
239 "foo\\.cc.1.?: Test A is listed more than once\\.");
240}
241
244 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C, D"),
245 "foo\\.cc.1.?: No test named D can be found in this test suite\\.");
246}
247
250 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, C"),
251 "foo\\.cc.1.?: You forgot to list test B\\.");
252}
253
254// Tests that defining a test for a parameterized test case generates
255// a run-time error if the test case has been registered.
256TEST_F(TypedTestSuitePStateDeathTest, DetectsTestAfterRegistration) {
257 state_.VerifyRegisteredTestNames("Suite", "foo.cc", 1, "A, B, C");
259 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
260 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_SUITE_P"
261 "\\(FooTest, \\.\\.\\.\\)\\.");
262}
263
264// Tests that SetUpTestSuite()/TearDownTestSuite(), fixture ctor/dtor,
265// and SetUp()/TearDown() work correctly in type-parameterized tests.
266
267template <typename T>
268class DerivedTest : public CommonTest<T> {
269};
270
272
273TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
274 // Static members of the fixture class template can be visited via
275 // the TestFixture:: prefix.
276 EXPECT_EQ(5, *TestFixture::shared_);
277
278 // Non-static members of the fixture class must be visited via
279 // 'this', as required by C++ for class templates.
280 EXPECT_EQ(2, this->value_);
281}
282
283// The second test makes sure shared_ is not deleted after the first
284// test.
285TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
286 // Static members of the fixture class template can also be visited
287 // via 'this'.
288 ASSERT_TRUE(this->shared_ != nullptr);
289 EXPECT_EQ(5, *this->shared_);
290 EXPECT_EQ(2, this->value_);
291}
292
294 ValuesAreCorrect, ValuesAreStillCorrect);
295
296typedef Types<short, long> MyTwoTypes;
298
299// Tests that custom names work with type parametrized tests. We reuse the
300// TwoTypes from above here.
301template <typename T>
303
305
309 ->current_test_info()
310 ->test_suite_name(),
311 "CustomName/TypeParametrizedTestWithNames/parChar0");
312 }
315 ->current_test_info()
316 ->test_suite_name(),
317 "CustomName/TypeParametrizedTestWithNames/parInt1");
318 }
319}
320
322
324 public:
325 template <typename T>
326 static std::string GetName(int i) {
328 return std::string("parChar") + ::testing::PrintToString(i);
329 }
331 return std::string("parInt") + ::testing::PrintToString(i);
332 }
333 }
334};
335
338
339// Tests that multiple TYPED_TEST_SUITE_P's can be defined in the same
340// translation unit.
341
342template <typename T>
343class TypedTestP1 : public Test {
344};
345
347
348// For testing that the code between TYPED_TEST_SUITE_P() and
349// TYPED_TEST_P() is not enclosed in a namespace.
351
354
355// For testing that the code between TYPED_TEST_P() and
356// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
358
360
361template <typename T>
362class TypedTestP2 : public Test {
363};
364
366
367// This also verifies that tests from different type-parameterized
368// test cases can share the same name.
370
372
373// Verifies that the code between TYPED_TEST_SUITE_P() and
374// REGISTER_TYPED_TEST_SUITE_P() is not enclosed in a namespace.
377
378// Verifies that the last argument of INSTANTIATE_TYPED_TEST_SUITE_P()
379// can be either a single type or a Types<...> type list.
382
383// Tests that the same type-parameterized test case can be
384// instantiated more than once in the same translation unit.
386
387// Tests that the same type-parameterized test case can be
388// instantiated in different translation units linked together.
389// (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
390typedef Types<std::vector<double>, std::set<char> > MyContainers;
392
393// Tests that a type-parameterized test case can be defined and
394// instantiated in a namespace.
395
396namespace library2 {
397
398template <typename T>
399class NumericTest : public Test {
400};
401
403
404TYPED_TEST_P(NumericTest, DefaultIsZero) {
405 EXPECT_EQ(0, TypeParam());
406}
407
408TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
409 EXPECT_LT(TypeParam(0), TypeParam(1));
410}
411
413 DefaultIsZero, ZeroIsLessThanOne);
414typedef Types<int, double> NumericTypes;
416
417static const char* GetTestName() {
419}
420// Test the stripping of space from test names
421template <typename T> class TrimmedTest : public Test { };
423TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
424TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
425TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
426TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
427TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
430 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
431template <typename T1, typename T2> struct MyPair {};
432// Be sure to try a type with a comma in its name just in case it matters.
433typedef Types<int, double, MyPair<int, int> > TrimTypes;
435
436} // namespace library2
437
int value
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2112
#define ASSERT_TRUE(condition)
Definition: gtest.h:1990
#define EXPECT_LT(val1, val2)
Definition: gtest.h:2049
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:323
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
int IntAfterTypedTestSuiteP
REGISTER_TYPED_TEST_SUITE_P(DerivedTest, ValuesAreCorrect, ValuesAreStillCorrect)
TYPED_TEST_P(DerivedTest, ValuesAreCorrect)
int IntBeforeRegisterTypedTestSuiteP
INSTANTIATE_TYPED_TEST_SUITE_P(My, DerivedTest, MyTwoTypes)
Types< std::vector< double >, std::set< char > > MyContainers
TEST_F(TypedTestSuitePStateTest, SucceedsForMatchingList)
TYPED_TEST_SUITE(CommonTest, TwoTypes)
IntAfterTypedTestSuiteP after
Types< char, int > TwoTypes
TYPED_TEST(CommonTest, ValuesAreCorrect)
TYPED_TEST_SUITE_P(DerivedTest)
IntBeforeRegisterTypedTestSuiteP before
Types< short, long > MyTwoTypes
::std::string PrintToString(const T &value)
FloatingPoint< double > Double
Types< int, long > NumericTypes
TYPED_TEST(NumericTest, DefaultIsZero)
TYPED_TEST_SUITE(NumericTest, NumericTypes)
TYPED_TEST_SUITE_P(NumericTest)
Types< int, double, MyPair< int, int > > TrimTypes
INSTANTIATE_TYPED_TEST_SUITE_P(My, NumericTest, NumericTypes)
Types< int, double > NumericTypes
REGISTER_TYPED_TEST_SUITE_P(NumericTest, DefaultIsZero, ZeroIsLessThanOne)
TYPED_TEST_P(NumericTest, DefaultIsZero)
const char * name() const
Definition: gtest.h:719
const TestInfo * current_test_info() const GTEST_LOCK_EXCLUDED_(mutex_)
Definition: gtest.cc:5469
static UnitTest * GetInstance()
Definition: gtest.cc:5123
static std::string GetName(int i)
static void TearDownTestSuite()
static void SetUpTestSuite()
void SetUp() override
void TearDown() override
std::set< int > IntSet
std::vector< T > Vector
~CommonTest() override
static std::string GetName(int i)