34 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 35 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 49 #if GTEST_HAS_PARAM_TEST 60 GTEST_API_ void ReportInvalidTestCaseType(
const char* test_case_name,
61 const char* file,
int line);
63 template <
typename>
class ParamGeneratorInterface;
64 template <
typename>
class ParamGenerator;
69 class ParamIteratorInterface {
71 virtual ~ParamIteratorInterface() {}
75 virtual const ParamGeneratorInterface<T>* BaseGenerator()
const = 0;
80 virtual void Advance() = 0;
83 virtual ParamIteratorInterface* Clone()
const = 0;
88 virtual const T* Current()
const = 0;
92 virtual bool Equals(
const ParamIteratorInterface& other)
const = 0;
101 typedef T value_type;
102 typedef const T& reference;
103 typedef ptrdiff_t difference_type;
106 ParamIterator(
const ParamIterator& other) : impl_(other.impl_->Clone()) {}
107 ParamIterator& operator=(
const ParamIterator& other) {
109 impl_.reset(other.impl_->Clone());
113 const T& operator*()
const {
return *impl_->Current(); }
114 const T* operator->()
const {
return impl_->Current(); }
116 ParamIterator& operator++() {
121 ParamIterator operator++(
int ) {
122 ParamIteratorInterface<T>* clone = impl_->Clone();
124 return ParamIterator(clone);
126 bool operator==(
const ParamIterator& other)
const {
127 return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
129 bool operator!=(
const ParamIterator& other)
const {
130 return !(*
this == other);
134 friend class ParamGenerator<
T>;
135 explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
136 scoped_ptr<ParamIteratorInterface<T> > impl_;
141 template <
typename T>
142 class ParamGeneratorInterface {
146 virtual ~ParamGeneratorInterface() {}
149 virtual ParamIteratorInterface<T>* Begin()
const = 0;
150 virtual ParamIteratorInterface<T>*
End()
const = 0;
159 class ParamGenerator {
161 typedef ParamIterator<T> iterator;
163 explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
164 ParamGenerator(
const ParamGenerator& other) : impl_(other.impl_) {}
166 ParamGenerator& operator=(
const ParamGenerator& other) {
171 iterator begin()
const {
return iterator(impl_->Begin()); }
172 iterator end()
const {
return iterator(impl_->End()); }
175 linked_ptr<const ParamGeneratorInterface<T> > impl_;
182 template <
typename T,
typename IncrementT>
183 class RangeGenerator :
public ParamGeneratorInterface<T> {
185 RangeGenerator(
T begin,
T end, IncrementT step)
186 : begin_(begin), end_(end),
187 step_(step), end_index_(CalculateEndIndex(begin, end, step)) {}
188 virtual ~RangeGenerator() {}
190 virtual ParamIteratorInterface<T>* Begin()
const {
191 return new Iterator(
this, begin_, 0, step_);
193 virtual ParamIteratorInterface<T>*
End()
const {
194 return new Iterator(
this, end_, end_index_, step_);
198 class Iterator :
public ParamIteratorInterface<T> {
200 Iterator(
const ParamGeneratorInterface<T>* base,
T value,
int index,
202 : base_(base), value_(value), index_(index), step_(step) {}
203 virtual ~Iterator() {}
205 virtual const ParamGeneratorInterface<T>* BaseGenerator()
const {
208 virtual void Advance() {
209 value_ = value_ + step_;
212 virtual ParamIteratorInterface<T>* Clone()
const {
213 return new Iterator(*
this);
215 virtual const T* Current()
const {
return &value_; }
216 virtual bool Equals(
const ParamIteratorInterface<T>& other)
const {
220 <<
"The program attempted to compare iterators " 221 <<
"from different generators." << std::endl;
222 const int other_index =
223 CheckedDowncastToActualType<const Iterator>(&other)->index_;
224 return index_ == other_index;
228 Iterator(
const Iterator& other)
229 : ParamIteratorInterface<
T>(),
230 base_(other.base_), value_(other.value_), index_(other.index_),
231 step_(other.step_) {}
234 void operator=(
const Iterator& other);
236 const ParamGeneratorInterface<T>*
const base_;
239 const IncrementT step_;
242 static int CalculateEndIndex(
const T& begin,
244 const IncrementT& step) {
246 for (
T i = begin; i < end; i = i + step)
252 void operator=(
const RangeGenerator& other);
256 const IncrementT step_;
259 const int end_index_;
267 template <
typename T>
268 class ValuesInIteratorRangeGenerator :
public ParamGeneratorInterface<T> {
270 template <
typename ForwardIterator>
271 ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
272 : container_(begin, end) {}
273 virtual ~ValuesInIteratorRangeGenerator() {}
275 virtual ParamIteratorInterface<T>* Begin()
const {
276 return new Iterator(
this, container_.begin());
278 virtual ParamIteratorInterface<T>*
End()
const {
279 return new Iterator(
this, container_.end());
283 typedef typename ::std::vector<T> ContainerType;
285 class Iterator :
public ParamIteratorInterface<T> {
287 Iterator(
const ParamGeneratorInterface<T>* base,
288 typename ContainerType::const_iterator iterator)
289 : base_(base), iterator_(iterator) {}
290 virtual ~Iterator() {}
292 virtual const ParamGeneratorInterface<T>* BaseGenerator()
const {
295 virtual void Advance() {
299 virtual ParamIteratorInterface<T>* Clone()
const {
300 return new Iterator(*
this);
309 virtual const T* Current()
const {
310 if (value_.get() == NULL)
311 value_.reset(
new T(*iterator_));
314 virtual bool Equals(
const ParamIteratorInterface<T>& other)
const {
318 <<
"The program attempted to compare iterators " 319 <<
"from different generators." << std::endl;
321 CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
325 Iterator(
const Iterator& other)
328 : ParamIteratorInterface<
T>(),
330 iterator_(other.iterator_) {}
332 const ParamGeneratorInterface<T>*
const base_;
333 typename ContainerType::const_iterator iterator_;
339 mutable scoped_ptr<const T> value_;
343 void operator=(
const ValuesInIteratorRangeGenerator& other);
345 const ContainerType container_;
352 template <
class TestClass>
353 class ParameterizedTestFactory :
public TestFactoryBase {
355 typedef typename TestClass::ParamType ParamType;
356 explicit ParameterizedTestFactory(ParamType parameter) :
357 parameter_(parameter) {}
358 virtual Test* CreateTest() {
359 TestClass::SetParam(¶meter_);
360 return new TestClass();
364 const ParamType parameter_;
373 template <
class ParamType>
374 class TestMetaFactoryBase {
376 virtual ~TestMetaFactoryBase() {}
378 virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
389 template <
class TestCase>
390 class TestMetaFactory
391 :
public TestMetaFactoryBase<typename TestCase::ParamType> {
393 typedef typename TestCase::ParamType ParamType;
397 virtual TestFactoryBase* CreateTestFactory(ParamType parameter) {
398 return new ParameterizedTestFactory<TestCase>(parameter);
415 class ParameterizedTestCaseInfoBase {
417 virtual ~ParameterizedTestCaseInfoBase() {}
420 virtual const string& GetTestCaseName()
const = 0;
422 virtual TypeId GetTestCaseTypeId()
const = 0;
427 virtual void RegisterTests() = 0;
430 ParameterizedTestCaseInfoBase() {}
443 template <
class TestCase>
444 class ParameterizedTestCaseInfo :
public ParameterizedTestCaseInfoBase {
449 typedef typename TestCase::ParamType ParamType;
451 typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
453 explicit ParameterizedTestCaseInfo(
const char* name)
454 : test_case_name_(name) {}
457 virtual const string& GetTestCaseName()
const {
return test_case_name_; }
459 virtual TypeId GetTestCaseTypeId()
const {
return GetTypeId<TestCase>(); }
466 void AddTestPattern(
const char* test_case_name,
467 const char* test_base_name,
468 TestMetaFactoryBase<ParamType>* meta_factory) {
469 tests_.push_back(linked_ptr<TestInfo>(
new TestInfo(test_case_name,
475 int AddTestCaseInstantiation(
const string& instantiation_name,
476 GeneratorCreationFunc* func,
479 instantiations_.push_back(::std::make_pair(instantiation_name, func));
487 virtual void RegisterTests() {
488 for (
typename TestInfoContainer::iterator test_it = tests_.begin();
489 test_it != tests_.end(); ++test_it) {
490 linked_ptr<TestInfo> test_info = *test_it;
491 for (
typename InstantiationContainer::iterator gen_it =
492 instantiations_.begin(); gen_it != instantiations_.end();
494 const string& instantiation_name = gen_it->first;
495 ParamGenerator<ParamType> generator((*gen_it->second)());
497 Message test_case_name_stream;
498 if ( !instantiation_name.empty() )
499 test_case_name_stream << instantiation_name <<
"/";
500 test_case_name_stream << test_info->test_case_base_name;
503 for (
typename ParamGenerator<ParamType>::iterator param_it =
505 param_it != generator.end(); ++param_it, ++i) {
506 Message test_name_stream;
507 test_name_stream << test_info->test_base_name <<
"/" << i;
509 test_case_name_stream.GetString().c_str(),
510 test_name_stream.GetString().c_str(),
514 TestCase::SetUpTestCase,
515 TestCase::TearDownTestCase,
516 test_info->test_meta_factory->CreateTestFactory(*param_it));
526 TestInfo(
const char* a_test_case_base_name,
527 const char* a_test_base_name,
528 TestMetaFactoryBase<ParamType>* a_test_meta_factory) :
529 test_case_base_name(a_test_case_base_name),
530 test_base_name(a_test_base_name),
531 test_meta_factory(a_test_meta_factory) {}
533 const string test_case_base_name;
534 const string test_base_name;
535 const scoped_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory;
537 typedef ::std::vector<linked_ptr<TestInfo> > TestInfoContainer;
540 typedef ::std::vector<std::pair<string, GeneratorCreationFunc*> >
541 InstantiationContainer;
543 const string test_case_name_;
544 TestInfoContainer tests_;
545 InstantiationContainer instantiations_;
556 class ParameterizedTestCaseRegistry {
558 ParameterizedTestCaseRegistry() {}
559 ~ParameterizedTestCaseRegistry() {
560 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
561 it != test_case_infos_.end(); ++it) {
568 template <
class TestCase>
569 ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
570 const char* test_case_name,
573 ParameterizedTestCaseInfo<TestCase>* typed_test_info = NULL;
574 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
575 it != test_case_infos_.end(); ++it) {
576 if ((*it)->GetTestCaseName() == test_case_name) {
577 if ((*it)->GetTestCaseTypeId() != GetTypeId<TestCase>()) {
581 ReportInvalidTestCaseType(test_case_name, file, line);
588 ParameterizedTestCaseInfo<TestCase> >(*it);
593 if (typed_test_info == NULL) {
594 typed_test_info =
new ParameterizedTestCaseInfo<TestCase>(test_case_name);
595 test_case_infos_.push_back(typed_test_info);
597 return typed_test_info;
599 void RegisterTests() {
600 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
601 it != test_case_infos_.end(); ++it) {
602 (*it)->RegisterTests();
607 typedef ::std::vector<ParameterizedTestCaseInfoBase*> TestCaseInfoContainer;
609 TestCaseInfoContainer test_case_infos_;
617 #endif // GTEST_HAS_PARAM_TEST 619 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
Definition: gmock-actions.h:49
::std::string PrintToString(const T &value)
Definition: gtest-printers.h:788
Definition: InputPrereqs.h:71
bool operator==(T *ptr, const linked_ptr< T > &x)
Definition: gtest-linked_ptr.h:213
#define GTEST_API_
Definition: gtest-port.h:662
GTEST_API_ TestInfo * MakeAndRegisterTestInfo(const char *test_case_name, const char *name, const char *type_param, const char *value_param, TypeId fixture_class_id, SetUpTestCaseFunc set_up_tc, TearDownTestCaseFunc tear_down_tc, TestFactoryBase *factory)
Definition: gtest.cc:2229
const void * TypeId
Definition: gtest-internal.h:474
#define GTEST_CHECK_(condition)
Definition: gtest-port.h:944
Definition: InputPrereqs.h:183
Definition: InputPrereqs.h:78
void Abort()
Definition: gtest-port.h:1664
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:620
bool operator!=(T *ptr, const linked_ptr< T > &x)
Definition: gtest-linked_ptr.h:218
Derived * CheckedDowncastToActualType(Base *base)
Definition: gtest-port.h:1029