106f32e7eSjoerg // Copyright 2008 Google Inc.
206f32e7eSjoerg // All Rights Reserved.
306f32e7eSjoerg //
406f32e7eSjoerg // Redistribution and use in source and binary forms, with or without
506f32e7eSjoerg // modification, are permitted provided that the following conditions are
606f32e7eSjoerg // met:
706f32e7eSjoerg //
806f32e7eSjoerg // * Redistributions of source code must retain the above copyright
906f32e7eSjoerg // notice, this list of conditions and the following disclaimer.
1006f32e7eSjoerg // * Redistributions in binary form must reproduce the above
1106f32e7eSjoerg // copyright notice, this list of conditions and the following disclaimer
1206f32e7eSjoerg // in the documentation and/or other materials provided with the
1306f32e7eSjoerg // distribution.
1406f32e7eSjoerg // * Neither the name of Google Inc. nor the names of its
1506f32e7eSjoerg // contributors may be used to endorse or promote products derived from
1606f32e7eSjoerg // this software without specific prior written permission.
1706f32e7eSjoerg //
1806f32e7eSjoerg // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1906f32e7eSjoerg // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2006f32e7eSjoerg // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2106f32e7eSjoerg // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2206f32e7eSjoerg // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2306f32e7eSjoerg // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2406f32e7eSjoerg // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2506f32e7eSjoerg // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2606f32e7eSjoerg // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2706f32e7eSjoerg // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2806f32e7eSjoerg // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*da58b97aSjoerg
3006f32e7eSjoerg
3106f32e7eSjoerg // Type and function utilities for implementing parameterized tests.
3206f32e7eSjoerg
33*da58b97aSjoerg // GOOGLETEST_CM0001 DO NOT DELETE
34*da58b97aSjoerg
3506f32e7eSjoerg #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
3606f32e7eSjoerg #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
3706f32e7eSjoerg
3806f32e7eSjoerg #include <ctype.h>
3906f32e7eSjoerg
40*da58b97aSjoerg #include <cassert>
4106f32e7eSjoerg #include <iterator>
42*da58b97aSjoerg #include <memory>
4306f32e7eSjoerg #include <set>
44*da58b97aSjoerg #include <tuple>
4506f32e7eSjoerg #include <utility>
4606f32e7eSjoerg #include <vector>
4706f32e7eSjoerg
4806f32e7eSjoerg #include "gtest/internal/gtest-internal.h"
4906f32e7eSjoerg #include "gtest/internal/gtest-port.h"
5006f32e7eSjoerg #include "gtest/gtest-printers.h"
5106f32e7eSjoerg
5206f32e7eSjoerg namespace testing {
5306f32e7eSjoerg // Input to a parameterized test name generator, describing a test parameter.
5406f32e7eSjoerg // Consists of the parameter value and the integer parameter index.
5506f32e7eSjoerg template <class ParamType>
5606f32e7eSjoerg struct TestParamInfo {
TestParamInfoTestParamInfo5706f32e7eSjoerg TestParamInfo(const ParamType& a_param, size_t an_index) :
5806f32e7eSjoerg param(a_param),
5906f32e7eSjoerg index(an_index) {}
6006f32e7eSjoerg ParamType param;
6106f32e7eSjoerg size_t index;
6206f32e7eSjoerg };
6306f32e7eSjoerg
6406f32e7eSjoerg // A builtin parameterized test name generator which returns the result of
6506f32e7eSjoerg // testing::PrintToString.
6606f32e7eSjoerg struct PrintToStringParamName {
6706f32e7eSjoerg template <class ParamType>
operatorPrintToStringParamName6806f32e7eSjoerg std::string operator()(const TestParamInfo<ParamType>& info) const {
6906f32e7eSjoerg return PrintToString(info.param);
7006f32e7eSjoerg }
7106f32e7eSjoerg };
7206f32e7eSjoerg
7306f32e7eSjoerg namespace internal {
7406f32e7eSjoerg
7506f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
76*da58b97aSjoerg // Utility Functions
77*da58b97aSjoerg
7806f32e7eSjoerg // Outputs a message explaining invalid registration of different
79*da58b97aSjoerg // fixture class for the same test suite. This may happen when
8006f32e7eSjoerg // TEST_P macro is used to define two tests with the same name
8106f32e7eSjoerg // but in different namespaces.
82*da58b97aSjoerg GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name,
8306f32e7eSjoerg CodeLocation code_location);
8406f32e7eSjoerg
8506f32e7eSjoerg template <typename> class ParamGeneratorInterface;
8606f32e7eSjoerg template <typename> class ParamGenerator;
8706f32e7eSjoerg
8806f32e7eSjoerg // Interface for iterating over elements provided by an implementation
8906f32e7eSjoerg // of ParamGeneratorInterface<T>.
9006f32e7eSjoerg template <typename T>
9106f32e7eSjoerg class ParamIteratorInterface {
9206f32e7eSjoerg public:
~ParamIteratorInterface()9306f32e7eSjoerg virtual ~ParamIteratorInterface() {}
9406f32e7eSjoerg // A pointer to the base generator instance.
9506f32e7eSjoerg // Used only for the purposes of iterator comparison
9606f32e7eSjoerg // to make sure that two iterators belong to the same generator.
9706f32e7eSjoerg virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
9806f32e7eSjoerg // Advances iterator to point to the next element
9906f32e7eSjoerg // provided by the generator. The caller is responsible
10006f32e7eSjoerg // for not calling Advance() on an iterator equal to
10106f32e7eSjoerg // BaseGenerator()->End().
10206f32e7eSjoerg virtual void Advance() = 0;
10306f32e7eSjoerg // Clones the iterator object. Used for implementing copy semantics
10406f32e7eSjoerg // of ParamIterator<T>.
10506f32e7eSjoerg virtual ParamIteratorInterface* Clone() const = 0;
10606f32e7eSjoerg // Dereferences the current iterator and provides (read-only) access
10706f32e7eSjoerg // to the pointed value. It is the caller's responsibility not to call
10806f32e7eSjoerg // Current() on an iterator equal to BaseGenerator()->End().
10906f32e7eSjoerg // Used for implementing ParamGenerator<T>::operator*().
11006f32e7eSjoerg virtual const T* Current() const = 0;
11106f32e7eSjoerg // Determines whether the given iterator and other point to the same
11206f32e7eSjoerg // element in the sequence generated by the generator.
11306f32e7eSjoerg // Used for implementing ParamGenerator<T>::operator==().
11406f32e7eSjoerg virtual bool Equals(const ParamIteratorInterface& other) const = 0;
11506f32e7eSjoerg };
11606f32e7eSjoerg
11706f32e7eSjoerg // Class iterating over elements provided by an implementation of
11806f32e7eSjoerg // ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
11906f32e7eSjoerg // and implements the const forward iterator concept.
12006f32e7eSjoerg template <typename T>
12106f32e7eSjoerg class ParamIterator {
12206f32e7eSjoerg public:
12306f32e7eSjoerg typedef T value_type;
12406f32e7eSjoerg typedef const T& reference;
12506f32e7eSjoerg typedef ptrdiff_t difference_type;
12606f32e7eSjoerg
12706f32e7eSjoerg // ParamIterator assumes ownership of the impl_ pointer.
ParamIterator(const ParamIterator & other)12806f32e7eSjoerg ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {}
12906f32e7eSjoerg ParamIterator& operator=(const ParamIterator& other) {
13006f32e7eSjoerg if (this != &other)
13106f32e7eSjoerg impl_.reset(other.impl_->Clone());
13206f32e7eSjoerg return *this;
13306f32e7eSjoerg }
13406f32e7eSjoerg
13506f32e7eSjoerg const T& operator*() const { return *impl_->Current(); }
13606f32e7eSjoerg const T* operator->() const { return impl_->Current(); }
13706f32e7eSjoerg // Prefix version of operator++.
13806f32e7eSjoerg ParamIterator& operator++() {
13906f32e7eSjoerg impl_->Advance();
14006f32e7eSjoerg return *this;
14106f32e7eSjoerg }
14206f32e7eSjoerg // Postfix version of operator++.
14306f32e7eSjoerg ParamIterator operator++(int /*unused*/) {
14406f32e7eSjoerg ParamIteratorInterface<T>* clone = impl_->Clone();
14506f32e7eSjoerg impl_->Advance();
14606f32e7eSjoerg return ParamIterator(clone);
14706f32e7eSjoerg }
14806f32e7eSjoerg bool operator==(const ParamIterator& other) const {
14906f32e7eSjoerg return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
15006f32e7eSjoerg }
15106f32e7eSjoerg bool operator!=(const ParamIterator& other) const {
15206f32e7eSjoerg return !(*this == other);
15306f32e7eSjoerg }
15406f32e7eSjoerg
15506f32e7eSjoerg private:
15606f32e7eSjoerg friend class ParamGenerator<T>;
ParamIterator(ParamIteratorInterface<T> * impl)15706f32e7eSjoerg explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
158*da58b97aSjoerg std::unique_ptr<ParamIteratorInterface<T> > impl_;
15906f32e7eSjoerg };
16006f32e7eSjoerg
16106f32e7eSjoerg // ParamGeneratorInterface<T> is the binary interface to access generators
16206f32e7eSjoerg // defined in other translation units.
16306f32e7eSjoerg template <typename T>
16406f32e7eSjoerg class ParamGeneratorInterface {
16506f32e7eSjoerg public:
16606f32e7eSjoerg typedef T ParamType;
16706f32e7eSjoerg
~ParamGeneratorInterface()16806f32e7eSjoerg virtual ~ParamGeneratorInterface() {}
16906f32e7eSjoerg
17006f32e7eSjoerg // Generator interface definition
17106f32e7eSjoerg virtual ParamIteratorInterface<T>* Begin() const = 0;
17206f32e7eSjoerg virtual ParamIteratorInterface<T>* End() const = 0;
17306f32e7eSjoerg };
17406f32e7eSjoerg
17506f32e7eSjoerg // Wraps ParamGeneratorInterface<T> and provides general generator syntax
17606f32e7eSjoerg // compatible with the STL Container concept.
17706f32e7eSjoerg // This class implements copy initialization semantics and the contained
17806f32e7eSjoerg // ParamGeneratorInterface<T> instance is shared among all copies
17906f32e7eSjoerg // of the original object. This is possible because that instance is immutable.
18006f32e7eSjoerg template<typename T>
18106f32e7eSjoerg class ParamGenerator {
18206f32e7eSjoerg public:
18306f32e7eSjoerg typedef ParamIterator<T> iterator;
18406f32e7eSjoerg
ParamGenerator(ParamGeneratorInterface<T> * impl)18506f32e7eSjoerg explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
ParamGenerator(const ParamGenerator & other)18606f32e7eSjoerg ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {}
18706f32e7eSjoerg
18806f32e7eSjoerg ParamGenerator& operator=(const ParamGenerator& other) {
18906f32e7eSjoerg impl_ = other.impl_;
19006f32e7eSjoerg return *this;
19106f32e7eSjoerg }
19206f32e7eSjoerg
begin()19306f32e7eSjoerg iterator begin() const { return iterator(impl_->Begin()); }
end()19406f32e7eSjoerg iterator end() const { return iterator(impl_->End()); }
19506f32e7eSjoerg
19606f32e7eSjoerg private:
197*da58b97aSjoerg std::shared_ptr<const ParamGeneratorInterface<T> > impl_;
19806f32e7eSjoerg };
19906f32e7eSjoerg
20006f32e7eSjoerg // Generates values from a range of two comparable values. Can be used to
20106f32e7eSjoerg // generate sequences of user-defined types that implement operator+() and
20206f32e7eSjoerg // operator<().
20306f32e7eSjoerg // This class is used in the Range() function.
20406f32e7eSjoerg template <typename T, typename IncrementT>
20506f32e7eSjoerg class RangeGenerator : public ParamGeneratorInterface<T> {
20606f32e7eSjoerg public:
RangeGenerator(T begin,T end,IncrementT step)20706f32e7eSjoerg RangeGenerator(T begin, T end, IncrementT step)
20806f32e7eSjoerg : begin_(begin), end_(end),
20906f32e7eSjoerg step_(step), end_index_(CalculateEndIndex(begin, end, step)) {}
~RangeGenerator()210*da58b97aSjoerg ~RangeGenerator() override {}
21106f32e7eSjoerg
Begin()212*da58b97aSjoerg ParamIteratorInterface<T>* Begin() const override {
21306f32e7eSjoerg return new Iterator(this, begin_, 0, step_);
21406f32e7eSjoerg }
End()215*da58b97aSjoerg ParamIteratorInterface<T>* End() const override {
21606f32e7eSjoerg return new Iterator(this, end_, end_index_, step_);
21706f32e7eSjoerg }
21806f32e7eSjoerg
21906f32e7eSjoerg private:
22006f32e7eSjoerg class Iterator : public ParamIteratorInterface<T> {
22106f32e7eSjoerg public:
Iterator(const ParamGeneratorInterface<T> * base,T value,int index,IncrementT step)22206f32e7eSjoerg Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
22306f32e7eSjoerg IncrementT step)
22406f32e7eSjoerg : base_(base), value_(value), index_(index), step_(step) {}
~Iterator()225*da58b97aSjoerg ~Iterator() override {}
22606f32e7eSjoerg
BaseGenerator()227*da58b97aSjoerg const ParamGeneratorInterface<T>* BaseGenerator() const override {
22806f32e7eSjoerg return base_;
22906f32e7eSjoerg }
Advance()230*da58b97aSjoerg void Advance() override {
23106f32e7eSjoerg value_ = static_cast<T>(value_ + step_);
23206f32e7eSjoerg index_++;
23306f32e7eSjoerg }
Clone()234*da58b97aSjoerg ParamIteratorInterface<T>* Clone() const override {
23506f32e7eSjoerg return new Iterator(*this);
23606f32e7eSjoerg }
Current()237*da58b97aSjoerg const T* Current() const override { return &value_; }
Equals(const ParamIteratorInterface<T> & other)238*da58b97aSjoerg bool Equals(const ParamIteratorInterface<T>& other) const override {
23906f32e7eSjoerg // Having the same base generator guarantees that the other
24006f32e7eSjoerg // iterator is of the same type and we can downcast.
24106f32e7eSjoerg GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
24206f32e7eSjoerg << "The program attempted to compare iterators "
24306f32e7eSjoerg << "from different generators." << std::endl;
24406f32e7eSjoerg const int other_index =
24506f32e7eSjoerg CheckedDowncastToActualType<const Iterator>(&other)->index_;
24606f32e7eSjoerg return index_ == other_index;
24706f32e7eSjoerg }
24806f32e7eSjoerg
24906f32e7eSjoerg private:
Iterator(const Iterator & other)25006f32e7eSjoerg Iterator(const Iterator& other)
25106f32e7eSjoerg : ParamIteratorInterface<T>(),
25206f32e7eSjoerg base_(other.base_), value_(other.value_), index_(other.index_),
25306f32e7eSjoerg step_(other.step_) {}
25406f32e7eSjoerg
25506f32e7eSjoerg // No implementation - assignment is unsupported.
25606f32e7eSjoerg void operator=(const Iterator& other);
25706f32e7eSjoerg
25806f32e7eSjoerg const ParamGeneratorInterface<T>* const base_;
25906f32e7eSjoerg T value_;
26006f32e7eSjoerg int index_;
26106f32e7eSjoerg const IncrementT step_;
26206f32e7eSjoerg }; // class RangeGenerator::Iterator
26306f32e7eSjoerg
CalculateEndIndex(const T & begin,const T & end,const IncrementT & step)26406f32e7eSjoerg static int CalculateEndIndex(const T& begin,
26506f32e7eSjoerg const T& end,
26606f32e7eSjoerg const IncrementT& step) {
26706f32e7eSjoerg int end_index = 0;
26806f32e7eSjoerg for (T i = begin; i < end; i = static_cast<T>(i + step))
26906f32e7eSjoerg end_index++;
27006f32e7eSjoerg return end_index;
27106f32e7eSjoerg }
27206f32e7eSjoerg
27306f32e7eSjoerg // No implementation - assignment is unsupported.
27406f32e7eSjoerg void operator=(const RangeGenerator& other);
27506f32e7eSjoerg
27606f32e7eSjoerg const T begin_;
27706f32e7eSjoerg const T end_;
27806f32e7eSjoerg const IncrementT step_;
27906f32e7eSjoerg // The index for the end() iterator. All the elements in the generated
28006f32e7eSjoerg // sequence are indexed (0-based) to aid iterator comparison.
28106f32e7eSjoerg const int end_index_;
28206f32e7eSjoerg }; // class RangeGenerator
28306f32e7eSjoerg
28406f32e7eSjoerg
28506f32e7eSjoerg // Generates values from a pair of STL-style iterators. Used in the
28606f32e7eSjoerg // ValuesIn() function. The elements are copied from the source range
28706f32e7eSjoerg // since the source can be located on the stack, and the generator
28806f32e7eSjoerg // is likely to persist beyond that stack frame.
28906f32e7eSjoerg template <typename T>
29006f32e7eSjoerg class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
29106f32e7eSjoerg public:
29206f32e7eSjoerg template <typename ForwardIterator>
ValuesInIteratorRangeGenerator(ForwardIterator begin,ForwardIterator end)29306f32e7eSjoerg ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
29406f32e7eSjoerg : container_(begin, end) {}
~ValuesInIteratorRangeGenerator()295*da58b97aSjoerg ~ValuesInIteratorRangeGenerator() override {}
29606f32e7eSjoerg
Begin()297*da58b97aSjoerg ParamIteratorInterface<T>* Begin() const override {
29806f32e7eSjoerg return new Iterator(this, container_.begin());
29906f32e7eSjoerg }
End()300*da58b97aSjoerg ParamIteratorInterface<T>* End() const override {
30106f32e7eSjoerg return new Iterator(this, container_.end());
30206f32e7eSjoerg }
30306f32e7eSjoerg
30406f32e7eSjoerg private:
30506f32e7eSjoerg typedef typename ::std::vector<T> ContainerType;
30606f32e7eSjoerg
30706f32e7eSjoerg class Iterator : public ParamIteratorInterface<T> {
30806f32e7eSjoerg public:
Iterator(const ParamGeneratorInterface<T> * base,typename ContainerType::const_iterator iterator)30906f32e7eSjoerg Iterator(const ParamGeneratorInterface<T>* base,
31006f32e7eSjoerg typename ContainerType::const_iterator iterator)
31106f32e7eSjoerg : base_(base), iterator_(iterator) {}
~Iterator()312*da58b97aSjoerg ~Iterator() override {}
31306f32e7eSjoerg
BaseGenerator()314*da58b97aSjoerg const ParamGeneratorInterface<T>* BaseGenerator() const override {
31506f32e7eSjoerg return base_;
31606f32e7eSjoerg }
Advance()317*da58b97aSjoerg void Advance() override {
31806f32e7eSjoerg ++iterator_;
31906f32e7eSjoerg value_.reset();
32006f32e7eSjoerg }
Clone()321*da58b97aSjoerg ParamIteratorInterface<T>* Clone() const override {
32206f32e7eSjoerg return new Iterator(*this);
32306f32e7eSjoerg }
32406f32e7eSjoerg // We need to use cached value referenced by iterator_ because *iterator_
32506f32e7eSjoerg // can return a temporary object (and of type other then T), so just
32606f32e7eSjoerg // having "return &*iterator_;" doesn't work.
32706f32e7eSjoerg // value_ is updated here and not in Advance() because Advance()
32806f32e7eSjoerg // can advance iterator_ beyond the end of the range, and we cannot
32906f32e7eSjoerg // detect that fact. The client code, on the other hand, is
33006f32e7eSjoerg // responsible for not calling Current() on an out-of-range iterator.
Current()331*da58b97aSjoerg const T* Current() const override {
332*da58b97aSjoerg if (value_.get() == nullptr) value_.reset(new T(*iterator_));
33306f32e7eSjoerg return value_.get();
33406f32e7eSjoerg }
Equals(const ParamIteratorInterface<T> & other)335*da58b97aSjoerg bool Equals(const ParamIteratorInterface<T>& other) const override {
33606f32e7eSjoerg // Having the same base generator guarantees that the other
33706f32e7eSjoerg // iterator is of the same type and we can downcast.
33806f32e7eSjoerg GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
33906f32e7eSjoerg << "The program attempted to compare iterators "
34006f32e7eSjoerg << "from different generators." << std::endl;
34106f32e7eSjoerg return iterator_ ==
34206f32e7eSjoerg CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
34306f32e7eSjoerg }
34406f32e7eSjoerg
34506f32e7eSjoerg private:
Iterator(const Iterator & other)34606f32e7eSjoerg Iterator(const Iterator& other)
34706f32e7eSjoerg // The explicit constructor call suppresses a false warning
34806f32e7eSjoerg // emitted by gcc when supplied with the -Wextra option.
34906f32e7eSjoerg : ParamIteratorInterface<T>(),
35006f32e7eSjoerg base_(other.base_),
35106f32e7eSjoerg iterator_(other.iterator_) {}
35206f32e7eSjoerg
35306f32e7eSjoerg const ParamGeneratorInterface<T>* const base_;
35406f32e7eSjoerg typename ContainerType::const_iterator iterator_;
35506f32e7eSjoerg // A cached value of *iterator_. We keep it here to allow access by
35606f32e7eSjoerg // pointer in the wrapping iterator's operator->().
35706f32e7eSjoerg // value_ needs to be mutable to be accessed in Current().
358*da58b97aSjoerg // Use of std::unique_ptr helps manage cached value's lifetime,
35906f32e7eSjoerg // which is bound by the lifespan of the iterator itself.
360*da58b97aSjoerg mutable std::unique_ptr<const T> value_;
36106f32e7eSjoerg }; // class ValuesInIteratorRangeGenerator::Iterator
36206f32e7eSjoerg
36306f32e7eSjoerg // No implementation - assignment is unsupported.
36406f32e7eSjoerg void operator=(const ValuesInIteratorRangeGenerator& other);
36506f32e7eSjoerg
36606f32e7eSjoerg const ContainerType container_;
36706f32e7eSjoerg }; // class ValuesInIteratorRangeGenerator
36806f32e7eSjoerg
36906f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
37006f32e7eSjoerg //
37106f32e7eSjoerg // Default parameterized test name generator, returns a string containing the
37206f32e7eSjoerg // integer test parameter index.
37306f32e7eSjoerg template <class ParamType>
DefaultParamName(const TestParamInfo<ParamType> & info)37406f32e7eSjoerg std::string DefaultParamName(const TestParamInfo<ParamType>& info) {
37506f32e7eSjoerg Message name_stream;
37606f32e7eSjoerg name_stream << info.index;
37706f32e7eSjoerg return name_stream.GetString();
37806f32e7eSjoerg }
37906f32e7eSjoerg
380*da58b97aSjoerg template <typename T = int>
TestNotEmpty()381*da58b97aSjoerg void TestNotEmpty() {
382*da58b97aSjoerg static_assert(sizeof(T) == 0, "Empty arguments are not allowed.");
38306f32e7eSjoerg }
384*da58b97aSjoerg template <typename T = int>
TestNotEmpty(const T &)385*da58b97aSjoerg void TestNotEmpty(const T&) {}
38606f32e7eSjoerg
38706f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
38806f32e7eSjoerg //
38906f32e7eSjoerg // Stores a parameter value and later creates tests parameterized with that
39006f32e7eSjoerg // value.
39106f32e7eSjoerg template <class TestClass>
39206f32e7eSjoerg class ParameterizedTestFactory : public TestFactoryBase {
39306f32e7eSjoerg public:
39406f32e7eSjoerg typedef typename TestClass::ParamType ParamType;
ParameterizedTestFactory(ParamType parameter)39506f32e7eSjoerg explicit ParameterizedTestFactory(ParamType parameter) :
39606f32e7eSjoerg parameter_(parameter) {}
CreateTest()397*da58b97aSjoerg Test* CreateTest() override {
39806f32e7eSjoerg TestClass::SetParam(¶meter_);
39906f32e7eSjoerg return new TestClass();
40006f32e7eSjoerg }
40106f32e7eSjoerg
40206f32e7eSjoerg private:
40306f32e7eSjoerg const ParamType parameter_;
40406f32e7eSjoerg
40506f32e7eSjoerg GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory);
40606f32e7eSjoerg };
40706f32e7eSjoerg
40806f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
40906f32e7eSjoerg //
41006f32e7eSjoerg // TestMetaFactoryBase is a base class for meta-factories that create
41106f32e7eSjoerg // test factories for passing into MakeAndRegisterTestInfo function.
41206f32e7eSjoerg template <class ParamType>
41306f32e7eSjoerg class TestMetaFactoryBase {
41406f32e7eSjoerg public:
~TestMetaFactoryBase()41506f32e7eSjoerg virtual ~TestMetaFactoryBase() {}
41606f32e7eSjoerg
41706f32e7eSjoerg virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
41806f32e7eSjoerg };
41906f32e7eSjoerg
42006f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
42106f32e7eSjoerg //
42206f32e7eSjoerg // TestMetaFactory creates test factories for passing into
42306f32e7eSjoerg // MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
42406f32e7eSjoerg // ownership of test factory pointer, same factory object cannot be passed
425*da58b97aSjoerg // into that method twice. But ParameterizedTestSuiteInfo is going to call
42606f32e7eSjoerg // it for each Test/Parameter value combination. Thus it needs meta factory
42706f32e7eSjoerg // creator class.
428*da58b97aSjoerg template <class TestSuite>
42906f32e7eSjoerg class TestMetaFactory
430*da58b97aSjoerg : public TestMetaFactoryBase<typename TestSuite::ParamType> {
43106f32e7eSjoerg public:
432*da58b97aSjoerg using ParamType = typename TestSuite::ParamType;
43306f32e7eSjoerg
TestMetaFactory()43406f32e7eSjoerg TestMetaFactory() {}
43506f32e7eSjoerg
CreateTestFactory(ParamType parameter)436*da58b97aSjoerg TestFactoryBase* CreateTestFactory(ParamType parameter) override {
437*da58b97aSjoerg return new ParameterizedTestFactory<TestSuite>(parameter);
43806f32e7eSjoerg }
43906f32e7eSjoerg
44006f32e7eSjoerg private:
44106f32e7eSjoerg GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory);
44206f32e7eSjoerg };
44306f32e7eSjoerg
44406f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
44506f32e7eSjoerg //
446*da58b97aSjoerg // ParameterizedTestSuiteInfoBase is a generic interface
447*da58b97aSjoerg // to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase
44806f32e7eSjoerg // accumulates test information provided by TEST_P macro invocations
449*da58b97aSjoerg // and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations
45006f32e7eSjoerg // and uses that information to register all resulting test instances
451*da58b97aSjoerg // in RegisterTests method. The ParameterizeTestSuiteRegistry class holds
452*da58b97aSjoerg // a collection of pointers to the ParameterizedTestSuiteInfo objects
45306f32e7eSjoerg // and calls RegisterTests() on each of them when asked.
454*da58b97aSjoerg class ParameterizedTestSuiteInfoBase {
45506f32e7eSjoerg public:
~ParameterizedTestSuiteInfoBase()456*da58b97aSjoerg virtual ~ParameterizedTestSuiteInfoBase() {}
45706f32e7eSjoerg
458*da58b97aSjoerg // Base part of test suite name for display purposes.
459*da58b97aSjoerg virtual const std::string& GetTestSuiteName() const = 0;
46006f32e7eSjoerg // Test case id to verify identity.
461*da58b97aSjoerg virtual TypeId GetTestSuiteTypeId() const = 0;
46206f32e7eSjoerg // UnitTest class invokes this method to register tests in this
463*da58b97aSjoerg // test suite right before running them in RUN_ALL_TESTS macro.
464*da58b97aSjoerg // This method should not be called more than once on any single
465*da58b97aSjoerg // instance of a ParameterizedTestSuiteInfoBase derived class.
46606f32e7eSjoerg virtual void RegisterTests() = 0;
46706f32e7eSjoerg
46806f32e7eSjoerg protected:
ParameterizedTestSuiteInfoBase()469*da58b97aSjoerg ParameterizedTestSuiteInfoBase() {}
47006f32e7eSjoerg
47106f32e7eSjoerg private:
472*da58b97aSjoerg GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestSuiteInfoBase);
47306f32e7eSjoerg };
47406f32e7eSjoerg
47506f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
47606f32e7eSjoerg //
477*da58b97aSjoerg // ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P
478*da58b97aSjoerg // macro invocations for a particular test suite and generators
479*da58b97aSjoerg // obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that
480*da58b97aSjoerg // test suite. It registers tests with all values generated by all
48106f32e7eSjoerg // generators when asked.
482*da58b97aSjoerg template <class TestSuite>
483*da58b97aSjoerg class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase {
48406f32e7eSjoerg public:
48506f32e7eSjoerg // ParamType and GeneratorCreationFunc are private types but are required
48606f32e7eSjoerg // for declarations of public methods AddTestPattern() and
487*da58b97aSjoerg // AddTestSuiteInstantiation().
488*da58b97aSjoerg using ParamType = typename TestSuite::ParamType;
48906f32e7eSjoerg // A function that returns an instance of appropriate generator type.
49006f32e7eSjoerg typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
491*da58b97aSjoerg using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&);
49206f32e7eSjoerg
ParameterizedTestSuiteInfo(const char * name,CodeLocation code_location)493*da58b97aSjoerg explicit ParameterizedTestSuiteInfo(const char* name,
494*da58b97aSjoerg CodeLocation code_location)
495*da58b97aSjoerg : test_suite_name_(name), code_location_(code_location) {}
49606f32e7eSjoerg
49706f32e7eSjoerg // Test case base name for display purposes.
GetTestSuiteName()498*da58b97aSjoerg const std::string& GetTestSuiteName() const override {
499*da58b97aSjoerg return test_suite_name_;
500*da58b97aSjoerg }
50106f32e7eSjoerg // Test case id to verify identity.
GetTestSuiteTypeId()502*da58b97aSjoerg TypeId GetTestSuiteTypeId() const override { return GetTypeId<TestSuite>(); }
50306f32e7eSjoerg // TEST_P macro uses AddTestPattern() to record information
50406f32e7eSjoerg // about a single test in a LocalTestInfo structure.
505*da58b97aSjoerg // test_suite_name is the base name of the test suite (without invocation
50606f32e7eSjoerg // prefix). test_base_name is the name of an individual test without
50706f32e7eSjoerg // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
508*da58b97aSjoerg // 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)509*da58b97aSjoerg void AddTestPattern(const char* test_suite_name, const char* test_base_name,
51006f32e7eSjoerg TestMetaFactoryBase<ParamType>* meta_factory) {
511*da58b97aSjoerg tests_.push_back(std::shared_ptr<TestInfo>(
512*da58b97aSjoerg new TestInfo(test_suite_name, test_base_name, meta_factory)));
51306f32e7eSjoerg }
514*da58b97aSjoerg // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information
51506f32e7eSjoerg // about a generator.
AddTestSuiteInstantiation(const std::string & instantiation_name,GeneratorCreationFunc * func,ParamNameGeneratorFunc * name_func,const char * file,int line)516*da58b97aSjoerg int AddTestSuiteInstantiation(const std::string& instantiation_name,
51706f32e7eSjoerg GeneratorCreationFunc* func,
51806f32e7eSjoerg ParamNameGeneratorFunc* name_func,
519*da58b97aSjoerg const char* file, int line) {
52006f32e7eSjoerg instantiations_.push_back(
52106f32e7eSjoerg InstantiationInfo(instantiation_name, func, name_func, file, line));
52206f32e7eSjoerg return 0; // Return value used only to run this method in namespace scope.
52306f32e7eSjoerg }
524*da58b97aSjoerg // UnitTest class invokes this method to register tests in this test suite
525*da58b97aSjoerg // test suites right before running tests in RUN_ALL_TESTS macro.
526*da58b97aSjoerg // This method should not be called more than once on any single
527*da58b97aSjoerg // instance of a ParameterizedTestSuiteInfoBase derived class.
528*da58b97aSjoerg // UnitTest has a guard to prevent from calling this method more than once.
RegisterTests()529*da58b97aSjoerg void RegisterTests() override {
53006f32e7eSjoerg for (typename TestInfoContainer::iterator test_it = tests_.begin();
53106f32e7eSjoerg test_it != tests_.end(); ++test_it) {
532*da58b97aSjoerg std::shared_ptr<TestInfo> test_info = *test_it;
53306f32e7eSjoerg for (typename InstantiationContainer::iterator gen_it =
53406f32e7eSjoerg instantiations_.begin(); gen_it != instantiations_.end();
53506f32e7eSjoerg ++gen_it) {
536*da58b97aSjoerg const std::string& instantiation_name = gen_it->name;
53706f32e7eSjoerg ParamGenerator<ParamType> generator((*gen_it->generator)());
53806f32e7eSjoerg ParamNameGeneratorFunc* name_func = gen_it->name_func;
53906f32e7eSjoerg const char* file = gen_it->file;
54006f32e7eSjoerg int line = gen_it->line;
54106f32e7eSjoerg
542*da58b97aSjoerg std::string test_suite_name;
54306f32e7eSjoerg if ( !instantiation_name.empty() )
544*da58b97aSjoerg test_suite_name = instantiation_name + "/";
545*da58b97aSjoerg test_suite_name += test_info->test_suite_base_name;
54606f32e7eSjoerg
54706f32e7eSjoerg size_t i = 0;
54806f32e7eSjoerg std::set<std::string> test_param_names;
54906f32e7eSjoerg for (typename ParamGenerator<ParamType>::iterator param_it =
55006f32e7eSjoerg generator.begin();
55106f32e7eSjoerg param_it != generator.end(); ++param_it, ++i) {
55206f32e7eSjoerg Message test_name_stream;
55306f32e7eSjoerg
55406f32e7eSjoerg std::string param_name = name_func(
55506f32e7eSjoerg TestParamInfo<ParamType>(*param_it, i));
55606f32e7eSjoerg
55706f32e7eSjoerg GTEST_CHECK_(IsValidParamName(param_name))
55806f32e7eSjoerg << "Parameterized test name '" << param_name
55906f32e7eSjoerg << "' is invalid, in " << file
56006f32e7eSjoerg << " line " << line << std::endl;
56106f32e7eSjoerg
56206f32e7eSjoerg GTEST_CHECK_(test_param_names.count(param_name) == 0)
56306f32e7eSjoerg << "Duplicate parameterized test name '" << param_name
56406f32e7eSjoerg << "', in " << file << " line " << line << std::endl;
56506f32e7eSjoerg
56606f32e7eSjoerg test_param_names.insert(param_name);
56706f32e7eSjoerg
568*da58b97aSjoerg if (!test_info->test_base_name.empty()) {
569*da58b97aSjoerg test_name_stream << test_info->test_base_name << "/";
570*da58b97aSjoerg }
571*da58b97aSjoerg test_name_stream << param_name;
57206f32e7eSjoerg MakeAndRegisterTestInfo(
573*da58b97aSjoerg test_suite_name.c_str(), test_name_stream.GetString().c_str(),
574*da58b97aSjoerg nullptr, // No type parameter.
575*da58b97aSjoerg PrintToString(*param_it).c_str(), code_location_,
576*da58b97aSjoerg GetTestSuiteTypeId(),
577*da58b97aSjoerg SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line),
578*da58b97aSjoerg SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line),
57906f32e7eSjoerg test_info->test_meta_factory->CreateTestFactory(*param_it));
58006f32e7eSjoerg } // for param_it
58106f32e7eSjoerg } // for gen_it
58206f32e7eSjoerg } // for test_it
58306f32e7eSjoerg } // RegisterTests
58406f32e7eSjoerg
58506f32e7eSjoerg private:
58606f32e7eSjoerg // LocalTestInfo structure keeps information about a single test registered
58706f32e7eSjoerg // with TEST_P macro.
58806f32e7eSjoerg struct TestInfo {
TestInfoTestInfo589*da58b97aSjoerg TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name,
590*da58b97aSjoerg TestMetaFactoryBase<ParamType>* a_test_meta_factory)
591*da58b97aSjoerg : test_suite_base_name(a_test_suite_base_name),
59206f32e7eSjoerg test_base_name(a_test_base_name),
59306f32e7eSjoerg test_meta_factory(a_test_meta_factory) {}
59406f32e7eSjoerg
595*da58b97aSjoerg const std::string test_suite_base_name;
596*da58b97aSjoerg const std::string test_base_name;
597*da58b97aSjoerg const std::unique_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory;
59806f32e7eSjoerg };
599*da58b97aSjoerg using TestInfoContainer = ::std::vector<std::shared_ptr<TestInfo> >;
600*da58b97aSjoerg // Records data received from INSTANTIATE_TEST_SUITE_P macros:
60106f32e7eSjoerg // <Instantiation name, Sequence generator creation function,
60206f32e7eSjoerg // Name generator function, Source file, Source line>
60306f32e7eSjoerg struct InstantiationInfo {
InstantiationInfoInstantiationInfo60406f32e7eSjoerg InstantiationInfo(const std::string &name_in,
60506f32e7eSjoerg GeneratorCreationFunc* generator_in,
60606f32e7eSjoerg ParamNameGeneratorFunc* name_func_in,
60706f32e7eSjoerg const char* file_in,
60806f32e7eSjoerg int line_in)
60906f32e7eSjoerg : name(name_in),
61006f32e7eSjoerg generator(generator_in),
61106f32e7eSjoerg name_func(name_func_in),
61206f32e7eSjoerg file(file_in),
61306f32e7eSjoerg line(line_in) {}
61406f32e7eSjoerg
61506f32e7eSjoerg std::string name;
61606f32e7eSjoerg GeneratorCreationFunc* generator;
61706f32e7eSjoerg ParamNameGeneratorFunc* name_func;
61806f32e7eSjoerg const char* file;
61906f32e7eSjoerg int line;
62006f32e7eSjoerg };
62106f32e7eSjoerg typedef ::std::vector<InstantiationInfo> InstantiationContainer;
62206f32e7eSjoerg
IsValidParamName(const std::string & name)62306f32e7eSjoerg static bool IsValidParamName(const std::string& name) {
62406f32e7eSjoerg // Check for empty string
62506f32e7eSjoerg if (name.empty())
62606f32e7eSjoerg return false;
62706f32e7eSjoerg
62806f32e7eSjoerg // Check for invalid characters
62906f32e7eSjoerg for (std::string::size_type index = 0; index < name.size(); ++index) {
63006f32e7eSjoerg if (!isalnum(name[index]) && name[index] != '_')
63106f32e7eSjoerg return false;
63206f32e7eSjoerg }
63306f32e7eSjoerg
63406f32e7eSjoerg return true;
63506f32e7eSjoerg }
63606f32e7eSjoerg
637*da58b97aSjoerg const std::string test_suite_name_;
63806f32e7eSjoerg CodeLocation code_location_;
63906f32e7eSjoerg TestInfoContainer tests_;
64006f32e7eSjoerg InstantiationContainer instantiations_;
64106f32e7eSjoerg
642*da58b97aSjoerg GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestSuiteInfo);
643*da58b97aSjoerg }; // class ParameterizedTestSuiteInfo
644*da58b97aSjoerg
645*da58b97aSjoerg // Legacy API is deprecated but still available
646*da58b97aSjoerg #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
647*da58b97aSjoerg template <class TestCase>
648*da58b97aSjoerg using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<TestCase>;
649*da58b97aSjoerg #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
65006f32e7eSjoerg
65106f32e7eSjoerg // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
65206f32e7eSjoerg //
653*da58b97aSjoerg // ParameterizedTestSuiteRegistry contains a map of
654*da58b97aSjoerg // ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P
655*da58b97aSjoerg // and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding
656*da58b97aSjoerg // ParameterizedTestSuiteInfo descriptors.
657*da58b97aSjoerg class ParameterizedTestSuiteRegistry {
65806f32e7eSjoerg public:
ParameterizedTestSuiteRegistry()659*da58b97aSjoerg ParameterizedTestSuiteRegistry() {}
~ParameterizedTestSuiteRegistry()660*da58b97aSjoerg ~ParameterizedTestSuiteRegistry() {
661*da58b97aSjoerg for (auto& test_suite_info : test_suite_infos_) {
662*da58b97aSjoerg delete test_suite_info;
66306f32e7eSjoerg }
66406f32e7eSjoerg }
66506f32e7eSjoerg
66606f32e7eSjoerg // Looks up or creates and returns a structure containing information about
667*da58b97aSjoerg // tests and instantiations of a particular test suite.
668*da58b97aSjoerg template <class TestSuite>
GetTestSuitePatternHolder(const char * test_suite_name,CodeLocation code_location)669*da58b97aSjoerg ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder(
670*da58b97aSjoerg const char* test_suite_name, CodeLocation code_location) {
671*da58b97aSjoerg ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr;
672*da58b97aSjoerg for (auto& test_suite_info : test_suite_infos_) {
673*da58b97aSjoerg if (test_suite_info->GetTestSuiteName() == test_suite_name) {
674*da58b97aSjoerg if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
67506f32e7eSjoerg // Complain about incorrect usage of Google Test facilities
67606f32e7eSjoerg // and terminate the program since we cannot guaranty correct
677*da58b97aSjoerg // test suite setup and tear-down in this case.
678*da58b97aSjoerg ReportInvalidTestSuiteType(test_suite_name, code_location);
67906f32e7eSjoerg posix::Abort();
68006f32e7eSjoerg } else {
68106f32e7eSjoerg // At this point we are sure that the object we found is of the same
68206f32e7eSjoerg // type we are looking for, so we downcast it to that type
68306f32e7eSjoerg // without further checks.
68406f32e7eSjoerg typed_test_info = CheckedDowncastToActualType<
685*da58b97aSjoerg ParameterizedTestSuiteInfo<TestSuite> >(test_suite_info);
68606f32e7eSjoerg }
68706f32e7eSjoerg break;
68806f32e7eSjoerg }
68906f32e7eSjoerg }
690*da58b97aSjoerg if (typed_test_info == nullptr) {
691*da58b97aSjoerg typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>(
692*da58b97aSjoerg test_suite_name, code_location);
693*da58b97aSjoerg test_suite_infos_.push_back(typed_test_info);
69406f32e7eSjoerg }
69506f32e7eSjoerg return typed_test_info;
69606f32e7eSjoerg }
RegisterTests()69706f32e7eSjoerg void RegisterTests() {
698*da58b97aSjoerg for (auto& test_suite_info : test_suite_infos_) {
699*da58b97aSjoerg test_suite_info->RegisterTests();
70006f32e7eSjoerg }
70106f32e7eSjoerg }
702*da58b97aSjoerg // Legacy API is deprecated but still available
703*da58b97aSjoerg #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
704*da58b97aSjoerg template <class TestCase>
GetTestCasePatternHolder(const char * test_case_name,CodeLocation code_location)705*da58b97aSjoerg ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
706*da58b97aSjoerg const char* test_case_name, CodeLocation code_location) {
707*da58b97aSjoerg return GetTestSuitePatternHolder<TestCase>(test_case_name, code_location);
708*da58b97aSjoerg }
709*da58b97aSjoerg
710*da58b97aSjoerg #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
711*da58b97aSjoerg
712*da58b97aSjoerg private:
713*da58b97aSjoerg using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
714*da58b97aSjoerg
715*da58b97aSjoerg TestSuiteInfoContainer test_suite_infos_;
716*da58b97aSjoerg
717*da58b97aSjoerg GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestSuiteRegistry);
718*da58b97aSjoerg };
719*da58b97aSjoerg
720*da58b97aSjoerg } // namespace internal
721*da58b97aSjoerg
722*da58b97aSjoerg // Forward declarations of ValuesIn(), which is implemented in
723*da58b97aSjoerg // include/gtest/gtest-param-test.h.
724*da58b97aSjoerg template <class Container>
725*da58b97aSjoerg internal::ParamGenerator<typename Container::value_type> ValuesIn(
726*da58b97aSjoerg const Container& container);
727*da58b97aSjoerg
728*da58b97aSjoerg namespace internal {
729*da58b97aSjoerg // Used in the Values() function to provide polymorphic capabilities.
730*da58b97aSjoerg
731*da58b97aSjoerg template <typename... Ts>
732*da58b97aSjoerg class ValueArray {
733*da58b97aSjoerg public:
ValueArray(Ts...v)734*da58b97aSjoerg ValueArray(Ts... v) : v_{std::move(v)...} {}
735*da58b97aSjoerg
736*da58b97aSjoerg template <typename T>
737*da58b97aSjoerg operator ParamGenerator<T>() const { // NOLINT
738*da58b97aSjoerg return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>()));
739*da58b97aSjoerg }
74006f32e7eSjoerg
74106f32e7eSjoerg private:
742*da58b97aSjoerg template <typename T, size_t... I>
MakeVector(IndexSequence<I...>)743*da58b97aSjoerg std::vector<T> MakeVector(IndexSequence<I...>) const {
744*da58b97aSjoerg return std::vector<T>{static_cast<T>(v_.template Get<I>())...};
745*da58b97aSjoerg }
74606f32e7eSjoerg
747*da58b97aSjoerg FlatTuple<Ts...> v_;
748*da58b97aSjoerg };
74906f32e7eSjoerg
750*da58b97aSjoerg template <typename... T>
751*da58b97aSjoerg class CartesianProductGenerator
752*da58b97aSjoerg : public ParamGeneratorInterface<::std::tuple<T...>> {
753*da58b97aSjoerg public:
754*da58b97aSjoerg typedef ::std::tuple<T...> ParamType;
755*da58b97aSjoerg
CartesianProductGenerator(const std::tuple<ParamGenerator<T>...> & g)756*da58b97aSjoerg CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g)
757*da58b97aSjoerg : generators_(g) {}
~CartesianProductGenerator()758*da58b97aSjoerg ~CartesianProductGenerator() override {}
759*da58b97aSjoerg
Begin()760*da58b97aSjoerg ParamIteratorInterface<ParamType>* Begin() const override {
761*da58b97aSjoerg return new Iterator(this, generators_, false);
762*da58b97aSjoerg }
End()763*da58b97aSjoerg ParamIteratorInterface<ParamType>* End() const override {
764*da58b97aSjoerg return new Iterator(this, generators_, true);
765*da58b97aSjoerg }
766*da58b97aSjoerg
767*da58b97aSjoerg private:
768*da58b97aSjoerg template <class I>
769*da58b97aSjoerg class IteratorImpl;
770*da58b97aSjoerg template <size_t... I>
771*da58b97aSjoerg class IteratorImpl<IndexSequence<I...>>
772*da58b97aSjoerg : public ParamIteratorInterface<ParamType> {
773*da58b97aSjoerg public:
IteratorImpl(const ParamGeneratorInterface<ParamType> * base,const std::tuple<ParamGenerator<T>...> & generators,bool is_end)774*da58b97aSjoerg IteratorImpl(const ParamGeneratorInterface<ParamType>* base,
775*da58b97aSjoerg const std::tuple<ParamGenerator<T>...>& generators, bool is_end)
776*da58b97aSjoerg : base_(base),
777*da58b97aSjoerg begin_(std::get<I>(generators).begin()...),
778*da58b97aSjoerg end_(std::get<I>(generators).end()...),
779*da58b97aSjoerg current_(is_end ? end_ : begin_) {
780*da58b97aSjoerg ComputeCurrentValue();
781*da58b97aSjoerg }
~IteratorImpl()782*da58b97aSjoerg ~IteratorImpl() override {}
783*da58b97aSjoerg
BaseGenerator()784*da58b97aSjoerg const ParamGeneratorInterface<ParamType>* BaseGenerator() const override {
785*da58b97aSjoerg return base_;
786*da58b97aSjoerg }
787*da58b97aSjoerg // Advance should not be called on beyond-of-range iterators
788*da58b97aSjoerg // so no component iterators must be beyond end of range, either.
Advance()789*da58b97aSjoerg void Advance() override {
790*da58b97aSjoerg assert(!AtEnd());
791*da58b97aSjoerg // Advance the last iterator.
792*da58b97aSjoerg ++std::get<sizeof...(T) - 1>(current_);
793*da58b97aSjoerg // if that reaches end, propagate that up.
794*da58b97aSjoerg AdvanceIfEnd<sizeof...(T) - 1>();
795*da58b97aSjoerg ComputeCurrentValue();
796*da58b97aSjoerg }
Clone()797*da58b97aSjoerg ParamIteratorInterface<ParamType>* Clone() const override {
798*da58b97aSjoerg return new IteratorImpl(*this);
799*da58b97aSjoerg }
800*da58b97aSjoerg
Current()801*da58b97aSjoerg const ParamType* Current() const override { return current_value_.get(); }
802*da58b97aSjoerg
Equals(const ParamIteratorInterface<ParamType> & other)803*da58b97aSjoerg bool Equals(const ParamIteratorInterface<ParamType>& other) const override {
804*da58b97aSjoerg // Having the same base generator guarantees that the other
805*da58b97aSjoerg // iterator is of the same type and we can downcast.
806*da58b97aSjoerg GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
807*da58b97aSjoerg << "The program attempted to compare iterators "
808*da58b97aSjoerg << "from different generators." << std::endl;
809*da58b97aSjoerg const IteratorImpl* typed_other =
810*da58b97aSjoerg CheckedDowncastToActualType<const IteratorImpl>(&other);
811*da58b97aSjoerg
812*da58b97aSjoerg // We must report iterators equal if they both point beyond their
813*da58b97aSjoerg // respective ranges. That can happen in a variety of fashions,
814*da58b97aSjoerg // so we have to consult AtEnd().
815*da58b97aSjoerg if (AtEnd() && typed_other->AtEnd()) return true;
816*da58b97aSjoerg
817*da58b97aSjoerg bool same = true;
818*da58b97aSjoerg bool dummy[] = {
819*da58b97aSjoerg (same = same && std::get<I>(current_) ==
820*da58b97aSjoerg std::get<I>(typed_other->current_))...};
821*da58b97aSjoerg (void)dummy;
822*da58b97aSjoerg return same;
823*da58b97aSjoerg }
824*da58b97aSjoerg
825*da58b97aSjoerg private:
826*da58b97aSjoerg template <size_t ThisI>
AdvanceIfEnd()827*da58b97aSjoerg void AdvanceIfEnd() {
828*da58b97aSjoerg if (std::get<ThisI>(current_) != std::get<ThisI>(end_)) return;
829*da58b97aSjoerg
830*da58b97aSjoerg bool last = ThisI == 0;
831*da58b97aSjoerg if (last) {
832*da58b97aSjoerg // We are done. Nothing else to propagate.
833*da58b97aSjoerg return;
834*da58b97aSjoerg }
835*da58b97aSjoerg
836*da58b97aSjoerg constexpr size_t NextI = ThisI - (ThisI != 0);
837*da58b97aSjoerg std::get<ThisI>(current_) = std::get<ThisI>(begin_);
838*da58b97aSjoerg ++std::get<NextI>(current_);
839*da58b97aSjoerg AdvanceIfEnd<NextI>();
840*da58b97aSjoerg }
841*da58b97aSjoerg
ComputeCurrentValue()842*da58b97aSjoerg void ComputeCurrentValue() {
843*da58b97aSjoerg if (!AtEnd())
844*da58b97aSjoerg current_value_ = std::make_shared<ParamType>(*std::get<I>(current_)...);
845*da58b97aSjoerg }
AtEnd()846*da58b97aSjoerg bool AtEnd() const {
847*da58b97aSjoerg bool at_end = false;
848*da58b97aSjoerg bool dummy[] = {
849*da58b97aSjoerg (at_end = at_end || std::get<I>(current_) == std::get<I>(end_))...};
850*da58b97aSjoerg (void)dummy;
851*da58b97aSjoerg return at_end;
852*da58b97aSjoerg }
853*da58b97aSjoerg
854*da58b97aSjoerg const ParamGeneratorInterface<ParamType>* const base_;
855*da58b97aSjoerg std::tuple<typename ParamGenerator<T>::iterator...> begin_;
856*da58b97aSjoerg std::tuple<typename ParamGenerator<T>::iterator...> end_;
857*da58b97aSjoerg std::tuple<typename ParamGenerator<T>::iterator...> current_;
858*da58b97aSjoerg std::shared_ptr<ParamType> current_value_;
859*da58b97aSjoerg };
860*da58b97aSjoerg
861*da58b97aSjoerg using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>;
862*da58b97aSjoerg
863*da58b97aSjoerg std::tuple<ParamGenerator<T>...> generators_;
864*da58b97aSjoerg };
865*da58b97aSjoerg
866*da58b97aSjoerg template <class... Gen>
867*da58b97aSjoerg class CartesianProductHolder {
868*da58b97aSjoerg public:
CartesianProductHolder(const Gen &...g)869*da58b97aSjoerg CartesianProductHolder(const Gen&... g) : generators_(g...) {}
870*da58b97aSjoerg template <typename... T>
871*da58b97aSjoerg operator ParamGenerator<::std::tuple<T...>>() const {
872*da58b97aSjoerg return ParamGenerator<::std::tuple<T...>>(
873*da58b97aSjoerg new CartesianProductGenerator<T...>(generators_));
874*da58b97aSjoerg }
875*da58b97aSjoerg
876*da58b97aSjoerg private:
877*da58b97aSjoerg std::tuple<Gen...> generators_;
87806f32e7eSjoerg };
87906f32e7eSjoerg
88006f32e7eSjoerg } // namespace internal
88106f32e7eSjoerg } // namespace testing
88206f32e7eSjoerg
88306f32e7eSjoerg #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
884