• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Type and function utilities for implementing parameterized tests.
31 
32 // IWYU pragma: private, include "gtest/gtest.h"
33 // IWYU pragma: friend gtest/.*
34 // IWYU pragma: friend gmock/.*
35 
36 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
37 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
38 
39 #include <ctype.h>
40 
41 #include <cassert>
42 #include <iterator>
43 #include <memory>
44 #include <set>
45 #include <tuple>
46 #include <type_traits>
47 #include <utility>
48 #include <vector>
49 
50 #include "gtest/gtest-printers.h"
51 #include "gtest/gtest-test-part.h"
52 #include "gtest/internal/gtest-internal.h"
53 #include "gtest/internal/gtest-port.h"
54 
55 namespace testing {
56 // Input to a parameterized test name generator, describing a test parameter.
57 // Consists of the parameter value and the integer parameter index.
58 template <class ParamType>
59 struct TestParamInfo {
TestParamInfoTestParamInfo60   TestParamInfo(const ParamType& a_param, size_t an_index)
61       : param(a_param), index(an_index) {}
62   ParamType param;
63   size_t index;
64 };
65 
66 // A builtin parameterized test name generator which returns the result of
67 // testing::PrintToString.
68 struct PrintToStringParamName {
69   template <class ParamType>
operatorPrintToStringParamName70   std::string operator()(const TestParamInfo<ParamType>& info) const {
71     return PrintToString(info.param);
72   }
73 };
74 
75 namespace internal {
76 
77 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
78 // Utility Functions
79 
80 // Outputs a message explaining invalid registration of different
81 // fixture class for the same test suite. This may happen when
82 // TEST_P macro is used to define two tests with the same name
83 // but in different namespaces.
84 GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name,
85                                            CodeLocation code_location);
86 
87 template <typename>
88 class ParamGeneratorInterface;
89 template <typename>
90 class ParamGenerator;
91 
92 // Interface for iterating over elements provided by an implementation
93 // of ParamGeneratorInterface<T>.
94 template <typename T>
95 class ParamIteratorInterface {
96  public:
~ParamIteratorInterface()97   virtual ~ParamIteratorInterface() {}
98   // A pointer to the base generator instance.
99   // Used only for the purposes of iterator comparison
100   // to make sure that two iterators belong to the same generator.
101   virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
102   // Advances iterator to point to the next element
103   // provided by the generator. The caller is responsible
104   // for not calling Advance() on an iterator equal to
105   // BaseGenerator()->End().
106   virtual void Advance() = 0;
107   // Clones the iterator object. Used for implementing copy semantics
108   // of ParamIterator<T>.
109   virtual ParamIteratorInterface* Clone() const = 0;
110   // Dereferences the current iterator and provides (read-only) access
111   // to the pointed value. It is the caller's responsibility not to call
112   // Current() on an iterator equal to BaseGenerator()->End().
113   // Used for implementing ParamGenerator<T>::operator*().
114   virtual const T* Current() const = 0;
115   // Determines whether the given iterator and other point to the same
116   // element in the sequence generated by the generator.
117   // Used for implementing ParamGenerator<T>::operator==().
118   virtual bool Equals(const ParamIteratorInterface& other) const = 0;
119 };
120 
121 // Class iterating over elements provided by an implementation of
122 // ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
123 // and implements the const forward iterator concept.
124 template <typename T>
125 class ParamIterator {
126  public:
127   typedef T value_type;
128   typedef const T& reference;
129   typedef ptrdiff_t difference_type;
130 
131   // ParamIterator assumes ownership of the impl_ pointer.
ParamIterator(const ParamIterator & other)132   ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {}
133   ParamIterator& operator=(const ParamIterator& other) {
134     if (this != &other) impl_.reset(other.impl_->Clone());
135     return *this;
136   }
137 
138   const T& operator*() const { return *impl_->Current(); }
139   const T* operator->() const { return impl_->Current(); }
140   // Prefix version of operator++.
141   ParamIterator& operator++() {
142     impl_->Advance();
143     return *this;
144   }
145   // Postfix version of operator++.
146   ParamIterator operator++(int /*unused*/) {
147     ParamIteratorInterface<T>* clone = impl_->Clone();
148     impl_->Advance();
149     return ParamIterator(clone);
150   }
151   bool operator==(const ParamIterator& other) const {
152     return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
153   }
154   bool operator!=(const ParamIterator& other) const {
155     return !(*this == other);
156   }
157 
158  private:
159   friend class ParamGenerator<T>;
ParamIterator(ParamIteratorInterface<T> * impl)160   explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
161   std::unique_ptr<ParamIteratorInterface<T>> impl_;
162 };
163 
164 // ParamGeneratorInterface<T> is the binary interface to access generators
165 // defined in other translation units.
166 template <typename T>
167 class ParamGeneratorInterface {
168  public:
169   typedef T ParamType;
170 
~ParamGeneratorInterface()171   virtual ~ParamGeneratorInterface() {}
172 
173   // Generator interface definition
174   virtual ParamIteratorInterface<T>* Begin() const = 0;
175   virtual ParamIteratorInterface<T>* End() const = 0;
176 };
177 
178 // Wraps ParamGeneratorInterface<T> and provides general generator syntax
179 // compatible with the STL Container concept.
180 // This class implements copy initialization semantics and the contained
181 // ParamGeneratorInterface<T> instance is shared among all copies
182 // of the original object. This is possible because that instance is immutable.
183 template <typename T>
184 class ParamGenerator {
185  public:
186   typedef ParamIterator<T> iterator;
187 
ParamGenerator(ParamGeneratorInterface<T> * impl)188   explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
ParamGenerator(const ParamGenerator & other)189   ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {}
190 
191   ParamGenerator& operator=(const ParamGenerator& other) {
192     impl_ = other.impl_;
193     return *this;
194   }
195 
begin()196   iterator begin() const { return iterator(impl_->Begin()); }
end()197   iterator end() const { return iterator(impl_->End()); }
198 
199  private:
200   std::shared_ptr<const ParamGeneratorInterface<T>> impl_;
201 };
202 
203 // Generates values from a range of two comparable values. Can be used to
204 // generate sequences of user-defined types that implement operator+() and
205 // operator<().
206 // This class is used in the Range() function.
207 template <typename T, typename IncrementT>
208 class RangeGenerator : public ParamGeneratorInterface<T> {
209  public:
RangeGenerator(T begin,T end,IncrementT step)210   RangeGenerator(T begin, T end, IncrementT step)
211       : begin_(begin),
212         end_(end),
213         step_(step),
214         end_index_(CalculateEndIndex(begin, end, step)) {}
~RangeGenerator()215   ~RangeGenerator() override {}
216 
Begin()217   ParamIteratorInterface<T>* Begin() const override {
218     return new Iterator(this, begin_, 0, step_);
219   }
End()220   ParamIteratorInterface<T>* End() const override {
221     return new Iterator(this, end_, end_index_, step_);
222   }
223 
224  private:
225   class Iterator : public ParamIteratorInterface<T> {
226    public:
Iterator(const ParamGeneratorInterface<T> * base,T value,int index,IncrementT step)227     Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
228              IncrementT step)
229         : base_(base), value_(value), index_(index), step_(step) {}
~Iterator()230     ~Iterator() override {}
231 
BaseGenerator()232     const ParamGeneratorInterface<T>* BaseGenerator() const override {
233       return base_;
234     }
Advance()235     void Advance() override {
236       value_ = static_cast<T>(value_ + step_);
237       index_++;
238     }
Clone()239     ParamIteratorInterface<T>* Clone() const override {
240       return new Iterator(*this);
241     }
Current()242     const T* Current() const override { return &value_; }
Equals(const ParamIteratorInterface<T> & other)243     bool Equals(const ParamIteratorInterface<T>& other) const override {
244       // Having the same base generator guarantees that the other
245       // iterator is of the same type and we can downcast.
246       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
247           << "The program attempted to compare iterators "
248           << "from different generators." << std::endl;
249       const int other_index =
250           CheckedDowncastToActualType<const Iterator>(&other)->index_;
251       return index_ == other_index;
252     }
253 
254    private:
Iterator(const Iterator & other)255     Iterator(const Iterator& other)
256         : ParamIteratorInterface<T>(),
257           base_(other.base_),
258           value_(other.value_),
259           index_(other.index_),
260           step_(other.step_) {}
261 
262     // No implementation - assignment is unsupported.
263     void operator=(const Iterator& other);
264 
265     const ParamGeneratorInterface<T>* const base_;
266     T value_;
267     int index_;
268     const IncrementT step_;
269   };  // class RangeGenerator::Iterator
270 
CalculateEndIndex(const T & begin,const T & end,const IncrementT & step)271   static int CalculateEndIndex(const T& begin, const T& end,
272                                const IncrementT& step) {
273     int end_index = 0;
274     for (T i = begin; i < end; i = static_cast<T>(i + step)) end_index++;
275     return end_index;
276   }
277 
278   // No implementation - assignment is unsupported.
279   void operator=(const RangeGenerator& other);
280 
281   const T begin_;
282   const T end_;
283   const IncrementT step_;
284   // The index for the end() iterator. All the elements in the generated
285   // sequence are indexed (0-based) to aid iterator comparison.
286   const int end_index_;
287 };  // class RangeGenerator
288 
289 // Generates values from a pair of STL-style iterators. Used in the
290 // ValuesIn() function. The elements are copied from the source range
291 // since the source can be located on the stack, and the generator
292 // is likely to persist beyond that stack frame.
293 template <typename T>
294 class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
295  public:
296   template <typename ForwardIterator>
ValuesInIteratorRangeGenerator(ForwardIterator begin,ForwardIterator end)297   ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
298       : container_(begin, end) {}
~ValuesInIteratorRangeGenerator()299   ~ValuesInIteratorRangeGenerator() override {}
300 
Begin()301   ParamIteratorInterface<T>* Begin() const override {
302     return new Iterator(this, container_.begin());
303   }
End()304   ParamIteratorInterface<T>* End() const override {
305     return new Iterator(this, container_.end());
306   }
307 
308  private:
309   typedef typename ::std::vector<T> ContainerType;
310 
311   class Iterator : public ParamIteratorInterface<T> {
312    public:
Iterator(const ParamGeneratorInterface<T> * base,typename ContainerType::const_iterator iterator)313     Iterator(const ParamGeneratorInterface<T>* base,
314              typename ContainerType::const_iterator iterator)
315         : base_(base), iterator_(iterator) {}
~Iterator()316     ~Iterator() override {}
317 
BaseGenerator()318     const ParamGeneratorInterface<T>* BaseGenerator() const override {
319       return base_;
320     }
Advance()321     void Advance() override {
322       ++iterator_;
323       value_.reset();
324     }
Clone()325     ParamIteratorInterface<T>* Clone() const override {
326       return new Iterator(*this);
327     }
328     // We need to use cached value referenced by iterator_ because *iterator_
329     // can return a temporary object (and of type other then T), so just
330     // having "return &*iterator_;" doesn't work.
331     // value_ is updated here and not in Advance() because Advance()
332     // can advance iterator_ beyond the end of the range, and we cannot
333     // detect that fact. The client code, on the other hand, is
334     // responsible for not calling Current() on an out-of-range iterator.
Current()335     const T* Current() const override {
336       if (value_.get() == nullptr) value_.reset(new T(*iterator_));
337       return value_.get();
338     }
Equals(const ParamIteratorInterface<T> & other)339     bool Equals(const ParamIteratorInterface<T>& other) const override {
340       // Having the same base generator guarantees that the other
341       // iterator is of the same type and we can downcast.
342       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
343           << "The program attempted to compare iterators "
344           << "from different generators." << std::endl;
345       return iterator_ ==
346              CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
347     }
348 
349    private:
Iterator(const Iterator & other)350     Iterator(const Iterator& other)
351         // The explicit constructor call suppresses a false warning
352         // emitted by gcc when supplied with the -Wextra option.
353         : ParamIteratorInterface<T>(),
354           base_(other.base_),
355           iterator_(other.iterator_) {}
356 
357     const ParamGeneratorInterface<T>* const base_;
358     typename ContainerType::const_iterator iterator_;
359     // A cached value of *iterator_. We keep it here to allow access by
360     // pointer in the wrapping iterator's operator->().
361     // value_ needs to be mutable to be accessed in Current().
362     // Use of std::unique_ptr helps manage cached value's lifetime,
363     // which is bound by the lifespan of the iterator itself.
364     mutable std::unique_ptr<const T> value_;
365   };  // class ValuesInIteratorRangeGenerator::Iterator
366 
367   // No implementation - assignment is unsupported.
368   void operator=(const ValuesInIteratorRangeGenerator& other);
369 
370   const ContainerType container_;
371 };  // class ValuesInIteratorRangeGenerator
372 
373 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
374 //
375 // Default parameterized test name generator, returns a string containing the
376 // integer test parameter index.
377 template <class ParamType>
DefaultParamName(const TestParamInfo<ParamType> & info)378 std::string DefaultParamName(const TestParamInfo<ParamType>& info) {
379   Message name_stream;
380   name_stream << info.index;
381   return name_stream.GetString();
382 }
383 
384 template <typename T = int>
TestNotEmpty()385 void TestNotEmpty() {
386   static_assert(sizeof(T) == 0, "Empty arguments are not allowed.");
387 }
388 template <typename T = int>
TestNotEmpty(const T &)389 void TestNotEmpty(const T&) {}
390 
391 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
392 //
393 // Stores a parameter value and later creates tests parameterized with that
394 // value.
395 template <class TestClass>
396 class ParameterizedTestFactory : public TestFactoryBase {
397  public:
398   typedef typename TestClass::ParamType ParamType;
ParameterizedTestFactory(ParamType parameter)399   explicit ParameterizedTestFactory(ParamType parameter)
400       : parameter_(parameter) {}
CreateTest()401   Test* CreateTest() override {
402     TestClass::SetParam(&parameter_);
403     return new TestClass();
404   }
405 
406  private:
407   const ParamType parameter_;
408 
409   ParameterizedTestFactory(const ParameterizedTestFactory&) = delete;
410   ParameterizedTestFactory& operator=(const ParameterizedTestFactory&) = delete;
411 };
412 
413 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
414 //
415 // TestMetaFactoryBase is a base class for meta-factories that create
416 // test factories for passing into MakeAndRegisterTestInfo function.
417 template <class ParamType>
418 class TestMetaFactoryBase {
419  public:
~TestMetaFactoryBase()420   virtual ~TestMetaFactoryBase() {}
421 
422   virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
423 };
424 
425 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
426 //
427 // TestMetaFactory creates test factories for passing into
428 // MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
429 // ownership of test factory pointer, same factory object cannot be passed
430 // into that method twice. But ParameterizedTestSuiteInfo is going to call
431 // it for each Test/Parameter value combination. Thus it needs meta factory
432 // creator class.
433 template <class TestSuite>
434 class TestMetaFactory
435     : public TestMetaFactoryBase<typename TestSuite::ParamType> {
436  public:
437   using ParamType = typename TestSuite::ParamType;
438 
TestMetaFactory()439   TestMetaFactory() {}
440 
CreateTestFactory(ParamType parameter)441   TestFactoryBase* CreateTestFactory(ParamType parameter) override {
442     return new ParameterizedTestFactory<TestSuite>(parameter);
443   }
444 
445  private:
446   TestMetaFactory(const TestMetaFactory&) = delete;
447   TestMetaFactory& operator=(const TestMetaFactory&) = delete;
448 };
449 
450 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
451 //
452 // ParameterizedTestSuiteInfoBase is a generic interface
453 // to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase
454 // accumulates test information provided by TEST_P macro invocations
455 // and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations
456 // and uses that information to register all resulting test instances
457 // in RegisterTests method. The ParameterizeTestSuiteRegistry class holds
458 // a collection of pointers to the ParameterizedTestSuiteInfo objects
459 // and calls RegisterTests() on each of them when asked.
460 class ParameterizedTestSuiteInfoBase {
461  public:
~ParameterizedTestSuiteInfoBase()462   virtual ~ParameterizedTestSuiteInfoBase() {}
463 
464   // Base part of test suite name for display purposes.
465   virtual const std::string& GetTestSuiteName() const = 0;
466   // Test suite id to verify identity.
467   virtual TypeId GetTestSuiteTypeId() const = 0;
468   // UnitTest class invokes this method to register tests in this
469   // test suite right before running them in RUN_ALL_TESTS macro.
470   // This method should not be called more than once on any single
471   // instance of a ParameterizedTestSuiteInfoBase derived class.
472   virtual void RegisterTests() = 0;
473 
474  protected:
ParameterizedTestSuiteInfoBase()475   ParameterizedTestSuiteInfoBase() {}
476 
477  private:
478   ParameterizedTestSuiteInfoBase(const ParameterizedTestSuiteInfoBase&) =
479       delete;
480   ParameterizedTestSuiteInfoBase& operator=(
481       const ParameterizedTestSuiteInfoBase&) = delete;
482 };
483 
484 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
485 //
486 // Report a the name of a test_suit as safe to ignore
487 // as the side effect of construction of this type.
488 struct GTEST_API_ MarkAsIgnored {
489   explicit MarkAsIgnored(const char* test_suite);
490 };
491 
492 GTEST_API_ void InsertSyntheticTestCase(const std::string& name,
493                                         CodeLocation location, bool has_test_p);
494 
495 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
496 //
497 // ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P
498 // macro invocations for a particular test suite and generators
499 // obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that
500 // test suite. It registers tests with all values generated by all
501 // generators when asked.
502 template <class TestSuite>
503 class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
504  public:
505   // ParamType and GeneratorCreationFunc are private types but are required
506   // for declarations of public methods AddTestPattern() and
507   // AddTestSuiteInstantiation().
508   using ParamType = typename TestSuite::ParamType;
509   // A function that returns an instance of appropriate generator type.
510   typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
511   using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&);
512 
ParameterizedTestSuiteInfo(const char * name,CodeLocation code_location)513   explicit ParameterizedTestSuiteInfo(const char* name,
514                                       CodeLocation code_location)
515       : test_suite_name_(name), code_location_(code_location) {}
516 
517   // Test suite base name for display purposes.
GetTestSuiteName()518   const std::string& GetTestSuiteName() const override {
519     return test_suite_name_;
520   }
521   // Test suite id to verify identity.
GetTestSuiteTypeId()522   TypeId GetTestSuiteTypeId() const override { return GetTypeId<TestSuite>(); }
523   // TEST_P macro uses AddTestPattern() to record information
524   // about a single test in a LocalTestInfo structure.
525   // test_suite_name is the base name of the test suite (without invocation
526   // prefix). test_base_name is the name of an individual test without
527   // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
528   // test suite base name and DoBar is test base name.
AddTestPattern(const char * test_suite_name,const char * test_base_name,TestMetaFactoryBase<ParamType> * meta_factory,CodeLocation code_location)529   void AddTestPattern(const char* test_suite_name, const char* test_base_name,
530                       TestMetaFactoryBase<ParamType>* meta_factory,
531                       CodeLocation code_location) {
532     tests_.push_back(std::shared_ptr<TestInfo>(new TestInfo(
533         test_suite_name, test_base_name, meta_factory, code_location)));
534   }
535   // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information
536   // about a generator.
AddTestSuiteInstantiation(const std::string & instantiation_name,GeneratorCreationFunc * func,ParamNameGeneratorFunc * name_func,const char * file,int line)537   int AddTestSuiteInstantiation(const std::string& instantiation_name,
538                                 GeneratorCreationFunc* func,
539                                 ParamNameGeneratorFunc* name_func,
540                                 const char* file, int line) {
541     instantiations_.push_back(
542         InstantiationInfo(instantiation_name, func, name_func, file, line));
543     return 0;  // Return value used only to run this method in namespace scope.
544   }
545   // UnitTest class invokes this method to register tests in this test suite
546   // right before running tests in RUN_ALL_TESTS macro.
547   // This method should not be called more than once on any single
548   // instance of a ParameterizedTestSuiteInfoBase derived class.
549   // UnitTest has a guard to prevent from calling this method more than once.
RegisterTests()550   void RegisterTests() override {
551     bool generated_instantiations = false;
552 
553     for (typename TestInfoContainer::iterator test_it = tests_.begin();
554          test_it != tests_.end(); ++test_it) {
555       std::shared_ptr<TestInfo> test_info = *test_it;
556       for (typename InstantiationContainer::iterator gen_it =
557                instantiations_.begin();
558            gen_it != instantiations_.end(); ++gen_it) {
559         const std::string& instantiation_name = gen_it->name;
560         ParamGenerator<ParamType> generator((*gen_it->generator)());
561         ParamNameGeneratorFunc* name_func = gen_it->name_func;
562         const char* file = gen_it->file;
563         int line = gen_it->line;
564 
565         std::string test_suite_name;
566         if (!instantiation_name.empty())
567           test_suite_name = instantiation_name + "/";
568         test_suite_name += test_info->test_suite_base_name;
569 
570         size_t i = 0;
571         std::set<std::string> test_param_names;
572         for (typename ParamGenerator<ParamType>::iterator param_it =
573                  generator.begin();
574              param_it != generator.end(); ++param_it, ++i) {
575           generated_instantiations = true;
576 
577           Message test_name_stream;
578 
579           std::string param_name =
580               name_func(TestParamInfo<ParamType>(*param_it, i));
581 
582           GTEST_CHECK_(IsValidParamName(param_name))
583               << "Parameterized test name '" << param_name
584               << "' is invalid, in " << file << " line " << line << std::endl;
585 
586           GTEST_CHECK_(test_param_names.count(param_name) == 0)
587               << "Duplicate parameterized test name '" << param_name << "', in "
588               << file << " line " << line << std::endl;
589 
590           test_param_names.insert(param_name);
591 
592           if (!test_info->test_base_name.empty()) {
593             test_name_stream << test_info->test_base_name << "/";
594           }
595           test_name_stream << param_name;
596           MakeAndRegisterTestInfo(
597               test_suite_name.c_str(), test_name_stream.GetString().c_str(),
598               nullptr,  // No type parameter.
599               PrintToString(*param_it).c_str(), test_info->code_location,
600               GetTestSuiteTypeId(),
601               SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line),
602               SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line),
603               test_info->test_meta_factory->CreateTestFactory(*param_it));
604         }  // for param_it
605       }    // for gen_it
606     }      // for test_it
607 
608     if (!generated_instantiations) {
609       // There are no generaotrs, or they all generate nothing ...
610       InsertSyntheticTestCase(GetTestSuiteName(), code_location_,
611                               !tests_.empty());
612     }
613   }  // RegisterTests
614 
615  private:
616   // LocalTestInfo structure keeps information about a single test registered
617   // with TEST_P macro.
618   struct TestInfo {
TestInfoTestInfo619     TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name,
620              TestMetaFactoryBase<ParamType>* a_test_meta_factory,
621              CodeLocation a_code_location)
622         : test_suite_base_name(a_test_suite_base_name),
623           test_base_name(a_test_base_name),
624           test_meta_factory(a_test_meta_factory),
625           code_location(a_code_location) {}
626 
627     const std::string test_suite_base_name;
628     const std::string test_base_name;
629     const std::unique_ptr<TestMetaFactoryBase<ParamType>> test_meta_factory;
630     const CodeLocation code_location;
631   };
632   using TestInfoContainer = ::std::vector<std::shared_ptr<TestInfo>>;
633   // Records data received from INSTANTIATE_TEST_SUITE_P macros:
634   //  <Instantiation name, Sequence generator creation function,
635   //     Name generator function, Source file, Source line>
636   struct InstantiationInfo {
InstantiationInfoInstantiationInfo637     InstantiationInfo(const std::string& name_in,
638                       GeneratorCreationFunc* generator_in,
639                       ParamNameGeneratorFunc* name_func_in, const char* file_in,
640                       int line_in)
641         : name(name_in),
642           generator(generator_in),
643           name_func(name_func_in),
644           file(file_in),
645           line(line_in) {}
646 
647     std::string name;
648     GeneratorCreationFunc* generator;
649     ParamNameGeneratorFunc* name_func;
650     const char* file;
651     int line;
652   };
653   typedef ::std::vector<InstantiationInfo> InstantiationContainer;
654 
IsValidParamName(const std::string & name)655   static bool IsValidParamName(const std::string& name) {
656     // Check for empty string
657     if (name.empty()) return false;
658 
659     // Check for invalid characters
660     for (std::string::size_type index = 0; index < name.size(); ++index) {
661       if (!IsAlNum(name[index]) && name[index] != '_') return false;
662     }
663 
664     return true;
665   }
666 
667   const std::string test_suite_name_;
668   CodeLocation code_location_;
669   TestInfoContainer tests_;
670   InstantiationContainer instantiations_;
671 
672   ParameterizedTestSuiteInfo(const ParameterizedTestSuiteInfo&) = delete;
673   ParameterizedTestSuiteInfo& operator=(const ParameterizedTestSuiteInfo&) =
674       delete;
675 };  // class ParameterizedTestSuiteInfo
676 
677 //  Legacy API is deprecated but still available
678 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
679 template <class TestCase>
680 using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<TestCase>;
681 #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
682 
683 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
684 //
685 // ParameterizedTestSuiteRegistry contains a map of
686 // ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P
687 // and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding
688 // ParameterizedTestSuiteInfo descriptors.
689 class ParameterizedTestSuiteRegistry {
690  public:
ParameterizedTestSuiteRegistry()691   ParameterizedTestSuiteRegistry() {}
~ParameterizedTestSuiteRegistry()692   ~ParameterizedTestSuiteRegistry() {
693     for (auto& test_suite_info : test_suite_infos_) {
694       delete test_suite_info;
695     }
696   }
697 
698   // Looks up or creates and returns a structure containing information about
699   // tests and instantiations of a particular test suite.
700   template <class TestSuite>
GetTestSuitePatternHolder(const char * test_suite_name,CodeLocation code_location)701   ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder(
702       const char* test_suite_name, CodeLocation code_location) {
703     ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr;
704     for (auto& test_suite_info : test_suite_infos_) {
705       if (test_suite_info->GetTestSuiteName() == test_suite_name) {
706         if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
707           // Complain about incorrect usage of Google Test facilities
708           // and terminate the program since we cannot guaranty correct
709           // test suite setup and tear-down in this case.
710           ReportInvalidTestSuiteType(test_suite_name, code_location);
711           posix::Abort();
712         } else {
713           // At this point we are sure that the object we found is of the same
714           // type we are looking for, so we downcast it to that type
715           // without further checks.
716           typed_test_info = CheckedDowncastToActualType<
717               ParameterizedTestSuiteInfo<TestSuite>>(test_suite_info);
718         }
719         break;
720       }
721     }
722     if (typed_test_info == nullptr) {
723       typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>(
724           test_suite_name, code_location);
725       test_suite_infos_.push_back(typed_test_info);
726     }
727     return typed_test_info;
728   }
RegisterTests()729   void RegisterTests() {
730     for (auto& test_suite_info : test_suite_infos_) {
731       test_suite_info->RegisterTests();
732     }
733   }
734 //  Legacy API is deprecated but still available
735 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
736   template <class TestCase>
GetTestCasePatternHolder(const char * test_case_name,CodeLocation code_location)737   ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
738       const char* test_case_name, CodeLocation code_location) {
739     return GetTestSuitePatternHolder<TestCase>(test_case_name, code_location);
740   }
741 
742 #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
743 
744  private:
745   using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
746 
747   TestSuiteInfoContainer test_suite_infos_;
748 
749   ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) =
750       delete;
751   ParameterizedTestSuiteRegistry& operator=(
752       const ParameterizedTestSuiteRegistry&) = delete;
753 };
754 
755 // Keep track of what type-parameterized test suite are defined and
756 // where as well as which are intatiated. This allows susequently
757 // identifying suits that are defined but never used.
758 class TypeParameterizedTestSuiteRegistry {
759  public:
760   // Add a suite definition
761   void RegisterTestSuite(const char* test_suite_name,
762                          CodeLocation code_location);
763 
764   // Add an instantiation of a suit.
765   void RegisterInstantiation(const char* test_suite_name);
766 
767   // For each suit repored as defined but not reported as instantiation,
768   // emit a test that reports that fact (configurably, as an error).
769   void CheckForInstantiations();
770 
771  private:
772   struct TypeParameterizedTestSuiteInfo {
TypeParameterizedTestSuiteInfoTypeParameterizedTestSuiteInfo773     explicit TypeParameterizedTestSuiteInfo(CodeLocation c)
774         : code_location(c), instantiated(false) {}
775 
776     CodeLocation code_location;
777     bool instantiated;
778   };
779 
780   std::map<std::string, TypeParameterizedTestSuiteInfo> suites_;
781 };
782 
783 }  // namespace internal
784 
785 // Forward declarations of ValuesIn(), which is implemented in
786 // include/gtest/gtest-param-test.h.
787 template <class Container>
788 internal::ParamGenerator<typename Container::value_type> ValuesIn(
789     const Container& container);
790 
791 namespace internal {
792 // Used in the Values() function to provide polymorphic capabilities.
793 
794 #ifdef _MSC_VER
795 #pragma warning(push)
796 #pragma warning(disable : 4100)
797 #endif
798 
799 template <typename... Ts>
800 class ValueArray {
801  public:
ValueArray(Ts...v)802   explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {}
803 
804   template <typename T>
805   operator ParamGenerator<T>() const {  // NOLINT
806     return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>()));
807   }
808 
809  private:
810   template <typename T, size_t... I>
MakeVector(IndexSequence<I...>)811   std::vector<T> MakeVector(IndexSequence<I...>) const {
812     return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
813   }
814 
815   FlatTuple<Ts...> v_;
816 };
817 
818 #ifdef _MSC_VER
819 #pragma warning(pop)
820 #endif
821 
822 template <typename... T>
823 class CartesianProductGenerator
824     : public ParamGeneratorInterface<::std::tuple<T...>> {
825  public:
826   typedef ::std::tuple<T...> ParamType;
827 
CartesianProductGenerator(const std::tuple<ParamGenerator<T>...> & g)828   CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g)
829       : generators_(g) {}
~CartesianProductGenerator()830   ~CartesianProductGenerator() override {}
831 
Begin()832   ParamIteratorInterface<ParamType>* Begin() const override {
833     return new Iterator(this, generators_, false);
834   }
End()835   ParamIteratorInterface<ParamType>* End() const override {
836     return new Iterator(this, generators_, true);
837   }
838 
839  private:
840   template <class I>
841   class IteratorImpl;
842   template <size_t... I>
843   class IteratorImpl<IndexSequence<I...>>
844       : public ParamIteratorInterface<ParamType> {
845    public:
IteratorImpl(const ParamGeneratorInterface<ParamType> * base,const std::tuple<ParamGenerator<T>...> & generators,bool is_end)846     IteratorImpl(const ParamGeneratorInterface<ParamType>* base,
847                  const std::tuple<ParamGenerator<T>...>& generators,
848                  bool is_end)
849         : base_(base),
850           begin_(std::get<I>(generators).begin()...),
851           end_(std::get<I>(generators).end()...),
852           current_(is_end ? end_ : begin_) {
853       ComputeCurrentValue();
854     }
~IteratorImpl()855     ~IteratorImpl() override {}
856 
BaseGenerator()857     const ParamGeneratorInterface<ParamType>* BaseGenerator() const override {
858       return base_;
859     }
860     // Advance should not be called on beyond-of-range iterators
861     // so no component iterators must be beyond end of range, either.
Advance()862     void Advance() override {
863       assert(!AtEnd());
864       // Advance the last iterator.
865       ++std::get<sizeof...(T) - 1>(current_);
866       // if that reaches end, propagate that up.
867       AdvanceIfEnd<sizeof...(T) - 1>();
868       ComputeCurrentValue();
869     }
Clone()870     ParamIteratorInterface<ParamType>* Clone() const override {
871       return new IteratorImpl(*this);
872     }
873 
Current()874     const ParamType* Current() const override { return current_value_.get(); }
875 
Equals(const ParamIteratorInterface<ParamType> & other)876     bool Equals(const ParamIteratorInterface<ParamType>& other) const override {
877       // Having the same base generator guarantees that the other
878       // iterator is of the same type and we can downcast.
879       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
880           << "The program attempted to compare iterators "
881           << "from different generators." << std::endl;
882       const IteratorImpl* typed_other =
883           CheckedDowncastToActualType<const IteratorImpl>(&other);
884 
885       // We must report iterators equal if they both point beyond their
886       // respective ranges. That can happen in a variety of fashions,
887       // so we have to consult AtEnd().
888       if (AtEnd() && typed_other->AtEnd()) return true;
889 
890       bool same = true;
891       bool dummy[] = {
892           (same = same && std::get<I>(current_) ==
893                               std::get<I>(typed_other->current_))...};
894       (void)dummy;
895       return same;
896     }
897 
898    private:
899     template <size_t ThisI>
AdvanceIfEnd()900     void AdvanceIfEnd() {
901       if (std::get<ThisI>(current_) != std::get<ThisI>(end_)) return;
902 
903       bool last = ThisI == 0;
904       if (last) {
905         // We are done. Nothing else to propagate.
906         return;
907       }
908 
909       constexpr size_t NextI = ThisI - (ThisI != 0);
910       std::get<ThisI>(current_) = std::get<ThisI>(begin_);
911       ++std::get<NextI>(current_);
912       AdvanceIfEnd<NextI>();
913     }
914 
ComputeCurrentValue()915     void ComputeCurrentValue() {
916       if (!AtEnd())
917         current_value_ = std::make_shared<ParamType>(*std::get<I>(current_)...);
918     }
AtEnd()919     bool AtEnd() const {
920       bool at_end = false;
921       bool dummy[] = {
922           (at_end = at_end || std::get<I>(current_) == std::get<I>(end_))...};
923       (void)dummy;
924       return at_end;
925     }
926 
927     const ParamGeneratorInterface<ParamType>* const base_;
928     std::tuple<typename ParamGenerator<T>::iterator...> begin_;
929     std::tuple<typename ParamGenerator<T>::iterator...> end_;
930     std::tuple<typename ParamGenerator<T>::iterator...> current_;
931     std::shared_ptr<ParamType> current_value_;
932   };
933 
934   using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>;
935 
936   std::tuple<ParamGenerator<T>...> generators_;
937 };
938 
939 template <class... Gen>
940 class CartesianProductHolder {
941  public:
CartesianProductHolder(const Gen &...g)942   CartesianProductHolder(const Gen&... g) : generators_(g...) {}
943   template <typename... T>
944   operator ParamGenerator<::std::tuple<T...>>() const {
945     return ParamGenerator<::std::tuple<T...>>(
946         new CartesianProductGenerator<T...>(generators_));
947   }
948 
949  private:
950   std::tuple<Gen...> generators_;
951 };
952 
953 }  // namespace internal
954 }  // namespace testing
955 
956 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
957