38 #if GTEST_HAS_PARAM_TEST
48 # define GTEST_IMPLEMENTATION_ 1
50 # undef GTEST_IMPLEMENTATION_
58 using ::testing::Bool;
59 using ::testing::Message;
61 using ::testing::TestWithParam;
62 using ::testing::Values;
63 using ::testing::ValuesIn;
65 # if GTEST_HAS_COMBINE
66 using ::testing::Combine;
69 using ::testing::tuple;
70 # endif // GTEST_HAS_COMBINE
72 using ::testing::internal::ParamGenerator;
73 using ::testing::internal::UnitTestOptions;
83 ::std::stringstream stream;
88 # if GTEST_HAS_COMBINE
96 template <
typename T1,
typename T2>
98 ::std::stringstream stream;
99 stream <<
"(" << get<0>(
value) <<
", " << get<1>(value) <<
")";
103 template <
typename T1,
typename T2,
typename T3>
105 ::std::stringstream stream;
106 stream <<
"(" << get<0>(
value) <<
", " << get<1>(value)
107 <<
", "<< get<2>(
value) <<
")";
111 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
112 typename T6,
typename T7,
typename T8,
typename T9,
typename T10>
115 ::std::stringstream stream;
116 stream <<
"(" << get<0>(
value) <<
", " << get<1>(value)
117 <<
", "<< get<2>(
value) <<
", " << get<3>(value)
118 <<
", "<< get<4>(
value) <<
", " << get<5>(value)
119 <<
", "<< get<6>(
value) <<
", " << get<7>(value)
120 <<
", "<< get<8>(
value) <<
", " << get<9>(value) <<
")";
124 # endif // GTEST_HAS_COMBINE
129 template <
typename T,
size_t N>
130 void VerifyGenerator(
const ParamGenerator<T>&
generator,
131 const T (&expected_values)[
N]) {
132 typename ParamGenerator<T>::iterator
it = generator.begin();
133 for (
size_t i = 0;
i <
N; ++
i) {
135 <<
"At element " <<
i <<
" when accessing via an iterator "
136 <<
"created with the copy constructor.\n";
140 <<
"where i is " <<
i
141 <<
", expected_values[i] is " << PrintValue(expected_values[
i])
142 <<
", *it is " << PrintValue(*it)
143 <<
", and 'it' is an iterator created with the copy constructor.\n";
147 <<
"At the presumed end of sequence when accessing via an iterator "
148 <<
"created with the copy constructor.\n";
154 it = generator.begin();
155 for (
size_t i = 0;
i <
N; ++
i) {
157 <<
"At element " <<
i <<
" when accessing via an iterator "
158 <<
"created with the assignment operator.\n";
160 <<
"where i is " <<
i
161 <<
", expected_values[i] is " << PrintValue(expected_values[
i])
162 <<
", *it is " << PrintValue(*it)
163 <<
", and 'it' is an iterator created with the copy constructor.\n";
167 <<
"At the presumed end of sequence when accessing via an iterator "
168 <<
"created with the assignment operator.\n";
171 template <
typename T>
172 void VerifyGeneratorIsEmpty(
const ParamGenerator<T>& generator) {
173 typename ParamGenerator<T>::iterator it = generator.begin();
176 it = generator.begin();
189 TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
190 const ParamGenerator<int>
gen =
Range(0, 10);
191 ParamGenerator<int>::iterator it = gen.begin();
194 ParamGenerator<int>::iterator it2 =
it;
195 EXPECT_TRUE(*it == *it2) <<
"Initialized iterators must point to the "
196 <<
"element same as its source points to";
202 EXPECT_TRUE(*it == *it2) <<
"Assigned iterators must point to the "
203 <<
"element same as its source points to";
206 EXPECT_EQ(&it, &(++it)) <<
"Result of the prefix operator++ must be "
207 <<
"refer to the original object";
211 int original_value = *
it;
224 TEST(RangeTest, IntRangeWithDefaultStep) {
225 const ParamGenerator<int> gen =
Range(0, 3);
226 const int expected_values[] = {0, 1, 2};
227 VerifyGenerator(gen, expected_values);
232 TEST(RangeTest, IntRangeSingleValue) {
233 const ParamGenerator<int> gen =
Range(0, 1);
234 const int expected_values[] = {0};
235 VerifyGenerator(gen, expected_values);
240 TEST(RangeTest, IntRangeEmpty) {
241 const ParamGenerator<int> gen =
Range(0, 0);
242 VerifyGeneratorIsEmpty(gen);
247 TEST(RangeTest, IntRangeWithCustomStep) {
248 const ParamGenerator<int> gen =
Range(0, 9, 3);
249 const int expected_values[] = {0, 3, 6};
250 VerifyGenerator(gen, expected_values);
257 TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
258 const ParamGenerator<int> gen =
Range(0, 4, 3);
259 const int expected_values[] = {0, 3};
260 VerifyGenerator(gen, expected_values);
267 explicit DogAdder(
const char* a_value) : value_(a_value) {}
268 DogAdder(
const DogAdder& other) : value_(other.value_.c_str()) {}
270 DogAdder
operator=(
const DogAdder& other) {
272 value_ = other.value_;
275 DogAdder
operator+(
const DogAdder& other)
const {
277 msg << value_.c_str() << other.value_.c_str();
278 return DogAdder(msg.GetString().c_str());
280 bool operator<(
const DogAdder& other)
const {
281 return value_ < other.value_;
289 TEST(RangeTest, WorksWithACustomType) {
290 const ParamGenerator<DogAdder> gen =
291 Range(DogAdder(
"cat"), DogAdder(
"catdogdog"), DogAdder(
"dog"));
292 ParamGenerator<DogAdder>::iterator it = gen.begin();
305 explicit IntWrapper(
int a_value) : value_(a_value) {}
306 IntWrapper(
const IntWrapper& other) : value_(other.value_) {}
308 IntWrapper
operator=(
const IntWrapper& other) {
309 value_ = other.value_;
313 IntWrapper
operator+(
int other)
const {
return IntWrapper(value_ + other); }
314 bool operator<(
const IntWrapper& other)
const {
315 return value_ < other.value_;
317 int value()
const {
return value_; }
323 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
324 const ParamGenerator<IntWrapper> gen =
Range(IntWrapper(0), IntWrapper(2));
325 ParamGenerator<IntWrapper>::iterator it = gen.begin();
338 TEST(ValuesInTest, ValuesInArray) {
339 int array[] = {3, 5, 8};
340 const ParamGenerator<int> gen = ValuesIn(array);
341 VerifyGenerator(gen, array);
346 TEST(ValuesInTest, ValuesInConstArray) {
347 const int array[] = {3, 5, 8};
348 const ParamGenerator<int> gen = ValuesIn(array);
349 VerifyGenerator(gen, array);
354 TEST(ValuesInTest, ValuesInSingleElementArray) {
356 const ParamGenerator<int> gen = ValuesIn(array);
357 VerifyGenerator(gen, array);
362 TEST(ValuesInTest, ValuesInVector) {
363 typedef ::std::vector<int> ContainerType;
368 const ParamGenerator<int> gen = ValuesIn(values);
370 const int expected_values[] = {3, 5, 8};
371 VerifyGenerator(gen, expected_values);
375 TEST(ValuesInTest, ValuesInIteratorRange) {
376 typedef ::std::vector<int> ContainerType;
381 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
383 const int expected_values[] = {3, 5, 8};
384 VerifyGenerator(gen, expected_values);
389 TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
390 typedef ::std::vector<int> ContainerType;
392 values.push_back(42);
393 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
395 const int expected_values[] = {42};
396 VerifyGenerator(gen, expected_values);
401 TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
402 typedef ::std::vector<int> ContainerType;
404 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
406 VerifyGeneratorIsEmpty(gen);
410 TEST(ValuesTest, ValuesWorks) {
411 const ParamGenerator<int> gen = Values(3, 5, 8);
413 const int expected_values[] = {3, 5, 8};
414 VerifyGenerator(gen, expected_values);
419 TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
420 const ParamGenerator<double> gen = Values(3, 5.0
f, 8.0);
422 const double expected_values[] = {3.0, 5.0, 8.0};
423 VerifyGenerator(gen, expected_values);
426 TEST(ValuesTest, ValuesWorksForMaxLengthList) {
427 const ParamGenerator<int> gen = 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);
434 const int expected_values[] = {
435 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
436 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
437 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
438 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
439 410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
440 VerifyGenerator(gen, expected_values);
445 TEST(ValuesTest, ValuesWithSingleParameter) {
446 const ParamGenerator<int> gen = Values(42);
448 const int expected_values[] = {42};
449 VerifyGenerator(gen, expected_values);
453 TEST(BoolTest, BoolWorks) {
454 const ParamGenerator<bool> gen =
Bool();
456 const bool expected_values[] = {
false,
true};
457 VerifyGenerator(gen, expected_values);
460 # if GTEST_HAS_COMBINE
463 TEST(CombineTest, CombineWithTwoParameters) {
464 const char*
foo =
"foo";
465 const char*
bar =
"bar";
466 const ParamGenerator<tuple<const char*, int> > gen =
467 Combine(Values(foo, bar), Values(3, 4));
472 VerifyGenerator(gen, expected_values);
476 TEST(CombineTest, CombineWithThreeParameters) {
477 const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1),
485 VerifyGenerator(gen, expected_values);
491 TEST(CombineTest, CombineWithFirstParameterSingleValue) {
492 const ParamGenerator<tuple<int, int> > gen = Combine(Values(42),
496 VerifyGenerator(gen, expected_values);
502 TEST(CombineTest, CombineWithSecondParameterSingleValue) {
503 const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
507 VerifyGenerator(gen, expected_values);
512 TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
513 const ParamGenerator<tuple<int, int> > gen = Combine(
Range(0, 0),
515 VerifyGeneratorIsEmpty(gen);
520 TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
521 const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
523 VerifyGeneratorIsEmpty(gen);
528 TEST(CombineTest, CombineWithMaxNumberOfParameters) {
529 const char* foo =
"foo";
530 const char* bar =
"bar";
531 const ParamGenerator<
tuple<
const char*, int, int, int, int, int, int, int,
532 int,
int> > gen = Combine(Values(foo, bar),
533 Values(1), Values(2),
534 Values(3), Values(4),
535 Values(5), Values(6),
536 Values(7), Values(8),
540 expected_values[] = {
make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
542 VerifyGenerator(gen, expected_values);
545 # endif // GTEST_HAS_COMBINE
549 TEST(ParamGeneratorTest, AssignmentWorks) {
550 ParamGenerator<int> gen = Values(1, 2);
551 const ParamGenerator<int> gen2 = Values(3, 4);
554 const int expected_values[] = {3, 4};
555 VerifyGenerator(gen, expected_values);
567 template <
int kExpectedCalls>
570 static TestGenerationEnvironment* Instance() {
571 static TestGenerationEnvironment* instance =
new TestGenerationEnvironment;
575 void FixtureConstructorExecuted() { fixture_constructor_count_++; }
576 void SetUpExecuted() { set_up_count_++; }
577 void TearDownExecuted() { tear_down_count_++; }
578 void TestBodyExecuted() { test_body_count_++; }
583 bool perform_check =
false;
585 for (
int i = 0;
i < kExpectedCalls; ++
i) {
587 msg <<
"TestsExpandedAndRun/" <<
i;
588 if (UnitTestOptions::FilterMatchesTest(
589 "TestExpansionModule/MultipleTestGenerationTest",
590 msg.GetString().c_str())) {
591 perform_check =
true;
595 EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
596 <<
"Fixture constructor of ParamTestGenerationTest test case "
597 <<
"has not been run as expected.";
599 <<
"Fixture SetUp method of ParamTestGenerationTest test case "
600 <<
"has not been run as expected.";
601 EXPECT_EQ(kExpectedCalls, tear_down_count_)
602 <<
"Fixture TearDown method of ParamTestGenerationTest test case "
603 <<
"has not been run as expected.";
604 EXPECT_EQ(kExpectedCalls, test_body_count_)
605 <<
"Test in ParamTestGenerationTest test case "
606 <<
"has not been run as expected.";
611 TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
612 tear_down_count_(0), test_body_count_(0) {}
614 int fixture_constructor_count_;
616 int tear_down_count_;
617 int test_body_count_;
622 const int test_generation_params[] = {36, 42, 72};
624 class TestGenerationTest :
public TestWithParam<int> {
628 sizeof(test_generation_params)/
sizeof(test_generation_params[0])
631 typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
633 TestGenerationTest() {
634 Environment::Instance()->FixtureConstructorExecuted();
635 current_parameter_ = GetParam();
637 virtual void SetUp() {
638 Environment::Instance()->SetUpExecuted();
639 EXPECT_EQ(current_parameter_, GetParam());
641 virtual void TearDown() {
642 Environment::Instance()->TearDownExecuted();
643 EXPECT_EQ(current_parameter_, GetParam());
646 static void SetUpTestCase() {
647 bool all_tests_in_test_case_selected =
true;
649 for (
int i = 0; i < PARAMETER_COUNT; ++
i) {
651 test_name <<
"TestsExpandedAndRun/" <<
i;
652 if ( !UnitTestOptions::FilterMatchesTest(
653 "TestExpansionModule/MultipleTestGenerationTest",
654 test_name.GetString())) {
655 all_tests_in_test_case_selected =
false;
659 <<
"When running the TestGenerationTest test case all of its tests\n"
660 <<
"must be selected by the filter flag for the test case to pass.\n"
661 <<
"If not all of them are enabled, we can't reliably conclude\n"
662 <<
"that the correct number of tests have been generated.";
664 collected_parameters_.clear();
667 static void TearDownTestCase() {
668 vector<int> expected_values(test_generation_params,
669 test_generation_params + PARAMETER_COUNT);
673 sort(expected_values.begin(), expected_values.end());
674 sort(collected_parameters_.begin(), collected_parameters_.end());
676 EXPECT_TRUE(collected_parameters_ == expected_values);
680 int current_parameter_;
681 static vector<int> collected_parameters_;
686 vector<int> TestGenerationTest::collected_parameters_;
688 TEST_P(TestGenerationTest, TestsExpandedAndRun) {
689 Environment::Instance()->TestBodyExecuted();
690 EXPECT_EQ(current_parameter_, GetParam());
691 collected_parameters_.push_back(GetParam());
694 ValuesIn(test_generation_params));
706 class GeneratorEvaluationTest :
public TestWithParam<int> {
708 static int param_value() {
return param_value_; }
709 static void set_param_value(
int param_value) { param_value_ = param_value; }
712 static int param_value_;
714 int GeneratorEvaluationTest::param_value_ = 0;
716 TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
720 GeneratorEvaluationTest,
721 Values(GeneratorEvaluationTest::param_value()));
725 extern ParamGenerator<int> extern_gen;
726 class ExternalGeneratorTest :
public TestWithParam<int> {};
727 TEST_P(ExternalGeneratorTest, ExternalGenerator) {
733 ExternalGeneratorTest,
740 TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
746 class MultipleInstantiationTest :
public TestWithParam<int> {};
747 TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
757 TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) {
761 InstantiationInMultipleTranslaionUnitsTest,
766 class SeparateInstanceTest :
public TestWithParam<int> {
768 SeparateInstanceTest() : count_(0) {}
770 static void TearDownTestCase() {
772 <<
"If some (but not all) SeparateInstanceTest tests have been "
773 <<
"filtered out this test will fail. Make sure that all "
774 <<
"GeneratorEvaluationTest are selected or de-selected together "
775 <<
"by the test filter.";
780 static int global_count_;
782 int SeparateInstanceTest::global_count_ = 0;
784 TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
795 class NamingTest :
public TestWithParam<int> {};
797 TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
798 const ::testing::TestInfo*
const test_info =
801 EXPECT_STREQ(
"ZeroToFiveSequence/NamingTest", test_info->test_case_name());
803 Message index_stream;
804 index_stream <<
"TestsReportCorrectNamesAndParameters/" << GetParam();
805 EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
816 class CustomFunctorNamingTest :
public TestWithParam<std::string> {};
817 TEST_P(CustomFunctorNamingTest, CustomTestNames) {}
819 struct CustomParamNameFunctor {
820 std::string operator()(const ::testing::TestParamInfo<std::string>& info) {
826 CustomFunctorNamingTest,
828 CustomParamNameFunctor());
831 CustomFunctorNamingTest,
832 Values(
"abcdefghijklmnopqrstuvwxyz",
833 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
835 CustomParamNameFunctor());
838 const ::testing::TestParamInfo<std::string>& info) {
842 class CustomFunctionNamingTest :
public TestWithParam<std::string> {};
843 TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
846 CustomFunctionNamingTest,
848 CustomParamNameFunction);
854 class CustomLambdaNamingTest :
public TestWithParam<std::string> {};
855 TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
858 CustomLambdaNamingTest,
860 [](const ::testing::TestParamInfo<std::string>& info) {
864 #endif // GTEST_LANG_CXX11
866 TEST(CustomNamingTest, CheckNameRegistry) {
868 std::set<std::string> test_names;
869 for (
int case_num = 0;
873 for (
int test_num = 0;
874 test_num < test_case->total_test_count();
876 const ::testing::TestInfo* test_info = test_case->
GetTestInfo(test_num);
880 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/FunctorName"));
881 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/FunctionName"));
883 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/LambdaName"));
884 #endif // GTEST_LANG_CXX11
889 class CustomIntegerNamingTest :
public TestWithParam<int> {};
891 TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) {
892 const ::testing::TestInfo*
const test_info =
894 Message test_name_stream;
895 test_name_stream <<
"TestsReportCorrectNames/" << GetParam();
896 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
900 CustomIntegerNamingTest,
902 ::testing::PrintToStringParamName());
906 struct CustomStruct {
907 explicit CustomStruct(
int value) :
x(value) {}
911 std::ostream&
operator<<(std::ostream& stream,
const CustomStruct& val) {
916 class CustomStructNamingTest :
public TestWithParam<CustomStruct> {};
918 TEST_P(CustomStructNamingTest, TestsReportCorrectNames) {
919 const ::testing::TestInfo*
const test_info =
921 Message test_name_stream;
922 test_name_stream <<
"TestsReportCorrectNames/" << GetParam();
923 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
927 CustomStructNamingTest,
928 Values(CustomStruct(0), CustomStruct(1)),
929 ::testing::PrintToStringParamName());
933 struct StatefulNamingFunctor {
934 StatefulNamingFunctor() :
sum(0) {}
935 std::string operator()(const ::testing::TestParamInfo<int>& info) {
936 int value = info.param +
sum;
943 class StatefulNamingTest :
public ::testing::TestWithParam<int> {
945 StatefulNamingTest() : sum_(0) {}
949 TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
950 const ::testing::TestInfo*
const test_info =
953 Message test_name_stream;
954 test_name_stream <<
"TestsReportCorrectNames/" << sum_;
955 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
961 StatefulNamingFunctor());
969 explicit Unstreamable(
int value) : value_(value) {}
975 class CommentTest :
public TestWithParam<Unstreamable> {};
977 TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
978 const ::testing::TestInfo*
const test_info =
986 Values(Unstreamable(1)));
994 NonParameterizedBaseTest() : n_(17) { }
999 class ParameterizedDerivedTest :
public NonParameterizedBaseTest,
1000 public ::testing::WithParamInterface<int> {
1002 ParameterizedDerivedTest() : count_(0) { }
1004 static int global_count_;
1007 int ParameterizedDerivedTest::global_count_ = 0;
1009 TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
1013 TEST_P(ParameterizedDerivedTest, SeesSequence) {
1019 class ParameterizedDeathTest :
public ::testing::TestWithParam<int> { };
1021 TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
1023 ".* value-parameterized test .*");
1028 #endif // GTEST_HAS_PARAM_TEST
1030 TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {
1031 #if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST
1032 FAIL() <<
"GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n"
1037 #if GTEST_HAS_PARAM_TEST
1042 GeneratorEvaluationTest::set_param_value(1);
1043 #endif // GTEST_HAS_PARAM_TEST
1047 #if GTEST_HAS_PARAM_TEST
1051 GeneratorEvaluationTest::set_param_value(2);
1052 #endif // GTEST_HAS_PARAM_TEST