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