1
// Copyright 2008 Google Inc.
2
// All Rights Reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
// Author: vladl@google.com (Vlad Losev)
32
// Type and function utilities for implementing parameterized tests.
34
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
35
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
41
// scripts/fuse_gtest.py depends on gtest's own header being #included
42
// *unconditionally*. Therefore these #includes cannot be moved
43
// inside #if GTEST_HAS_PARAM_TEST.
44
#include "gtest/internal/gtest-internal.h"
45
#include "gtest/internal/gtest-linked_ptr.h"
46
#include "gtest/internal/gtest-port.h"
47
#include "gtest/gtest-printers.h"
49
#if GTEST_HAS_PARAM_TEST
54
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
56
// Outputs a message explaining invalid registration of different
57
// fixture class for the same test case. This may happen when
58
// TEST_P macro is used to define two tests with the same name
59
// but in different namespaces.
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;
66
// Interface for iterating over elements provided by an implementation
67
// of ParamGeneratorInterface<T>.
69
class ParamIteratorInterface {
71
virtual ~ParamIteratorInterface() {}
72
// A pointer to the base generator instance.
73
// Used only for the purposes of iterator comparison
74
// to make sure that two iterators belong to the same generator.
75
virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
76
// Advances iterator to point to the next element
77
// provided by the generator. The caller is responsible
78
// for not calling Advance() on an iterator equal to
79
// BaseGenerator()->End().
80
virtual void Advance() = 0;
81
// Clones the iterator object. Used for implementing copy semantics
82
// of ParamIterator<T>.
83
virtual ParamIteratorInterface* Clone() const = 0;
84
// Dereferences the current iterator and provides (read-only) access
85
// to the pointed value. It is the caller's responsibility not to call
86
// Current() on an iterator equal to BaseGenerator()->End().
87
// Used for implementing ParamGenerator<T>::operator*().
88
virtual const T* Current() const = 0;
89
// Determines whether the given iterator and other point to the same
90
// element in the sequence generated by the generator.
91
// Used for implementing ParamGenerator<T>::operator==().
92
virtual bool Equals(const ParamIteratorInterface& other) const = 0;
95
// Class iterating over elements provided by an implementation of
96
// ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
97
// and implements the const forward iterator concept.
101
typedef T value_type;
102
typedef const T& reference;
103
typedef ptrdiff_t difference_type;
105
// ParamIterator assumes ownership of the impl_ pointer.
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(); }
115
// Prefix version of operator++.
116
ParamIterator& operator++() {
120
// Postfix version of operator++.
121
ParamIterator operator++(int /*unused*/) {
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_;
139
// ParamGeneratorInterface<T> is the binary interface to access generators
140
// defined in other translation units.
141
template <typename T>
142
class ParamGeneratorInterface {
146
virtual ~ParamGeneratorInterface() {}
148
// Generator interface definition
149
virtual ParamIteratorInterface<T>* Begin() const = 0;
150
virtual ParamIteratorInterface<T>* End() const = 0;
153
// Wraps ParamGeneratorInterface<T> and provides general generator syntax
154
// compatible with the STL Container concept.
155
// This class implements copy initialization semantics and the contained
156
// ParamGeneratorInterface<T> instance is shared among all copies
157
// of the original object. This is possible because that instance is immutable.
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_;
178
// Generates values from a range of two comparable values. Can be used to
179
// generate sequences of user-defined types that implement operator+() and
181
// This class is used in the Range() function.
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 {
217
// Having the same base generator guarantees that the other
218
// iterator is of the same type and we can downcast.
219
GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
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_) {}
233
// No implementation - assignment is unsupported.
234
void operator=(const Iterator& other);
236
const ParamGeneratorInterface<T>* const base_;
239
const IncrementT step_;
240
}; // class RangeGenerator::Iterator
242
static int CalculateEndIndex(const T& begin,
244
const IncrementT& step) {
246
for (T i = begin; i < end; i = i + step)
251
// No implementation - assignment is unsupported.
252
void operator=(const RangeGenerator& other);
256
const IncrementT step_;
257
// The index for the end() iterator. All the elements in the generated
258
// sequence are indexed (0-based) to aid iterator comparison.
259
const int end_index_;
260
}; // class RangeGenerator
263
// Generates values from a pair of STL-style iterators. Used in the
264
// ValuesIn() function. The elements are copied from the source range
265
// since the source can be located on the stack, and the generator
266
// is likely to persist beyond that stack frame.
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);
302
// We need to use cached value referenced by iterator_ because *iterator_
303
// can return a temporary object (and of type other then T), so just
304
// having "return &*iterator_;" doesn't work.
305
// value_ is updated here and not in Advance() because Advance()
306
// can advance iterator_ beyond the end of the range, and we cannot
307
// detect that fact. The client code, on the other hand, is
308
// responsible for not calling Current() on an out-of-range iterator.
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 {
315
// Having the same base generator guarantees that the other
316
// iterator is of the same type and we can downcast.
317
GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
318
<< "The program attempted to compare iterators "
319
<< "from different generators." << std::endl;
321
CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
325
Iterator(const Iterator& other)
326
// The explicit constructor call suppresses a false warning
327
// emitted by gcc when supplied with the -Wextra option.
328
: ParamIteratorInterface<T>(),
330
iterator_(other.iterator_) {}
332
const ParamGeneratorInterface<T>* const base_;
333
typename ContainerType::const_iterator iterator_;
334
// A cached value of *iterator_. We keep it here to allow access by
335
// pointer in the wrapping iterator's operator->().
336
// value_ needs to be mutable to be accessed in Current().
337
// Use of scoped_ptr helps manage cached value's lifetime,
338
// which is bound by the lifespan of the iterator itself.
339
mutable scoped_ptr<const T> value_;
340
}; // class ValuesInIteratorRangeGenerator::Iterator
342
// No implementation - assignment is unsupported.
343
void operator=(const ValuesInIteratorRangeGenerator& other);
345
const ContainerType container_;
346
}; // class ValuesInIteratorRangeGenerator
348
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
350
// Stores a parameter value and later creates tests parameterized with that
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_;
366
GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory);
369
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
371
// TestMetaFactoryBase is a base class for meta-factories that create
372
// test factories for passing into MakeAndRegisterTestInfo function.
373
template <class ParamType>
374
class TestMetaFactoryBase {
376
virtual ~TestMetaFactoryBase() {}
378
virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
381
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
383
// TestMetaFactory creates test factories for passing into
384
// MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
385
// ownership of test factory pointer, same factory object cannot be passed
386
// into that method twice. But ParameterizedTestCaseInfo is going to call
387
// it for each Test/Parameter value combination. Thus it needs meta factory
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);
402
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory);
405
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
407
// ParameterizedTestCaseInfoBase is a generic interface
408
// to ParameterizedTestCaseInfo classes. ParameterizedTestCaseInfoBase
409
// accumulates test information provided by TEST_P macro invocations
410
// and generators provided by INSTANTIATE_TEST_CASE_P macro invocations
411
// and uses that information to register all resulting test instances
412
// in RegisterTests method. The ParameterizeTestCaseRegistry class holds
413
// a collection of pointers to the ParameterizedTestCaseInfo objects
414
// and calls RegisterTests() on each of them when asked.
415
class ParameterizedTestCaseInfoBase {
417
virtual ~ParameterizedTestCaseInfoBase() {}
419
// Base part of test case name for display purposes.
420
virtual const string& GetTestCaseName() const = 0;
421
// Test case id to verify identity.
422
virtual TypeId GetTestCaseTypeId() const = 0;
423
// UnitTest class invokes this method to register tests in this
424
// test case right before running them in RUN_ALL_TESTS macro.
425
// This method should not be called more then once on any single
426
// instance of a ParameterizedTestCaseInfoBase derived class.
427
virtual void RegisterTests() = 0;
430
ParameterizedTestCaseInfoBase() {}
433
GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfoBase);
436
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
438
// ParameterizedTestCaseInfo accumulates tests obtained from TEST_P
439
// macro invocations for a particular test case and generators
440
// obtained from INSTANTIATE_TEST_CASE_P macro invocations for that
441
// test case. It registers tests with all values generated by all
442
// generators when asked.
443
template <class TestCase>
444
class ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase {
446
// ParamType and GeneratorCreationFunc are private types but are required
447
// for declarations of public methods AddTestPattern() and
448
// AddTestCaseInstantiation().
449
typedef typename TestCase::ParamType ParamType;
450
// A function that returns an instance of appropriate generator type.
451
typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
453
explicit ParameterizedTestCaseInfo(const char* name)
454
: test_case_name_(name) {}
456
// Test case base name for display purposes.
457
virtual const string& GetTestCaseName() const { return test_case_name_; }
458
// Test case id to verify identity.
459
virtual TypeId GetTestCaseTypeId() const { return GetTypeId<TestCase>(); }
460
// TEST_P macro uses AddTestPattern() to record information
461
// about a single test in a LocalTestInfo structure.
462
// test_case_name is the base name of the test case (without invocation
463
// prefix). test_base_name is the name of an individual test without
464
// parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
465
// test case base name and DoBar is test base name.
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,
473
// INSTANTIATE_TEST_CASE_P macro uses AddGenerator() to record information
474
// about a generator.
475
int AddTestCaseInstantiation(const string& instantiation_name,
476
GeneratorCreationFunc* func,
477
const char* /* file */,
479
instantiations_.push_back(::std::make_pair(instantiation_name, func));
480
return 0; // Return value used only to run this method in namespace scope.
482
// UnitTest class invokes this method to register tests in this test case
483
// test cases right before running tests in RUN_ALL_TESTS macro.
484
// This method should not be called more then once on any single
485
// instance of a ParameterizedTestCaseInfoBase derived class.
486
// UnitTest has a guard to prevent from calling this method more then once.
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
string test_case_name;
498
if ( !instantiation_name.empty() )
499
test_case_name = instantiation_name + "/";
500
test_case_name += 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;
508
MakeAndRegisterTestInfo(
509
test_case_name.c_str(),
510
test_name_stream.GetString().c_str(),
511
NULL, // No type parameter.
512
PrintToString(*param_it).c_str(),
514
TestCase::SetUpTestCase,
515
TestCase::TearDownTestCase,
516
test_info->test_meta_factory->CreateTestFactory(*param_it));
523
// LocalTestInfo structure keeps information about a single test registered
524
// with TEST_P macro.
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;
538
// Keeps pairs of <Instantiation name, Sequence generator creation function>
539
// received from INSTANTIATE_TEST_CASE_P macros.
540
typedef ::std::vector<std::pair<string, GeneratorCreationFunc*> >
541
InstantiationContainer;
543
const string test_case_name_;
544
TestInfoContainer tests_;
545
InstantiationContainer instantiations_;
547
GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfo);
548
}; // class ParameterizedTestCaseInfo
550
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
552
// ParameterizedTestCaseRegistry contains a map of ParameterizedTestCaseInfoBase
553
// classes accessed by test case names. TEST_P and INSTANTIATE_TEST_CASE_P
554
// macros use it to locate their corresponding ParameterizedTestCaseInfo
556
class ParameterizedTestCaseRegistry {
558
ParameterizedTestCaseRegistry() {}
559
~ParameterizedTestCaseRegistry() {
560
for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
561
it != test_case_infos_.end(); ++it) {
566
// Looks up or creates and returns a structure containing information about
567
// tests and instantiations of a particular test case.
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>()) {
578
// Complain about incorrect usage of Google Test facilities
579
// and terminate the program since we cannot guaranty correct
580
// test case setup and tear-down in this case.
581
ReportInvalidTestCaseType(test_case_name, file, line);
584
// At this point we are sure that the object we found is of the same
585
// type we are looking for, so we downcast it to that type
586
// without further checks.
587
typed_test_info = CheckedDowncastToActualType<
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_;
611
GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry);
614
} // namespace internal
615
} // namespace testing
617
#endif // GTEST_HAS_PARAM_TEST
619
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_