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