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 // Author: vladl@google.com (Vlad Losev)
31 //
32 // Tests for Google Test itself. This file verifies that the parameter
33 // generators objects produce correct parameter sequences and that
34 // Google Test runtime instantiates correct tests from those sequences.
35 
36 #include <gtest/gtest.h>
37 
38 #if GTEST_HAS_PARAM_TEST
39 
40 #include <algorithm>
41 #include <iostream>
42 #include <list>
43 #include <sstream>
44 #include <string>
45 #include <vector>
46 
47 // To include gtest-internal-inl.h.
48 #define GTEST_IMPLEMENTATION_ 1
49 #include "src/gtest-internal-inl.h"  // for UnitTestOptions
50 #undef GTEST_IMPLEMENTATION_
51 
52 #include "test/gtest-param-test_test.h"
53 
54 using ::std::vector;
55 using ::std::sort;
56 
57 using ::testing::AddGlobalTestEnvironment;
58 using ::testing::Bool;
59 using ::testing::Message;
60 using ::testing::Range;
61 using ::testing::TestWithParam;
62 using ::testing::Values;
63 using ::testing::ValuesIn;
64 
65 #if GTEST_HAS_COMBINE
66 using ::testing::Combine;
67 using ::std::tr1::get;
68 using ::std::tr1::make_tuple;
69 using ::std::tr1::tuple;
70 #endif  // GTEST_HAS_COMBINE
71 
72 using ::testing::internal::ParamGenerator;
73 using ::testing::internal::UnitTestOptions;
74 
75 // Prints a value to a string.
76 //
77 // TODO(wan@google.com): remove PrintValue() when we move matchers and
78 // EXPECT_THAT() from Google Mock to Google Test.  At that time, we
79 // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
80 // EXPECT_THAT() and the matchers know how to print tuples.
81 template <typename T>
PrintValue(const T & value)82 ::std::string PrintValue(const T& value) {
83   ::std::stringstream stream;
84   stream << value;
85   return stream.str();
86 }
87 
88 #if GTEST_HAS_COMBINE
89 
90 // These overloads allow printing tuples in our tests.  We cannot
91 // define an operator<< for tuples, as that definition needs to be in
92 // the std namespace in order to be picked up by Google Test via
93 // Argument-Dependent Lookup, yet defining anything in the std
94 // namespace in non-STL code is undefined behavior.
95 
96 template <typename T1, typename T2>
PrintValue(const tuple<T1,T2> & value)97 ::std::string PrintValue(const tuple<T1, T2>& value) {
98   ::std::stringstream stream;
99   stream << "(" << get<0>(value) << ", " << get<1>(value) << ")";
100   return stream.str();
101 }
102 
103 template <typename T1, typename T2, typename T3>
PrintValue(const tuple<T1,T2,T3> & value)104 ::std::string PrintValue(const tuple<T1, T2, T3>& value) {
105   ::std::stringstream stream;
106   stream << "(" << get<0>(value) << ", " << get<1>(value)
107          << ", "<< get<2>(value) << ")";
108   return stream.str();
109 }
110 
111 template <typename T1, typename T2, typename T3, typename T4, typename T5,
112           typename T6, typename T7, typename T8, typename T9, typename T10>
PrintValue(const tuple<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10> & value)113 ::std::string PrintValue(
114     const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) {
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) << ")";
121   return stream.str();
122 }
123 
124 #endif  // GTEST_HAS_COMBINE
125 
126 // Verifies that a sequence generated by the generator and accessed
127 // via the iterator object matches the expected one using Google Test
128 // assertions.
129 template <typename T, size_t N>
VerifyGenerator(const ParamGenerator<T> & generator,const T (& expected_values)[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) {
134     ASSERT_FALSE(it == generator.end())
135         << "At element " << i << " when accessing via an iterator "
136         << "created with the copy constructor.\n";
137     // We cannot use EXPECT_EQ() here as the values may be tuples,
138     // which don't support <<.
139     EXPECT_TRUE(expected_values[i] == *it)
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";
144     it++;
145   }
146   EXPECT_TRUE(it == generator.end())
147         << "At the presumed end of sequence when accessing via an iterator "
148         << "created with the copy constructor.\n";
149 
150   // Test the iterator assignment. The following lines verify that
151   // the sequence accessed via an iterator initialized via the
152   // assignment operator (as opposed to a copy constructor) matches
153   // just the same.
154   it = generator.begin();
155   for (size_t i = 0; i < N; ++i) {
156     ASSERT_FALSE(it == generator.end())
157         << "At element " << i << " when accessing via an iterator "
158         << "created with the assignment operator.\n";
159     EXPECT_TRUE(expected_values[i] == *it)
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";
164     it++;
165   }
166   EXPECT_TRUE(it == generator.end())
167         << "At the presumed end of sequence when accessing via an iterator "
168         << "created with the assignment operator.\n";
169 }
170 
171 template <typename T>
VerifyGeneratorIsEmpty(const ParamGenerator<T> & generator)172 void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
173   typename ParamGenerator<T>::iterator it = generator.begin();
174   EXPECT_TRUE(it == generator.end());
175 
176   it = generator.begin();
177   EXPECT_TRUE(it == generator.end());
178 }
179 
180 // Generator tests. They test that each of the provided generator functions
181 // generates an expected sequence of values. The general test pattern
182 // instantiates a generator using one of the generator functions,
183 // checks the sequence produced by the generator using its iterator API,
184 // and then resets the iterator back to the beginning of the sequence
185 // and checks the sequence again.
186 
187 // Tests that iterators produced by generator functions conform to the
188 // ForwardIterator concept.
TEST(IteratorTest,ParamIteratorConformsToForwardIteratorConcept)189 TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
190   const ParamGenerator<int> gen = Range(0, 10);
191   ParamGenerator<int>::iterator it = gen.begin();
192 
193   // Verifies that iterator initialization works as expected.
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";
197 
198   // Verifies that iterator assignment works as expected.
199   it++;
200   EXPECT_FALSE(*it == *it2);
201   it2 = it;
202   EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
203                            << "element same as its source points to";
204 
205   // Verifies that prefix operator++() returns *this.
206   EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
207                           << "refer to the original object";
208 
209   // Verifies that the result of the postfix operator++ points to the value
210   // pointed to by the original iterator.
211   int original_value = *it;  // Have to compute it outside of macro call to be
212                              // unaffected by the parameter evaluation order.
213   EXPECT_EQ(original_value, *(it++));
214 
215   // Verifies that prefix and postfix operator++() advance an iterator
216   // all the same.
217   it2 = it;
218   it++;
219   ++it2;
220   EXPECT_TRUE(*it == *it2);
221 }
222 
223 // Tests that Range() generates the expected sequence.
TEST(RangeTest,IntRangeWithDefaultStep)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);
228 }
229 
230 // Edge case. Tests that Range() generates the single element sequence
231 // as expected when provided with range limits that are equal.
TEST(RangeTest,IntRangeSingleValue)232 TEST(RangeTest, IntRangeSingleValue) {
233   const ParamGenerator<int> gen = Range(0, 1);
234   const int expected_values[] = {0};
235   VerifyGenerator(gen, expected_values);
236 }
237 
238 // Edge case. Tests that Range() with generates empty sequence when
239 // supplied with an empty range.
TEST(RangeTest,IntRangeEmpty)240 TEST(RangeTest, IntRangeEmpty) {
241   const ParamGenerator<int> gen = Range(0, 0);
242   VerifyGeneratorIsEmpty(gen);
243 }
244 
245 // Tests that Range() with custom step (greater then one) generates
246 // the expected sequence.
TEST(RangeTest,IntRangeWithCustomStep)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);
251 }
252 
253 // Tests that Range() with custom step (greater then one) generates
254 // the expected sequence when the last element does not fall on the
255 // upper range limit. Sequences generated by Range() must not have
256 // elements beyond the range limits.
TEST(RangeTest,IntRangeWithCustomStepOverUpperBound)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);
261 }
262 
263 // Verifies that Range works with user-defined types that define
264 // copy constructor, operator=(), operator+(), and operator<().
265 class DogAdder {
266  public:
DogAdder(const char * a_value)267   explicit DogAdder(const char* a_value) : value_(a_value) {}
DogAdder(const DogAdder & other)268   DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
269 
operator =(const DogAdder & other)270   DogAdder operator=(const DogAdder& other) {
271     if (this != &other)
272       value_ = other.value_;
273     return *this;
274   }
operator +(const DogAdder & other) const275   DogAdder operator+(const DogAdder& other) const {
276     Message msg;
277     msg << value_.c_str() << other.value_.c_str();
278     return DogAdder(msg.GetString().c_str());
279   }
operator <(const DogAdder & other) const280   bool operator<(const DogAdder& other) const {
281     return value_ < other.value_;
282   }
value() const283   const ::testing::internal::String& value() const { return value_; }
284 
285  private:
286   ::testing::internal::String value_;
287 };
288 
TEST(RangeTest,WorksWithACustomType)289 TEST(RangeTest, WorksWithACustomType) {
290   const ParamGenerator<DogAdder> gen =
291       Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
292   ParamGenerator<DogAdder>::iterator it = gen.begin();
293 
294   ASSERT_FALSE(it == gen.end());
295   EXPECT_STREQ("cat", it->value().c_str());
296 
297   ASSERT_FALSE(++it == gen.end());
298   EXPECT_STREQ("catdog", it->value().c_str());
299 
300   EXPECT_TRUE(++it == gen.end());
301 }
302 
303 class IntWrapper {
304  public:
IntWrapper(int a_value)305   explicit IntWrapper(int a_value) : value_(a_value) {}
IntWrapper(const IntWrapper & other)306   IntWrapper(const IntWrapper& other) : value_(other.value_) {}
307 
operator =(const IntWrapper & other)308   IntWrapper operator=(const IntWrapper& other) {
309     value_ = other.value_;
310     return *this;
311   }
312   // operator+() adds a different type.
operator +(int other) const313   IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
operator <(const IntWrapper & other) const314   bool operator<(const IntWrapper& other) const {
315     return value_ < other.value_;
316   }
value() const317   int value() const { return value_; }
318 
319  private:
320   int value_;
321 };
322 
TEST(RangeTest,WorksWithACustomTypeWithDifferentIncrementType)323 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
324   const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
325   ParamGenerator<IntWrapper>::iterator it = gen.begin();
326 
327   ASSERT_FALSE(it == gen.end());
328   EXPECT_EQ(0, it->value());
329 
330   ASSERT_FALSE(++it == gen.end());
331   EXPECT_EQ(1, it->value());
332 
333   EXPECT_TRUE(++it == gen.end());
334 }
335 
336 // Tests that ValuesIn() with an array parameter generates
337 // the expected sequence.
TEST(ValuesInTest,ValuesInArray)338 TEST(ValuesInTest, ValuesInArray) {
339   int array[] = {3, 5, 8};
340   const ParamGenerator<int> gen = ValuesIn(array);
341   VerifyGenerator(gen, array);
342 }
343 
344 // Tests that ValuesIn() with a const array parameter generates
345 // the expected sequence.
TEST(ValuesInTest,ValuesInConstArray)346 TEST(ValuesInTest, ValuesInConstArray) {
347   const int array[] = {3, 5, 8};
348   const ParamGenerator<int> gen = ValuesIn(array);
349   VerifyGenerator(gen, array);
350 }
351 
352 // Edge case. Tests that ValuesIn() with an array parameter containing a
353 // single element generates the single element sequence.
TEST(ValuesInTest,ValuesInSingleElementArray)354 TEST(ValuesInTest, ValuesInSingleElementArray) {
355   int array[] = {42};
356   const ParamGenerator<int> gen = ValuesIn(array);
357   VerifyGenerator(gen, array);
358 }
359 
360 // Tests that ValuesIn() generates the expected sequence for an STL
361 // container (vector).
TEST(ValuesInTest,ValuesInVector)362 TEST(ValuesInTest, ValuesInVector) {
363   typedef ::std::vector<int> ContainerType;
364   ContainerType values;
365   values.push_back(3);
366   values.push_back(5);
367   values.push_back(8);
368   const ParamGenerator<int> gen = ValuesIn(values);
369 
370   const int expected_values[] = {3, 5, 8};
371   VerifyGenerator(gen, expected_values);
372 }
373 
374 // Tests that ValuesIn() generates the expected sequence.
TEST(ValuesInTest,ValuesInIteratorRange)375 TEST(ValuesInTest, ValuesInIteratorRange) {
376   typedef ::std::vector<int> ContainerType;
377   ContainerType values;
378   values.push_back(3);
379   values.push_back(5);
380   values.push_back(8);
381   const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
382 
383   const int expected_values[] = {3, 5, 8};
384   VerifyGenerator(gen, expected_values);
385 }
386 
387 // Edge case. Tests that ValuesIn() provided with an iterator range specifying a
388 // single value generates a single-element sequence.
TEST(ValuesInTest,ValuesInSingleElementIteratorRange)389 TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
390   typedef ::std::vector<int> ContainerType;
391   ContainerType values;
392   values.push_back(42);
393   const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
394 
395   const int expected_values[] = {42};
396   VerifyGenerator(gen, expected_values);
397 }
398 
399 // Edge case. Tests that ValuesIn() provided with an empty iterator range
400 // generates an empty sequence.
TEST(ValuesInTest,ValuesInEmptyIteratorRange)401 TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
402   typedef ::std::vector<int> ContainerType;
403   ContainerType values;
404   const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
405 
406   VerifyGeneratorIsEmpty(gen);
407 }
408 
409 // Tests that the Values() generates the expected sequence.
TEST(ValuesTest,ValuesWorks)410 TEST(ValuesTest, ValuesWorks) {
411   const ParamGenerator<int> gen = Values(3, 5, 8);
412 
413   const int expected_values[] = {3, 5, 8};
414   VerifyGenerator(gen, expected_values);
415 }
416 
417 // Tests that Values() generates the expected sequences from elements of
418 // different types convertible to ParamGenerator's parameter type.
TEST(ValuesTest,ValuesWorksForValuesOfCompatibleTypes)419 TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
420   const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
421 
422   const double expected_values[] = {3.0, 5.0, 8.0};
423   VerifyGenerator(gen, expected_values);
424 }
425 
TEST(ValuesTest,ValuesWorksForMaxLengthList)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);
433 
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);
441 }
442 
443 // Edge case test. Tests that single-parameter Values() generates the sequence
444 // with the single value.
TEST(ValuesTest,ValuesWithSingleParameter)445 TEST(ValuesTest, ValuesWithSingleParameter) {
446   const ParamGenerator<int> gen = Values(42);
447 
448   const int expected_values[] = {42};
449   VerifyGenerator(gen, expected_values);
450 }
451 
452 // Tests that Bool() generates sequence (false, true).
TEST(BoolTest,BoolWorks)453 TEST(BoolTest, BoolWorks) {
454   const ParamGenerator<bool> gen = Bool();
455 
456   const bool expected_values[] = {false, true};
457   VerifyGenerator(gen, expected_values);
458 }
459 
460 #if GTEST_HAS_COMBINE
461 
462 // Tests that Combine() with two parameters generates the expected sequence.
TEST(CombineTest,CombineWithTwoParameters)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));
468 
469   tuple<const char*, int> expected_values[] = {
470     make_tuple(foo, 3), make_tuple(foo, 4),
471     make_tuple(bar, 3), make_tuple(bar, 4)};
472   VerifyGenerator(gen, expected_values);
473 }
474 
475 // Tests that Combine() with three parameters generates the expected sequence.
TEST(CombineTest,CombineWithThreeParameters)476 TEST(CombineTest, CombineWithThreeParameters) {
477   const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1),
478                                                             Values(3, 4),
479                                                             Values(5, 6));
480   tuple<int, int, int> expected_values[] = {
481     make_tuple(0, 3, 5), make_tuple(0, 3, 6),
482     make_tuple(0, 4, 5), make_tuple(0, 4, 6),
483     make_tuple(1, 3, 5), make_tuple(1, 3, 6),
484     make_tuple(1, 4, 5), make_tuple(1, 4, 6)};
485   VerifyGenerator(gen, expected_values);
486 }
487 
488 // Tests that the Combine() with the first parameter generating a single value
489 // sequence generates a sequence with the number of elements equal to the
490 // number of elements in the sequence generated by the second parameter.
TEST(CombineTest,CombineWithFirstParameterSingleValue)491 TEST(CombineTest, CombineWithFirstParameterSingleValue) {
492   const ParamGenerator<tuple<int, int> > gen = Combine(Values(42),
493                                                        Values(0, 1));
494 
495   tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)};
496   VerifyGenerator(gen, expected_values);
497 }
498 
499 // Tests that the Combine() with the second parameter generating a single value
500 // sequence generates a sequence with the number of elements equal to the
501 // number of elements in the sequence generated by the first parameter.
TEST(CombineTest,CombineWithSecondParameterSingleValue)502 TEST(CombineTest, CombineWithSecondParameterSingleValue) {
503   const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
504                                                        Values(42));
505 
506   tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)};
507   VerifyGenerator(gen, expected_values);
508 }
509 
510 // Tests that when the first parameter produces an empty sequence,
511 // Combine() produces an empty sequence, too.
TEST(CombineTest,CombineWithFirstParameterEmptyRange)512 TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
513   const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0),
514                                                        Values(0, 1));
515   VerifyGeneratorIsEmpty(gen);
516 }
517 
518 // Tests that when the second parameter produces an empty sequence,
519 // Combine() produces an empty sequence, too.
TEST(CombineTest,CombineWithSecondParameterEmptyRange)520 TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
521   const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
522                                                        Range(1, 1));
523   VerifyGeneratorIsEmpty(gen);
524 }
525 
526 // Edge case. Tests that combine works with the maximum number
527 // of parameters supported by Google Test (currently 10).
TEST(CombineTest,CombineWithMaxNumberOfParameters)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),
537                                                        Values(9));
538 
539   tuple<const char*, int, int, int, int, int, int, int, int, int>
540       expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
541                            make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
542   VerifyGenerator(gen, expected_values);
543 }
544 
545 #endif  // GTEST_HAS_COMBINE
546 
547 // Tests that an generator produces correct sequence after being
548 // assigned from another generator.
TEST(ParamGeneratorTest,AssignmentWorks)549 TEST(ParamGeneratorTest, AssignmentWorks) {
550   ParamGenerator<int> gen = Values(1, 2);
551   const ParamGenerator<int> gen2 = Values(3, 4);
552   gen = gen2;
553 
554   const int expected_values[] = {3, 4};
555   VerifyGenerator(gen, expected_values);
556 }
557 
558 // This test verifies that the tests are expanded and run as specified:
559 // one test per element from the sequence produced by the generator
560 // specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's
561 // fixture constructor, SetUp(), and TearDown() have run and have been
562 // supplied with the correct parameters.
563 
564 // The use of environment object allows detection of the case where no test
565 // case functionality is run at all. In this case TestCaseTearDown will not
566 // be able to detect missing tests, naturally.
567 template <int kExpectedCalls>
568 class TestGenerationEnvironment : public ::testing::Environment {
569  public:
Instance()570   static TestGenerationEnvironment* Instance() {
571     static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
572     return instance;
573   }
574 
FixtureConstructorExecuted()575   void FixtureConstructorExecuted() { fixture_constructor_count_++; }
SetUpExecuted()576   void SetUpExecuted() { set_up_count_++; }
TearDownExecuted()577   void TearDownExecuted() { tear_down_count_++; }
TestBodyExecuted()578   void TestBodyExecuted() { test_body_count_++; }
579 
TearDown()580   virtual void TearDown() {
581     // If all MultipleTestGenerationTest tests have been de-selected
582     // by the filter flag, the following checks make no sense.
583     bool perform_check = false;
584 
585     for (int i = 0; i < kExpectedCalls; ++i) {
586       Message msg;
587       msg << "TestsExpandedAndRun/" << i;
588       if (UnitTestOptions::FilterMatchesTest(
589              "TestExpansionModule/MultipleTestGenerationTest",
590               msg.GetString().c_str())) {
591         perform_check = true;
592       }
593     }
594     if (perform_check) {
595       EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
596           << "Fixture constructor of ParamTestGenerationTest test case "
597           << "has not been run as expected.";
598       EXPECT_EQ(kExpectedCalls, set_up_count_)
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.";
607     }
608   }
609  private:
TestGenerationEnvironment()610   TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
611                                 tear_down_count_(0), test_body_count_(0) {}
612 
613   int fixture_constructor_count_;
614   int set_up_count_;
615   int tear_down_count_;
616   int test_body_count_;
617 
618   GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
619 };
620 
621 const int test_generation_params[] = {36, 42, 72};
622 
623 class TestGenerationTest : public TestWithParam<int> {
624  public:
625   enum {
626     PARAMETER_COUNT =
627         sizeof(test_generation_params)/sizeof(test_generation_params[0])
628   };
629 
630   typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
631 
TestGenerationTest()632   TestGenerationTest() {
633     Environment::Instance()->FixtureConstructorExecuted();
634     current_parameter_ = GetParam();
635   }
SetUp()636   virtual void SetUp() {
637     Environment::Instance()->SetUpExecuted();
638     EXPECT_EQ(current_parameter_, GetParam());
639   }
TearDown()640   virtual void TearDown() {
641     Environment::Instance()->TearDownExecuted();
642     EXPECT_EQ(current_parameter_, GetParam());
643   }
644 
SetUpTestCase()645   static void SetUpTestCase() {
646     bool all_tests_in_test_case_selected = true;
647 
648     for (int i = 0; i < PARAMETER_COUNT; ++i) {
649       Message test_name;
650       test_name << "TestsExpandedAndRun/" << i;
651       if ( !UnitTestOptions::FilterMatchesTest(
652                 "TestExpansionModule/MultipleTestGenerationTest",
653                 test_name.GetString())) {
654         all_tests_in_test_case_selected = false;
655       }
656     }
657     EXPECT_TRUE(all_tests_in_test_case_selected)
658         << "When running the TestGenerationTest test case all of its tests\n"
659         << "must be selected by the filter flag for the test case to pass.\n"
660         << "If not all of them are enabled, we can't reliably conclude\n"
661         << "that the correct number of tests have been generated.";
662 
663     collected_parameters_.clear();
664   }
665 
TearDownTestCase()666   static void TearDownTestCase() {
667     vector<int> expected_values(test_generation_params,
668                                 test_generation_params + PARAMETER_COUNT);
669     // Test execution order is not guaranteed by Google Test,
670     // so the order of values in collected_parameters_ can be
671     // different and we have to sort to compare.
672     sort(expected_values.begin(), expected_values.end());
673     sort(collected_parameters_.begin(), collected_parameters_.end());
674 
675     EXPECT_TRUE(collected_parameters_ == expected_values);
676   }
677  protected:
678   int current_parameter_;
679   static vector<int> collected_parameters_;
680 
681  private:
682   GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
683 };
684 vector<int> TestGenerationTest::collected_parameters_;
685 
TEST_P(TestGenerationTest,TestsExpandedAndRun)686 TEST_P(TestGenerationTest, TestsExpandedAndRun) {
687   Environment::Instance()->TestBodyExecuted();
688   EXPECT_EQ(current_parameter_, GetParam());
689   collected_parameters_.push_back(GetParam());
690 }
691 INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest,
692                         ValuesIn(test_generation_params));
693 
694 // This test verifies that the element sequence (third parameter of
695 // INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at
696 // the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS().  For
697 // that, we declare param_value_ to be a static member of
698 // GeneratorEvaluationTest and initialize it to 0.  We set it to 1 in
699 // main(), just before invocation of InitGoogleTest().  After calling
700 // InitGoogleTest(), we set the value to 2.  If the sequence is evaluated
701 // before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a
702 // test with parameter other than 1, and the test body will fail the
703 // assertion.
704 class GeneratorEvaluationTest : public TestWithParam<int> {
705  public:
param_value()706   static int param_value() { return param_value_; }
set_param_value(int param_value)707   static void set_param_value(int param_value) { param_value_ = param_value; }
708 
709  private:
710   static int param_value_;
711 };
712 int GeneratorEvaluationTest::param_value_ = 0;
713 
TEST_P(GeneratorEvaluationTest,GeneratorsEvaluatedInMain)714 TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
715   EXPECT_EQ(1, GetParam());
716 }
717 INSTANTIATE_TEST_CASE_P(GenEvalModule,
718                         GeneratorEvaluationTest,
719                         Values(GeneratorEvaluationTest::param_value()));
720 
721 // Tests that generators defined in a different translation unit are
722 // functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
723 extern ParamGenerator<int> extern_gen;
724 class ExternalGeneratorTest : public TestWithParam<int> {};
TEST_P(ExternalGeneratorTest,ExternalGenerator)725 TEST_P(ExternalGeneratorTest, ExternalGenerator) {
726   // Sequence produced by extern_gen contains only a single value
727   // which we verify here.
728   EXPECT_EQ(GetParam(), 33);
729 }
730 INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule,
731                         ExternalGeneratorTest,
732                         extern_gen);
733 
734 // Tests that a parameterized test case can be defined in one translation
735 // unit and instantiated in another. This test will be instantiated in
736 // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
737 // defined in gtest-param-test_test.h.
TEST_P(ExternalInstantiationTest,IsMultipleOf33)738 TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
739   EXPECT_EQ(0, GetParam() % 33);
740 }
741 
742 // Tests that a parameterized test case can be instantiated with multiple
743 // generators.
744 class MultipleInstantiationTest : public TestWithParam<int> {};
TEST_P(MultipleInstantiationTest,AllowsMultipleInstances)745 TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
746 }
747 INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
748 INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
749 
750 // Tests that a parameterized test case can be instantiated
751 // in multiple translation units. This test will be instantiated
752 // here and in gtest-param-test_test2.cc.
753 // InstantiationInMultipleTranslationUnitsTest fixture class
754 // is defined in gtest-param-test_test.h.
TEST_P(InstantiationInMultipleTranslaionUnitsTest,IsMultipleOf42)755 TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) {
756   EXPECT_EQ(0, GetParam() % 42);
757 }
758 INSTANTIATE_TEST_CASE_P(Sequence1,
759                         InstantiationInMultipleTranslaionUnitsTest,
760                         Values(42, 42*2));
761 
762 // Tests that each iteration of parameterized test runs in a separate test
763 // object.
764 class SeparateInstanceTest : public TestWithParam<int> {
765  public:
SeparateInstanceTest()766   SeparateInstanceTest() : count_(0) {}
767 
TearDownTestCase()768   static void TearDownTestCase() {
769     EXPECT_GE(global_count_, 2)
770         << "If some (but not all) SeparateInstanceTest tests have been "
771         << "filtered out this test will fail. Make sure that all "
772         << "GeneratorEvaluationTest are selected or de-selected together "
773         << "by the test filter.";
774   }
775 
776  protected:
777   int count_;
778   static int global_count_;
779 };
780 int SeparateInstanceTest::global_count_ = 0;
781 
TEST_P(SeparateInstanceTest,TestsRunInSeparateInstances)782 TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
783   EXPECT_EQ(0, count_++);
784   global_count_++;
785 }
786 INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
787 
788 // Tests that all instantiations of a test have named appropriately. Test
789 // defined with TEST_P(TestCaseName, TestName) and instantiated with
790 // INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named
791 // SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the
792 // sequence element used to instantiate the test.
793 class NamingTest : public TestWithParam<int> {};
794 
TEST_P(NamingTest,TestsAreNamedAppropriately)795 TEST_P(NamingTest, TestsAreNamedAppropriately) {
796   const ::testing::TestInfo* const test_info =
797      ::testing::UnitTest::GetInstance()->current_test_info();
798 
799   EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
800 
801   Message msg;
802   msg << "TestsAreNamedAppropriately/" << GetParam();
803   EXPECT_STREQ(msg.GetString().c_str(), test_info->name());
804 }
805 
806 INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
807 
808 #endif  // GTEST_HAS_PARAM_TEST
809 
TEST(CompileTest,CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined)810 TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {
811 #if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST
812   FAIL() << "GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n"
813 #endif
814 }
815 
main(int argc,char ** argv)816 int main(int argc, char **argv) {
817 #if GTEST_HAS_PARAM_TEST
818   // Used in TestGenerationTest test case.
819   AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
820   // Used in GeneratorEvaluationTest test case. Tests that the updated value
821   // will be picked up for instantiating tests in GeneratorEvaluationTest.
822   GeneratorEvaluationTest::set_param_value(1);
823 #endif  // GTEST_HAS_PARAM_TEST
824 
825   ::testing::InitGoogleTest(&argc, argv);
826 
827 #if GTEST_HAS_PARAM_TEST
828   // Used in GeneratorEvaluationTest test case. Tests that value updated
829   // here will NOT be used for instantiating tests in
830   // GeneratorEvaluationTest.
831   GeneratorEvaluationTest::set_param_value(2);
832 #endif  // GTEST_HAS_PARAM_TEST
833 
834   return RUN_ALL_TESTS();
835 }
836