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>
50*5ca8c28cSEnji Cooper #include <unordered_map>
51b89a7cc2SEnji Cooper #include <utility>
52b89a7cc2SEnji Cooper #include <vector>
53b89a7cc2SEnji Cooper
54b89a7cc2SEnji Cooper #include "gtest/gtest-printers.h"
5528f6c2f2SEnji Cooper #include "gtest/gtest-test-part.h"
5628f6c2f2SEnji Cooper #include "gtest/internal/gtest-internal.h"
5728f6c2f2SEnji Cooper #include "gtest/internal/gtest-port.h"
58b89a7cc2SEnji Cooper
59b89a7cc2SEnji Cooper namespace testing {
60b89a7cc2SEnji Cooper // Input to a parameterized test name generator, describing a test parameter.
61b89a7cc2SEnji Cooper // Consists of the parameter value and the integer parameter index.
62b89a7cc2SEnji Cooper template <class ParamType>
63b89a7cc2SEnji Cooper struct TestParamInfo {
TestParamInfoTestParamInfo6428f6c2f2SEnji Cooper TestParamInfo(const ParamType& a_param, size_t an_index)
6528f6c2f2SEnji Cooper : param(a_param), index(an_index) {}
66b89a7cc2SEnji Cooper ParamType param;
67b89a7cc2SEnji Cooper size_t index;
68b89a7cc2SEnji Cooper };
69b89a7cc2SEnji Cooper
70b89a7cc2SEnji Cooper // A builtin parameterized test name generator which returns the result of
71b89a7cc2SEnji Cooper // testing::PrintToString.
72b89a7cc2SEnji Cooper struct PrintToStringParamName {
73b89a7cc2SEnji Cooper template <class ParamType>
operatorPrintToStringParamName74b89a7cc2SEnji Cooper std::string operator()(const TestParamInfo<ParamType>& info) const {
75b89a7cc2SEnji Cooper return PrintToString(info.param);
76b89a7cc2SEnji Cooper }
77b89a7cc2SEnji Cooper };
78b89a7cc2SEnji Cooper
79b89a7cc2SEnji Cooper namespace internal {
80b89a7cc2SEnji Cooper
81b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
8228f6c2f2SEnji Cooper // Utility Functions
8328f6c2f2SEnji Cooper
84b89a7cc2SEnji Cooper // Outputs a message explaining invalid registration of different
8528f6c2f2SEnji Cooper // fixture class for the same test suite. This may happen when
86b89a7cc2SEnji Cooper // TEST_P macro is used to define two tests with the same name
87b89a7cc2SEnji Cooper // but in different namespaces.
8828f6c2f2SEnji Cooper GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name,
89*5ca8c28cSEnji Cooper const CodeLocation& code_location);
90b89a7cc2SEnji Cooper
9128f6c2f2SEnji Cooper template <typename>
9228f6c2f2SEnji Cooper class ParamGeneratorInterface;
9328f6c2f2SEnji Cooper template <typename>
9428f6c2f2SEnji Cooper class ParamGenerator;
95b89a7cc2SEnji Cooper
96b89a7cc2SEnji Cooper // Interface for iterating over elements provided by an implementation
97b89a7cc2SEnji Cooper // of ParamGeneratorInterface<T>.
98b89a7cc2SEnji Cooper template <typename T>
99b89a7cc2SEnji Cooper class ParamIteratorInterface {
100b89a7cc2SEnji Cooper public:
10128f6c2f2SEnji Cooper virtual ~ParamIteratorInterface() = default;
102b89a7cc2SEnji Cooper // A pointer to the base generator instance.
103b89a7cc2SEnji Cooper // Used only for the purposes of iterator comparison
104b89a7cc2SEnji Cooper // to make sure that two iterators belong to the same generator.
105b89a7cc2SEnji Cooper virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
106b89a7cc2SEnji Cooper // Advances iterator to point to the next element
107b89a7cc2SEnji Cooper // provided by the generator. The caller is responsible
108b89a7cc2SEnji Cooper // for not calling Advance() on an iterator equal to
109b89a7cc2SEnji Cooper // BaseGenerator()->End().
110b89a7cc2SEnji Cooper virtual void Advance() = 0;
111b89a7cc2SEnji Cooper // Clones the iterator object. Used for implementing copy semantics
112b89a7cc2SEnji Cooper // of ParamIterator<T>.
113b89a7cc2SEnji Cooper virtual ParamIteratorInterface* Clone() const = 0;
114b89a7cc2SEnji Cooper // Dereferences the current iterator and provides (read-only) access
115b89a7cc2SEnji Cooper // to the pointed value. It is the caller's responsibility not to call
116b89a7cc2SEnji Cooper // Current() on an iterator equal to BaseGenerator()->End().
117b89a7cc2SEnji Cooper // Used for implementing ParamGenerator<T>::operator*().
118b89a7cc2SEnji Cooper virtual const T* Current() const = 0;
119b89a7cc2SEnji Cooper // Determines whether the given iterator and other point to the same
120b89a7cc2SEnji Cooper // element in the sequence generated by the generator.
121b89a7cc2SEnji Cooper // Used for implementing ParamGenerator<T>::operator==().
122b89a7cc2SEnji Cooper virtual bool Equals(const ParamIteratorInterface& other) const = 0;
123b89a7cc2SEnji Cooper };
124b89a7cc2SEnji Cooper
125b89a7cc2SEnji Cooper // Class iterating over elements provided by an implementation of
126b89a7cc2SEnji Cooper // ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
127b89a7cc2SEnji Cooper // and implements the const forward iterator concept.
128b89a7cc2SEnji Cooper template <typename T>
129b89a7cc2SEnji Cooper class ParamIterator {
130b89a7cc2SEnji Cooper public:
131b89a7cc2SEnji Cooper typedef T value_type;
132b89a7cc2SEnji Cooper typedef const T& reference;
133b89a7cc2SEnji Cooper typedef ptrdiff_t difference_type;
134b89a7cc2SEnji Cooper
135b89a7cc2SEnji Cooper // ParamIterator assumes ownership of the impl_ pointer.
ParamIterator(const ParamIterator & other)136b89a7cc2SEnji Cooper ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {}
137b89a7cc2SEnji Cooper ParamIterator& operator=(const ParamIterator& other) {
13828f6c2f2SEnji Cooper if (this != &other) impl_.reset(other.impl_->Clone());
139b89a7cc2SEnji Cooper return *this;
140b89a7cc2SEnji Cooper }
141b89a7cc2SEnji Cooper
142b89a7cc2SEnji Cooper const T& operator*() const { return *impl_->Current(); }
143b89a7cc2SEnji Cooper const T* operator->() const { return impl_->Current(); }
144b89a7cc2SEnji Cooper // Prefix version of operator++.
145b89a7cc2SEnji Cooper ParamIterator& operator++() {
146b89a7cc2SEnji Cooper impl_->Advance();
147b89a7cc2SEnji Cooper return *this;
148b89a7cc2SEnji Cooper }
149b89a7cc2SEnji Cooper // Postfix version of operator++.
150b89a7cc2SEnji Cooper ParamIterator operator++(int /*unused*/) {
151b89a7cc2SEnji Cooper ParamIteratorInterface<T>* clone = impl_->Clone();
152b89a7cc2SEnji Cooper impl_->Advance();
153b89a7cc2SEnji Cooper return ParamIterator(clone);
154b89a7cc2SEnji Cooper }
155b89a7cc2SEnji Cooper bool operator==(const ParamIterator& other) const {
156b89a7cc2SEnji Cooper return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
157b89a7cc2SEnji Cooper }
158b89a7cc2SEnji Cooper bool operator!=(const ParamIterator& other) const {
159b89a7cc2SEnji Cooper return !(*this == other);
160b89a7cc2SEnji Cooper }
161b89a7cc2SEnji Cooper
162b89a7cc2SEnji Cooper private:
163b89a7cc2SEnji Cooper friend class ParamGenerator<T>;
ParamIterator(ParamIteratorInterface<T> * impl)164b89a7cc2SEnji Cooper explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
16528f6c2f2SEnji Cooper std::unique_ptr<ParamIteratorInterface<T>> impl_;
166b89a7cc2SEnji Cooper };
167b89a7cc2SEnji Cooper
168b89a7cc2SEnji Cooper // ParamGeneratorInterface<T> is the binary interface to access generators
169b89a7cc2SEnji Cooper // defined in other translation units.
170b89a7cc2SEnji Cooper template <typename T>
171b89a7cc2SEnji Cooper class ParamGeneratorInterface {
172b89a7cc2SEnji Cooper public:
173b89a7cc2SEnji Cooper typedef T ParamType;
174b89a7cc2SEnji Cooper
17528f6c2f2SEnji Cooper virtual ~ParamGeneratorInterface() = default;
176b89a7cc2SEnji Cooper
177b89a7cc2SEnji Cooper // Generator interface definition
178b89a7cc2SEnji Cooper virtual ParamIteratorInterface<T>* Begin() const = 0;
179b89a7cc2SEnji Cooper virtual ParamIteratorInterface<T>* End() const = 0;
180b89a7cc2SEnji Cooper };
181b89a7cc2SEnji Cooper
182b89a7cc2SEnji Cooper // Wraps ParamGeneratorInterface<T> and provides general generator syntax
183b89a7cc2SEnji Cooper // compatible with the STL Container concept.
184b89a7cc2SEnji Cooper // This class implements copy initialization semantics and the contained
185b89a7cc2SEnji Cooper // ParamGeneratorInterface<T> instance is shared among all copies
186b89a7cc2SEnji Cooper // of the original object. This is possible because that instance is immutable.
187b89a7cc2SEnji Cooper template <typename T>
188b89a7cc2SEnji Cooper class ParamGenerator {
189b89a7cc2SEnji Cooper public:
190b89a7cc2SEnji Cooper typedef ParamIterator<T> iterator;
191b89a7cc2SEnji Cooper
ParamGenerator(ParamGeneratorInterface<T> * impl)192b89a7cc2SEnji Cooper explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
ParamGenerator(const ParamGenerator & other)193b89a7cc2SEnji Cooper ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {}
194b89a7cc2SEnji Cooper
195b89a7cc2SEnji Cooper ParamGenerator& operator=(const ParamGenerator& other) {
196b89a7cc2SEnji Cooper impl_ = other.impl_;
197b89a7cc2SEnji Cooper return *this;
198b89a7cc2SEnji Cooper }
199b89a7cc2SEnji Cooper
begin()200b89a7cc2SEnji Cooper iterator begin() const { return iterator(impl_->Begin()); }
end()201b89a7cc2SEnji Cooper iterator end() const { return iterator(impl_->End()); }
202b89a7cc2SEnji Cooper
203b89a7cc2SEnji Cooper private:
20428f6c2f2SEnji Cooper std::shared_ptr<const ParamGeneratorInterface<T>> impl_;
205b89a7cc2SEnji Cooper };
206b89a7cc2SEnji Cooper
207b89a7cc2SEnji Cooper // Generates values from a range of two comparable values. Can be used to
208b89a7cc2SEnji Cooper // generate sequences of user-defined types that implement operator+() and
209b89a7cc2SEnji Cooper // operator<().
210b89a7cc2SEnji Cooper // This class is used in the Range() function.
211b89a7cc2SEnji Cooper template <typename T, typename IncrementT>
212b89a7cc2SEnji Cooper class RangeGenerator : public ParamGeneratorInterface<T> {
213b89a7cc2SEnji Cooper public:
RangeGenerator(T begin,T end,IncrementT step)214b89a7cc2SEnji Cooper RangeGenerator(T begin, T end, IncrementT step)
21528f6c2f2SEnji Cooper : begin_(begin),
21628f6c2f2SEnji Cooper end_(end),
21728f6c2f2SEnji Cooper step_(step),
21828f6c2f2SEnji Cooper end_index_(CalculateEndIndex(begin, end, step)) {}
21928f6c2f2SEnji Cooper ~RangeGenerator() override = default;
220b89a7cc2SEnji Cooper
Begin()22128f6c2f2SEnji Cooper ParamIteratorInterface<T>* Begin() const override {
222b89a7cc2SEnji Cooper return new Iterator(this, begin_, 0, step_);
223b89a7cc2SEnji Cooper }
End()22428f6c2f2SEnji Cooper ParamIteratorInterface<T>* End() const override {
225b89a7cc2SEnji Cooper return new Iterator(this, end_, end_index_, step_);
226b89a7cc2SEnji Cooper }
227b89a7cc2SEnji Cooper
228b89a7cc2SEnji Cooper private:
229b89a7cc2SEnji Cooper class Iterator : public ParamIteratorInterface<T> {
230b89a7cc2SEnji Cooper public:
Iterator(const ParamGeneratorInterface<T> * base,T value,int index,IncrementT step)231b89a7cc2SEnji Cooper Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
232b89a7cc2SEnji Cooper IncrementT step)
233b89a7cc2SEnji Cooper : base_(base), value_(value), index_(index), step_(step) {}
23428f6c2f2SEnji Cooper ~Iterator() override = default;
235b89a7cc2SEnji Cooper
BaseGenerator()23628f6c2f2SEnji Cooper const ParamGeneratorInterface<T>* BaseGenerator() const override {
237b89a7cc2SEnji Cooper return base_;
238b89a7cc2SEnji Cooper }
Advance()23928f6c2f2SEnji Cooper void Advance() override {
240b89a7cc2SEnji Cooper value_ = static_cast<T>(value_ + step_);
241b89a7cc2SEnji Cooper index_++;
242b89a7cc2SEnji Cooper }
Clone()24328f6c2f2SEnji Cooper ParamIteratorInterface<T>* Clone() const override {
244b89a7cc2SEnji Cooper return new Iterator(*this);
245b89a7cc2SEnji Cooper }
Current()24628f6c2f2SEnji Cooper const T* Current() const override { return &value_; }
Equals(const ParamIteratorInterface<T> & other)24728f6c2f2SEnji Cooper bool Equals(const ParamIteratorInterface<T>& other) const override {
248b89a7cc2SEnji Cooper // Having the same base generator guarantees that the other
249b89a7cc2SEnji Cooper // iterator is of the same type and we can downcast.
250b89a7cc2SEnji Cooper GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
251b89a7cc2SEnji Cooper << "The program attempted to compare iterators "
252b89a7cc2SEnji Cooper << "from different generators." << std::endl;
253b89a7cc2SEnji Cooper const int other_index =
254b89a7cc2SEnji Cooper CheckedDowncastToActualType<const Iterator>(&other)->index_;
255b89a7cc2SEnji Cooper return index_ == other_index;
256b89a7cc2SEnji Cooper }
257b89a7cc2SEnji Cooper
258b89a7cc2SEnji Cooper private:
Iterator(const Iterator & other)259b89a7cc2SEnji Cooper Iterator(const Iterator& other)
260b89a7cc2SEnji Cooper : ParamIteratorInterface<T>(),
26128f6c2f2SEnji Cooper base_(other.base_),
26228f6c2f2SEnji Cooper value_(other.value_),
26328f6c2f2SEnji Cooper index_(other.index_),
264b89a7cc2SEnji Cooper step_(other.step_) {}
265b89a7cc2SEnji Cooper
266b89a7cc2SEnji Cooper // No implementation - assignment is unsupported.
267b89a7cc2SEnji Cooper void operator=(const Iterator& other);
268b89a7cc2SEnji Cooper
269b89a7cc2SEnji Cooper const ParamGeneratorInterface<T>* const base_;
270b89a7cc2SEnji Cooper T value_;
271b89a7cc2SEnji Cooper int index_;
272b89a7cc2SEnji Cooper const IncrementT step_;
273b89a7cc2SEnji Cooper }; // class RangeGenerator::Iterator
274b89a7cc2SEnji Cooper
CalculateEndIndex(const T & begin,const T & end,const IncrementT & step)27528f6c2f2SEnji Cooper static int CalculateEndIndex(const T& begin, const T& end,
276b89a7cc2SEnji Cooper const IncrementT& step) {
277b89a7cc2SEnji Cooper int end_index = 0;
27828f6c2f2SEnji Cooper for (T i = begin; i < end; i = static_cast<T>(i + step)) end_index++;
279b89a7cc2SEnji Cooper return end_index;
280b89a7cc2SEnji Cooper }
281b89a7cc2SEnji Cooper
282b89a7cc2SEnji Cooper // No implementation - assignment is unsupported.
283b89a7cc2SEnji Cooper void operator=(const RangeGenerator& other);
284b89a7cc2SEnji Cooper
285b89a7cc2SEnji Cooper const T begin_;
286b89a7cc2SEnji Cooper const T end_;
287b89a7cc2SEnji Cooper const IncrementT step_;
288b89a7cc2SEnji Cooper // The index for the end() iterator. All the elements in the generated
289b89a7cc2SEnji Cooper // sequence are indexed (0-based) to aid iterator comparison.
290b89a7cc2SEnji Cooper const int end_index_;
291b89a7cc2SEnji Cooper }; // class RangeGenerator
292b89a7cc2SEnji Cooper
293b89a7cc2SEnji Cooper // Generates values from a pair of STL-style iterators. Used in the
294b89a7cc2SEnji Cooper // ValuesIn() function. The elements are copied from the source range
295b89a7cc2SEnji Cooper // since the source can be located on the stack, and the generator
296b89a7cc2SEnji Cooper // is likely to persist beyond that stack frame.
297b89a7cc2SEnji Cooper template <typename T>
298b89a7cc2SEnji Cooper class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
299b89a7cc2SEnji Cooper public:
300b89a7cc2SEnji Cooper template <typename ForwardIterator>
ValuesInIteratorRangeGenerator(ForwardIterator begin,ForwardIterator end)301b89a7cc2SEnji Cooper ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
302b89a7cc2SEnji Cooper : container_(begin, end) {}
30328f6c2f2SEnji Cooper ~ValuesInIteratorRangeGenerator() override = default;
304b89a7cc2SEnji Cooper
Begin()30528f6c2f2SEnji Cooper ParamIteratorInterface<T>* Begin() const override {
306b89a7cc2SEnji Cooper return new Iterator(this, container_.begin());
307b89a7cc2SEnji Cooper }
End()30828f6c2f2SEnji Cooper ParamIteratorInterface<T>* End() const override {
309b89a7cc2SEnji Cooper return new Iterator(this, container_.end());
310b89a7cc2SEnji Cooper }
311b89a7cc2SEnji Cooper
312b89a7cc2SEnji Cooper private:
313b89a7cc2SEnji Cooper typedef typename ::std::vector<T> ContainerType;
314b89a7cc2SEnji Cooper
315b89a7cc2SEnji Cooper class Iterator : public ParamIteratorInterface<T> {
316b89a7cc2SEnji Cooper public:
Iterator(const ParamGeneratorInterface<T> * base,typename ContainerType::const_iterator iterator)317b89a7cc2SEnji Cooper Iterator(const ParamGeneratorInterface<T>* base,
318b89a7cc2SEnji Cooper typename ContainerType::const_iterator iterator)
319b89a7cc2SEnji Cooper : base_(base), iterator_(iterator) {}
32028f6c2f2SEnji Cooper ~Iterator() override = default;
321b89a7cc2SEnji Cooper
BaseGenerator()32228f6c2f2SEnji Cooper const ParamGeneratorInterface<T>* BaseGenerator() const override {
323b89a7cc2SEnji Cooper return base_;
324b89a7cc2SEnji Cooper }
Advance()32528f6c2f2SEnji Cooper void Advance() override {
326b89a7cc2SEnji Cooper ++iterator_;
327b89a7cc2SEnji Cooper value_.reset();
328b89a7cc2SEnji Cooper }
Clone()32928f6c2f2SEnji Cooper ParamIteratorInterface<T>* Clone() const override {
330b89a7cc2SEnji Cooper return new Iterator(*this);
331b89a7cc2SEnji Cooper }
332b89a7cc2SEnji Cooper // We need to use cached value referenced by iterator_ because *iterator_
333b89a7cc2SEnji Cooper // can return a temporary object (and of type other then T), so just
334b89a7cc2SEnji Cooper // having "return &*iterator_;" doesn't work.
335b89a7cc2SEnji Cooper // value_ is updated here and not in Advance() because Advance()
336b89a7cc2SEnji Cooper // can advance iterator_ beyond the end of the range, and we cannot
337b89a7cc2SEnji Cooper // detect that fact. The client code, on the other hand, is
338b89a7cc2SEnji Cooper // responsible for not calling Current() on an out-of-range iterator.
Current()33928f6c2f2SEnji Cooper const T* Current() const override {
34028f6c2f2SEnji Cooper if (value_.get() == nullptr) value_.reset(new T(*iterator_));
341b89a7cc2SEnji Cooper return value_.get();
342b89a7cc2SEnji Cooper }
Equals(const ParamIteratorInterface<T> & other)34328f6c2f2SEnji Cooper bool Equals(const ParamIteratorInterface<T>& other) const override {
344b89a7cc2SEnji Cooper // Having the same base generator guarantees that the other
345b89a7cc2SEnji Cooper // iterator is of the same type and we can downcast.
346b89a7cc2SEnji Cooper GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
347b89a7cc2SEnji Cooper << "The program attempted to compare iterators "
348b89a7cc2SEnji Cooper << "from different generators." << std::endl;
349b89a7cc2SEnji Cooper return iterator_ ==
350b89a7cc2SEnji Cooper CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
351b89a7cc2SEnji Cooper }
352b89a7cc2SEnji Cooper
353b89a7cc2SEnji Cooper private:
Iterator(const Iterator & other)354b89a7cc2SEnji Cooper Iterator(const Iterator& other)
355b89a7cc2SEnji Cooper // The explicit constructor call suppresses a false warning
356b89a7cc2SEnji Cooper // emitted by gcc when supplied with the -Wextra option.
357b89a7cc2SEnji Cooper : ParamIteratorInterface<T>(),
358b89a7cc2SEnji Cooper base_(other.base_),
359b89a7cc2SEnji Cooper iterator_(other.iterator_) {}
360b89a7cc2SEnji Cooper
361b89a7cc2SEnji Cooper const ParamGeneratorInterface<T>* const base_;
362b89a7cc2SEnji Cooper typename ContainerType::const_iterator iterator_;
363b89a7cc2SEnji Cooper // A cached value of *iterator_. We keep it here to allow access by
364b89a7cc2SEnji Cooper // pointer in the wrapping iterator's operator->().
365b89a7cc2SEnji Cooper // value_ needs to be mutable to be accessed in Current().
36628f6c2f2SEnji Cooper // Use of std::unique_ptr helps manage cached value's lifetime,
367b89a7cc2SEnji Cooper // which is bound by the lifespan of the iterator itself.
36828f6c2f2SEnji Cooper mutable std::unique_ptr<const T> value_;
369b89a7cc2SEnji Cooper }; // class ValuesInIteratorRangeGenerator::Iterator
370b89a7cc2SEnji Cooper
371b89a7cc2SEnji Cooper // No implementation - assignment is unsupported.
372b89a7cc2SEnji Cooper void operator=(const ValuesInIteratorRangeGenerator& other);
373b89a7cc2SEnji Cooper
374b89a7cc2SEnji Cooper const ContainerType container_;
375b89a7cc2SEnji Cooper }; // class ValuesInIteratorRangeGenerator
376b89a7cc2SEnji Cooper
377b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
378b89a7cc2SEnji Cooper //
379b89a7cc2SEnji Cooper // Default parameterized test name generator, returns a string containing the
380b89a7cc2SEnji Cooper // integer test parameter index.
381b89a7cc2SEnji Cooper template <class ParamType>
DefaultParamName(const TestParamInfo<ParamType> & info)382b89a7cc2SEnji Cooper std::string DefaultParamName(const TestParamInfo<ParamType>& info) {
383*5ca8c28cSEnji Cooper return std::to_string(info.index);
384b89a7cc2SEnji Cooper }
385b89a7cc2SEnji Cooper
38628f6c2f2SEnji Cooper template <typename T = int>
TestNotEmpty()38728f6c2f2SEnji Cooper void TestNotEmpty() {
38828f6c2f2SEnji Cooper static_assert(sizeof(T) == 0, "Empty arguments are not allowed.");
389b89a7cc2SEnji Cooper }
39028f6c2f2SEnji Cooper template <typename T = int>
TestNotEmpty(const T &)39128f6c2f2SEnji Cooper void TestNotEmpty(const T&) {}
392b89a7cc2SEnji Cooper
393b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
394b89a7cc2SEnji Cooper //
395b89a7cc2SEnji Cooper // Stores a parameter value and later creates tests parameterized with that
396b89a7cc2SEnji Cooper // value.
397b89a7cc2SEnji Cooper template <class TestClass>
398b89a7cc2SEnji Cooper class ParameterizedTestFactory : public TestFactoryBase {
399b89a7cc2SEnji Cooper public:
400b89a7cc2SEnji Cooper typedef typename TestClass::ParamType ParamType;
ParameterizedTestFactory(ParamType parameter)40128f6c2f2SEnji Cooper explicit ParameterizedTestFactory(ParamType parameter)
40228f6c2f2SEnji Cooper : parameter_(parameter) {}
CreateTest()40328f6c2f2SEnji Cooper Test* CreateTest() override {
404b89a7cc2SEnji Cooper TestClass::SetParam(¶meter_);
405b89a7cc2SEnji Cooper return new TestClass();
406b89a7cc2SEnji Cooper }
407b89a7cc2SEnji Cooper
408b89a7cc2SEnji Cooper private:
409b89a7cc2SEnji Cooper const ParamType parameter_;
410b89a7cc2SEnji Cooper
41128f6c2f2SEnji Cooper ParameterizedTestFactory(const ParameterizedTestFactory&) = delete;
41228f6c2f2SEnji Cooper ParameterizedTestFactory& operator=(const ParameterizedTestFactory&) = delete;
413b89a7cc2SEnji Cooper };
414b89a7cc2SEnji Cooper
415b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
416b89a7cc2SEnji Cooper //
417b89a7cc2SEnji Cooper // TestMetaFactoryBase is a base class for meta-factories that create
418b89a7cc2SEnji Cooper // test factories for passing into MakeAndRegisterTestInfo function.
419b89a7cc2SEnji Cooper template <class ParamType>
420b89a7cc2SEnji Cooper class TestMetaFactoryBase {
421b89a7cc2SEnji Cooper public:
42228f6c2f2SEnji Cooper virtual ~TestMetaFactoryBase() = default;
423b89a7cc2SEnji Cooper
424b89a7cc2SEnji Cooper virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
425b89a7cc2SEnji Cooper };
426b89a7cc2SEnji Cooper
427b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
428b89a7cc2SEnji Cooper //
429b89a7cc2SEnji Cooper // TestMetaFactory creates test factories for passing into
430b89a7cc2SEnji Cooper // MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
431b89a7cc2SEnji Cooper // ownership of test factory pointer, same factory object cannot be passed
43228f6c2f2SEnji Cooper // into that method twice. But ParameterizedTestSuiteInfo is going to call
433b89a7cc2SEnji Cooper // it for each Test/Parameter value combination. Thus it needs meta factory
434b89a7cc2SEnji Cooper // creator class.
43528f6c2f2SEnji Cooper template <class TestSuite>
436b89a7cc2SEnji Cooper class TestMetaFactory
43728f6c2f2SEnji Cooper : public TestMetaFactoryBase<typename TestSuite::ParamType> {
438b89a7cc2SEnji Cooper public:
43928f6c2f2SEnji Cooper using ParamType = typename TestSuite::ParamType;
440b89a7cc2SEnji Cooper
44128f6c2f2SEnji Cooper TestMetaFactory() = default;
442b89a7cc2SEnji Cooper
CreateTestFactory(ParamType parameter)44328f6c2f2SEnji Cooper TestFactoryBase* CreateTestFactory(ParamType parameter) override {
44428f6c2f2SEnji Cooper return new ParameterizedTestFactory<TestSuite>(parameter);
445b89a7cc2SEnji Cooper }
446b89a7cc2SEnji Cooper
447b89a7cc2SEnji Cooper private:
44828f6c2f2SEnji Cooper TestMetaFactory(const TestMetaFactory&) = delete;
44928f6c2f2SEnji Cooper TestMetaFactory& operator=(const TestMetaFactory&) = delete;
450b89a7cc2SEnji Cooper };
451b89a7cc2SEnji Cooper
452b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
453b89a7cc2SEnji Cooper //
45428f6c2f2SEnji Cooper // ParameterizedTestSuiteInfoBase is a generic interface
45528f6c2f2SEnji Cooper // to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase
456b89a7cc2SEnji Cooper // accumulates test information provided by TEST_P macro invocations
45728f6c2f2SEnji Cooper // and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations
458b89a7cc2SEnji Cooper // and uses that information to register all resulting test instances
45928f6c2f2SEnji Cooper // in RegisterTests method. The ParameterizeTestSuiteRegistry class holds
46028f6c2f2SEnji Cooper // a collection of pointers to the ParameterizedTestSuiteInfo objects
461b89a7cc2SEnji Cooper // and calls RegisterTests() on each of them when asked.
46228f6c2f2SEnji Cooper class ParameterizedTestSuiteInfoBase {
463b89a7cc2SEnji Cooper public:
46428f6c2f2SEnji Cooper virtual ~ParameterizedTestSuiteInfoBase() = default;
465b89a7cc2SEnji Cooper
46628f6c2f2SEnji Cooper // Base part of test suite name for display purposes.
46728f6c2f2SEnji Cooper virtual const std::string& GetTestSuiteName() const = 0;
46828f6c2f2SEnji Cooper // Test suite id to verify identity.
46928f6c2f2SEnji Cooper virtual TypeId GetTestSuiteTypeId() const = 0;
470b89a7cc2SEnji Cooper // UnitTest class invokes this method to register tests in this
47128f6c2f2SEnji Cooper // test suite right before running them in RUN_ALL_TESTS macro.
47228f6c2f2SEnji Cooper // This method should not be called more than once on any single
47328f6c2f2SEnji Cooper // instance of a ParameterizedTestSuiteInfoBase derived class.
474b89a7cc2SEnji Cooper virtual void RegisterTests() = 0;
475b89a7cc2SEnji Cooper
476b89a7cc2SEnji Cooper protected:
ParameterizedTestSuiteInfoBase()47728f6c2f2SEnji Cooper ParameterizedTestSuiteInfoBase() {}
478b89a7cc2SEnji Cooper
479b89a7cc2SEnji Cooper private:
48028f6c2f2SEnji Cooper ParameterizedTestSuiteInfoBase(const ParameterizedTestSuiteInfoBase&) =
48128f6c2f2SEnji Cooper delete;
48228f6c2f2SEnji Cooper ParameterizedTestSuiteInfoBase& operator=(
48328f6c2f2SEnji Cooper const ParameterizedTestSuiteInfoBase&) = delete;
484b89a7cc2SEnji Cooper };
485b89a7cc2SEnji Cooper
486b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
487b89a7cc2SEnji Cooper //
48828f6c2f2SEnji Cooper // Report a the name of a test_suit as safe to ignore
48928f6c2f2SEnji Cooper // as the side effect of construction of this type.
49028f6c2f2SEnji Cooper struct GTEST_API_ MarkAsIgnored {
49128f6c2f2SEnji Cooper explicit MarkAsIgnored(const char* test_suite);
49228f6c2f2SEnji Cooper };
49328f6c2f2SEnji Cooper
49428f6c2f2SEnji Cooper GTEST_API_ void InsertSyntheticTestCase(const std::string& name,
49528f6c2f2SEnji Cooper CodeLocation location, bool has_test_p);
49628f6c2f2SEnji Cooper
49728f6c2f2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
49828f6c2f2SEnji Cooper //
49928f6c2f2SEnji Cooper // ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P
50028f6c2f2SEnji Cooper // macro invocations for a particular test suite and generators
50128f6c2f2SEnji Cooper // obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that
50228f6c2f2SEnji Cooper // test suite. It registers tests with all values generated by all
503b89a7cc2SEnji Cooper // generators when asked.
50428f6c2f2SEnji Cooper template <class TestSuite>
50528f6c2f2SEnji Cooper class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
506b89a7cc2SEnji Cooper public:
507b89a7cc2SEnji Cooper // ParamType and GeneratorCreationFunc are private types but are required
508b89a7cc2SEnji Cooper // for declarations of public methods AddTestPattern() and
50928f6c2f2SEnji Cooper // AddTestSuiteInstantiation().
51028f6c2f2SEnji Cooper using ParamType = typename TestSuite::ParamType;
511b89a7cc2SEnji Cooper // A function that returns an instance of appropriate generator type.
512b89a7cc2SEnji Cooper typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
51328f6c2f2SEnji Cooper using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&);
514b89a7cc2SEnji Cooper
ParameterizedTestSuiteInfo(std::string name,CodeLocation code_location)515*5ca8c28cSEnji Cooper explicit ParameterizedTestSuiteInfo(std::string name,
51628f6c2f2SEnji Cooper CodeLocation code_location)
517*5ca8c28cSEnji Cooper : test_suite_name_(std::move(name)),
518*5ca8c28cSEnji Cooper code_location_(std::move(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 *,const char * test_base_name,TestMetaFactoryBase<ParamType> * meta_factory,CodeLocation code_location)532*5ca8c28cSEnji Cooper void AddTestPattern(const char*,
533*5ca8c28cSEnji Cooper const char* test_base_name,
53428f6c2f2SEnji Cooper TestMetaFactoryBase<ParamType>* meta_factory,
53528f6c2f2SEnji Cooper CodeLocation code_location) {
536*5ca8c28cSEnji Cooper tests_.emplace_back(
537*5ca8c28cSEnji Cooper new TestInfo(test_base_name, meta_factory, std::move(code_location)));
538b89a7cc2SEnji Cooper }
53928f6c2f2SEnji Cooper // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information
540b89a7cc2SEnji Cooper // about a generator.
AddTestSuiteInstantiation(std::string instantiation_name,GeneratorCreationFunc * func,ParamNameGeneratorFunc * name_func,const char * file,int line)541*5ca8c28cSEnji Cooper int AddTestSuiteInstantiation(std::string instantiation_name,
542b89a7cc2SEnji Cooper GeneratorCreationFunc* func,
543b89a7cc2SEnji Cooper ParamNameGeneratorFunc* name_func,
544b89a7cc2SEnji Cooper const char* file, int line) {
545*5ca8c28cSEnji Cooper instantiations_.emplace_back(std::move(instantiation_name), func, name_func,
546*5ca8c28cSEnji Cooper file, line);
547b89a7cc2SEnji Cooper return 0; // Return value used only to run this method in namespace scope.
548b89a7cc2SEnji Cooper }
54928f6c2f2SEnji Cooper // UnitTest class invokes this method to register tests in this test suite
55028f6c2f2SEnji Cooper // right before running tests in RUN_ALL_TESTS macro.
55128f6c2f2SEnji Cooper // This method should not be called more than once on any single
55228f6c2f2SEnji Cooper // instance of a ParameterizedTestSuiteInfoBase derived class.
55328f6c2f2SEnji Cooper // UnitTest has a guard to prevent from calling this method more than once.
RegisterTests()55428f6c2f2SEnji Cooper void RegisterTests() override {
55528f6c2f2SEnji Cooper bool generated_instantiations = false;
55628f6c2f2SEnji Cooper
55728f6c2f2SEnji Cooper std::string test_suite_name;
558*5ca8c28cSEnji Cooper std::string test_name;
559*5ca8c28cSEnji Cooper for (const std::shared_ptr<TestInfo>& test_info : tests_) {
560*5ca8c28cSEnji Cooper for (const InstantiationInfo& instantiation : instantiations_) {
561*5ca8c28cSEnji Cooper const std::string& instantiation_name = instantiation.name;
562*5ca8c28cSEnji Cooper ParamGenerator<ParamType> generator((*instantiation.generator)());
563*5ca8c28cSEnji Cooper ParamNameGeneratorFunc* name_func = instantiation.name_func;
564*5ca8c28cSEnji Cooper const char* file = instantiation.file;
565*5ca8c28cSEnji Cooper int line = instantiation.line;
566*5ca8c28cSEnji Cooper
567b89a7cc2SEnji Cooper if (!instantiation_name.empty())
56828f6c2f2SEnji Cooper test_suite_name = instantiation_name + "/";
569*5ca8c28cSEnji Cooper else
570*5ca8c28cSEnji Cooper test_suite_name.clear();
571*5ca8c28cSEnji Cooper test_suite_name += test_suite_name_;
572b89a7cc2SEnji Cooper
573b89a7cc2SEnji Cooper size_t i = 0;
574b89a7cc2SEnji Cooper std::set<std::string> test_param_names;
575*5ca8c28cSEnji Cooper for (const auto& param : generator) {
57628f6c2f2SEnji Cooper generated_instantiations = true;
57728f6c2f2SEnji Cooper
578*5ca8c28cSEnji Cooper test_name.clear();
579b89a7cc2SEnji Cooper
58028f6c2f2SEnji Cooper std::string param_name =
581*5ca8c28cSEnji Cooper name_func(TestParamInfo<ParamType>(param, i));
582b89a7cc2SEnji Cooper
583b89a7cc2SEnji Cooper GTEST_CHECK_(IsValidParamName(param_name))
584b89a7cc2SEnji Cooper << "Parameterized test name '" << param_name
585*5ca8c28cSEnji Cooper << "' is invalid (contains spaces, dashes, or any "
586*5ca8c28cSEnji Cooper "non-alphanumeric characters other than underscores), in "
587*5ca8c28cSEnji Cooper << 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
59328f6c2f2SEnji Cooper if (!test_info->test_base_name.empty()) {
594*5ca8c28cSEnji Cooper test_name.append(test_info->test_base_name).append("/");
59528f6c2f2SEnji Cooper }
596*5ca8c28cSEnji Cooper test_name += param_name;
597*5ca8c28cSEnji Cooper
598*5ca8c28cSEnji Cooper test_param_names.insert(std::move(param_name));
599*5ca8c28cSEnji Cooper
600b89a7cc2SEnji Cooper MakeAndRegisterTestInfo(
601*5ca8c28cSEnji Cooper test_suite_name, test_name.c_str(),
60228f6c2f2SEnji Cooper nullptr, // No type parameter.
603*5ca8c28cSEnji Cooper PrintToString(param).c_str(), test_info->code_location,
60428f6c2f2SEnji Cooper GetTestSuiteTypeId(),
60528f6c2f2SEnji Cooper SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line),
60628f6c2f2SEnji Cooper SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line),
607*5ca8c28cSEnji Cooper test_info->test_meta_factory->CreateTestFactory(param));
608*5ca8c28cSEnji Cooper ++i;
609*5ca8c28cSEnji Cooper } // for param
610*5ca8c28cSEnji Cooper } // for instantiation
611*5ca8c28cSEnji Cooper } // for test_info
61228f6c2f2SEnji Cooper
61328f6c2f2SEnji Cooper if (!generated_instantiations) {
61428f6c2f2SEnji Cooper // There are no generaotrs, or they all generate nothing ...
61528f6c2f2SEnji Cooper InsertSyntheticTestCase(GetTestSuiteName(), code_location_,
61628f6c2f2SEnji Cooper !tests_.empty());
61728f6c2f2SEnji Cooper }
618b89a7cc2SEnji Cooper } // RegisterTests
619b89a7cc2SEnji Cooper
620b89a7cc2SEnji Cooper private:
621b89a7cc2SEnji Cooper // LocalTestInfo structure keeps information about a single test registered
622b89a7cc2SEnji Cooper // with TEST_P macro.
623b89a7cc2SEnji Cooper struct TestInfo {
TestInfoTestInfo624*5ca8c28cSEnji Cooper TestInfo(const char* a_test_base_name,
62528f6c2f2SEnji Cooper TestMetaFactoryBase<ParamType>* a_test_meta_factory,
62628f6c2f2SEnji Cooper CodeLocation a_code_location)
627*5ca8c28cSEnji Cooper : test_base_name(a_test_base_name),
62828f6c2f2SEnji Cooper test_meta_factory(a_test_meta_factory),
629*5ca8c28cSEnji Cooper code_location(std::move(a_code_location)) {}
630b89a7cc2SEnji Cooper
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 {
InstantiationInfoInstantiationInfo640*5ca8c28cSEnji Cooper InstantiationInfo(std::string name_in, GeneratorCreationFunc* generator_in,
64128f6c2f2SEnji Cooper ParamNameGeneratorFunc* name_func_in, const char* file_in,
642b89a7cc2SEnji Cooper int line_in)
643*5ca8c28cSEnji Cooper : name(std::move(name_in)),
644b89a7cc2SEnji Cooper generator(generator_in),
645b89a7cc2SEnji Cooper name_func(name_func_in),
646b89a7cc2SEnji Cooper file(file_in),
647b89a7cc2SEnji Cooper line(line_in) {}
648b89a7cc2SEnji Cooper
649b89a7cc2SEnji Cooper std::string name;
650b89a7cc2SEnji Cooper GeneratorCreationFunc* generator;
651b89a7cc2SEnji Cooper ParamNameGeneratorFunc* name_func;
652b89a7cc2SEnji Cooper const char* file;
653b89a7cc2SEnji Cooper int line;
654b89a7cc2SEnji Cooper };
655b89a7cc2SEnji Cooper typedef ::std::vector<InstantiationInfo> InstantiationContainer;
656b89a7cc2SEnji Cooper
IsValidParamName(const std::string & name)657b89a7cc2SEnji Cooper static bool IsValidParamName(const std::string& name) {
658b89a7cc2SEnji Cooper // Check for empty string
65928f6c2f2SEnji Cooper if (name.empty()) return false;
660b89a7cc2SEnji Cooper
661b89a7cc2SEnji Cooper // Check for invalid characters
662b89a7cc2SEnji Cooper for (std::string::size_type index = 0; index < name.size(); ++index) {
66328f6c2f2SEnji Cooper if (!IsAlNum(name[index]) && name[index] != '_') return false;
664b89a7cc2SEnji Cooper }
665b89a7cc2SEnji Cooper
666b89a7cc2SEnji Cooper return true;
667b89a7cc2SEnji Cooper }
668b89a7cc2SEnji Cooper
66928f6c2f2SEnji Cooper const std::string test_suite_name_;
670b89a7cc2SEnji Cooper CodeLocation code_location_;
671b89a7cc2SEnji Cooper TestInfoContainer tests_;
672b89a7cc2SEnji Cooper InstantiationContainer instantiations_;
673b89a7cc2SEnji Cooper
67428f6c2f2SEnji Cooper ParameterizedTestSuiteInfo(const ParameterizedTestSuiteInfo&) = delete;
67528f6c2f2SEnji Cooper ParameterizedTestSuiteInfo& operator=(const ParameterizedTestSuiteInfo&) =
67628f6c2f2SEnji Cooper delete;
67728f6c2f2SEnji Cooper }; // class ParameterizedTestSuiteInfo
67828f6c2f2SEnji Cooper
67928f6c2f2SEnji Cooper // Legacy API is deprecated but still available
68028f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
68128f6c2f2SEnji Cooper template <class TestCase>
68228f6c2f2SEnji Cooper using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<TestCase>;
68328f6c2f2SEnji Cooper #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
684b89a7cc2SEnji Cooper
685b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
686b89a7cc2SEnji Cooper //
68728f6c2f2SEnji Cooper // ParameterizedTestSuiteRegistry contains a map of
68828f6c2f2SEnji Cooper // ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P
68928f6c2f2SEnji Cooper // and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding
69028f6c2f2SEnji Cooper // ParameterizedTestSuiteInfo descriptors.
69128f6c2f2SEnji Cooper class ParameterizedTestSuiteRegistry {
692b89a7cc2SEnji Cooper public:
69328f6c2f2SEnji Cooper ParameterizedTestSuiteRegistry() = default;
~ParameterizedTestSuiteRegistry()69428f6c2f2SEnji Cooper ~ParameterizedTestSuiteRegistry() {
69528f6c2f2SEnji Cooper for (auto& test_suite_info : test_suite_infos_) {
69628f6c2f2SEnji Cooper delete test_suite_info;
697b89a7cc2SEnji Cooper }
698b89a7cc2SEnji Cooper }
699b89a7cc2SEnji Cooper
700b89a7cc2SEnji Cooper // Looks up or creates and returns a structure containing information about
70128f6c2f2SEnji Cooper // tests and instantiations of a particular test suite.
70228f6c2f2SEnji Cooper template <class TestSuite>
GetTestSuitePatternHolder(std::string test_suite_name,CodeLocation code_location)70328f6c2f2SEnji Cooper ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder(
704*5ca8c28cSEnji Cooper std::string test_suite_name, CodeLocation code_location) {
70528f6c2f2SEnji Cooper ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr;
706*5ca8c28cSEnji Cooper
707*5ca8c28cSEnji Cooper auto item_it = suite_name_to_info_index_.find(test_suite_name);
708*5ca8c28cSEnji Cooper if (item_it != suite_name_to_info_index_.end()) {
709*5ca8c28cSEnji Cooper auto* test_suite_info = test_suite_infos_[item_it->second];
71028f6c2f2SEnji Cooper if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
711b89a7cc2SEnji Cooper // Complain about incorrect usage of Google Test facilities
712b89a7cc2SEnji Cooper // and terminate the program since we cannot guaranty correct
71328f6c2f2SEnji Cooper // test suite setup and tear-down in this case.
714*5ca8c28cSEnji Cooper ReportInvalidTestSuiteType(test_suite_name.c_str(), code_location);
715b89a7cc2SEnji Cooper posix::Abort();
716b89a7cc2SEnji Cooper } else {
717b89a7cc2SEnji Cooper // At this point we are sure that the object we found is of the same
718b89a7cc2SEnji Cooper // type we are looking for, so we downcast it to that type
719b89a7cc2SEnji Cooper // without further checks.
720*5ca8c28cSEnji Cooper typed_test_info =
721*5ca8c28cSEnji Cooper CheckedDowncastToActualType<ParameterizedTestSuiteInfo<TestSuite>>(
722*5ca8c28cSEnji Cooper test_suite_info);
723b89a7cc2SEnji Cooper }
724b89a7cc2SEnji Cooper }
72528f6c2f2SEnji Cooper if (typed_test_info == nullptr) {
72628f6c2f2SEnji Cooper typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>(
727*5ca8c28cSEnji Cooper test_suite_name, std::move(code_location));
728*5ca8c28cSEnji Cooper suite_name_to_info_index_.emplace(std::move(test_suite_name),
729*5ca8c28cSEnji Cooper test_suite_infos_.size());
73028f6c2f2SEnji Cooper test_suite_infos_.push_back(typed_test_info);
731b89a7cc2SEnji Cooper }
732b89a7cc2SEnji Cooper return typed_test_info;
733b89a7cc2SEnji Cooper }
RegisterTests()734b89a7cc2SEnji Cooper void RegisterTests() {
73528f6c2f2SEnji Cooper for (auto& test_suite_info : test_suite_infos_) {
73628f6c2f2SEnji Cooper test_suite_info->RegisterTests();
737b89a7cc2SEnji Cooper }
738b89a7cc2SEnji Cooper }
73928f6c2f2SEnji Cooper // Legacy API is deprecated but still available
74028f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
74128f6c2f2SEnji Cooper template <class TestCase>
GetTestCasePatternHolder(std::string test_case_name,CodeLocation code_location)74228f6c2f2SEnji Cooper ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
743*5ca8c28cSEnji Cooper std::string test_case_name, CodeLocation code_location) {
744*5ca8c28cSEnji Cooper return GetTestSuitePatternHolder<TestCase>(std::move(test_case_name),
745*5ca8c28cSEnji Cooper std::move(code_location));
74628f6c2f2SEnji Cooper }
74728f6c2f2SEnji Cooper
74828f6c2f2SEnji Cooper #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
74928f6c2f2SEnji Cooper
75028f6c2f2SEnji Cooper private:
75128f6c2f2SEnji Cooper using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
75228f6c2f2SEnji Cooper
75328f6c2f2SEnji Cooper TestSuiteInfoContainer test_suite_infos_;
754*5ca8c28cSEnji Cooper ::std::unordered_map<std::string, size_t> suite_name_to_info_index_;
75528f6c2f2SEnji Cooper
75628f6c2f2SEnji Cooper ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) =
75728f6c2f2SEnji Cooper delete;
75828f6c2f2SEnji Cooper ParameterizedTestSuiteRegistry& operator=(
75928f6c2f2SEnji Cooper const ParameterizedTestSuiteRegistry&) = delete;
76028f6c2f2SEnji Cooper };
76128f6c2f2SEnji Cooper
76228f6c2f2SEnji Cooper // Keep track of what type-parameterized test suite are defined and
76328f6c2f2SEnji Cooper // where as well as which are intatiated. This allows susequently
76428f6c2f2SEnji Cooper // identifying suits that are defined but never used.
76528f6c2f2SEnji Cooper class TypeParameterizedTestSuiteRegistry {
76628f6c2f2SEnji Cooper public:
76728f6c2f2SEnji Cooper // Add a suite definition
76828f6c2f2SEnji Cooper void RegisterTestSuite(const char* test_suite_name,
76928f6c2f2SEnji Cooper CodeLocation code_location);
77028f6c2f2SEnji Cooper
77128f6c2f2SEnji Cooper // Add an instantiation of a suit.
77228f6c2f2SEnji Cooper void RegisterInstantiation(const char* test_suite_name);
77328f6c2f2SEnji Cooper
77428f6c2f2SEnji Cooper // For each suit repored as defined but not reported as instantiation,
77528f6c2f2SEnji Cooper // emit a test that reports that fact (configurably, as an error).
77628f6c2f2SEnji Cooper void CheckForInstantiations();
77728f6c2f2SEnji Cooper
77828f6c2f2SEnji Cooper private:
77928f6c2f2SEnji Cooper struct TypeParameterizedTestSuiteInfo {
TypeParameterizedTestSuiteInfoTypeParameterizedTestSuiteInfo78028f6c2f2SEnji Cooper explicit TypeParameterizedTestSuiteInfo(CodeLocation c)
781*5ca8c28cSEnji Cooper : code_location(std::move(c)), instantiated(false) {}
78228f6c2f2SEnji Cooper
78328f6c2f2SEnji Cooper CodeLocation code_location;
78428f6c2f2SEnji Cooper bool instantiated;
78528f6c2f2SEnji Cooper };
78628f6c2f2SEnji Cooper
78728f6c2f2SEnji Cooper std::map<std::string, TypeParameterizedTestSuiteInfo> suites_;
78828f6c2f2SEnji Cooper };
78928f6c2f2SEnji Cooper
79028f6c2f2SEnji Cooper } // namespace internal
79128f6c2f2SEnji Cooper
79228f6c2f2SEnji Cooper // Forward declarations of ValuesIn(), which is implemented in
79328f6c2f2SEnji Cooper // include/gtest/gtest-param-test.h.
79428f6c2f2SEnji Cooper template <class Container>
79528f6c2f2SEnji Cooper internal::ParamGenerator<typename Container::value_type> ValuesIn(
79628f6c2f2SEnji Cooper const Container& container);
79728f6c2f2SEnji Cooper
79828f6c2f2SEnji Cooper namespace internal {
79928f6c2f2SEnji Cooper // Used in the Values() function to provide polymorphic capabilities.
80028f6c2f2SEnji Cooper
80128f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100)
80228f6c2f2SEnji Cooper
80328f6c2f2SEnji Cooper template <typename... Ts>
80428f6c2f2SEnji Cooper class ValueArray {
80528f6c2f2SEnji Cooper public:
ValueArray(Ts...v)80628f6c2f2SEnji Cooper explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {}
80728f6c2f2SEnji Cooper
80828f6c2f2SEnji Cooper template <typename T>
80928f6c2f2SEnji Cooper operator ParamGenerator<T>() const { // NOLINT
810*5ca8c28cSEnji Cooper return ValuesIn(MakeVector<T>(std::make_index_sequence<sizeof...(Ts)>()));
81128f6c2f2SEnji Cooper }
812b89a7cc2SEnji Cooper
813b89a7cc2SEnji Cooper private:
81428f6c2f2SEnji Cooper template <typename T, size_t... I>
MakeVector(std::index_sequence<I...>)815*5ca8c28cSEnji Cooper std::vector<T> MakeVector(std::index_sequence<I...>) const {
81628f6c2f2SEnji Cooper return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
81728f6c2f2SEnji Cooper }
818b89a7cc2SEnji Cooper
81928f6c2f2SEnji Cooper FlatTuple<Ts...> v_;
82028f6c2f2SEnji Cooper };
821b89a7cc2SEnji Cooper
GTEST_DISABLE_MSC_WARNINGS_POP_()82228f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4100
82328f6c2f2SEnji Cooper
82428f6c2f2SEnji Cooper template <typename... T>
82528f6c2f2SEnji Cooper class CartesianProductGenerator
82628f6c2f2SEnji Cooper : public ParamGeneratorInterface<::std::tuple<T...>> {
82728f6c2f2SEnji Cooper public:
82828f6c2f2SEnji Cooper typedef ::std::tuple<T...> ParamType;
82928f6c2f2SEnji Cooper
83028f6c2f2SEnji Cooper CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g)
83128f6c2f2SEnji Cooper : generators_(g) {}
83228f6c2f2SEnji Cooper ~CartesianProductGenerator() override = default;
83328f6c2f2SEnji Cooper
83428f6c2f2SEnji Cooper ParamIteratorInterface<ParamType>* Begin() const override {
83528f6c2f2SEnji Cooper return new Iterator(this, generators_, false);
83628f6c2f2SEnji Cooper }
83728f6c2f2SEnji Cooper ParamIteratorInterface<ParamType>* End() const override {
83828f6c2f2SEnji Cooper return new Iterator(this, generators_, true);
83928f6c2f2SEnji Cooper }
84028f6c2f2SEnji Cooper
84128f6c2f2SEnji Cooper private:
84228f6c2f2SEnji Cooper template <class I>
84328f6c2f2SEnji Cooper class IteratorImpl;
84428f6c2f2SEnji Cooper template <size_t... I>
845*5ca8c28cSEnji Cooper class IteratorImpl<std::index_sequence<I...>>
84628f6c2f2SEnji Cooper : public ParamIteratorInterface<ParamType> {
84728f6c2f2SEnji Cooper public:
84828f6c2f2SEnji Cooper IteratorImpl(const ParamGeneratorInterface<ParamType>* base,
84928f6c2f2SEnji Cooper const std::tuple<ParamGenerator<T>...>& generators,
85028f6c2f2SEnji Cooper bool is_end)
85128f6c2f2SEnji Cooper : base_(base),
85228f6c2f2SEnji Cooper begin_(std::get<I>(generators).begin()...),
85328f6c2f2SEnji Cooper end_(std::get<I>(generators).end()...),
85428f6c2f2SEnji Cooper current_(is_end ? end_ : begin_) {
85528f6c2f2SEnji Cooper ComputeCurrentValue();
85628f6c2f2SEnji Cooper }
85728f6c2f2SEnji Cooper ~IteratorImpl() override = default;
85828f6c2f2SEnji Cooper
85928f6c2f2SEnji Cooper const ParamGeneratorInterface<ParamType>* BaseGenerator() const override {
86028f6c2f2SEnji Cooper return base_;
86128f6c2f2SEnji Cooper }
86228f6c2f2SEnji Cooper // Advance should not be called on beyond-of-range iterators
86328f6c2f2SEnji Cooper // so no component iterators must be beyond end of range, either.
86428f6c2f2SEnji Cooper void Advance() override {
86528f6c2f2SEnji Cooper assert(!AtEnd());
86628f6c2f2SEnji Cooper // Advance the last iterator.
86728f6c2f2SEnji Cooper ++std::get<sizeof...(T) - 1>(current_);
86828f6c2f2SEnji Cooper // if that reaches end, propagate that up.
86928f6c2f2SEnji Cooper AdvanceIfEnd<sizeof...(T) - 1>();
87028f6c2f2SEnji Cooper ComputeCurrentValue();
87128f6c2f2SEnji Cooper }
87228f6c2f2SEnji Cooper ParamIteratorInterface<ParamType>* Clone() const override {
87328f6c2f2SEnji Cooper return new IteratorImpl(*this);
87428f6c2f2SEnji Cooper }
87528f6c2f2SEnji Cooper
87628f6c2f2SEnji Cooper const ParamType* Current() const override { return current_value_.get(); }
87728f6c2f2SEnji Cooper
87828f6c2f2SEnji Cooper bool Equals(const ParamIteratorInterface<ParamType>& other) const override {
87928f6c2f2SEnji Cooper // Having the same base generator guarantees that the other
88028f6c2f2SEnji Cooper // iterator is of the same type and we can downcast.
88128f6c2f2SEnji Cooper GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
88228f6c2f2SEnji Cooper << "The program attempted to compare iterators "
88328f6c2f2SEnji Cooper << "from different generators." << std::endl;
88428f6c2f2SEnji Cooper const IteratorImpl* typed_other =
88528f6c2f2SEnji Cooper CheckedDowncastToActualType<const IteratorImpl>(&other);
88628f6c2f2SEnji Cooper
88728f6c2f2SEnji Cooper // We must report iterators equal if they both point beyond their
88828f6c2f2SEnji Cooper // respective ranges. That can happen in a variety of fashions,
88928f6c2f2SEnji Cooper // so we have to consult AtEnd().
89028f6c2f2SEnji Cooper if (AtEnd() && typed_other->AtEnd()) return true;
89128f6c2f2SEnji Cooper
89228f6c2f2SEnji Cooper bool same = true;
89328f6c2f2SEnji Cooper bool dummy[] = {
89428f6c2f2SEnji Cooper (same = same && std::get<I>(current_) ==
89528f6c2f2SEnji Cooper std::get<I>(typed_other->current_))...};
89628f6c2f2SEnji Cooper (void)dummy;
89728f6c2f2SEnji Cooper return same;
89828f6c2f2SEnji Cooper }
89928f6c2f2SEnji Cooper
90028f6c2f2SEnji Cooper private:
90128f6c2f2SEnji Cooper template <size_t ThisI>
90228f6c2f2SEnji Cooper void AdvanceIfEnd() {
90328f6c2f2SEnji Cooper if (std::get<ThisI>(current_) != std::get<ThisI>(end_)) return;
90428f6c2f2SEnji Cooper
90528f6c2f2SEnji Cooper bool last = ThisI == 0;
90628f6c2f2SEnji Cooper if (last) {
90728f6c2f2SEnji Cooper // We are done. Nothing else to propagate.
90828f6c2f2SEnji Cooper return;
90928f6c2f2SEnji Cooper }
91028f6c2f2SEnji Cooper
91128f6c2f2SEnji Cooper constexpr size_t NextI = ThisI - (ThisI != 0);
91228f6c2f2SEnji Cooper std::get<ThisI>(current_) = std::get<ThisI>(begin_);
91328f6c2f2SEnji Cooper ++std::get<NextI>(current_);
91428f6c2f2SEnji Cooper AdvanceIfEnd<NextI>();
91528f6c2f2SEnji Cooper }
91628f6c2f2SEnji Cooper
91728f6c2f2SEnji Cooper void ComputeCurrentValue() {
91828f6c2f2SEnji Cooper if (!AtEnd())
91928f6c2f2SEnji Cooper current_value_ = std::make_shared<ParamType>(*std::get<I>(current_)...);
92028f6c2f2SEnji Cooper }
92128f6c2f2SEnji Cooper bool AtEnd() const {
92228f6c2f2SEnji Cooper bool at_end = false;
92328f6c2f2SEnji Cooper bool dummy[] = {
92428f6c2f2SEnji Cooper (at_end = at_end || std::get<I>(current_) == std::get<I>(end_))...};
92528f6c2f2SEnji Cooper (void)dummy;
92628f6c2f2SEnji Cooper return at_end;
92728f6c2f2SEnji Cooper }
92828f6c2f2SEnji Cooper
92928f6c2f2SEnji Cooper const ParamGeneratorInterface<ParamType>* const base_;
93028f6c2f2SEnji Cooper std::tuple<typename ParamGenerator<T>::iterator...> begin_;
93128f6c2f2SEnji Cooper std::tuple<typename ParamGenerator<T>::iterator...> end_;
93228f6c2f2SEnji Cooper std::tuple<typename ParamGenerator<T>::iterator...> current_;
93328f6c2f2SEnji Cooper std::shared_ptr<ParamType> current_value_;
93428f6c2f2SEnji Cooper };
93528f6c2f2SEnji Cooper
936*5ca8c28cSEnji Cooper using Iterator = IteratorImpl<std::make_index_sequence<sizeof...(T)>>;
93728f6c2f2SEnji Cooper
93828f6c2f2SEnji Cooper std::tuple<ParamGenerator<T>...> generators_;
93928f6c2f2SEnji Cooper };
94028f6c2f2SEnji Cooper
94128f6c2f2SEnji Cooper template <class... Gen>
94228f6c2f2SEnji Cooper class CartesianProductHolder {
94328f6c2f2SEnji Cooper public:
CartesianProductHolder(const Gen &...g)94428f6c2f2SEnji Cooper CartesianProductHolder(const Gen&... g) : generators_(g...) {}
94528f6c2f2SEnji Cooper template <typename... T>
94628f6c2f2SEnji Cooper operator ParamGenerator<::std::tuple<T...>>() const {
94728f6c2f2SEnji Cooper return ParamGenerator<::std::tuple<T...>>(
94828f6c2f2SEnji Cooper new CartesianProductGenerator<T...>(generators_));
94928f6c2f2SEnji Cooper }
95028f6c2f2SEnji Cooper
95128f6c2f2SEnji Cooper private:
95228f6c2f2SEnji Cooper std::tuple<Gen...> generators_;
95328f6c2f2SEnji Cooper };
95428f6c2f2SEnji Cooper
95528f6c2f2SEnji Cooper template <typename From, typename To>
95628f6c2f2SEnji Cooper class ParamGeneratorConverter : public ParamGeneratorInterface<To> {
95728f6c2f2SEnji Cooper public:
ParamGeneratorConverter(ParamGenerator<From> gen)95828f6c2f2SEnji Cooper ParamGeneratorConverter(ParamGenerator<From> gen) // NOLINT
95928f6c2f2SEnji Cooper : generator_(std::move(gen)) {}
96028f6c2f2SEnji Cooper
Begin()96128f6c2f2SEnji Cooper ParamIteratorInterface<To>* Begin() const override {
96228f6c2f2SEnji Cooper return new Iterator(this, generator_.begin(), generator_.end());
96328f6c2f2SEnji Cooper }
End()96428f6c2f2SEnji Cooper ParamIteratorInterface<To>* End() const override {
96528f6c2f2SEnji Cooper return new Iterator(this, generator_.end(), generator_.end());
96628f6c2f2SEnji Cooper }
96728f6c2f2SEnji Cooper
96828f6c2f2SEnji Cooper private:
96928f6c2f2SEnji Cooper class Iterator : public ParamIteratorInterface<To> {
97028f6c2f2SEnji Cooper public:
Iterator(const ParamGeneratorInterface<To> * base,ParamIterator<From> it,ParamIterator<From> end)97128f6c2f2SEnji Cooper Iterator(const ParamGeneratorInterface<To>* base, ParamIterator<From> it,
97228f6c2f2SEnji Cooper ParamIterator<From> end)
97328f6c2f2SEnji Cooper : base_(base), it_(it), end_(end) {
97428f6c2f2SEnji Cooper if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_));
97528f6c2f2SEnji Cooper }
97628f6c2f2SEnji Cooper ~Iterator() override = default;
97728f6c2f2SEnji Cooper
BaseGenerator()97828f6c2f2SEnji Cooper const ParamGeneratorInterface<To>* BaseGenerator() const override {
97928f6c2f2SEnji Cooper return base_;
98028f6c2f2SEnji Cooper }
Advance()98128f6c2f2SEnji Cooper void Advance() override {
98228f6c2f2SEnji Cooper ++it_;
98328f6c2f2SEnji Cooper if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_));
98428f6c2f2SEnji Cooper }
Clone()98528f6c2f2SEnji Cooper ParamIteratorInterface<To>* Clone() const override {
98628f6c2f2SEnji Cooper return new Iterator(*this);
98728f6c2f2SEnji Cooper }
Current()98828f6c2f2SEnji Cooper const To* Current() const override { return value_.get(); }
Equals(const ParamIteratorInterface<To> & other)98928f6c2f2SEnji Cooper bool Equals(const ParamIteratorInterface<To>& other) const override {
99028f6c2f2SEnji Cooper // Having the same base generator guarantees that the other
99128f6c2f2SEnji Cooper // iterator is of the same type and we can downcast.
99228f6c2f2SEnji Cooper GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
99328f6c2f2SEnji Cooper << "The program attempted to compare iterators "
99428f6c2f2SEnji Cooper << "from different generators." << std::endl;
99528f6c2f2SEnji Cooper const ParamIterator<From> other_it =
99628f6c2f2SEnji Cooper CheckedDowncastToActualType<const Iterator>(&other)->it_;
99728f6c2f2SEnji Cooper return it_ == other_it;
99828f6c2f2SEnji Cooper }
99928f6c2f2SEnji Cooper
100028f6c2f2SEnji Cooper private:
100128f6c2f2SEnji Cooper Iterator(const Iterator& other) = default;
100228f6c2f2SEnji Cooper
100328f6c2f2SEnji Cooper const ParamGeneratorInterface<To>* const base_;
100428f6c2f2SEnji Cooper ParamIterator<From> it_;
100528f6c2f2SEnji Cooper ParamIterator<From> end_;
100628f6c2f2SEnji Cooper std::shared_ptr<To> value_;
100728f6c2f2SEnji Cooper }; // class ParamGeneratorConverter::Iterator
100828f6c2f2SEnji Cooper
100928f6c2f2SEnji Cooper ParamGenerator<From> generator_;
101028f6c2f2SEnji Cooper }; // class ParamGeneratorConverter
101128f6c2f2SEnji Cooper
101228f6c2f2SEnji Cooper template <class Gen>
101328f6c2f2SEnji Cooper class ParamConverterGenerator {
101428f6c2f2SEnji Cooper public:
ParamConverterGenerator(ParamGenerator<Gen> g)101528f6c2f2SEnji Cooper ParamConverterGenerator(ParamGenerator<Gen> g) // NOLINT
101628f6c2f2SEnji Cooper : generator_(std::move(g)) {}
101728f6c2f2SEnji Cooper
101828f6c2f2SEnji Cooper template <typename T>
101928f6c2f2SEnji Cooper operator ParamGenerator<T>() const { // NOLINT
102028f6c2f2SEnji Cooper return ParamGenerator<T>(new ParamGeneratorConverter<Gen, T>(generator_));
102128f6c2f2SEnji Cooper }
102228f6c2f2SEnji Cooper
102328f6c2f2SEnji Cooper private:
102428f6c2f2SEnji Cooper ParamGenerator<Gen> generator_;
1025b89a7cc2SEnji Cooper };
1026b89a7cc2SEnji Cooper
1027b89a7cc2SEnji Cooper } // namespace internal
1028b89a7cc2SEnji Cooper } // namespace testing
1029b89a7cc2SEnji Cooper
103028f6c2f2SEnji Cooper #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
1031