]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/googletest/googletest/test/googletest-param-test-test.cc
MFC r345203,r345205,r345353,r345645,r345708,r345709,r345735,r345770,r346574,r346576:
[FreeBSD/FreeBSD.git] / contrib / googletest / googletest / test / googletest-param-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 //
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.
34
35 #include "gtest/gtest.h"
36
37 # include <algorithm>
38 # include <iostream>
39 # include <list>
40 # include <sstream>
41 # include <string>
42 # include <vector>
43
44 # include "src/gtest-internal-inl.h"  // for UnitTestOptions
45 # include "test/googletest-param-test-test.h"
46
47 using ::std::vector;
48 using ::std::sort;
49
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;
57
58 # if GTEST_HAS_COMBINE
59 using ::testing::Combine;
60 using ::testing::get;
61 using ::testing::make_tuple;
62 using ::testing::tuple;
63 # endif  // GTEST_HAS_COMBINE
64
65 using ::testing::internal::ParamGenerator;
66 using ::testing::internal::UnitTestOptions;
67
68 // Prints a value to a string.
69 //
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.
74 template <typename T>
75 ::std::string PrintValue(const T& value) {
76   ::std::stringstream stream;
77   stream << value;
78   return stream.str();
79 }
80
81 # if GTEST_HAS_COMBINE
82
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.
88
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) << ")";
93   return stream.str();
94 }
95
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) << ")";
101   return stream.str();
102 }
103
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) << ")";
114   return stream.str();
115 }
116
117 # endif  // GTEST_HAS_COMBINE
118
119 // Verifies that a sequence generated by the generator and accessed
120 // via the iterator object matches the expected one using Google Test
121 // assertions.
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";
137     ++it;
138   }
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";
142
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
146   // just the same.
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";
157     ++it;
158   }
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";
162 }
163
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());
168
169   it = generator.begin();
170   EXPECT_TRUE(it == generator.end());
171 }
172
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.
179
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();
185
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";
190
191   // Verifies that iterator assignment works as expected.
192   ++it;
193   EXPECT_FALSE(*it == *it2);
194   it2 = it;
195   EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
196                            << "element same as its source points to";
197
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";
201
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++));
207
208   // Verifies that prefix and postfix operator++() advance an iterator
209   // all the same.
210   it2 = it;
211   ++it;
212   ++it2;
213   EXPECT_TRUE(*it == *it2);
214 }
215
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);
221 }
222
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);
229 }
230
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);
236 }
237
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);
244 }
245
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);
254 }
255
256 // Verifies that Range works with user-defined types that define
257 // copy constructor, operator=(), operator+(), and operator<().
258 class DogAdder {
259  public:
260   explicit DogAdder(const char* a_value) : value_(a_value) {}
261   DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
262
263   DogAdder operator=(const DogAdder& other) {
264     if (this != &other)
265       value_ = other.value_;
266     return *this;
267   }
268   DogAdder operator+(const DogAdder& other) const {
269     Message msg;
270     msg << value_.c_str() << other.value_.c_str();
271     return DogAdder(msg.GetString().c_str());
272   }
273   bool operator<(const DogAdder& other) const {
274     return value_ < other.value_;
275   }
276   const std::string& value() const { return value_; }
277
278  private:
279   std::string value_;
280 };
281
282 TEST(RangeTest, WorksWithACustomType) {
283   const ParamGenerator<DogAdder> gen =
284       Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
285   ParamGenerator<DogAdder>::iterator it = gen.begin();
286
287   ASSERT_FALSE(it == gen.end());
288   EXPECT_STREQ("cat", it->value().c_str());
289
290   ASSERT_FALSE(++it == gen.end());
291   EXPECT_STREQ("catdog", it->value().c_str());
292
293   EXPECT_TRUE(++it == gen.end());
294 }
295
296 class IntWrapper {
297  public:
298   explicit IntWrapper(int a_value) : value_(a_value) {}
299   IntWrapper(const IntWrapper& other) : value_(other.value_) {}
300
301   IntWrapper operator=(const IntWrapper& other) {
302     value_ = other.value_;
303     return *this;
304   }
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_;
309   }
310   int value() const { return value_; }
311
312  private:
313   int value_;
314 };
315
316 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
317   const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
318   ParamGenerator<IntWrapper>::iterator it = gen.begin();
319
320   ASSERT_FALSE(it == gen.end());
321   EXPECT_EQ(0, it->value());
322
323   ASSERT_FALSE(++it == gen.end());
324   EXPECT_EQ(1, it->value());
325
326   EXPECT_TRUE(++it == gen.end());
327 }
328
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);
335 }
336
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);
343 }
344
345 // Edge case. Tests that ValuesIn() with an array parameter containing a
346 // single element generates the single element sequence.
347 TEST(ValuesInTest, ValuesInSingleElementArray) {
348   int array[] = {42};
349   const ParamGenerator<int> gen = ValuesIn(array);
350   VerifyGenerator(gen, array);
351 }
352
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;
358   values.push_back(3);
359   values.push_back(5);
360   values.push_back(8);
361   const ParamGenerator<int> gen = ValuesIn(values);
362
363   const int expected_values[] = {3, 5, 8};
364   VerifyGenerator(gen, expected_values);
365 }
366
367 // Tests that ValuesIn() generates the expected sequence.
368 TEST(ValuesInTest, ValuesInIteratorRange) {
369   typedef ::std::vector<int> ContainerType;
370   ContainerType values;
371   values.push_back(3);
372   values.push_back(5);
373   values.push_back(8);
374   const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
375
376   const int expected_values[] = {3, 5, 8};
377   VerifyGenerator(gen, expected_values);
378 }
379
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());
387
388   const int expected_values[] = {42};
389   VerifyGenerator(gen, expected_values);
390 }
391
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());
398
399   VerifyGeneratorIsEmpty(gen);
400 }
401
402 // Tests that the Values() generates the expected sequence.
403 TEST(ValuesTest, ValuesWorks) {
404   const ParamGenerator<int> gen = Values(3, 5, 8);
405
406   const int expected_values[] = {3, 5, 8};
407   VerifyGenerator(gen, expected_values);
408 }
409
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);
414
415   const double expected_values[] = {3.0, 5.0, 8.0};
416   VerifyGenerator(gen, expected_values);
417 }
418
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);
426
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);
434 }
435
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);
440
441   const int expected_values[] = {42};
442   VerifyGenerator(gen, expected_values);
443 }
444
445 // Tests that Bool() generates sequence (false, true).
446 TEST(BoolTest, BoolWorks) {
447   const ParamGenerator<bool> gen = Bool();
448
449   const bool expected_values[] = {false, true};
450   VerifyGenerator(gen, expected_values);
451 }
452
453 # if GTEST_HAS_COMBINE
454
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));
461
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);
466 }
467
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),
471                                                             Values(3, 4),
472                                                             Values(5, 6));
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);
479 }
480
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),
486                                                        Values(0, 1));
487
488   tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)};
489   VerifyGenerator(gen, expected_values);
490 }
491
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),
497                                                        Values(42));
498
499   tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)};
500   VerifyGenerator(gen, expected_values);
501 }
502
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),
507                                                        Values(0, 1));
508   VerifyGeneratorIsEmpty(gen);
509 }
510
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),
515                                                        Range(1, 1));
516   VerifyGeneratorIsEmpty(gen);
517 }
518
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),
530                                                        Values(9));
531
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);
536 }
537
538 #if GTEST_LANG_CXX11
539
540 class NonDefaultConstructAssignString {
541  public:
542   NonDefaultConstructAssignString(const std::string& s) : str_(s) {}
543
544   const std::string& str() const { return str_; }
545
546  private:
547   std::string str_;
548
549   // Not default constructible
550   NonDefaultConstructAssignString();
551   // Not assignable
552   void operator=(const NonDefaultConstructAssignString&);
553 };
554
555 TEST(CombineTest, NonDefaultConstructAssign) {
556   const ParamGenerator<tuple<int, NonDefaultConstructAssignString> > gen =
557       Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"),
558                                    NonDefaultConstructAssignString("B")));
559
560   ParamGenerator<tuple<int, NonDefaultConstructAssignString> >::iterator it =
561       gen.begin();
562
563   EXPECT_EQ(0, std::get<0>(*it));
564   EXPECT_EQ("A", std::get<1>(*it).str());
565   ++it;
566
567   EXPECT_EQ(0, std::get<0>(*it));
568   EXPECT_EQ("B", std::get<1>(*it).str());
569   ++it;
570
571   EXPECT_EQ(1, std::get<0>(*it));
572   EXPECT_EQ("A", std::get<1>(*it).str());
573   ++it;
574
575   EXPECT_EQ(1, std::get<0>(*it));
576   EXPECT_EQ("B", std::get<1>(*it).str());
577   ++it;
578
579   EXPECT_TRUE(it == gen.end());
580 }
581
582 #endif   // GTEST_LANG_CXX11
583 # endif  // GTEST_HAS_COMBINE
584
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);
590   gen = gen2;
591
592   const int expected_values[] = {3, 4};
593   VerifyGenerator(gen, expected_values);
594 }
595
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.
601
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 {
607  public:
608   static TestGenerationEnvironment* Instance() {
609     static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
610     return instance;
611   }
612
613   void FixtureConstructorExecuted() { fixture_constructor_count_++; }
614   void SetUpExecuted() { set_up_count_++; }
615   void TearDownExecuted() { tear_down_count_++; }
616   void TestBodyExecuted() { test_body_count_++; }
617
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;
622
623     for (int i = 0; i < kExpectedCalls; ++i) {
624       Message msg;
625       msg << "TestsExpandedAndRun/" << i;
626       if (UnitTestOptions::FilterMatchesTest(
627              "TestExpansionModule/MultipleTestGenerationTest",
628               msg.GetString().c_str())) {
629         perform_check = true;
630       }
631     }
632     if (perform_check) {
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.";
645     }
646   }
647
648  private:
649   TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
650                                 tear_down_count_(0), test_body_count_(0) {}
651
652   int fixture_constructor_count_;
653   int set_up_count_;
654   int tear_down_count_;
655   int test_body_count_;
656
657   GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
658 };
659
660 const int test_generation_params[] = {36, 42, 72};
661
662 class TestGenerationTest : public TestWithParam<int> {
663  public:
664   enum {
665     PARAMETER_COUNT =
666         sizeof(test_generation_params)/sizeof(test_generation_params[0])
667   };
668
669   typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
670
671   TestGenerationTest() {
672     Environment::Instance()->FixtureConstructorExecuted();
673     current_parameter_ = GetParam();
674   }
675   virtual void SetUp() {
676     Environment::Instance()->SetUpExecuted();
677     EXPECT_EQ(current_parameter_, GetParam());
678   }
679   virtual void TearDown() {
680     Environment::Instance()->TearDownExecuted();
681     EXPECT_EQ(current_parameter_, GetParam());
682   }
683
684   static void SetUpTestCase() {
685     bool all_tests_in_test_case_selected = true;
686
687     for (int i = 0; i < PARAMETER_COUNT; ++i) {
688       Message test_name;
689       test_name << "TestsExpandedAndRun/" << i;
690       if ( !UnitTestOptions::FilterMatchesTest(
691                 "TestExpansionModule/MultipleTestGenerationTest",
692                 test_name.GetString())) {
693         all_tests_in_test_case_selected = false;
694       }
695     }
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.";
701
702     collected_parameters_.clear();
703   }
704
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());
713
714     EXPECT_TRUE(collected_parameters_ == expected_values);
715   }
716
717  protected:
718   int current_parameter_;
719   static vector<int> collected_parameters_;
720
721  private:
722   GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
723 };
724 vector<int> TestGenerationTest::collected_parameters_;
725
726 TEST_P(TestGenerationTest, TestsExpandedAndRun) {
727   Environment::Instance()->TestBodyExecuted();
728   EXPECT_EQ(current_parameter_, GetParam());
729   collected_parameters_.push_back(GetParam());
730 }
731 INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest,
732                         ValuesIn(test_generation_params));
733
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
743 // assertion.
744 class GeneratorEvaluationTest : public TestWithParam<int> {
745  public:
746   static int param_value() { return param_value_; }
747   static void set_param_value(int param_value) { param_value_ = param_value; }
748
749  private:
750   static int param_value_;
751 };
752 int GeneratorEvaluationTest::param_value_ = 0;
753
754 TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
755   EXPECT_EQ(1, GetParam());
756 }
757 INSTANTIATE_TEST_CASE_P(GenEvalModule,
758                         GeneratorEvaluationTest,
759                         Values(GeneratorEvaluationTest::param_value()));
760
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);
769 }
770 INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule,
771                         ExternalGeneratorTest,
772                         extern_gen);
773
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);
780 }
781
782 // Tests that a parameterized test case can be instantiated with multiple
783 // generators.
784 class MultipleInstantiationTest : public TestWithParam<int> {};
785 TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
786 }
787 INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
788 INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
789
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);
797 }
798 INSTANTIATE_TEST_CASE_P(Sequence1,
799                         InstantiationInMultipleTranslaionUnitsTest,
800                         Values(42, 42*2));
801
802 // Tests that each iteration of parameterized test runs in a separate test
803 // object.
804 class SeparateInstanceTest : public TestWithParam<int> {
805  public:
806   SeparateInstanceTest() : count_(0) {}
807
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.";
814   }
815
816  protected:
817   int count_;
818   static int global_count_;
819 };
820 int SeparateInstanceTest::global_count_ = 0;
821
822 TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
823   EXPECT_EQ(0, count_++);
824   global_count_++;
825 }
826 INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
827
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> {};
834
835 TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
836   const ::testing::TestInfo* const test_info =
837      ::testing::UnitTest::GetInstance()->current_test_info();
838
839   EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
840
841   Message index_stream;
842   index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
843   EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
844
845   EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
846 }
847
848 INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
849
850 // Tests that macros in test names are expanded correctly.
851 class MacroNamingTest : public TestWithParam<int> {};
852
853 #define PREFIX_WITH_FOO(test_name) Foo##test_name
854 #define PREFIX_WITH_MACRO(test_name) Macro##test_name
855
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();
859
860   EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_case_name());
861   EXPECT_STREQ("FooSomeTestName", test_info->name());
862 }
863
864 INSTANTIATE_TEST_CASE_P(FortyTwo, MacroNamingTest, Values(42));
865
866 // Tests the same thing for non-parametrized tests.
867 class MacroNamingTestNonParametrized : public ::testing::Test {};
868
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();
873
874   EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_case_name());
875   EXPECT_STREQ("FooSomeTestName", test_info->name());
876 }
877
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) {}
884
885 struct CustomParamNameFunctor {
886   std::string operator()(const ::testing::TestParamInfo<std::string>& inf) {
887     return inf.param;
888   }
889 };
890
891 INSTANTIATE_TEST_CASE_P(CustomParamNameFunctor,
892                         CustomFunctorNamingTest,
893                         Values(std::string("FunctorName")),
894                         CustomParamNameFunctor());
895
896 INSTANTIATE_TEST_CASE_P(AllAllowedCharacters,
897                         CustomFunctorNamingTest,
898                         Values("abcdefghijklmnopqrstuvwxyz",
899                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
900                                "01234567890_"),
901                         CustomParamNameFunctor());
902
903 inline std::string CustomParamNameFunction(
904     const ::testing::TestParamInfo<std::string>& inf) {
905   return inf.param;
906 }
907
908 class CustomFunctionNamingTest : public TestWithParam<std::string> {};
909 TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
910
911 INSTANTIATE_TEST_CASE_P(CustomParamNameFunction,
912                         CustomFunctionNamingTest,
913                         Values(std::string("FunctionName")),
914                         CustomParamNameFunction);
915
916 #if GTEST_LANG_CXX11
917
918 // Test custom naming with a lambda
919
920 class CustomLambdaNamingTest : public TestWithParam<std::string> {};
921 TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
922
923 INSTANTIATE_TEST_CASE_P(CustomParamNameLambda, CustomLambdaNamingTest,
924                         Values(std::string("LambdaName")),
925                         [](const ::testing::TestParamInfo<std::string>& inf) {
926                           return inf.param;
927                         });
928
929 #endif  // GTEST_LANG_CXX11
930
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();
936        ++case_num) {
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();
940          ++test_num) {
941       const ::testing::TestInfo* test_info = test_case->GetTestInfo(test_num);
942       test_names.insert(std::string(test_info->name()));
943     }
944   }
945   EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctorName"));
946   EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionName"));
947 #if GTEST_LANG_CXX11
948   EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName"));
949 #endif  // GTEST_LANG_CXX11
950 }
951
952 // Test a numeric name to ensure PrintToStringParamName works correctly.
953
954 class CustomIntegerNamingTest : public TestWithParam<int> {};
955
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());
962 }
963
964 INSTANTIATE_TEST_CASE_P(PrintToString,
965                         CustomIntegerNamingTest,
966                         Range(0, 5),
967                         ::testing::PrintToStringParamName());
968
969 // Test a custom struct with PrintToString.
970
971 struct CustomStruct {
972   explicit CustomStruct(int value) : x(value) {}
973   int x;
974 };
975
976 std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) {
977   stream << val.x;
978   return stream;
979 }
980
981 class CustomStructNamingTest : public TestWithParam<CustomStruct> {};
982
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());
989 }
990
991 INSTANTIATE_TEST_CASE_P(PrintToString,
992                         CustomStructNamingTest,
993                         Values(CustomStruct(0), CustomStruct(1)),
994                         ::testing::PrintToStringParamName());
995
996 // Test that using a stateful parameter naming function works as expected.
997
998 struct StatefulNamingFunctor {
999   StatefulNamingFunctor() : sum(0) {}
1000   std::string operator()(const ::testing::TestParamInfo<int>& info) {
1001     int value = info.param + sum;
1002     sum += info.param;
1003     return ::testing::PrintToString(value);
1004   }
1005   int sum;
1006 };
1007
1008 class StatefulNamingTest : public ::testing::TestWithParam<int> {
1009  protected:
1010   StatefulNamingTest() : sum_(0) {}
1011   int sum_;
1012 };
1013
1014 TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
1015   const ::testing::TestInfo* const test_info =
1016      ::testing::UnitTest::GetInstance()->current_test_info();
1017   sum_ += GetParam();
1018   Message test_name_stream;
1019   test_name_stream << "TestsReportCorrectNames/" << sum_;
1020   EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
1021 }
1022
1023 INSTANTIATE_TEST_CASE_P(StatefulNamingFunctor,
1024                         StatefulNamingTest,
1025                         Range(0, 5),
1026                         StatefulNamingFunctor());
1027
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
1031 // what we need.
1032 class Unstreamable {
1033  public:
1034   explicit Unstreamable(int value) : value_(value) {}
1035
1036  private:
1037   int value_;
1038 };
1039
1040 class CommentTest : public TestWithParam<Unstreamable> {};
1041
1042 TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
1043   const ::testing::TestInfo* const test_info =
1044      ::testing::UnitTest::GetInstance()->current_test_info();
1045
1046   EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
1047 }
1048
1049 INSTANTIATE_TEST_CASE_P(InstantiationWithComments,
1050                         CommentTest,
1051                         Values(Unstreamable(1)));
1052
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 {
1058  public:
1059   NonParameterizedBaseTest() : n_(17) { }
1060  protected:
1061   int n_;
1062 };
1063
1064 class ParameterizedDerivedTest : public NonParameterizedBaseTest,
1065                                  public ::testing::WithParamInterface<int> {
1066  protected:
1067   ParameterizedDerivedTest() : count_(0) { }
1068   int count_;
1069   static int global_count_;
1070 };
1071
1072 int ParameterizedDerivedTest::global_count_ = 0;
1073
1074 TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
1075   EXPECT_EQ(17, n_);
1076 }
1077
1078 TEST_P(ParameterizedDerivedTest, SeesSequence) {
1079   EXPECT_EQ(17, n_);
1080   EXPECT_EQ(0, count_++);
1081   EXPECT_EQ(GetParam(), global_count_++);
1082 }
1083
1084 class ParameterizedDeathTest : public ::testing::TestWithParam<int> { };
1085
1086 TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
1087   EXPECT_DEATH_IF_SUPPORTED(GetParam(),
1088                             ".* value-parameterized test .*");
1089 }
1090
1091 INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5));
1092
1093
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);
1100
1101   ::testing::InitGoogleTest(&argc, argv);
1102
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);
1107
1108   return RUN_ALL_TESTS();
1109 }