11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2008 Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All Rights Reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: vladl@google.com (Vlad Losev)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Type and function utilities for implementing parameterized tests.
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <iterator>
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <utility>
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <vector>
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// scripts/fuse_gtest.py depends on gtest's own header being #included
4241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// *unconditionally*.  Therefore these #includes cannot be moved
4341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// inside #if GTEST_HAS_PARAM_TEST.
4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-internal.h"
4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-linked_ptr.h"
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-port.h"
4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-printers.h"
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Outputs a message explaining invalid registration of different
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fixture class for the same test case. This may happen when
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST_P macro is used to define two tests with the same name
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// but in different namespaces.
6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ void ReportInvalidTestCaseType(const char* test_case_name,
6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                          const char* file, int line);
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename> class ParamGeneratorInterface;
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename> class ParamGenerator;
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Interface for iterating over elements provided by an implementation
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of ParamGeneratorInterface<T>.
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParamIteratorInterface {
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~ParamIteratorInterface() {}
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A pointer to the base generator instance.
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Used only for the purposes of iterator comparison
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to make sure that two iterators belong to the same generator.
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Advances iterator to point to the next element
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // provided by the generator. The caller is responsible
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // for not calling Advance() on an iterator equal to
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // BaseGenerator()->End().
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual void Advance() = 0;
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Clones the iterator object. Used for implementing copy semantics
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // of ParamIterator<T>.
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface* Clone() const = 0;
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Dereferences the current iterator and provides (read-only) access
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to the pointed value. It is the caller's responsibility not to call
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Current() on an iterator equal to BaseGenerator()->End().
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Used for implementing ParamGenerator<T>::operator*().
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual const T* Current() const = 0;
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Determines whether the given iterator and other point to the same
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // element in the sequence generated by the generator.
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Used for implementing ParamGenerator<T>::operator==().
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual bool Equals(const ParamIteratorInterface& other) const = 0;
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class iterating over elements provided by an implementation of
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and implements the const forward iterator concept.
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParamIterator {
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef T value_type;
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef const T& reference;
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef ptrdiff_t difference_type;
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // ParamIterator assumes ownership of the impl_ pointer.
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {}
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParamIterator& operator=(const ParamIterator& other) {
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (this != &other)
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      impl_.reset(other.impl_->Clone());
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return *this;
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const T& operator*() const { return *impl_->Current(); }
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const T* operator->() const { return impl_->Current(); }
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Prefix version of operator++.
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParamIterator& operator++() {
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl_->Advance();
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return *this;
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Postfix version of operator++.
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParamIterator operator++(int /*unused*/) {
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ParamIteratorInterface<T>* clone = impl_->Clone();
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl_->Advance();
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return ParamIterator(clone);
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator==(const ParamIterator& other) const {
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator!=(const ParamIterator& other) const {
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return !(*this == other);
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  friend class ParamGenerator<T>;
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  scoped_ptr<ParamIteratorInterface<T> > impl_;
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ParamGeneratorInterface<T> is the binary interface to access generators
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// defined in other translation units.
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParamGeneratorInterface {
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef T ParamType;
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~ParamGeneratorInterface() {}
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Generator interface definition
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface<T>* Begin() const = 0;
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface<T>* End() const = 0;
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Wraps ParamGeneratorInterface<T> and provides general generator syntax
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compatible with the STL Container concept.
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class implements copy initialization semantics and the contained
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ParamGeneratorInterface<T> instance is shared among all copies
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of the original object. This is possible because that instance is immutable.
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate<typename T>
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParamGenerator {
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef ParamIterator<T> iterator;
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {}
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParamGenerator& operator=(const ParamGenerator& other) {
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl_ = other.impl_;
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return *this;
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  iterator begin() const { return iterator(impl_->Begin()); }
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  iterator end() const { return iterator(impl_->End()); }
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
17541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  linked_ptr<const ParamGeneratorInterface<T> > impl_;
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Generates values from a range of two comparable values. Can be used to
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generate sequences of user-defined types that implement operator+() and
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// operator<().
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class is used in the Range() function.
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T, typename IncrementT>
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass RangeGenerator : public ParamGeneratorInterface<T> {
1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  RangeGenerator(T begin, T end, IncrementT step)
1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      : begin_(begin), end_(end),
1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        step_(step), end_index_(CalculateEndIndex(begin, end, step)) {}
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~RangeGenerator() {}
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface<T>* Begin() const {
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return new Iterator(this, begin_, 0, step_);
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface<T>* End() const {
1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return new Iterator(this, end_, end_index_, step_);
1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  class Iterator : public ParamIteratorInterface<T> {
1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania   public:
2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania             IncrementT step)
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        : base_(base), value_(value), index_(index), step_(step) {}
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual ~Iterator() {}
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual const ParamGeneratorInterface<T>* BaseGenerator() const {
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return base_;
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual void Advance() {
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      value_ = value_ + step_;
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      index_++;
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual ParamIteratorInterface<T>* Clone() const {
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return new Iterator(*this);
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual const T* Current() const { return &value_; }
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual bool Equals(const ParamIteratorInterface<T>& other) const {
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Having the same base generator guarantees that the other
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // iterator is of the same type and we can downcast.
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "The program attempted to compare iterators "
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "from different generators." << std::endl;
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const int other_index =
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          CheckedDowncastToActualType<const Iterator>(&other)->index_;
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return index_ == other_index;
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania   private:
2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Iterator(const Iterator& other)
22941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        : ParamIteratorInterface<T>(),
23041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          base_(other.base_), value_(other.value_), index_(other.index_),
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          step_(other.step_) {}
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // No implementation - assignment is unsupported.
23441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    void operator=(const Iterator& other);
23541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const ParamGeneratorInterface<T>* const base_;
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    T value_;
2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    int index_;
2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const IncrementT step_;
2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  };  // class RangeGenerator::Iterator
2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static int CalculateEndIndex(const T& begin,
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const T& end,
2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const IncrementT& step) {
2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    int end_index = 0;
2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for (T i = begin; i < end; i = i + step)
2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      end_index++;
2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return end_index;
2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // No implementation - assignment is unsupported.
25241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  void operator=(const RangeGenerator& other);
25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const T begin_;
2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const T end_;
2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const IncrementT step_;
2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The index for the end() iterator. All the elements in the generated
2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // sequence are indexed (0-based) to aid iterator comparison.
2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int end_index_;
2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};  // class RangeGenerator
2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Generates values from a pair of STL-style iterators. Used in the
2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ValuesIn() function. The elements are copied from the source range
2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// since the source can be located on the stack, and the generator
2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is likely to persist beyond that stack frame.
2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <typename ForwardIterator>
2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      : container_(begin, end) {}
2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~ValuesInIteratorRangeGenerator() {}
2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface<T>* Begin() const {
2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return new Iterator(this, container_.begin());
2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ParamIteratorInterface<T>* End() const {
2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return new Iterator(this, container_.end());
2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef typename ::std::vector<T> ContainerType;
2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  class Iterator : public ParamIteratorInterface<T> {
2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania   public:
2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Iterator(const ParamGeneratorInterface<T>* base,
2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania             typename ContainerType::const_iterator iterator)
28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        : base_(base), iterator_(iterator) {}
2901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual ~Iterator() {}
2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual const ParamGeneratorInterface<T>* BaseGenerator() const {
2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return base_;
2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual void Advance() {
2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ++iterator_;
2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      value_.reset();
2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual ParamIteratorInterface<T>* Clone() const {
3001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return new Iterator(*this);
3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // We need to use cached value referenced by iterator_ because *iterator_
3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // can return a temporary object (and of type other then T), so just
3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // having "return &*iterator_;" doesn't work.
3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // value_ is updated here and not in Advance() because Advance()
3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // can advance iterator_ beyond the end of the range, and we cannot
3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // detect that fact. The client code, on the other hand, is
3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // responsible for not calling Current() on an out-of-range iterator.
3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual const T* Current() const {
3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      if (value_.get() == NULL)
3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        value_.reset(new T(*iterator_));
3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return value_.get();
3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    virtual bool Equals(const ParamIteratorInterface<T>& other) const {
3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Having the same base generator guarantees that the other
3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // iterator is of the same type and we can downcast.
3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "The program attempted to compare iterators "
3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "from different generators." << std::endl;
3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return iterator_ ==
3211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania   private:
3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Iterator(const Iterator& other)
3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // The explicit constructor call suppresses a false warning
3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // emitted by gcc when supplied with the -Wextra option.
3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        : ParamIteratorInterface<T>(),
3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          base_(other.base_),
3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          iterator_(other.iterator_) {}
3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const ParamGeneratorInterface<T>* const base_;
3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    typename ContainerType::const_iterator iterator_;
3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // A cached value of *iterator_. We keep it here to allow access by
3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // pointer in the wrapping iterator's operator->().
3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // value_ needs to be mutable to be accessed in Current().
3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Use of scoped_ptr helps manage cached value's lifetime,
3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // which is bound by the lifespan of the iterator itself.
3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    mutable scoped_ptr<const T> value_;
34041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  };  // class ValuesInIteratorRangeGenerator::Iterator
34141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
34241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // No implementation - assignment is unsupported.
34341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  void operator=(const ValuesInIteratorRangeGenerator& other);
3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const ContainerType container_;
3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};  // class ValuesInIteratorRangeGenerator
3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Stores a parameter value and later creates tests parameterized with that
3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value.
3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <class TestClass>
3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParameterizedTestFactory : public TestFactoryBase {
3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef typename TestClass::ParamType ParamType;
3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit ParameterizedTestFactory(ParamType parameter) :
3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      parameter_(parameter) {}
3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual Test* CreateTest() {
3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestClass::SetParam(&parameter_);
3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return new TestClass();
3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const ParamType parameter_;
3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory);
3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestMetaFactoryBase is a base class for meta-factories that create
3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test factories for passing into MakeAndRegisterTestInfo function.
3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <class ParamType>
3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestMetaFactoryBase {
3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~TestMetaFactoryBase() {}
3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestMetaFactory creates test factories for passing into
3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ownership of test factory pointer, same factory object cannot be passed
3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// into that method twice. But ParameterizedTestCaseInfo is going to call
3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it for each Test/Parameter value combination. Thus it needs meta factory
3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// creator class.
3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <class TestCase>
3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestMetaFactory
3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : public TestMetaFactoryBase<typename TestCase::ParamType> {
3921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef typename TestCase::ParamType ParamType;
3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestMetaFactory() {}
3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual TestFactoryBase* CreateTestFactory(ParamType parameter) {
3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return new ParameterizedTestFactory<TestCase>(parameter);
3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory);
4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ParameterizedTestCaseInfoBase is a generic interface
4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to ParameterizedTestCaseInfo classes. ParameterizedTestCaseInfoBase
4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// accumulates test information provided by TEST_P macro invocations
4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and generators provided by INSTANTIATE_TEST_CASE_P macro invocations
4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and uses that information to register all resulting test instances
4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in RegisterTests method. The ParameterizeTestCaseRegistry class holds
4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// a collection of pointers to the ParameterizedTestCaseInfo objects
4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and calls RegisterTests() on each of them when asked.
4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParameterizedTestCaseInfoBase {
4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~ParameterizedTestCaseInfoBase() {}
4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Base part of test case name for display purposes.
42041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual const string& GetTestCaseName() const = 0;
4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Test case id to verify identity.
4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual TypeId GetTestCaseTypeId() const = 0;
4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // UnitTest class invokes this method to register tests in this
4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test case right before running them in RUN_ALL_TESTS macro.
4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This method should not be called more then once on any single
4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // instance of a ParameterizedTestCaseInfoBase derived class.
4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual void RegisterTests() = 0;
4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected:
4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParameterizedTestCaseInfoBase() {}
4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfoBase);
4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ParameterizedTestCaseInfo accumulates tests obtained from TEST_P
4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// macro invocations for a particular test case and generators
4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// obtained from INSTANTIATE_TEST_CASE_P macro invocations for that
4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case. It registers tests with all values generated by all
4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generators when asked.
4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <class TestCase>
4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase {
4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // ParamType and GeneratorCreationFunc are private types but are required
4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // for declarations of public methods AddTestPattern() and
4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // AddTestCaseInstantiation().
4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef typename TestCase::ParamType ParamType;
4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A function that returns an instance of appropriate generator type.
4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit ParameterizedTestCaseInfo(const char* name)
4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      : test_case_name_(name) {}
4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Test case base name for display purposes.
45741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual const string& GetTestCaseName() const { return test_case_name_; }
4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Test case id to verify identity.
4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual TypeId GetTestCaseTypeId() const { return GetTypeId<TestCase>(); }
4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TEST_P macro uses AddTestPattern() to record information
4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // about a single test in a LocalTestInfo structure.
4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test_case_name is the base name of the test case (without invocation
4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // prefix). test_base_name is the name of an individual test without
4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test case base name and DoBar is test base name.
4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void AddTestPattern(const char* test_case_name,
4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      const char* test_base_name,
4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      TestMetaFactoryBase<ParamType>* meta_factory) {
4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    tests_.push_back(linked_ptr<TestInfo>(new TestInfo(test_case_name,
4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                       test_base_name,
4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                       meta_factory)));
4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // INSTANTIATE_TEST_CASE_P macro uses AddGenerator() to record information
4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // about a generator.
47541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  int AddTestCaseInstantiation(const string& instantiation_name,
4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               GeneratorCreationFunc* func,
47741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                               const char* /* file */,
47841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                               int /* line */) {
4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    instantiations_.push_back(::std::make_pair(instantiation_name, func));
4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return 0;  // Return value used only to run this method in namespace scope.
4811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // UnitTest class invokes this method to register tests in this test case
4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test cases right before running tests in RUN_ALL_TESTS macro.
4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This method should not be called more then once on any single
4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // instance of a ParameterizedTestCaseInfoBase derived class.
4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // UnitTest has a guard to prevent from calling this method more then once.
4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual void RegisterTests() {
4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for (typename TestInfoContainer::iterator test_it = tests_.begin();
4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania         test_it != tests_.end(); ++test_it) {
4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      linked_ptr<TestInfo> test_info = *test_it;
4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      for (typename InstantiationContainer::iterator gen_it =
4921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania               instantiations_.begin(); gen_it != instantiations_.end();
4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania               ++gen_it) {
49441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        const string& instantiation_name = gen_it->first;
4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParamGenerator<ParamType> generator((*gen_it->second)());
4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        Message test_case_name_stream;
4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        if ( !instantiation_name.empty() )
49941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          test_case_name_stream << instantiation_name << "/";
50041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        test_case_name_stream << test_info->test_case_base_name;
5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        int i = 0;
5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        for (typename ParamGenerator<ParamType>::iterator param_it =
5041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                 generator.begin();
5051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania             param_it != generator.end(); ++param_it, ++i) {
5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          Message test_name_stream;
50741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          test_name_stream << test_info->test_base_name << "/" << i;
50841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          MakeAndRegisterTestInfo(
5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              test_case_name_stream.GetString().c_str(),
5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              test_name_stream.GetString().c_str(),
51141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot              NULL,  // No type parameter.
51241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot              PrintToString(*param_it).c_str(),
5131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              GetTestCaseTypeId(),
5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              TestCase::SetUpTestCase,
5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              TestCase::TearDownTestCase,
5161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              test_info->test_meta_factory->CreateTestFactory(*param_it));
5171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        }  // for param_it
5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      }  // for gen_it
5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }  // for test_it
5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }  // RegisterTests
5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
5231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // LocalTestInfo structure keeps information about a single test registered
5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // with TEST_P macro.
5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  struct TestInfo {
52641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    TestInfo(const char* a_test_case_base_name,
52741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot             const char* a_test_base_name,
52841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot             TestMetaFactoryBase<ParamType>* a_test_meta_factory) :
52941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        test_case_base_name(a_test_case_base_name),
53041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        test_base_name(a_test_base_name),
53141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        test_meta_factory(a_test_meta_factory) {}
53241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
53341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const string test_case_base_name;
53441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const string test_base_name;
5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const scoped_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory;
5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  };
5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef ::std::vector<linked_ptr<TestInfo> > TestInfoContainer;
5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Keeps pairs of <Instantiation name, Sequence generator creation function>
5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // received from INSTANTIATE_TEST_CASE_P macros.
54041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  typedef ::std::vector<std::pair<string, GeneratorCreationFunc*> >
5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      InstantiationContainer;
5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
54341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const string test_case_name_;
5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestInfoContainer tests_;
5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  InstantiationContainer instantiations_;
5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfo);
5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};  // class ParameterizedTestCaseInfo
5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ParameterizedTestCaseRegistry contains a map of ParameterizedTestCaseInfoBase
5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// classes accessed by test case names. TEST_P and INSTANTIATE_TEST_CASE_P
5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// macros use it to locate their corresponding ParameterizedTestCaseInfo
5551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// descriptors.
5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ParameterizedTestCaseRegistry {
5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParameterizedTestCaseRegistry() {}
5591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ~ParameterizedTestCaseRegistry() {
5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania         it != test_case_infos_.end(); ++it) {
5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      delete *it;
5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Looks up or creates and returns a structure containing information about
5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // tests and instantiations of a particular test case.
5681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  template <class TestCase>
5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const char* test_case_name,
5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const char* file,
5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      int line) {
5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ParameterizedTestCaseInfo<TestCase>* typed_test_info = NULL;
5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania         it != test_case_infos_.end(); ++it) {
5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      if ((*it)->GetTestCaseName() == test_case_name) {
5771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        if ((*it)->GetTestCaseTypeId() != GetTypeId<TestCase>()) {
5781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // Complain about incorrect usage of Google Test facilities
5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // and terminate the program since we cannot guaranty correct
5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // test case setup and tear-down in this case.
5811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          ReportInvalidTestCaseType(test_case_name,  file, line);
58241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          posix::Abort();
5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        } else {
5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // At this point we are sure that the object we found is of the same
5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // type we are looking for, so we downcast it to that type
5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          // without further checks.
5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          typed_test_info = CheckedDowncastToActualType<
5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              ParameterizedTestCaseInfo<TestCase> >(*it);
5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        }
5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        break;
5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      }
5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (typed_test_info == NULL) {
5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      typed_test_info = new ParameterizedTestCaseInfo<TestCase>(test_case_name);
5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      test_case_infos_.push_back(typed_test_info);
5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return typed_test_info;
5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void RegisterTests() {
6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania         it != test_case_infos_.end(); ++it) {
6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      (*it)->RegisterTests();
6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef ::std::vector<ParameterizedTestCaseInfoBase*> TestCaseInfoContainer;
6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestCaseInfoContainer test_case_infos_;
6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry);
6121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
6131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  //  GTEST_HAS_PARAM_TEST
6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
620