1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Tests for Google Test itself. This file verifies that the parameter
32 // generators objects produce correct parameter sequences and that
33 // Google Test runtime instantiates correct tests from those sequences.
35 #include "gtest/gtest.h"
44 # include "src/gtest-internal-inl.h" // for UnitTestOptions
45 # include "test/googletest-param-test-test.h"
50 using ::testing::AddGlobalTestEnvironment;
51 using ::testing::Bool;
52 using ::testing::Message;
53 using ::testing::Range;
54 using ::testing::TestWithParam;
55 using ::testing::Values;
56 using ::testing::ValuesIn;
58 # if GTEST_HAS_COMBINE
59 using ::testing::Combine;
61 using ::testing::make_tuple;
62 using ::testing::tuple;
63 # endif // GTEST_HAS_COMBINE
65 using ::testing::internal::ParamGenerator;
66 using ::testing::internal::UnitTestOptions;
68 // Prints a value to a string.
70 // FIXME: remove PrintValue() when we move matchers and
71 // EXPECT_THAT() from Google Mock to Google Test. At that time, we
72 // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
73 // EXPECT_THAT() and the matchers know how to print tuples.
75 ::std::string PrintValue(const T& value) {
76 ::std::stringstream stream;
81 # if GTEST_HAS_COMBINE
83 // These overloads allow printing tuples in our tests. We cannot
84 // define an operator<< for tuples, as that definition needs to be in
85 // the std namespace in order to be picked up by Google Test via
86 // Argument-Dependent Lookup, yet defining anything in the std
87 // namespace in non-STL code is undefined behavior.
89 template <typename T1, typename T2>
90 ::std::string PrintValue(const tuple<T1, T2>& value) {
91 ::std::stringstream stream;
92 stream << "(" << get<0>(value) << ", " << get<1>(value) << ")";
96 template <typename T1, typename T2, typename T3>
97 ::std::string PrintValue(const tuple<T1, T2, T3>& value) {
98 ::std::stringstream stream;
99 stream << "(" << get<0>(value) << ", " << get<1>(value)
100 << ", "<< get<2>(value) << ")";
104 template <typename T1, typename T2, typename T3, typename T4, typename T5,
105 typename T6, typename T7, typename T8, typename T9, typename T10>
106 ::std::string PrintValue(
107 const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) {
108 ::std::stringstream stream;
109 stream << "(" << get<0>(value) << ", " << get<1>(value)
110 << ", "<< get<2>(value) << ", " << get<3>(value)
111 << ", "<< get<4>(value) << ", " << get<5>(value)
112 << ", "<< get<6>(value) << ", " << get<7>(value)
113 << ", "<< get<8>(value) << ", " << get<9>(value) << ")";
117 # endif // GTEST_HAS_COMBINE
119 // Verifies that a sequence generated by the generator and accessed
120 // via the iterator object matches the expected one using Google Test
122 template <typename T, size_t N>
123 void VerifyGenerator(const ParamGenerator<T>& generator,
124 const T (&expected_values)[N]) {
125 typename ParamGenerator<T>::iterator it = generator.begin();
126 for (size_t i = 0; i < N; ++i) {
127 ASSERT_FALSE(it == generator.end())
128 << "At element " << i << " when accessing via an iterator "
129 << "created with the copy constructor.\n";
130 // We cannot use EXPECT_EQ() here as the values may be tuples,
131 // which don't support <<.
132 EXPECT_TRUE(expected_values[i] == *it)
133 << "where i is " << i
134 << ", expected_values[i] is " << PrintValue(expected_values[i])
135 << ", *it is " << PrintValue(*it)
136 << ", and 'it' is an iterator created with the copy constructor.\n";
139 EXPECT_TRUE(it == generator.end())
140 << "At the presumed end of sequence when accessing via an iterator "
141 << "created with the copy constructor.\n";
143 // Test the iterator assignment. The following lines verify that
144 // the sequence accessed via an iterator initialized via the
145 // assignment operator (as opposed to a copy constructor) matches
147 it = generator.begin();
148 for (size_t i = 0; i < N; ++i) {
149 ASSERT_FALSE(it == generator.end())
150 << "At element " << i << " when accessing via an iterator "
151 << "created with the assignment operator.\n";
152 EXPECT_TRUE(expected_values[i] == *it)
153 << "where i is " << i
154 << ", expected_values[i] is " << PrintValue(expected_values[i])
155 << ", *it is " << PrintValue(*it)
156 << ", and 'it' is an iterator created with the copy constructor.\n";
159 EXPECT_TRUE(it == generator.end())
160 << "At the presumed end of sequence when accessing via an iterator "
161 << "created with the assignment operator.\n";
164 template <typename T>
165 void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
166 typename ParamGenerator<T>::iterator it = generator.begin();
167 EXPECT_TRUE(it == generator.end());
169 it = generator.begin();
170 EXPECT_TRUE(it == generator.end());
173 // Generator tests. They test that each of the provided generator functions
174 // generates an expected sequence of values. The general test pattern
175 // instantiates a generator using one of the generator functions,
176 // checks the sequence produced by the generator using its iterator API,
177 // and then resets the iterator back to the beginning of the sequence
178 // and checks the sequence again.
180 // Tests that iterators produced by generator functions conform to the
181 // ForwardIterator concept.
182 TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
183 const ParamGenerator<int> gen = Range(0, 10);
184 ParamGenerator<int>::iterator it = gen.begin();
186 // Verifies that iterator initialization works as expected.
187 ParamGenerator<int>::iterator it2 = it;
188 EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
189 << "element same as its source points to";
191 // Verifies that iterator assignment works as expected.
193 EXPECT_FALSE(*it == *it2);
195 EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
196 << "element same as its source points to";
198 // Verifies that prefix operator++() returns *this.
199 EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
200 << "refer to the original object";
202 // Verifies that the result of the postfix operator++ points to the value
203 // pointed to by the original iterator.
204 int original_value = *it; // Have to compute it outside of macro call to be
205 // unaffected by the parameter evaluation order.
206 EXPECT_EQ(original_value, *(it++));
208 // Verifies that prefix and postfix operator++() advance an iterator
213 EXPECT_TRUE(*it == *it2);
216 // Tests that Range() generates the expected sequence.
217 TEST(RangeTest, IntRangeWithDefaultStep) {
218 const ParamGenerator<int> gen = Range(0, 3);
219 const int expected_values[] = {0, 1, 2};
220 VerifyGenerator(gen, expected_values);
223 // Edge case. Tests that Range() generates the single element sequence
224 // as expected when provided with range limits that are equal.
225 TEST(RangeTest, IntRangeSingleValue) {
226 const ParamGenerator<int> gen = Range(0, 1);
227 const int expected_values[] = {0};
228 VerifyGenerator(gen, expected_values);
231 // Edge case. Tests that Range() with generates empty sequence when
232 // supplied with an empty range.
233 TEST(RangeTest, IntRangeEmpty) {
234 const ParamGenerator<int> gen = Range(0, 0);
235 VerifyGeneratorIsEmpty(gen);
238 // Tests that Range() with custom step (greater then one) generates
239 // the expected sequence.
240 TEST(RangeTest, IntRangeWithCustomStep) {
241 const ParamGenerator<int> gen = Range(0, 9, 3);
242 const int expected_values[] = {0, 3, 6};
243 VerifyGenerator(gen, expected_values);
246 // Tests that Range() with custom step (greater then one) generates
247 // the expected sequence when the last element does not fall on the
248 // upper range limit. Sequences generated by Range() must not have
249 // elements beyond the range limits.
250 TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
251 const ParamGenerator<int> gen = Range(0, 4, 3);
252 const int expected_values[] = {0, 3};
253 VerifyGenerator(gen, expected_values);
256 // Verifies that Range works with user-defined types that define
257 // copy constructor, operator=(), operator+(), and operator<().
260 explicit DogAdder(const char* a_value) : value_(a_value) {}
261 DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
263 DogAdder operator=(const DogAdder& other) {
265 value_ = other.value_;
268 DogAdder operator+(const DogAdder& other) const {
270 msg << value_.c_str() << other.value_.c_str();
271 return DogAdder(msg.GetString().c_str());
273 bool operator<(const DogAdder& other) const {
274 return value_ < other.value_;
276 const std::string& value() const { return value_; }
282 TEST(RangeTest, WorksWithACustomType) {
283 const ParamGenerator<DogAdder> gen =
284 Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
285 ParamGenerator<DogAdder>::iterator it = gen.begin();
287 ASSERT_FALSE(it == gen.end());
288 EXPECT_STREQ("cat", it->value().c_str());
290 ASSERT_FALSE(++it == gen.end());
291 EXPECT_STREQ("catdog", it->value().c_str());
293 EXPECT_TRUE(++it == gen.end());
298 explicit IntWrapper(int a_value) : value_(a_value) {}
299 IntWrapper(const IntWrapper& other) : value_(other.value_) {}
301 IntWrapper operator=(const IntWrapper& other) {
302 value_ = other.value_;
305 // operator+() adds a different type.
306 IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
307 bool operator<(const IntWrapper& other) const {
308 return value_ < other.value_;
310 int value() const { return value_; }
316 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
317 const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
318 ParamGenerator<IntWrapper>::iterator it = gen.begin();
320 ASSERT_FALSE(it == gen.end());
321 EXPECT_EQ(0, it->value());
323 ASSERT_FALSE(++it == gen.end());
324 EXPECT_EQ(1, it->value());
326 EXPECT_TRUE(++it == gen.end());
329 // Tests that ValuesIn() with an array parameter generates
330 // the expected sequence.
331 TEST(ValuesInTest, ValuesInArray) {
332 int array[] = {3, 5, 8};
333 const ParamGenerator<int> gen = ValuesIn(array);
334 VerifyGenerator(gen, array);
337 // Tests that ValuesIn() with a const array parameter generates
338 // the expected sequence.
339 TEST(ValuesInTest, ValuesInConstArray) {
340 const int array[] = {3, 5, 8};
341 const ParamGenerator<int> gen = ValuesIn(array);
342 VerifyGenerator(gen, array);
345 // Edge case. Tests that ValuesIn() with an array parameter containing a
346 // single element generates the single element sequence.
347 TEST(ValuesInTest, ValuesInSingleElementArray) {
349 const ParamGenerator<int> gen = ValuesIn(array);
350 VerifyGenerator(gen, array);
353 // Tests that ValuesIn() generates the expected sequence for an STL
354 // container (vector).
355 TEST(ValuesInTest, ValuesInVector) {
356 typedef ::std::vector<int> ContainerType;
357 ContainerType values;
361 const ParamGenerator<int> gen = ValuesIn(values);
363 const int expected_values[] = {3, 5, 8};
364 VerifyGenerator(gen, expected_values);
367 // Tests that ValuesIn() generates the expected sequence.
368 TEST(ValuesInTest, ValuesInIteratorRange) {
369 typedef ::std::vector<int> ContainerType;
370 ContainerType values;
374 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
376 const int expected_values[] = {3, 5, 8};
377 VerifyGenerator(gen, expected_values);
380 // Edge case. Tests that ValuesIn() provided with an iterator range specifying a
381 // single value generates a single-element sequence.
382 TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
383 typedef ::std::vector<int> ContainerType;
384 ContainerType values;
385 values.push_back(42);
386 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
388 const int expected_values[] = {42};
389 VerifyGenerator(gen, expected_values);
392 // Edge case. Tests that ValuesIn() provided with an empty iterator range
393 // generates an empty sequence.
394 TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
395 typedef ::std::vector<int> ContainerType;
396 ContainerType values;
397 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
399 VerifyGeneratorIsEmpty(gen);
402 // Tests that the Values() generates the expected sequence.
403 TEST(ValuesTest, ValuesWorks) {
404 const ParamGenerator<int> gen = Values(3, 5, 8);
406 const int expected_values[] = {3, 5, 8};
407 VerifyGenerator(gen, expected_values);
410 // Tests that Values() generates the expected sequences from elements of
411 // different types convertible to ParamGenerator's parameter type.
412 TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
413 const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
415 const double expected_values[] = {3.0, 5.0, 8.0};
416 VerifyGenerator(gen, expected_values);
419 TEST(ValuesTest, ValuesWorksForMaxLengthList) {
420 const ParamGenerator<int> gen = Values(
421 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
422 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
423 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
424 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
425 410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
427 const int expected_values[] = {
428 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
429 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
430 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
431 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
432 410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
433 VerifyGenerator(gen, expected_values);
436 // Edge case test. Tests that single-parameter Values() generates the sequence
437 // with the single value.
438 TEST(ValuesTest, ValuesWithSingleParameter) {
439 const ParamGenerator<int> gen = Values(42);
441 const int expected_values[] = {42};
442 VerifyGenerator(gen, expected_values);
445 // Tests that Bool() generates sequence (false, true).
446 TEST(BoolTest, BoolWorks) {
447 const ParamGenerator<bool> gen = Bool();
449 const bool expected_values[] = {false, true};
450 VerifyGenerator(gen, expected_values);
453 # if GTEST_HAS_COMBINE
455 // Tests that Combine() with two parameters generates the expected sequence.
456 TEST(CombineTest, CombineWithTwoParameters) {
457 const char* foo = "foo";
458 const char* bar = "bar";
459 const ParamGenerator<tuple<const char*, int> > gen =
460 Combine(Values(foo, bar), Values(3, 4));
462 tuple<const char*, int> expected_values[] = {
463 make_tuple(foo, 3), make_tuple(foo, 4),
464 make_tuple(bar, 3), make_tuple(bar, 4)};
465 VerifyGenerator(gen, expected_values);
468 // Tests that Combine() with three parameters generates the expected sequence.
469 TEST(CombineTest, CombineWithThreeParameters) {
470 const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1),
473 tuple<int, int, int> expected_values[] = {
474 make_tuple(0, 3, 5), make_tuple(0, 3, 6),
475 make_tuple(0, 4, 5), make_tuple(0, 4, 6),
476 make_tuple(1, 3, 5), make_tuple(1, 3, 6),
477 make_tuple(1, 4, 5), make_tuple(1, 4, 6)};
478 VerifyGenerator(gen, expected_values);
481 // Tests that the Combine() with the first parameter generating a single value
482 // sequence generates a sequence with the number of elements equal to the
483 // number of elements in the sequence generated by the second parameter.
484 TEST(CombineTest, CombineWithFirstParameterSingleValue) {
485 const ParamGenerator<tuple<int, int> > gen = Combine(Values(42),
488 tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)};
489 VerifyGenerator(gen, expected_values);
492 // Tests that the Combine() with the second parameter generating a single value
493 // sequence generates a sequence with the number of elements equal to the
494 // number of elements in the sequence generated by the first parameter.
495 TEST(CombineTest, CombineWithSecondParameterSingleValue) {
496 const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
499 tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)};
500 VerifyGenerator(gen, expected_values);
503 // Tests that when the first parameter produces an empty sequence,
504 // Combine() produces an empty sequence, too.
505 TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
506 const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0),
508 VerifyGeneratorIsEmpty(gen);
511 // Tests that when the second parameter produces an empty sequence,
512 // Combine() produces an empty sequence, too.
513 TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
514 const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
516 VerifyGeneratorIsEmpty(gen);
519 // Edge case. Tests that combine works with the maximum number
520 // of parameters supported by Google Test (currently 10).
521 TEST(CombineTest, CombineWithMaxNumberOfParameters) {
522 const char* foo = "foo";
523 const char* bar = "bar";
524 const ParamGenerator<tuple<const char*, int, int, int, int, int, int, int,
525 int, int> > gen = Combine(Values(foo, bar),
526 Values(1), Values(2),
527 Values(3), Values(4),
528 Values(5), Values(6),
529 Values(7), Values(8),
532 tuple<const char*, int, int, int, int, int, int, int, int, int>
533 expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
534 make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
535 VerifyGenerator(gen, expected_values);
540 class NonDefaultConstructAssignString {
542 NonDefaultConstructAssignString(const std::string& s) : str_(s) {}
544 const std::string& str() const { return str_; }
549 // Not default constructible
550 NonDefaultConstructAssignString();
552 void operator=(const NonDefaultConstructAssignString&);
555 TEST(CombineTest, NonDefaultConstructAssign) {
556 const ParamGenerator<tuple<int, NonDefaultConstructAssignString> > gen =
557 Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"),
558 NonDefaultConstructAssignString("B")));
560 ParamGenerator<tuple<int, NonDefaultConstructAssignString> >::iterator it =
563 EXPECT_EQ(0, std::get<0>(*it));
564 EXPECT_EQ("A", std::get<1>(*it).str());
567 EXPECT_EQ(0, std::get<0>(*it));
568 EXPECT_EQ("B", std::get<1>(*it).str());
571 EXPECT_EQ(1, std::get<0>(*it));
572 EXPECT_EQ("A", std::get<1>(*it).str());
575 EXPECT_EQ(1, std::get<0>(*it));
576 EXPECT_EQ("B", std::get<1>(*it).str());
579 EXPECT_TRUE(it == gen.end());
582 #endif // GTEST_LANG_CXX11
583 # endif // GTEST_HAS_COMBINE
585 // Tests that an generator produces correct sequence after being
586 // assigned from another generator.
587 TEST(ParamGeneratorTest, AssignmentWorks) {
588 ParamGenerator<int> gen = Values(1, 2);
589 const ParamGenerator<int> gen2 = Values(3, 4);
592 const int expected_values[] = {3, 4};
593 VerifyGenerator(gen, expected_values);
596 // This test verifies that the tests are expanded and run as specified:
597 // one test per element from the sequence produced by the generator
598 // specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's
599 // fixture constructor, SetUp(), and TearDown() have run and have been
600 // supplied with the correct parameters.
602 // The use of environment object allows detection of the case where no test
603 // case functionality is run at all. In this case TestCaseTearDown will not
604 // be able to detect missing tests, naturally.
605 template <int kExpectedCalls>
606 class TestGenerationEnvironment : public ::testing::Environment {
608 static TestGenerationEnvironment* Instance() {
609 static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
613 void FixtureConstructorExecuted() { fixture_constructor_count_++; }
614 void SetUpExecuted() { set_up_count_++; }
615 void TearDownExecuted() { tear_down_count_++; }
616 void TestBodyExecuted() { test_body_count_++; }
618 virtual void TearDown() {
619 // If all MultipleTestGenerationTest tests have been de-selected
620 // by the filter flag, the following checks make no sense.
621 bool perform_check = false;
623 for (int i = 0; i < kExpectedCalls; ++i) {
625 msg << "TestsExpandedAndRun/" << i;
626 if (UnitTestOptions::FilterMatchesTest(
627 "TestExpansionModule/MultipleTestGenerationTest",
628 msg.GetString().c_str())) {
629 perform_check = true;
633 EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
634 << "Fixture constructor of ParamTestGenerationTest test case "
635 << "has not been run as expected.";
636 EXPECT_EQ(kExpectedCalls, set_up_count_)
637 << "Fixture SetUp method of ParamTestGenerationTest test case "
638 << "has not been run as expected.";
639 EXPECT_EQ(kExpectedCalls, tear_down_count_)
640 << "Fixture TearDown method of ParamTestGenerationTest test case "
641 << "has not been run as expected.";
642 EXPECT_EQ(kExpectedCalls, test_body_count_)
643 << "Test in ParamTestGenerationTest test case "
644 << "has not been run as expected.";
649 TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
650 tear_down_count_(0), test_body_count_(0) {}
652 int fixture_constructor_count_;
654 int tear_down_count_;
655 int test_body_count_;
657 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
660 const int test_generation_params[] = {36, 42, 72};
662 class TestGenerationTest : public TestWithParam<int> {
666 sizeof(test_generation_params)/sizeof(test_generation_params[0])
669 typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
671 TestGenerationTest() {
672 Environment::Instance()->FixtureConstructorExecuted();
673 current_parameter_ = GetParam();
675 virtual void SetUp() {
676 Environment::Instance()->SetUpExecuted();
677 EXPECT_EQ(current_parameter_, GetParam());
679 virtual void TearDown() {
680 Environment::Instance()->TearDownExecuted();
681 EXPECT_EQ(current_parameter_, GetParam());
684 static void SetUpTestCase() {
685 bool all_tests_in_test_case_selected = true;
687 for (int i = 0; i < PARAMETER_COUNT; ++i) {
689 test_name << "TestsExpandedAndRun/" << i;
690 if ( !UnitTestOptions::FilterMatchesTest(
691 "TestExpansionModule/MultipleTestGenerationTest",
692 test_name.GetString())) {
693 all_tests_in_test_case_selected = false;
696 EXPECT_TRUE(all_tests_in_test_case_selected)
697 << "When running the TestGenerationTest test case all of its tests\n"
698 << "must be selected by the filter flag for the test case to pass.\n"
699 << "If not all of them are enabled, we can't reliably conclude\n"
700 << "that the correct number of tests have been generated.";
702 collected_parameters_.clear();
705 static void TearDownTestCase() {
706 vector<int> expected_values(test_generation_params,
707 test_generation_params + PARAMETER_COUNT);
708 // Test execution order is not guaranteed by Google Test,
709 // so the order of values in collected_parameters_ can be
710 // different and we have to sort to compare.
711 sort(expected_values.begin(), expected_values.end());
712 sort(collected_parameters_.begin(), collected_parameters_.end());
714 EXPECT_TRUE(collected_parameters_ == expected_values);
718 int current_parameter_;
719 static vector<int> collected_parameters_;
722 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
724 vector<int> TestGenerationTest::collected_parameters_;
726 TEST_P(TestGenerationTest, TestsExpandedAndRun) {
727 Environment::Instance()->TestBodyExecuted();
728 EXPECT_EQ(current_parameter_, GetParam());
729 collected_parameters_.push_back(GetParam());
731 INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest,
732 ValuesIn(test_generation_params));
734 // This test verifies that the element sequence (third parameter of
735 // INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at
736 // the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS(). For
737 // that, we declare param_value_ to be a static member of
738 // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in
739 // main(), just before invocation of InitGoogleTest(). After calling
740 // InitGoogleTest(), we set the value to 2. If the sequence is evaluated
741 // before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a
742 // test with parameter other than 1, and the test body will fail the
744 class GeneratorEvaluationTest : public TestWithParam<int> {
746 static int param_value() { return param_value_; }
747 static void set_param_value(int param_value) { param_value_ = param_value; }
750 static int param_value_;
752 int GeneratorEvaluationTest::param_value_ = 0;
754 TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
755 EXPECT_EQ(1, GetParam());
757 INSTANTIATE_TEST_CASE_P(GenEvalModule,
758 GeneratorEvaluationTest,
759 Values(GeneratorEvaluationTest::param_value()));
761 // Tests that generators defined in a different translation unit are
762 // functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
763 extern ParamGenerator<int> extern_gen;
764 class ExternalGeneratorTest : public TestWithParam<int> {};
765 TEST_P(ExternalGeneratorTest, ExternalGenerator) {
766 // Sequence produced by extern_gen contains only a single value
767 // which we verify here.
768 EXPECT_EQ(GetParam(), 33);
770 INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule,
771 ExternalGeneratorTest,
774 // Tests that a parameterized test case can be defined in one translation
775 // unit and instantiated in another. This test will be instantiated in
776 // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
777 // defined in gtest-param-test_test.h.
778 TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
779 EXPECT_EQ(0, GetParam() % 33);
782 // Tests that a parameterized test case can be instantiated with multiple
784 class MultipleInstantiationTest : public TestWithParam<int> {};
785 TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
787 INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
788 INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
790 // Tests that a parameterized test case can be instantiated
791 // in multiple translation units. This test will be instantiated
792 // here and in gtest-param-test_test2.cc.
793 // InstantiationInMultipleTranslationUnitsTest fixture class
794 // is defined in gtest-param-test_test.h.
795 TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) {
796 EXPECT_EQ(0, GetParam() % 42);
798 INSTANTIATE_TEST_CASE_P(Sequence1,
799 InstantiationInMultipleTranslaionUnitsTest,
802 // Tests that each iteration of parameterized test runs in a separate test
804 class SeparateInstanceTest : public TestWithParam<int> {
806 SeparateInstanceTest() : count_(0) {}
808 static void TearDownTestCase() {
809 EXPECT_GE(global_count_, 2)
810 << "If some (but not all) SeparateInstanceTest tests have been "
811 << "filtered out this test will fail. Make sure that all "
812 << "GeneratorEvaluationTest are selected or de-selected together "
813 << "by the test filter.";
818 static int global_count_;
820 int SeparateInstanceTest::global_count_ = 0;
822 TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
823 EXPECT_EQ(0, count_++);
826 INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
828 // Tests that all instantiations of a test have named appropriately. Test
829 // defined with TEST_P(TestCaseName, TestName) and instantiated with
830 // INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named
831 // SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the
832 // sequence element used to instantiate the test.
833 class NamingTest : public TestWithParam<int> {};
835 TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
836 const ::testing::TestInfo* const test_info =
837 ::testing::UnitTest::GetInstance()->current_test_info();
839 EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
841 Message index_stream;
842 index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
843 EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
845 EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
848 INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
850 // Tests that macros in test names are expanded correctly.
851 class MacroNamingTest : public TestWithParam<int> {};
853 #define PREFIX_WITH_FOO(test_name) Foo##test_name
854 #define PREFIX_WITH_MACRO(test_name) Macro##test_name
856 TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) {
857 const ::testing::TestInfo* const test_info =
858 ::testing::UnitTest::GetInstance()->current_test_info();
860 EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_case_name());
861 EXPECT_STREQ("FooSomeTestName", test_info->name());
864 INSTANTIATE_TEST_CASE_P(FortyTwo, MacroNamingTest, Values(42));
866 // Tests the same thing for non-parametrized tests.
867 class MacroNamingTestNonParametrized : public ::testing::Test {};
869 TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized),
870 PREFIX_WITH_FOO(SomeTestName)) {
871 const ::testing::TestInfo* const test_info =
872 ::testing::UnitTest::GetInstance()->current_test_info();
874 EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_case_name());
875 EXPECT_STREQ("FooSomeTestName", test_info->name());
878 // Tests that user supplied custom parameter names are working correctly.
879 // Runs the test with a builtin helper method which uses PrintToString,
880 // as well as a custom function and custom functor to ensure all possible
881 // uses work correctly.
882 class CustomFunctorNamingTest : public TestWithParam<std::string> {};
883 TEST_P(CustomFunctorNamingTest, CustomTestNames) {}
885 struct CustomParamNameFunctor {
886 std::string operator()(const ::testing::TestParamInfo<std::string>& inf) {
891 INSTANTIATE_TEST_CASE_P(CustomParamNameFunctor,
892 CustomFunctorNamingTest,
893 Values(std::string("FunctorName")),
894 CustomParamNameFunctor());
896 INSTANTIATE_TEST_CASE_P(AllAllowedCharacters,
897 CustomFunctorNamingTest,
898 Values("abcdefghijklmnopqrstuvwxyz",
899 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
901 CustomParamNameFunctor());
903 inline std::string CustomParamNameFunction(
904 const ::testing::TestParamInfo<std::string>& inf) {
908 class CustomFunctionNamingTest : public TestWithParam<std::string> {};
909 TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
911 INSTANTIATE_TEST_CASE_P(CustomParamNameFunction,
912 CustomFunctionNamingTest,
913 Values(std::string("FunctionName")),
914 CustomParamNameFunction);
918 // Test custom naming with a lambda
920 class CustomLambdaNamingTest : public TestWithParam<std::string> {};
921 TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
923 INSTANTIATE_TEST_CASE_P(CustomParamNameLambda, CustomLambdaNamingTest,
924 Values(std::string("LambdaName")),
925 [](const ::testing::TestParamInfo<std::string>& inf) {
929 #endif // GTEST_LANG_CXX11
931 TEST(CustomNamingTest, CheckNameRegistry) {
932 ::testing::UnitTest* unit_test = ::testing::UnitTest::GetInstance();
933 std::set<std::string> test_names;
934 for (int case_num = 0;
935 case_num < unit_test->total_test_case_count();
937 const ::testing::TestCase* test_case = unit_test->GetTestCase(case_num);
938 for (int test_num = 0;
939 test_num < test_case->total_test_count();
941 const ::testing::TestInfo* test_info = test_case->GetTestInfo(test_num);
942 test_names.insert(std::string(test_info->name()));
945 EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctorName"));
946 EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionName"));
948 EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName"));
949 #endif // GTEST_LANG_CXX11
952 // Test a numeric name to ensure PrintToStringParamName works correctly.
954 class CustomIntegerNamingTest : public TestWithParam<int> {};
956 TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) {
957 const ::testing::TestInfo* const test_info =
958 ::testing::UnitTest::GetInstance()->current_test_info();
959 Message test_name_stream;
960 test_name_stream << "TestsReportCorrectNames/" << GetParam();
961 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
964 INSTANTIATE_TEST_CASE_P(PrintToString,
965 CustomIntegerNamingTest,
967 ::testing::PrintToStringParamName());
969 // Test a custom struct with PrintToString.
971 struct CustomStruct {
972 explicit CustomStruct(int value) : x(value) {}
976 std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) {
981 class CustomStructNamingTest : public TestWithParam<CustomStruct> {};
983 TEST_P(CustomStructNamingTest, TestsReportCorrectNames) {
984 const ::testing::TestInfo* const test_info =
985 ::testing::UnitTest::GetInstance()->current_test_info();
986 Message test_name_stream;
987 test_name_stream << "TestsReportCorrectNames/" << GetParam();
988 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
991 INSTANTIATE_TEST_CASE_P(PrintToString,
992 CustomStructNamingTest,
993 Values(CustomStruct(0), CustomStruct(1)),
994 ::testing::PrintToStringParamName());
996 // Test that using a stateful parameter naming function works as expected.
998 struct StatefulNamingFunctor {
999 StatefulNamingFunctor() : sum(0) {}
1000 std::string operator()(const ::testing::TestParamInfo<int>& info) {
1001 int value = info.param + sum;
1003 return ::testing::PrintToString(value);
1008 class StatefulNamingTest : public ::testing::TestWithParam<int> {
1010 StatefulNamingTest() : sum_(0) {}
1014 TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
1015 const ::testing::TestInfo* const test_info =
1016 ::testing::UnitTest::GetInstance()->current_test_info();
1018 Message test_name_stream;
1019 test_name_stream << "TestsReportCorrectNames/" << sum_;
1020 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
1023 INSTANTIATE_TEST_CASE_P(StatefulNamingFunctor,
1026 StatefulNamingFunctor());
1028 // Class that cannot be streamed into an ostream. It needs to be copyable
1029 // (and, in case of MSVC, also assignable) in order to be a test parameter
1030 // type. Its default copy constructor and assignment operator do exactly
1032 class Unstreamable {
1034 explicit Unstreamable(int value) : value_(value) {}
1040 class CommentTest : public TestWithParam<Unstreamable> {};
1042 TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
1043 const ::testing::TestInfo* const test_info =
1044 ::testing::UnitTest::GetInstance()->current_test_info();
1046 EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
1049 INSTANTIATE_TEST_CASE_P(InstantiationWithComments,
1051 Values(Unstreamable(1)));
1053 // Verify that we can create a hierarchy of test fixtures, where the base
1054 // class fixture is not parameterized and the derived class is. In this case
1055 // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We
1056 // perform simple tests on both.
1057 class NonParameterizedBaseTest : public ::testing::Test {
1059 NonParameterizedBaseTest() : n_(17) { }
1064 class ParameterizedDerivedTest : public NonParameterizedBaseTest,
1065 public ::testing::WithParamInterface<int> {
1067 ParameterizedDerivedTest() : count_(0) { }
1069 static int global_count_;
1072 int ParameterizedDerivedTest::global_count_ = 0;
1074 TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
1078 TEST_P(ParameterizedDerivedTest, SeesSequence) {
1080 EXPECT_EQ(0, count_++);
1081 EXPECT_EQ(GetParam(), global_count_++);
1084 class ParameterizedDeathTest : public ::testing::TestWithParam<int> { };
1086 TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
1087 EXPECT_DEATH_IF_SUPPORTED(GetParam(),
1088 ".* value-parameterized test .*");
1091 INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5));
1094 int main(int argc, char **argv) {
1095 // Used in TestGenerationTest test case.
1096 AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
1097 // Used in GeneratorEvaluationTest test case. Tests that the updated value
1098 // will be picked up for instantiating tests in GeneratorEvaluationTest.
1099 GeneratorEvaluationTest::set_param_value(1);
1101 ::testing::InitGoogleTest(&argc, argv);
1103 // Used in GeneratorEvaluationTest test case. Tests that value updated
1104 // here will NOT be used for instantiating tests in
1105 // GeneratorEvaluationTest.
1106 GeneratorEvaluationTest::set_param_value(2);
1108 return RUN_ALL_TESTS();