1b89a7cc2SEnji Cooper // Copyright 2008 Google Inc.
2b89a7cc2SEnji Cooper // All Rights Reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper // Type and function utilities for implementing parameterized tests.
31b89a7cc2SEnji Cooper 
3228f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h"
3328f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.*
3428f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.*
35b89a7cc2SEnji Cooper 
3628f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
3728f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
38b89a7cc2SEnji Cooper 
39b89a7cc2SEnji Cooper #include <ctype.h>
40b89a7cc2SEnji Cooper 
4128f6c2f2SEnji Cooper #include <cassert>
42b89a7cc2SEnji Cooper #include <iterator>
4328f6c2f2SEnji Cooper #include <map>
4428f6c2f2SEnji Cooper #include <memory>
4528f6c2f2SEnji Cooper #include <ostream>
46b89a7cc2SEnji Cooper #include <set>
4728f6c2f2SEnji Cooper #include <string>
4828f6c2f2SEnji Cooper #include <tuple>
4928f6c2f2SEnji Cooper #include <type_traits>
50b89a7cc2SEnji Cooper #include <utility>
51b89a7cc2SEnji Cooper #include <vector>
52b89a7cc2SEnji Cooper 
53b89a7cc2SEnji Cooper #include "gtest/gtest-printers.h"
5428f6c2f2SEnji Cooper #include "gtest/gtest-test-part.h"
5528f6c2f2SEnji Cooper #include "gtest/internal/gtest-internal.h"
5628f6c2f2SEnji Cooper #include "gtest/internal/gtest-port.h"
57b89a7cc2SEnji Cooper 
58b89a7cc2SEnji Cooper namespace testing {
59b89a7cc2SEnji Cooper // Input to a parameterized test name generator, describing a test parameter.
60b89a7cc2SEnji Cooper // Consists of the parameter value and the integer parameter index.
61b89a7cc2SEnji Cooper template <class ParamType>
62b89a7cc2SEnji Cooper struct TestParamInfo {
TestParamInfoTestParamInfo6328f6c2f2SEnji Cooper   TestParamInfo(const ParamType& a_param, size_t an_index)
6428f6c2f2SEnji Cooper       : param(a_param), index(an_index) {}
65b89a7cc2SEnji Cooper   ParamType param;
66b89a7cc2SEnji Cooper   size_t index;
67b89a7cc2SEnji Cooper };
68b89a7cc2SEnji Cooper 
69b89a7cc2SEnji Cooper // A builtin parameterized test name generator which returns the result of
70b89a7cc2SEnji Cooper // testing::PrintToString.
71b89a7cc2SEnji Cooper struct PrintToStringParamName {
72b89a7cc2SEnji Cooper   template <class ParamType>
operatorPrintToStringParamName73b89a7cc2SEnji Cooper   std::string operator()(const TestParamInfo<ParamType>& info) const {
74b89a7cc2SEnji Cooper     return PrintToString(info.param);
75b89a7cc2SEnji Cooper   }
76b89a7cc2SEnji Cooper };
77b89a7cc2SEnji Cooper 
78b89a7cc2SEnji Cooper namespace internal {
79b89a7cc2SEnji Cooper 
80b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
8128f6c2f2SEnji Cooper // Utility Functions
8228f6c2f2SEnji Cooper 
83b89a7cc2SEnji Cooper // Outputs a message explaining invalid registration of different
8428f6c2f2SEnji Cooper // fixture class for the same test suite. This may happen when
85b89a7cc2SEnji Cooper // TEST_P macro is used to define two tests with the same name
86b89a7cc2SEnji Cooper // but in different namespaces.
8728f6c2f2SEnji Cooper GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name,
88b89a7cc2SEnji Cooper                                            CodeLocation code_location);
89b89a7cc2SEnji Cooper 
9028f6c2f2SEnji Cooper template <typename>
9128f6c2f2SEnji Cooper class ParamGeneratorInterface;
9228f6c2f2SEnji Cooper template <typename>
9328f6c2f2SEnji Cooper class ParamGenerator;
94b89a7cc2SEnji Cooper 
95b89a7cc2SEnji Cooper // Interface for iterating over elements provided by an implementation
96b89a7cc2SEnji Cooper // of ParamGeneratorInterface<T>.
97b89a7cc2SEnji Cooper template <typename T>
98b89a7cc2SEnji Cooper class ParamIteratorInterface {
99b89a7cc2SEnji Cooper  public:
10028f6c2f2SEnji Cooper   virtual ~ParamIteratorInterface() = default;
101b89a7cc2SEnji Cooper   // A pointer to the base generator instance.
102b89a7cc2SEnji Cooper   // Used only for the purposes of iterator comparison
103b89a7cc2SEnji Cooper   // to make sure that two iterators belong to the same generator.
104b89a7cc2SEnji Cooper   virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
105b89a7cc2SEnji Cooper   // Advances iterator to point to the next element
106b89a7cc2SEnji Cooper   // provided by the generator. The caller is responsible
107b89a7cc2SEnji Cooper   // for not calling Advance() on an iterator equal to
108b89a7cc2SEnji Cooper   // BaseGenerator()->End().
109b89a7cc2SEnji Cooper   virtual void Advance() = 0;
110b89a7cc2SEnji Cooper   // Clones the iterator object. Used for implementing copy semantics
111b89a7cc2SEnji Cooper   // of ParamIterator<T>.
112b89a7cc2SEnji Cooper   virtual ParamIteratorInterface* Clone() const = 0;
113b89a7cc2SEnji Cooper   // Dereferences the current iterator and provides (read-only) access
114b89a7cc2SEnji Cooper   // to the pointed value. It is the caller's responsibility not to call
115b89a7cc2SEnji Cooper   // Current() on an iterator equal to BaseGenerator()->End().
116b89a7cc2SEnji Cooper   // Used for implementing ParamGenerator<T>::operator*().
117b89a7cc2SEnji Cooper   virtual const T* Current() const = 0;
118b89a7cc2SEnji Cooper   // Determines whether the given iterator and other point to the same
119b89a7cc2SEnji Cooper   // element in the sequence generated by the generator.
120b89a7cc2SEnji Cooper   // Used for implementing ParamGenerator<T>::operator==().
121b89a7cc2SEnji Cooper   virtual bool Equals(const ParamIteratorInterface& other) const = 0;
122b89a7cc2SEnji Cooper };
123b89a7cc2SEnji Cooper 
124b89a7cc2SEnji Cooper // Class iterating over elements provided by an implementation of
125b89a7cc2SEnji Cooper // ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
126b89a7cc2SEnji Cooper // and implements the const forward iterator concept.
127b89a7cc2SEnji Cooper template <typename T>
128b89a7cc2SEnji Cooper class ParamIterator {
129b89a7cc2SEnji Cooper  public:
130b89a7cc2SEnji Cooper   typedef T value_type;
131b89a7cc2SEnji Cooper   typedef const T& reference;
132b89a7cc2SEnji Cooper   typedef ptrdiff_t difference_type;
133b89a7cc2SEnji Cooper 
134b89a7cc2SEnji Cooper   // ParamIterator assumes ownership of the impl_ pointer.
ParamIterator(const ParamIterator & other)135b89a7cc2SEnji Cooper   ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {}
136b89a7cc2SEnji Cooper   ParamIterator& operator=(const ParamIterator& other) {
13728f6c2f2SEnji Cooper     if (this != &other) impl_.reset(other.impl_->Clone());
138b89a7cc2SEnji Cooper     return *this;
139b89a7cc2SEnji Cooper   }
140b89a7cc2SEnji Cooper 
141b89a7cc2SEnji Cooper   const T& operator*() const { return *impl_->Current(); }
142b89a7cc2SEnji Cooper   const T* operator->() const { return impl_->Current(); }
143b89a7cc2SEnji Cooper   // Prefix version of operator++.
144b89a7cc2SEnji Cooper   ParamIterator& operator++() {
145b89a7cc2SEnji Cooper     impl_->Advance();
146b89a7cc2SEnji Cooper     return *this;
147b89a7cc2SEnji Cooper   }
148b89a7cc2SEnji Cooper   // Postfix version of operator++.
149b89a7cc2SEnji Cooper   ParamIterator operator++(int /*unused*/) {
150b89a7cc2SEnji Cooper     ParamIteratorInterface<T>* clone = impl_->Clone();
151b89a7cc2SEnji Cooper     impl_->Advance();
152b89a7cc2SEnji Cooper     return ParamIterator(clone);
153b89a7cc2SEnji Cooper   }
154b89a7cc2SEnji Cooper   bool operator==(const ParamIterator& other) const {
155b89a7cc2SEnji Cooper     return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
156b89a7cc2SEnji Cooper   }
157b89a7cc2SEnji Cooper   bool operator!=(const ParamIterator& other) const {
158b89a7cc2SEnji Cooper     return !(*this == other);
159b89a7cc2SEnji Cooper   }
160b89a7cc2SEnji Cooper 
161b89a7cc2SEnji Cooper  private:
162b89a7cc2SEnji Cooper   friend class ParamGenerator<T>;
ParamIterator(ParamIteratorInterface<T> * impl)163b89a7cc2SEnji Cooper   explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
16428f6c2f2SEnji Cooper   std::unique_ptr<ParamIteratorInterface<T>> impl_;
165b89a7cc2SEnji Cooper };
166b89a7cc2SEnji Cooper 
167b89a7cc2SEnji Cooper // ParamGeneratorInterface<T> is the binary interface to access generators
168b89a7cc2SEnji Cooper // defined in other translation units.
169b89a7cc2SEnji Cooper template <typename T>
170b89a7cc2SEnji Cooper class ParamGeneratorInterface {
171b89a7cc2SEnji Cooper  public:
172b89a7cc2SEnji Cooper   typedef T ParamType;
173b89a7cc2SEnji Cooper 
17428f6c2f2SEnji Cooper   virtual ~ParamGeneratorInterface() = default;
175b89a7cc2SEnji Cooper 
176b89a7cc2SEnji Cooper   // Generator interface definition
177b89a7cc2SEnji Cooper   virtual ParamIteratorInterface<T>* Begin() const = 0;
178b89a7cc2SEnji Cooper   virtual ParamIteratorInterface<T>* End() const = 0;
179b89a7cc2SEnji Cooper };
180b89a7cc2SEnji Cooper 
181b89a7cc2SEnji Cooper // Wraps ParamGeneratorInterface<T> and provides general generator syntax
182b89a7cc2SEnji Cooper // compatible with the STL Container concept.
183b89a7cc2SEnji Cooper // This class implements copy initialization semantics and the contained
184b89a7cc2SEnji Cooper // ParamGeneratorInterface<T> instance is shared among all copies
185b89a7cc2SEnji Cooper // of the original object. This is possible because that instance is immutable.
186b89a7cc2SEnji Cooper template <typename T>
187b89a7cc2SEnji Cooper class ParamGenerator {
188b89a7cc2SEnji Cooper  public:
189b89a7cc2SEnji Cooper   typedef ParamIterator<T> iterator;
190b89a7cc2SEnji Cooper 
ParamGenerator(ParamGeneratorInterface<T> * impl)191b89a7cc2SEnji Cooper   explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
ParamGenerator(const ParamGenerator & other)192b89a7cc2SEnji Cooper   ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {}
193b89a7cc2SEnji Cooper 
194b89a7cc2SEnji Cooper   ParamGenerator& operator=(const ParamGenerator& other) {
195b89a7cc2SEnji Cooper     impl_ = other.impl_;
196b89a7cc2SEnji Cooper     return *this;
197b89a7cc2SEnji Cooper   }
198b89a7cc2SEnji Cooper 
begin()199b89a7cc2SEnji Cooper   iterator begin() const { return iterator(impl_->Begin()); }
end()200b89a7cc2SEnji Cooper   iterator end() const { return iterator(impl_->End()); }
201b89a7cc2SEnji Cooper 
202b89a7cc2SEnji Cooper  private:
20328f6c2f2SEnji Cooper   std::shared_ptr<const ParamGeneratorInterface<T>> impl_;
204b89a7cc2SEnji Cooper };
205b89a7cc2SEnji Cooper 
206b89a7cc2SEnji Cooper // Generates values from a range of two comparable values. Can be used to
207b89a7cc2SEnji Cooper // generate sequences of user-defined types that implement operator+() and
208b89a7cc2SEnji Cooper // operator<().
209b89a7cc2SEnji Cooper // This class is used in the Range() function.
210b89a7cc2SEnji Cooper template <typename T, typename IncrementT>
211b89a7cc2SEnji Cooper class RangeGenerator : public ParamGeneratorInterface<T> {
212b89a7cc2SEnji Cooper  public:
RangeGenerator(T begin,T end,IncrementT step)213b89a7cc2SEnji Cooper   RangeGenerator(T begin, T end, IncrementT step)
21428f6c2f2SEnji Cooper       : begin_(begin),
21528f6c2f2SEnji Cooper         end_(end),
21628f6c2f2SEnji Cooper         step_(step),
21728f6c2f2SEnji Cooper         end_index_(CalculateEndIndex(begin, end, step)) {}
21828f6c2f2SEnji Cooper   ~RangeGenerator() override = default;
219b89a7cc2SEnji Cooper 
Begin()22028f6c2f2SEnji Cooper   ParamIteratorInterface<T>* Begin() const override {
221b89a7cc2SEnji Cooper     return new Iterator(this, begin_, 0, step_);
222b89a7cc2SEnji Cooper   }
End()22328f6c2f2SEnji Cooper   ParamIteratorInterface<T>* End() const override {
224b89a7cc2SEnji Cooper     return new Iterator(this, end_, end_index_, step_);
225b89a7cc2SEnji Cooper   }
226b89a7cc2SEnji Cooper 
227b89a7cc2SEnji Cooper  private:
228b89a7cc2SEnji Cooper   class Iterator : public ParamIteratorInterface<T> {
229b89a7cc2SEnji Cooper    public:
Iterator(const ParamGeneratorInterface<T> * base,T value,int index,IncrementT step)230b89a7cc2SEnji Cooper     Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
231b89a7cc2SEnji Cooper              IncrementT step)
232b89a7cc2SEnji Cooper         : base_(base), value_(value), index_(index), step_(step) {}
23328f6c2f2SEnji Cooper     ~Iterator() override = default;
234b89a7cc2SEnji Cooper 
BaseGenerator()23528f6c2f2SEnji Cooper     const ParamGeneratorInterface<T>* BaseGenerator() const override {
236b89a7cc2SEnji Cooper       return base_;
237b89a7cc2SEnji Cooper     }
Advance()23828f6c2f2SEnji Cooper     void Advance() override {
239b89a7cc2SEnji Cooper       value_ = static_cast<T>(value_ + step_);
240b89a7cc2SEnji Cooper       index_++;
241b89a7cc2SEnji Cooper     }
Clone()24228f6c2f2SEnji Cooper     ParamIteratorInterface<T>* Clone() const override {
243b89a7cc2SEnji Cooper       return new Iterator(*this);
244b89a7cc2SEnji Cooper     }
Current()24528f6c2f2SEnji Cooper     const T* Current() const override { return &value_; }
Equals(const ParamIteratorInterface<T> & other)24628f6c2f2SEnji Cooper     bool Equals(const ParamIteratorInterface<T>& other) const override {
247b89a7cc2SEnji Cooper       // Having the same base generator guarantees that the other
248b89a7cc2SEnji Cooper       // iterator is of the same type and we can downcast.
249b89a7cc2SEnji Cooper       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
250b89a7cc2SEnji Cooper           << "The program attempted to compare iterators "
251b89a7cc2SEnji Cooper           << "from different generators." << std::endl;
252b89a7cc2SEnji Cooper       const int other_index =
253b89a7cc2SEnji Cooper           CheckedDowncastToActualType<const Iterator>(&other)->index_;
254b89a7cc2SEnji Cooper       return index_ == other_index;
255b89a7cc2SEnji Cooper     }
256b89a7cc2SEnji Cooper 
257b89a7cc2SEnji Cooper    private:
Iterator(const Iterator & other)258b89a7cc2SEnji Cooper     Iterator(const Iterator& other)
259b89a7cc2SEnji Cooper         : ParamIteratorInterface<T>(),
26028f6c2f2SEnji Cooper           base_(other.base_),
26128f6c2f2SEnji Cooper           value_(other.value_),
26228f6c2f2SEnji Cooper           index_(other.index_),
263b89a7cc2SEnji Cooper           step_(other.step_) {}
264b89a7cc2SEnji Cooper 
265b89a7cc2SEnji Cooper     // No implementation - assignment is unsupported.
266b89a7cc2SEnji Cooper     void operator=(const Iterator& other);
267b89a7cc2SEnji Cooper 
268b89a7cc2SEnji Cooper     const ParamGeneratorInterface<T>* const base_;
269b89a7cc2SEnji Cooper     T value_;
270b89a7cc2SEnji Cooper     int index_;
271b89a7cc2SEnji Cooper     const IncrementT step_;
272b89a7cc2SEnji Cooper   };  // class RangeGenerator::Iterator
273b89a7cc2SEnji Cooper 
CalculateEndIndex(const T & begin,const T & end,const IncrementT & step)27428f6c2f2SEnji Cooper   static int CalculateEndIndex(const T& begin, const T& end,
275b89a7cc2SEnji Cooper                                const IncrementT& step) {
276b89a7cc2SEnji Cooper     int end_index = 0;
27728f6c2f2SEnji Cooper     for (T i = begin; i < end; i = static_cast<T>(i + step)) end_index++;
278b89a7cc2SEnji Cooper     return end_index;
279b89a7cc2SEnji Cooper   }
280b89a7cc2SEnji Cooper 
281b89a7cc2SEnji Cooper   // No implementation - assignment is unsupported.
282b89a7cc2SEnji Cooper   void operator=(const RangeGenerator& other);
283b89a7cc2SEnji Cooper 
284b89a7cc2SEnji Cooper   const T begin_;
285b89a7cc2SEnji Cooper   const T end_;
286b89a7cc2SEnji Cooper   const IncrementT step_;
287b89a7cc2SEnji Cooper   // The index for the end() iterator. All the elements in the generated
288b89a7cc2SEnji Cooper   // sequence are indexed (0-based) to aid iterator comparison.
289b89a7cc2SEnji Cooper   const int end_index_;
290b89a7cc2SEnji Cooper };  // class RangeGenerator
291b89a7cc2SEnji Cooper 
292b89a7cc2SEnji Cooper // Generates values from a pair of STL-style iterators. Used in the
293b89a7cc2SEnji Cooper // ValuesIn() function. The elements are copied from the source range
294b89a7cc2SEnji Cooper // since the source can be located on the stack, and the generator
295b89a7cc2SEnji Cooper // is likely to persist beyond that stack frame.
296b89a7cc2SEnji Cooper template <typename T>
297b89a7cc2SEnji Cooper class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
298b89a7cc2SEnji Cooper  public:
299b89a7cc2SEnji Cooper   template <typename ForwardIterator>
ValuesInIteratorRangeGenerator(ForwardIterator begin,ForwardIterator end)300b89a7cc2SEnji Cooper   ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
301b89a7cc2SEnji Cooper       : container_(begin, end) {}
30228f6c2f2SEnji Cooper   ~ValuesInIteratorRangeGenerator() override = default;
303b89a7cc2SEnji Cooper 
Begin()30428f6c2f2SEnji Cooper   ParamIteratorInterface<T>* Begin() const override {
305b89a7cc2SEnji Cooper     return new Iterator(this, container_.begin());
306b89a7cc2SEnji Cooper   }
End()30728f6c2f2SEnji Cooper   ParamIteratorInterface<T>* End() const override {
308b89a7cc2SEnji Cooper     return new Iterator(this, container_.end());
309b89a7cc2SEnji Cooper   }
310b89a7cc2SEnji Cooper 
311b89a7cc2SEnji Cooper  private:
312b89a7cc2SEnji Cooper   typedef typename ::std::vector<T> ContainerType;
313b89a7cc2SEnji Cooper 
314b89a7cc2SEnji Cooper   class Iterator : public ParamIteratorInterface<T> {
315b89a7cc2SEnji Cooper    public:
Iterator(const ParamGeneratorInterface<T> * base,typename ContainerType::const_iterator iterator)316b89a7cc2SEnji Cooper     Iterator(const ParamGeneratorInterface<T>* base,
317b89a7cc2SEnji Cooper              typename ContainerType::const_iterator iterator)
318b89a7cc2SEnji Cooper         : base_(base), iterator_(iterator) {}
31928f6c2f2SEnji Cooper     ~Iterator() override = default;
320b89a7cc2SEnji Cooper 
BaseGenerator()32128f6c2f2SEnji Cooper     const ParamGeneratorInterface<T>* BaseGenerator() const override {
322b89a7cc2SEnji Cooper       return base_;
323b89a7cc2SEnji Cooper     }
Advance()32428f6c2f2SEnji Cooper     void Advance() override {
325b89a7cc2SEnji Cooper       ++iterator_;
326b89a7cc2SEnji Cooper       value_.reset();
327b89a7cc2SEnji Cooper     }
Clone()32828f6c2f2SEnji Cooper     ParamIteratorInterface<T>* Clone() const override {
329b89a7cc2SEnji Cooper       return new Iterator(*this);
330b89a7cc2SEnji Cooper     }
331b89a7cc2SEnji Cooper     // We need to use cached value referenced by iterator_ because *iterator_
332b89a7cc2SEnji Cooper     // can return a temporary object (and of type other then T), so just
333b89a7cc2SEnji Cooper     // having "return &*iterator_;" doesn't work.
334b89a7cc2SEnji Cooper     // value_ is updated here and not in Advance() because Advance()
335b89a7cc2SEnji Cooper     // can advance iterator_ beyond the end of the range, and we cannot
336b89a7cc2SEnji Cooper     // detect that fact. The client code, on the other hand, is
337b89a7cc2SEnji Cooper     // responsible for not calling Current() on an out-of-range iterator.
Current()33828f6c2f2SEnji Cooper     const T* Current() const override {
33928f6c2f2SEnji Cooper       if (value_.get() == nullptr) value_.reset(new T(*iterator_));
340b89a7cc2SEnji Cooper       return value_.get();
341b89a7cc2SEnji Cooper     }
Equals(const ParamIteratorInterface<T> & other)34228f6c2f2SEnji Cooper     bool Equals(const ParamIteratorInterface<T>& other) const override {
343b89a7cc2SEnji Cooper       // Having the same base generator guarantees that the other
344b89a7cc2SEnji Cooper       // iterator is of the same type and we can downcast.
345b89a7cc2SEnji Cooper       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
346b89a7cc2SEnji Cooper           << "The program attempted to compare iterators "
347b89a7cc2SEnji Cooper           << "from different generators." << std::endl;
348b89a7cc2SEnji Cooper       return iterator_ ==
349b89a7cc2SEnji Cooper              CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
350b89a7cc2SEnji Cooper     }
351b89a7cc2SEnji Cooper 
352b89a7cc2SEnji Cooper    private:
Iterator(const Iterator & other)353b89a7cc2SEnji Cooper     Iterator(const Iterator& other)
354b89a7cc2SEnji Cooper         // The explicit constructor call suppresses a false warning
355b89a7cc2SEnji Cooper         // emitted by gcc when supplied with the -Wextra option.
356b89a7cc2SEnji Cooper         : ParamIteratorInterface<T>(),
357b89a7cc2SEnji Cooper           base_(other.base_),
358b89a7cc2SEnji Cooper           iterator_(other.iterator_) {}
359b89a7cc2SEnji Cooper 
360b89a7cc2SEnji Cooper     const ParamGeneratorInterface<T>* const base_;
361b89a7cc2SEnji Cooper     typename ContainerType::const_iterator iterator_;
362b89a7cc2SEnji Cooper     // A cached value of *iterator_. We keep it here to allow access by
363b89a7cc2SEnji Cooper     // pointer in the wrapping iterator's operator->().
364b89a7cc2SEnji Cooper     // value_ needs to be mutable to be accessed in Current().
36528f6c2f2SEnji Cooper     // Use of std::unique_ptr helps manage cached value's lifetime,
366b89a7cc2SEnji Cooper     // which is bound by the lifespan of the iterator itself.
36728f6c2f2SEnji Cooper     mutable std::unique_ptr<const T> value_;
368b89a7cc2SEnji Cooper   };  // class ValuesInIteratorRangeGenerator::Iterator
369b89a7cc2SEnji Cooper 
370b89a7cc2SEnji Cooper   // No implementation - assignment is unsupported.
371b89a7cc2SEnji Cooper   void operator=(const ValuesInIteratorRangeGenerator& other);
372b89a7cc2SEnji Cooper 
373b89a7cc2SEnji Cooper   const ContainerType container_;
374b89a7cc2SEnji Cooper };  // class ValuesInIteratorRangeGenerator
375b89a7cc2SEnji Cooper 
376b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
377b89a7cc2SEnji Cooper //
378b89a7cc2SEnji Cooper // Default parameterized test name generator, returns a string containing the
379b89a7cc2SEnji Cooper // integer test parameter index.
380b89a7cc2SEnji Cooper template <class ParamType>
DefaultParamName(const TestParamInfo<ParamType> & info)381b89a7cc2SEnji Cooper std::string DefaultParamName(const TestParamInfo<ParamType>& info) {
382b89a7cc2SEnji Cooper   Message name_stream;
383b89a7cc2SEnji Cooper   name_stream << info.index;
384b89a7cc2SEnji Cooper   return name_stream.GetString();
385b89a7cc2SEnji Cooper }
386b89a7cc2SEnji Cooper 
38728f6c2f2SEnji Cooper template <typename T = int>
TestNotEmpty()38828f6c2f2SEnji Cooper void TestNotEmpty() {
38928f6c2f2SEnji Cooper   static_assert(sizeof(T) == 0, "Empty arguments are not allowed.");
390b89a7cc2SEnji Cooper }
39128f6c2f2SEnji Cooper template <typename T = int>
TestNotEmpty(const T &)39228f6c2f2SEnji Cooper void TestNotEmpty(const T&) {}
393b89a7cc2SEnji Cooper 
394b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
395b89a7cc2SEnji Cooper //
396b89a7cc2SEnji Cooper // Stores a parameter value and later creates tests parameterized with that
397b89a7cc2SEnji Cooper // value.
398b89a7cc2SEnji Cooper template <class TestClass>
399b89a7cc2SEnji Cooper class ParameterizedTestFactory : public TestFactoryBase {
400b89a7cc2SEnji Cooper  public:
401b89a7cc2SEnji Cooper   typedef typename TestClass::ParamType ParamType;
ParameterizedTestFactory(ParamType parameter)40228f6c2f2SEnji Cooper   explicit ParameterizedTestFactory(ParamType parameter)
40328f6c2f2SEnji Cooper       : parameter_(parameter) {}
CreateTest()40428f6c2f2SEnji Cooper   Test* CreateTest() override {
405b89a7cc2SEnji Cooper     TestClass::SetParam(&parameter_);
406b89a7cc2SEnji Cooper     return new TestClass();
407b89a7cc2SEnji Cooper   }
408b89a7cc2SEnji Cooper 
409b89a7cc2SEnji Cooper  private:
410b89a7cc2SEnji Cooper   const ParamType parameter_;
411b89a7cc2SEnji Cooper 
41228f6c2f2SEnji Cooper   ParameterizedTestFactory(const ParameterizedTestFactory&) = delete;
41328f6c2f2SEnji Cooper   ParameterizedTestFactory& operator=(const ParameterizedTestFactory&) = delete;
414b89a7cc2SEnji Cooper };
415b89a7cc2SEnji Cooper 
416b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
417b89a7cc2SEnji Cooper //
418b89a7cc2SEnji Cooper // TestMetaFactoryBase is a base class for meta-factories that create
419b89a7cc2SEnji Cooper // test factories for passing into MakeAndRegisterTestInfo function.
420b89a7cc2SEnji Cooper template <class ParamType>
421b89a7cc2SEnji Cooper class TestMetaFactoryBase {
422b89a7cc2SEnji Cooper  public:
42328f6c2f2SEnji Cooper   virtual ~TestMetaFactoryBase() = default;
424b89a7cc2SEnji Cooper 
425b89a7cc2SEnji Cooper   virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
426b89a7cc2SEnji Cooper };
427b89a7cc2SEnji Cooper 
428b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
429b89a7cc2SEnji Cooper //
430b89a7cc2SEnji Cooper // TestMetaFactory creates test factories for passing into
431b89a7cc2SEnji Cooper // MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
432b89a7cc2SEnji Cooper // ownership of test factory pointer, same factory object cannot be passed
43328f6c2f2SEnji Cooper // into that method twice. But ParameterizedTestSuiteInfo is going to call
434b89a7cc2SEnji Cooper // it for each Test/Parameter value combination. Thus it needs meta factory
435b89a7cc2SEnji Cooper // creator class.
43628f6c2f2SEnji Cooper template <class TestSuite>
437b89a7cc2SEnji Cooper class TestMetaFactory
43828f6c2f2SEnji Cooper     : public TestMetaFactoryBase<typename TestSuite::ParamType> {
439b89a7cc2SEnji Cooper  public:
44028f6c2f2SEnji Cooper   using ParamType = typename TestSuite::ParamType;
441b89a7cc2SEnji Cooper 
44228f6c2f2SEnji Cooper   TestMetaFactory() = default;
443b89a7cc2SEnji Cooper 
CreateTestFactory(ParamType parameter)44428f6c2f2SEnji Cooper   TestFactoryBase* CreateTestFactory(ParamType parameter) override {
44528f6c2f2SEnji Cooper     return new ParameterizedTestFactory<TestSuite>(parameter);
446b89a7cc2SEnji Cooper   }
447b89a7cc2SEnji Cooper 
448b89a7cc2SEnji Cooper  private:
44928f6c2f2SEnji Cooper   TestMetaFactory(const TestMetaFactory&) = delete;
45028f6c2f2SEnji Cooper   TestMetaFactory& operator=(const TestMetaFactory&) = delete;
451b89a7cc2SEnji Cooper };
452b89a7cc2SEnji Cooper 
453b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
454b89a7cc2SEnji Cooper //
45528f6c2f2SEnji Cooper // ParameterizedTestSuiteInfoBase is a generic interface
45628f6c2f2SEnji Cooper // to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase
457b89a7cc2SEnji Cooper // accumulates test information provided by TEST_P macro invocations
45828f6c2f2SEnji Cooper // and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations
459b89a7cc2SEnji Cooper // and uses that information to register all resulting test instances
46028f6c2f2SEnji Cooper // in RegisterTests method. The ParameterizeTestSuiteRegistry class holds
46128f6c2f2SEnji Cooper // a collection of pointers to the ParameterizedTestSuiteInfo objects
462b89a7cc2SEnji Cooper // and calls RegisterTests() on each of them when asked.
46328f6c2f2SEnji Cooper class ParameterizedTestSuiteInfoBase {
464b89a7cc2SEnji Cooper  public:
46528f6c2f2SEnji Cooper   virtual ~ParameterizedTestSuiteInfoBase() = default;
466b89a7cc2SEnji Cooper 
46728f6c2f2SEnji Cooper   // Base part of test suite name for display purposes.
46828f6c2f2SEnji Cooper   virtual const std::string& GetTestSuiteName() const = 0;
46928f6c2f2SEnji Cooper   // Test suite id to verify identity.
47028f6c2f2SEnji Cooper   virtual TypeId GetTestSuiteTypeId() const = 0;
471b89a7cc2SEnji Cooper   // UnitTest class invokes this method to register tests in this
47228f6c2f2SEnji Cooper   // test suite right before running them in RUN_ALL_TESTS macro.
47328f6c2f2SEnji Cooper   // This method should not be called more than once on any single
47428f6c2f2SEnji Cooper   // instance of a ParameterizedTestSuiteInfoBase derived class.
475b89a7cc2SEnji Cooper   virtual void RegisterTests() = 0;
476b89a7cc2SEnji Cooper 
477b89a7cc2SEnji Cooper  protected:
ParameterizedTestSuiteInfoBase()47828f6c2f2SEnji Cooper   ParameterizedTestSuiteInfoBase() {}
479b89a7cc2SEnji Cooper 
480b89a7cc2SEnji Cooper  private:
48128f6c2f2SEnji Cooper   ParameterizedTestSuiteInfoBase(const ParameterizedTestSuiteInfoBase&) =
48228f6c2f2SEnji Cooper       delete;
48328f6c2f2SEnji Cooper   ParameterizedTestSuiteInfoBase& operator=(
48428f6c2f2SEnji Cooper       const ParameterizedTestSuiteInfoBase&) = delete;
485b89a7cc2SEnji Cooper };
486b89a7cc2SEnji Cooper 
487b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
488b89a7cc2SEnji Cooper //
48928f6c2f2SEnji Cooper // Report a the name of a test_suit as safe to ignore
49028f6c2f2SEnji Cooper // as the side effect of construction of this type.
49128f6c2f2SEnji Cooper struct GTEST_API_ MarkAsIgnored {
49228f6c2f2SEnji Cooper   explicit MarkAsIgnored(const char* test_suite);
49328f6c2f2SEnji Cooper };
49428f6c2f2SEnji Cooper 
49528f6c2f2SEnji Cooper GTEST_API_ void InsertSyntheticTestCase(const std::string& name,
49628f6c2f2SEnji Cooper                                         CodeLocation location, bool has_test_p);
49728f6c2f2SEnji Cooper 
49828f6c2f2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
49928f6c2f2SEnji Cooper //
50028f6c2f2SEnji Cooper // ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P
50128f6c2f2SEnji Cooper // macro invocations for a particular test suite and generators
50228f6c2f2SEnji Cooper // obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that
50328f6c2f2SEnji Cooper // test suite. It registers tests with all values generated by all
504b89a7cc2SEnji Cooper // generators when asked.
50528f6c2f2SEnji Cooper template <class TestSuite>
50628f6c2f2SEnji Cooper class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
507b89a7cc2SEnji Cooper  public:
508b89a7cc2SEnji Cooper   // ParamType and GeneratorCreationFunc are private types but are required
509b89a7cc2SEnji Cooper   // for declarations of public methods AddTestPattern() and
51028f6c2f2SEnji Cooper   // AddTestSuiteInstantiation().
51128f6c2f2SEnji Cooper   using ParamType = typename TestSuite::ParamType;
512b89a7cc2SEnji Cooper   // A function that returns an instance of appropriate generator type.
513b89a7cc2SEnji Cooper   typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
51428f6c2f2SEnji Cooper   using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&);
515b89a7cc2SEnji Cooper 
ParameterizedTestSuiteInfo(const char * name,CodeLocation code_location)51628f6c2f2SEnji Cooper   explicit ParameterizedTestSuiteInfo(const char* name,
51728f6c2f2SEnji Cooper                                       CodeLocation code_location)
51828f6c2f2SEnji Cooper       : test_suite_name_(name), code_location_(code_location) {}
519b89a7cc2SEnji Cooper 
52028f6c2f2SEnji Cooper   // Test suite base name for display purposes.
GetTestSuiteName()52128f6c2f2SEnji Cooper   const std::string& GetTestSuiteName() const override {
52228f6c2f2SEnji Cooper     return test_suite_name_;
52328f6c2f2SEnji Cooper   }
52428f6c2f2SEnji Cooper   // Test suite id to verify identity.
GetTestSuiteTypeId()52528f6c2f2SEnji Cooper   TypeId GetTestSuiteTypeId() const override { return GetTypeId<TestSuite>(); }
526b89a7cc2SEnji Cooper   // TEST_P macro uses AddTestPattern() to record information
527b89a7cc2SEnji Cooper   // about a single test in a LocalTestInfo structure.
52828f6c2f2SEnji Cooper   // test_suite_name is the base name of the test suite (without invocation
529b89a7cc2SEnji Cooper   // prefix). test_base_name is the name of an individual test without
530b89a7cc2SEnji Cooper   // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
53128f6c2f2SEnji Cooper   // 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)53228f6c2f2SEnji Cooper   void AddTestPattern(const char* test_suite_name, const char* test_base_name,
53328f6c2f2SEnji Cooper                       TestMetaFactoryBase<ParamType>* meta_factory,
53428f6c2f2SEnji Cooper                       CodeLocation code_location) {
53528f6c2f2SEnji Cooper     tests_.push_back(std::shared_ptr<TestInfo>(new TestInfo(
53628f6c2f2SEnji Cooper         test_suite_name, test_base_name, meta_factory, code_location)));
537b89a7cc2SEnji Cooper   }
53828f6c2f2SEnji Cooper   // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information
539b89a7cc2SEnji Cooper   // about a generator.
AddTestSuiteInstantiation(const std::string & instantiation_name,GeneratorCreationFunc * func,ParamNameGeneratorFunc * name_func,const char * file,int line)54028f6c2f2SEnji Cooper   int AddTestSuiteInstantiation(const std::string& instantiation_name,
541b89a7cc2SEnji Cooper                                 GeneratorCreationFunc* func,
542b89a7cc2SEnji Cooper                                 ParamNameGeneratorFunc* name_func,
543b89a7cc2SEnji Cooper                                 const char* file, int line) {
544b89a7cc2SEnji Cooper     instantiations_.push_back(
545b89a7cc2SEnji Cooper         InstantiationInfo(instantiation_name, func, name_func, file, line));
546b89a7cc2SEnji Cooper     return 0;  // Return value used only to run this method in namespace scope.
547b89a7cc2SEnji Cooper   }
54828f6c2f2SEnji Cooper   // UnitTest class invokes this method to register tests in this test suite
54928f6c2f2SEnji Cooper   // right before running tests in RUN_ALL_TESTS macro.
55028f6c2f2SEnji Cooper   // This method should not be called more than once on any single
55128f6c2f2SEnji Cooper   // instance of a ParameterizedTestSuiteInfoBase derived class.
55228f6c2f2SEnji Cooper   // UnitTest has a guard to prevent from calling this method more than once.
RegisterTests()55328f6c2f2SEnji Cooper   void RegisterTests() override {
55428f6c2f2SEnji Cooper     bool generated_instantiations = false;
55528f6c2f2SEnji Cooper 
556b89a7cc2SEnji Cooper     for (typename TestInfoContainer::iterator test_it = tests_.begin();
557b89a7cc2SEnji Cooper          test_it != tests_.end(); ++test_it) {
55828f6c2f2SEnji Cooper       std::shared_ptr<TestInfo> test_info = *test_it;
559b89a7cc2SEnji Cooper       for (typename InstantiationContainer::iterator gen_it =
56028f6c2f2SEnji Cooper                instantiations_.begin();
56128f6c2f2SEnji Cooper            gen_it != instantiations_.end(); ++gen_it) {
562b89a7cc2SEnji Cooper         const std::string& instantiation_name = gen_it->name;
563b89a7cc2SEnji Cooper         ParamGenerator<ParamType> generator((*gen_it->generator)());
564b89a7cc2SEnji Cooper         ParamNameGeneratorFunc* name_func = gen_it->name_func;
565b89a7cc2SEnji Cooper         const char* file = gen_it->file;
566b89a7cc2SEnji Cooper         int line = gen_it->line;
567b89a7cc2SEnji Cooper 
56828f6c2f2SEnji Cooper         std::string test_suite_name;
569b89a7cc2SEnji Cooper         if (!instantiation_name.empty())
57028f6c2f2SEnji Cooper           test_suite_name = instantiation_name + "/";
57128f6c2f2SEnji Cooper         test_suite_name += test_info->test_suite_base_name;
572b89a7cc2SEnji Cooper 
573b89a7cc2SEnji Cooper         size_t i = 0;
574b89a7cc2SEnji Cooper         std::set<std::string> test_param_names;
575b89a7cc2SEnji Cooper         for (typename ParamGenerator<ParamType>::iterator param_it =
576b89a7cc2SEnji Cooper                  generator.begin();
577b89a7cc2SEnji Cooper              param_it != generator.end(); ++param_it, ++i) {
57828f6c2f2SEnji Cooper           generated_instantiations = true;
57928f6c2f2SEnji Cooper 
580b89a7cc2SEnji Cooper           Message test_name_stream;
581b89a7cc2SEnji Cooper 
58228f6c2f2SEnji Cooper           std::string param_name =
58328f6c2f2SEnji Cooper               name_func(TestParamInfo<ParamType>(*param_it, i));
584b89a7cc2SEnji Cooper 
585b89a7cc2SEnji Cooper           GTEST_CHECK_(IsValidParamName(param_name))
586b89a7cc2SEnji Cooper               << "Parameterized test name '" << param_name
58728f6c2f2SEnji Cooper               << "' is invalid, in " << file << " line " << line << std::endl;
588b89a7cc2SEnji Cooper 
589b89a7cc2SEnji Cooper           GTEST_CHECK_(test_param_names.count(param_name) == 0)
59028f6c2f2SEnji Cooper               << "Duplicate parameterized test name '" << param_name << "', in "
59128f6c2f2SEnji Cooper               << file << " line " << line << std::endl;
592b89a7cc2SEnji Cooper 
593b89a7cc2SEnji Cooper           test_param_names.insert(param_name);
594b89a7cc2SEnji Cooper 
59528f6c2f2SEnji Cooper           if (!test_info->test_base_name.empty()) {
59628f6c2f2SEnji Cooper             test_name_stream << test_info->test_base_name << "/";
59728f6c2f2SEnji Cooper           }
59828f6c2f2SEnji Cooper           test_name_stream << param_name;
599b89a7cc2SEnji Cooper           MakeAndRegisterTestInfo(
60028f6c2f2SEnji Cooper               test_suite_name.c_str(), test_name_stream.GetString().c_str(),
60128f6c2f2SEnji Cooper               nullptr,  // No type parameter.
60228f6c2f2SEnji Cooper               PrintToString(*param_it).c_str(), test_info->code_location,
60328f6c2f2SEnji Cooper               GetTestSuiteTypeId(),
60428f6c2f2SEnji Cooper               SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line),
60528f6c2f2SEnji Cooper               SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line),
606b89a7cc2SEnji Cooper               test_info->test_meta_factory->CreateTestFactory(*param_it));
607b89a7cc2SEnji Cooper         }  // for param_it
608b89a7cc2SEnji Cooper       }    // for gen_it
609b89a7cc2SEnji Cooper     }      // for test_it
61028f6c2f2SEnji Cooper 
61128f6c2f2SEnji Cooper     if (!generated_instantiations) {
61228f6c2f2SEnji Cooper       // There are no generaotrs, or they all generate nothing ...
61328f6c2f2SEnji Cooper       InsertSyntheticTestCase(GetTestSuiteName(), code_location_,
61428f6c2f2SEnji Cooper                               !tests_.empty());
61528f6c2f2SEnji Cooper     }
616b89a7cc2SEnji Cooper   }  // RegisterTests
617b89a7cc2SEnji Cooper 
618b89a7cc2SEnji Cooper  private:
619b89a7cc2SEnji Cooper   // LocalTestInfo structure keeps information about a single test registered
620b89a7cc2SEnji Cooper   // with TEST_P macro.
621b89a7cc2SEnji Cooper   struct TestInfo {
TestInfoTestInfo62228f6c2f2SEnji Cooper     TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name,
62328f6c2f2SEnji Cooper              TestMetaFactoryBase<ParamType>* a_test_meta_factory,
62428f6c2f2SEnji Cooper              CodeLocation a_code_location)
62528f6c2f2SEnji Cooper         : test_suite_base_name(a_test_suite_base_name),
626b89a7cc2SEnji Cooper           test_base_name(a_test_base_name),
62728f6c2f2SEnji Cooper           test_meta_factory(a_test_meta_factory),
62828f6c2f2SEnji Cooper           code_location(a_code_location) {}
629b89a7cc2SEnji Cooper 
63028f6c2f2SEnji Cooper     const std::string test_suite_base_name;
631b89a7cc2SEnji Cooper     const std::string test_base_name;
63228f6c2f2SEnji Cooper     const std::unique_ptr<TestMetaFactoryBase<ParamType>> test_meta_factory;
63328f6c2f2SEnji Cooper     const CodeLocation code_location;
634b89a7cc2SEnji Cooper   };
63528f6c2f2SEnji Cooper   using TestInfoContainer = ::std::vector<std::shared_ptr<TestInfo>>;
63628f6c2f2SEnji Cooper   // Records data received from INSTANTIATE_TEST_SUITE_P macros:
637b89a7cc2SEnji Cooper   //  <Instantiation name, Sequence generator creation function,
638b89a7cc2SEnji Cooper   //     Name generator function, Source file, Source line>
639b89a7cc2SEnji Cooper   struct InstantiationInfo {
InstantiationInfoInstantiationInfo640b89a7cc2SEnji Cooper     InstantiationInfo(const std::string& name_in,
641b89a7cc2SEnji Cooper                       GeneratorCreationFunc* generator_in,
64228f6c2f2SEnji Cooper                       ParamNameGeneratorFunc* name_func_in, const char* file_in,
643b89a7cc2SEnji Cooper                       int line_in)
644b89a7cc2SEnji Cooper         : name(name_in),
645b89a7cc2SEnji Cooper           generator(generator_in),
646b89a7cc2SEnji Cooper           name_func(name_func_in),
647b89a7cc2SEnji Cooper           file(file_in),
648b89a7cc2SEnji Cooper           line(line_in) {}
649b89a7cc2SEnji Cooper 
650b89a7cc2SEnji Cooper     std::string name;
651b89a7cc2SEnji Cooper     GeneratorCreationFunc* generator;
652b89a7cc2SEnji Cooper     ParamNameGeneratorFunc* name_func;
653b89a7cc2SEnji Cooper     const char* file;
654b89a7cc2SEnji Cooper     int line;
655b89a7cc2SEnji Cooper   };
656b89a7cc2SEnji Cooper   typedef ::std::vector<InstantiationInfo> InstantiationContainer;
657b89a7cc2SEnji Cooper 
IsValidParamName(const std::string & name)658b89a7cc2SEnji Cooper   static bool IsValidParamName(const std::string& name) {
659b89a7cc2SEnji Cooper     // Check for empty string
66028f6c2f2SEnji Cooper     if (name.empty()) return false;
661b89a7cc2SEnji Cooper 
662b89a7cc2SEnji Cooper     // Check for invalid characters
663b89a7cc2SEnji Cooper     for (std::string::size_type index = 0; index < name.size(); ++index) {
66428f6c2f2SEnji Cooper       if (!IsAlNum(name[index]) && name[index] != '_') return false;
665b89a7cc2SEnji Cooper     }
666b89a7cc2SEnji Cooper 
667b89a7cc2SEnji Cooper     return true;
668b89a7cc2SEnji Cooper   }
669b89a7cc2SEnji Cooper 
67028f6c2f2SEnji Cooper   const std::string test_suite_name_;
671b89a7cc2SEnji Cooper   CodeLocation code_location_;
672b89a7cc2SEnji Cooper   TestInfoContainer tests_;
673b89a7cc2SEnji Cooper   InstantiationContainer instantiations_;
674b89a7cc2SEnji Cooper 
67528f6c2f2SEnji Cooper   ParameterizedTestSuiteInfo(const ParameterizedTestSuiteInfo&) = delete;
67628f6c2f2SEnji Cooper   ParameterizedTestSuiteInfo& operator=(const ParameterizedTestSuiteInfo&) =
67728f6c2f2SEnji Cooper       delete;
67828f6c2f2SEnji Cooper };  // class ParameterizedTestSuiteInfo
67928f6c2f2SEnji Cooper 
68028f6c2f2SEnji Cooper //  Legacy API is deprecated but still available
68128f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
68228f6c2f2SEnji Cooper template <class TestCase>
68328f6c2f2SEnji Cooper using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<TestCase>;
68428f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
685b89a7cc2SEnji Cooper 
686b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
687b89a7cc2SEnji Cooper //
68828f6c2f2SEnji Cooper // ParameterizedTestSuiteRegistry contains a map of
68928f6c2f2SEnji Cooper // ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P
69028f6c2f2SEnji Cooper // and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding
69128f6c2f2SEnji Cooper // ParameterizedTestSuiteInfo descriptors.
69228f6c2f2SEnji Cooper class ParameterizedTestSuiteRegistry {
693b89a7cc2SEnji Cooper  public:
69428f6c2f2SEnji Cooper   ParameterizedTestSuiteRegistry() = default;
~ParameterizedTestSuiteRegistry()69528f6c2f2SEnji Cooper   ~ParameterizedTestSuiteRegistry() {
69628f6c2f2SEnji Cooper     for (auto& test_suite_info : test_suite_infos_) {
69728f6c2f2SEnji Cooper       delete test_suite_info;
698b89a7cc2SEnji Cooper     }
699b89a7cc2SEnji Cooper   }
700b89a7cc2SEnji Cooper 
701b89a7cc2SEnji Cooper   // Looks up or creates and returns a structure containing information about
70228f6c2f2SEnji Cooper   // tests and instantiations of a particular test suite.
70328f6c2f2SEnji Cooper   template <class TestSuite>
GetTestSuitePatternHolder(const char * test_suite_name,CodeLocation code_location)70428f6c2f2SEnji Cooper   ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder(
70528f6c2f2SEnji Cooper       const char* test_suite_name, CodeLocation code_location) {
70628f6c2f2SEnji Cooper     ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr;
70728f6c2f2SEnji Cooper     for (auto& test_suite_info : test_suite_infos_) {
70828f6c2f2SEnji Cooper       if (test_suite_info->GetTestSuiteName() == test_suite_name) {
70928f6c2f2SEnji Cooper         if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
710b89a7cc2SEnji Cooper           // Complain about incorrect usage of Google Test facilities
711b89a7cc2SEnji Cooper           // and terminate the program since we cannot guaranty correct
71228f6c2f2SEnji Cooper           // test suite setup and tear-down in this case.
71328f6c2f2SEnji Cooper           ReportInvalidTestSuiteType(test_suite_name, code_location);
714b89a7cc2SEnji Cooper           posix::Abort();
715b89a7cc2SEnji Cooper         } else {
716b89a7cc2SEnji Cooper           // At this point we are sure that the object we found is of the same
717b89a7cc2SEnji Cooper           // type we are looking for, so we downcast it to that type
718b89a7cc2SEnji Cooper           // without further checks.
719b89a7cc2SEnji Cooper           typed_test_info = CheckedDowncastToActualType<
72028f6c2f2SEnji Cooper               ParameterizedTestSuiteInfo<TestSuite>>(test_suite_info);
721b89a7cc2SEnji Cooper         }
722b89a7cc2SEnji Cooper         break;
723b89a7cc2SEnji Cooper       }
724b89a7cc2SEnji Cooper     }
72528f6c2f2SEnji Cooper     if (typed_test_info == nullptr) {
72628f6c2f2SEnji Cooper       typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>(
72728f6c2f2SEnji Cooper           test_suite_name, code_location);
72828f6c2f2SEnji Cooper       test_suite_infos_.push_back(typed_test_info);
729b89a7cc2SEnji Cooper     }
730b89a7cc2SEnji Cooper     return typed_test_info;
731b89a7cc2SEnji Cooper   }
RegisterTests()732b89a7cc2SEnji Cooper   void RegisterTests() {
73328f6c2f2SEnji Cooper     for (auto& test_suite_info : test_suite_infos_) {
73428f6c2f2SEnji Cooper       test_suite_info->RegisterTests();
735b89a7cc2SEnji Cooper     }
736b89a7cc2SEnji Cooper   }
73728f6c2f2SEnji Cooper //  Legacy API is deprecated but still available
73828f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
73928f6c2f2SEnji Cooper   template <class TestCase>
GetTestCasePatternHolder(const char * test_case_name,CodeLocation code_location)74028f6c2f2SEnji Cooper   ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
74128f6c2f2SEnji Cooper       const char* test_case_name, CodeLocation code_location) {
74228f6c2f2SEnji Cooper     return GetTestSuitePatternHolder<TestCase>(test_case_name, code_location);
74328f6c2f2SEnji Cooper   }
74428f6c2f2SEnji Cooper 
74528f6c2f2SEnji Cooper #endif  //  GTEST_REMOVE_LEGACY_TEST_CASEAPI_
74628f6c2f2SEnji Cooper 
74728f6c2f2SEnji Cooper  private:
74828f6c2f2SEnji Cooper   using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
74928f6c2f2SEnji Cooper 
75028f6c2f2SEnji Cooper   TestSuiteInfoContainer test_suite_infos_;
75128f6c2f2SEnji Cooper 
75228f6c2f2SEnji Cooper   ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) =
75328f6c2f2SEnji Cooper       delete;
75428f6c2f2SEnji Cooper   ParameterizedTestSuiteRegistry& operator=(
75528f6c2f2SEnji Cooper       const ParameterizedTestSuiteRegistry&) = delete;
75628f6c2f2SEnji Cooper };
75728f6c2f2SEnji Cooper 
75828f6c2f2SEnji Cooper // Keep track of what type-parameterized test suite are defined and
75928f6c2f2SEnji Cooper // where as well as which are intatiated. This allows susequently
76028f6c2f2SEnji Cooper // identifying suits that are defined but never used.
76128f6c2f2SEnji Cooper class TypeParameterizedTestSuiteRegistry {
76228f6c2f2SEnji Cooper  public:
76328f6c2f2SEnji Cooper   // Add a suite definition
76428f6c2f2SEnji Cooper   void RegisterTestSuite(const char* test_suite_name,
76528f6c2f2SEnji Cooper                          CodeLocation code_location);
76628f6c2f2SEnji Cooper 
76728f6c2f2SEnji Cooper   // Add an instantiation of a suit.
76828f6c2f2SEnji Cooper   void RegisterInstantiation(const char* test_suite_name);
76928f6c2f2SEnji Cooper 
77028f6c2f2SEnji Cooper   // For each suit repored as defined but not reported as instantiation,
77128f6c2f2SEnji Cooper   // emit a test that reports that fact (configurably, as an error).
77228f6c2f2SEnji Cooper   void CheckForInstantiations();
77328f6c2f2SEnji Cooper 
77428f6c2f2SEnji Cooper  private:
77528f6c2f2SEnji Cooper   struct TypeParameterizedTestSuiteInfo {
TypeParameterizedTestSuiteInfoTypeParameterizedTestSuiteInfo77628f6c2f2SEnji Cooper     explicit TypeParameterizedTestSuiteInfo(CodeLocation c)
77728f6c2f2SEnji Cooper         : code_location(c), instantiated(false) {}
77828f6c2f2SEnji Cooper 
77928f6c2f2SEnji Cooper     CodeLocation code_location;
78028f6c2f2SEnji Cooper     bool instantiated;
78128f6c2f2SEnji Cooper   };
78228f6c2f2SEnji Cooper 
78328f6c2f2SEnji Cooper   std::map<std::string, TypeParameterizedTestSuiteInfo> suites_;
78428f6c2f2SEnji Cooper };
78528f6c2f2SEnji Cooper 
78628f6c2f2SEnji Cooper }  // namespace internal
78728f6c2f2SEnji Cooper 
78828f6c2f2SEnji Cooper // Forward declarations of ValuesIn(), which is implemented in
78928f6c2f2SEnji Cooper // include/gtest/gtest-param-test.h.
79028f6c2f2SEnji Cooper template <class Container>
79128f6c2f2SEnji Cooper internal::ParamGenerator<typename Container::value_type> ValuesIn(
79228f6c2f2SEnji Cooper     const Container& container);
79328f6c2f2SEnji Cooper 
79428f6c2f2SEnji Cooper namespace internal {
79528f6c2f2SEnji Cooper // Used in the Values() function to provide polymorphic capabilities.
79628f6c2f2SEnji Cooper 
79728f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
79828f6c2f2SEnji Cooper 
79928f6c2f2SEnji Cooper template <typename... Ts>
80028f6c2f2SEnji Cooper class ValueArray {
80128f6c2f2SEnji Cooper  public:
ValueArray(Ts...v)80228f6c2f2SEnji Cooper   explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {}
80328f6c2f2SEnji Cooper 
80428f6c2f2SEnji Cooper   template <typename T>
80528f6c2f2SEnji Cooper   operator ParamGenerator<T>() const {  // NOLINT
80628f6c2f2SEnji Cooper     return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>()));
80728f6c2f2SEnji Cooper   }
808b89a7cc2SEnji Cooper 
809b89a7cc2SEnji Cooper  private:
81028f6c2f2SEnji Cooper   template <typename T, size_t... I>
MakeVector(IndexSequence<I...>)81128f6c2f2SEnji Cooper   std::vector<T> MakeVector(IndexSequence<I...>) const {
81228f6c2f2SEnji Cooper     return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
81328f6c2f2SEnji Cooper   }
814b89a7cc2SEnji Cooper 
81528f6c2f2SEnji Cooper   FlatTuple<Ts...> v_;
81628f6c2f2SEnji Cooper };
817b89a7cc2SEnji Cooper 
GTEST_DISABLE_MSC_WARNINGS_POP_()81828f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4100
81928f6c2f2SEnji Cooper 
82028f6c2f2SEnji Cooper template <typename... T>
82128f6c2f2SEnji Cooper class CartesianProductGenerator
82228f6c2f2SEnji Cooper     : public ParamGeneratorInterface<::std::tuple<T...>> {
82328f6c2f2SEnji Cooper  public:
82428f6c2f2SEnji Cooper   typedef ::std::tuple<T...> ParamType;
82528f6c2f2SEnji Cooper 
82628f6c2f2SEnji Cooper   CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g)
82728f6c2f2SEnji Cooper       : generators_(g) {}
82828f6c2f2SEnji Cooper   ~CartesianProductGenerator() override = default;
82928f6c2f2SEnji Cooper 
83028f6c2f2SEnji Cooper   ParamIteratorInterface<ParamType>* Begin() const override {
83128f6c2f2SEnji Cooper     return new Iterator(this, generators_, false);
83228f6c2f2SEnji Cooper   }
83328f6c2f2SEnji Cooper   ParamIteratorInterface<ParamType>* End() const override {
83428f6c2f2SEnji Cooper     return new Iterator(this, generators_, true);
83528f6c2f2SEnji Cooper   }
83628f6c2f2SEnji Cooper 
83728f6c2f2SEnji Cooper  private:
83828f6c2f2SEnji Cooper   template <class I>
83928f6c2f2SEnji Cooper   class IteratorImpl;
84028f6c2f2SEnji Cooper   template <size_t... I>
84128f6c2f2SEnji Cooper   class IteratorImpl<IndexSequence<I...>>
84228f6c2f2SEnji Cooper       : public ParamIteratorInterface<ParamType> {
84328f6c2f2SEnji Cooper    public:
84428f6c2f2SEnji Cooper     IteratorImpl(const ParamGeneratorInterface<ParamType>* base,
84528f6c2f2SEnji Cooper                  const std::tuple<ParamGenerator<T>...>& generators,
84628f6c2f2SEnji Cooper                  bool is_end)
84728f6c2f2SEnji Cooper         : base_(base),
84828f6c2f2SEnji Cooper           begin_(std::get<I>(generators).begin()...),
84928f6c2f2SEnji Cooper           end_(std::get<I>(generators).end()...),
85028f6c2f2SEnji Cooper           current_(is_end ? end_ : begin_) {
85128f6c2f2SEnji Cooper       ComputeCurrentValue();
85228f6c2f2SEnji Cooper     }
85328f6c2f2SEnji Cooper     ~IteratorImpl() override = default;
85428f6c2f2SEnji Cooper 
85528f6c2f2SEnji Cooper     const ParamGeneratorInterface<ParamType>* BaseGenerator() const override {
85628f6c2f2SEnji Cooper       return base_;
85728f6c2f2SEnji Cooper     }
85828f6c2f2SEnji Cooper     // Advance should not be called on beyond-of-range iterators
85928f6c2f2SEnji Cooper     // so no component iterators must be beyond end of range, either.
86028f6c2f2SEnji Cooper     void Advance() override {
86128f6c2f2SEnji Cooper       assert(!AtEnd());
86228f6c2f2SEnji Cooper       // Advance the last iterator.
86328f6c2f2SEnji Cooper       ++std::get<sizeof...(T) - 1>(current_);
86428f6c2f2SEnji Cooper       // if that reaches end, propagate that up.
86528f6c2f2SEnji Cooper       AdvanceIfEnd<sizeof...(T) - 1>();
86628f6c2f2SEnji Cooper       ComputeCurrentValue();
86728f6c2f2SEnji Cooper     }
86828f6c2f2SEnji Cooper     ParamIteratorInterface<ParamType>* Clone() const override {
86928f6c2f2SEnji Cooper       return new IteratorImpl(*this);
87028f6c2f2SEnji Cooper     }
87128f6c2f2SEnji Cooper 
87228f6c2f2SEnji Cooper     const ParamType* Current() const override { return current_value_.get(); }
87328f6c2f2SEnji Cooper 
87428f6c2f2SEnji Cooper     bool Equals(const ParamIteratorInterface<ParamType>& other) const override {
87528f6c2f2SEnji Cooper       // Having the same base generator guarantees that the other
87628f6c2f2SEnji Cooper       // iterator is of the same type and we can downcast.
87728f6c2f2SEnji Cooper       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
87828f6c2f2SEnji Cooper           << "The program attempted to compare iterators "
87928f6c2f2SEnji Cooper           << "from different generators." << std::endl;
88028f6c2f2SEnji Cooper       const IteratorImpl* typed_other =
88128f6c2f2SEnji Cooper           CheckedDowncastToActualType<const IteratorImpl>(&other);
88228f6c2f2SEnji Cooper 
88328f6c2f2SEnji Cooper       // We must report iterators equal if they both point beyond their
88428f6c2f2SEnji Cooper       // respective ranges. That can happen in a variety of fashions,
88528f6c2f2SEnji Cooper       // so we have to consult AtEnd().
88628f6c2f2SEnji Cooper       if (AtEnd() && typed_other->AtEnd()) return true;
88728f6c2f2SEnji Cooper 
88828f6c2f2SEnji Cooper       bool same = true;
88928f6c2f2SEnji Cooper       bool dummy[] = {
89028f6c2f2SEnji Cooper           (same = same && std::get<I>(current_) ==
89128f6c2f2SEnji Cooper                               std::get<I>(typed_other->current_))...};
89228f6c2f2SEnji Cooper       (void)dummy;
89328f6c2f2SEnji Cooper       return same;
89428f6c2f2SEnji Cooper     }
89528f6c2f2SEnji Cooper 
89628f6c2f2SEnji Cooper    private:
89728f6c2f2SEnji Cooper     template <size_t ThisI>
89828f6c2f2SEnji Cooper     void AdvanceIfEnd() {
89928f6c2f2SEnji Cooper       if (std::get<ThisI>(current_) != std::get<ThisI>(end_)) return;
90028f6c2f2SEnji Cooper 
90128f6c2f2SEnji Cooper       bool last = ThisI == 0;
90228f6c2f2SEnji Cooper       if (last) {
90328f6c2f2SEnji Cooper         // We are done. Nothing else to propagate.
90428f6c2f2SEnji Cooper         return;
90528f6c2f2SEnji Cooper       }
90628f6c2f2SEnji Cooper 
90728f6c2f2SEnji Cooper       constexpr size_t NextI = ThisI - (ThisI != 0);
90828f6c2f2SEnji Cooper       std::get<ThisI>(current_) = std::get<ThisI>(begin_);
90928f6c2f2SEnji Cooper       ++std::get<NextI>(current_);
91028f6c2f2SEnji Cooper       AdvanceIfEnd<NextI>();
91128f6c2f2SEnji Cooper     }
91228f6c2f2SEnji Cooper 
91328f6c2f2SEnji Cooper     void ComputeCurrentValue() {
91428f6c2f2SEnji Cooper       if (!AtEnd())
91528f6c2f2SEnji Cooper         current_value_ = std::make_shared<ParamType>(*std::get<I>(current_)...);
91628f6c2f2SEnji Cooper     }
91728f6c2f2SEnji Cooper     bool AtEnd() const {
91828f6c2f2SEnji Cooper       bool at_end = false;
91928f6c2f2SEnji Cooper       bool dummy[] = {
92028f6c2f2SEnji Cooper           (at_end = at_end || std::get<I>(current_) == std::get<I>(end_))...};
92128f6c2f2SEnji Cooper       (void)dummy;
92228f6c2f2SEnji Cooper       return at_end;
92328f6c2f2SEnji Cooper     }
92428f6c2f2SEnji Cooper 
92528f6c2f2SEnji Cooper     const ParamGeneratorInterface<ParamType>* const base_;
92628f6c2f2SEnji Cooper     std::tuple<typename ParamGenerator<T>::iterator...> begin_;
92728f6c2f2SEnji Cooper     std::tuple<typename ParamGenerator<T>::iterator...> end_;
92828f6c2f2SEnji Cooper     std::tuple<typename ParamGenerator<T>::iterator...> current_;
92928f6c2f2SEnji Cooper     std::shared_ptr<ParamType> current_value_;
93028f6c2f2SEnji Cooper   };
93128f6c2f2SEnji Cooper 
93228f6c2f2SEnji Cooper   using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>;
93328f6c2f2SEnji Cooper 
93428f6c2f2SEnji Cooper   std::tuple<ParamGenerator<T>...> generators_;
93528f6c2f2SEnji Cooper };
93628f6c2f2SEnji Cooper 
93728f6c2f2SEnji Cooper template <class... Gen>
93828f6c2f2SEnji Cooper class CartesianProductHolder {
93928f6c2f2SEnji Cooper  public:
CartesianProductHolder(const Gen &...g)94028f6c2f2SEnji Cooper   CartesianProductHolder(const Gen&... g) : generators_(g...) {}
94128f6c2f2SEnji Cooper   template <typename... T>
94228f6c2f2SEnji Cooper   operator ParamGenerator<::std::tuple<T...>>() const {
94328f6c2f2SEnji Cooper     return ParamGenerator<::std::tuple<T...>>(
94428f6c2f2SEnji Cooper         new CartesianProductGenerator<T...>(generators_));
94528f6c2f2SEnji Cooper   }
94628f6c2f2SEnji Cooper 
94728f6c2f2SEnji Cooper  private:
94828f6c2f2SEnji Cooper   std::tuple<Gen...> generators_;
94928f6c2f2SEnji Cooper };
95028f6c2f2SEnji Cooper 
95128f6c2f2SEnji Cooper template <typename From, typename To>
95228f6c2f2SEnji Cooper class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
95328f6c2f2SEnji Cooper  public:
ParamGeneratorConverter(ParamGenerator<From> gen)95428f6c2f2SEnji Cooper   ParamGeneratorConverter(ParamGenerator<From> gen)  // NOLINT
95528f6c2f2SEnji Cooper       : generator_(std::move(gen)) {}
95628f6c2f2SEnji Cooper 
Begin()95728f6c2f2SEnji Cooper   ParamIteratorInterface<To>* Begin() const override {
95828f6c2f2SEnji Cooper     return new Iterator(this, generator_.begin(), generator_.end());
95928f6c2f2SEnji Cooper   }
End()96028f6c2f2SEnji Cooper   ParamIteratorInterface<To>* End() const override {
96128f6c2f2SEnji Cooper     return new Iterator(this, generator_.end(), generator_.end());
96228f6c2f2SEnji Cooper   }
96328f6c2f2SEnji Cooper 
96428f6c2f2SEnji Cooper  private:
96528f6c2f2SEnji Cooper   class Iterator : public ParamIteratorInterface<To> {
96628f6c2f2SEnji Cooper    public:
Iterator(const ParamGeneratorInterface<To> * base,ParamIterator<From> it,ParamIterator<From> end)96728f6c2f2SEnji Cooper     Iterator(const ParamGeneratorInterface<To>* base, ParamIterator<From> it,
96828f6c2f2SEnji Cooper              ParamIterator<From> end)
96928f6c2f2SEnji Cooper         : base_(base), it_(it), end_(end) {
97028f6c2f2SEnji Cooper       if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_));
97128f6c2f2SEnji Cooper     }
97228f6c2f2SEnji Cooper     ~Iterator() override = default;
97328f6c2f2SEnji Cooper 
BaseGenerator()97428f6c2f2SEnji Cooper     const ParamGeneratorInterface<To>* BaseGenerator() const override {
97528f6c2f2SEnji Cooper       return base_;
97628f6c2f2SEnji Cooper     }
Advance()97728f6c2f2SEnji Cooper     void Advance() override {
97828f6c2f2SEnji Cooper       ++it_;
97928f6c2f2SEnji Cooper       if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_));
98028f6c2f2SEnji Cooper     }
Clone()98128f6c2f2SEnji Cooper     ParamIteratorInterface<To>* Clone() const override {
98228f6c2f2SEnji Cooper       return new Iterator(*this);
98328f6c2f2SEnji Cooper     }
Current()98428f6c2f2SEnji Cooper     const To* Current() const override { return value_.get(); }
Equals(const ParamIteratorInterface<To> & other)98528f6c2f2SEnji Cooper     bool Equals(const ParamIteratorInterface<To>& other) const override {
98628f6c2f2SEnji Cooper       // Having the same base generator guarantees that the other
98728f6c2f2SEnji Cooper       // iterator is of the same type and we can downcast.
98828f6c2f2SEnji Cooper       GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
98928f6c2f2SEnji Cooper           << "The program attempted to compare iterators "
99028f6c2f2SEnji Cooper           << "from different generators." << std::endl;
99128f6c2f2SEnji Cooper       const ParamIterator<From> other_it =
99228f6c2f2SEnji Cooper           CheckedDowncastToActualType<const Iterator>(&other)->it_;
99328f6c2f2SEnji Cooper       return it_ == other_it;
99428f6c2f2SEnji Cooper     }
99528f6c2f2SEnji Cooper 
99628f6c2f2SEnji Cooper    private:
99728f6c2f2SEnji Cooper     Iterator(const Iterator& other) = default;
99828f6c2f2SEnji Cooper 
99928f6c2f2SEnji Cooper     const ParamGeneratorInterface<To>* const base_;
100028f6c2f2SEnji Cooper     ParamIterator<From> it_;
100128f6c2f2SEnji Cooper     ParamIterator<From> end_;
100228f6c2f2SEnji Cooper     std::shared_ptr<To> value_;
100328f6c2f2SEnji Cooper   };  // class ParamGeneratorConverter::Iterator
100428f6c2f2SEnji Cooper 
100528f6c2f2SEnji Cooper   ParamGenerator<From> generator_;
100628f6c2f2SEnji Cooper };  // class ParamGeneratorConverter
100728f6c2f2SEnji Cooper 
100828f6c2f2SEnji Cooper template <class Gen>
100928f6c2f2SEnji Cooper class ParamConverterGenerator {
101028f6c2f2SEnji Cooper  public:
ParamConverterGenerator(ParamGenerator<Gen> g)101128f6c2f2SEnji Cooper   ParamConverterGenerator(ParamGenerator<Gen> g)  // NOLINT
101228f6c2f2SEnji Cooper       : generator_(std::move(g)) {}
101328f6c2f2SEnji Cooper 
101428f6c2f2SEnji Cooper   template <typename T>
101528f6c2f2SEnji Cooper   operator ParamGenerator<T>() const {  // NOLINT
101628f6c2f2SEnji Cooper     return ParamGenerator<T>(new ParamGeneratorConverter<Gen, T>(generator_));
101728f6c2f2SEnji Cooper   }
101828f6c2f2SEnji Cooper 
101928f6c2f2SEnji Cooper  private:
102028f6c2f2SEnji Cooper   ParamGenerator<Gen> generator_;
1021b89a7cc2SEnji Cooper };
1022b89a7cc2SEnji Cooper 
1023b89a7cc2SEnji Cooper }  // namespace internal
1024b89a7cc2SEnji Cooper }  // namespace testing
1025b89a7cc2SEnji Cooper 
102628f6c2f2SEnji Cooper #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
1027