35 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 36 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 45 #include "gtest/internal/gtest-internal.h" 46 #include "gtest/internal/gtest-linked_ptr.h" 47 #include "gtest/internal/gtest-port.h" 48 #include "gtest/gtest-printers.h" 54 template <
class ParamType>
66 template <
class ParamType>
68 return PrintToString(info.param);
80 GTEST_API_
void ReportInvalidTestCaseType(
const char* test_case_name,
81 CodeLocation code_location);
100 virtual void Advance() = 0;
108 virtual const T* Current()
const = 0;
118 template <
typename T>
121 typedef T value_type;
122 typedef const T& reference;
123 typedef ptrdiff_t difference_type;
129 impl_.reset(other.impl_->Clone());
133 const T& operator*()
const {
return *impl_->Current(); }
134 const T* operator->()
const {
return impl_->Current(); }
147 return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
150 return !(*
this == other);
161 template <
typename T>
191 iterator begin()
const {
return iterator(impl_->Begin()); }
192 iterator end()
const {
return iterator(impl_->End()); }
202 template <
typename T,
typename IncrementT>
206 : begin_(begin), end_(end),
207 step_(step), end_index_(CalculateEndIndex(begin, end, step)) {}
211 return new Iterator(
this, begin_, 0, step_);
214 return new Iterator(
this, end_, end_index_, step_);
222 : base_(base), value_(value), index_(index), step_(step) {}
223 virtual ~Iterator() {}
228 virtual void Advance() {
229 value_ =
static_cast<T
>(value_ + step_);
233 return new Iterator(*
this);
235 virtual const T* Current()
const {
return &value_; }
239 GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
240 <<
"The program attempted to compare iterators " 241 <<
"from different generators." << std::endl;
242 const int other_index =
243 CheckedDowncastToActualType<const Iterator>(&other)->index_;
244 return index_ == other_index;
248 Iterator(
const Iterator& other)
250 base_(other.base_), value_(other.value_), index_(other.index_),
251 step_(other.step_) {}
254 void operator=(
const Iterator& other);
259 const IncrementT step_;
262 static int CalculateEndIndex(
const T& begin,
264 const IncrementT& step) {
266 for (T i = begin; i < end; i = static_cast<T>(i + step))
276 const IncrementT step_;
279 const int end_index_;
287 template <
typename T>
290 template <
typename ForwardIterator>
292 : container_(begin, end) {}
296 return new Iterator(
this, container_.begin());
299 return new Iterator(
this, container_.end());
303 typedef typename ::std::vector<T> ContainerType;
308 typename ContainerType::const_iterator iterator)
309 : base_(base), iterator_(iterator) {}
310 virtual ~Iterator() {}
315 virtual void Advance() {
320 return new Iterator(*
this);
329 virtual const T* Current()
const {
330 if (value_.get() == NULL)
331 value_.reset(
new T(*iterator_));
337 GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
338 <<
"The program attempted to compare iterators " 339 <<
"from different generators." << std::endl;
341 CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
345 Iterator(
const Iterator& other)
350 iterator_(other.iterator_) {}
353 typename ContainerType::const_iterator iterator_;
365 const ContainerType container_;
372 template <
class ParamType>
375 name_stream << info.index;
376 return name_stream.GetString();
384 template <
class ParamType,
class ParamNameGenFunctor>
385 ParamNameGenFunctor GetParamNameGen(ParamNameGenFunctor func) {
389 template <
class ParamType>
394 template <
class ParamType>
396 return DefaultParamName;
403 template <
class TestClass>
406 typedef typename TestClass::ParamType ParamType;
408 parameter_(parameter) {}
409 virtual Test* CreateTest() {
410 TestClass::SetParam(¶meter_);
411 return new TestClass();
415 const ParamType parameter_;
424 template <
class ParamType>
440 template <
class TestCase>
444 typedef typename TestCase::ParamType ParamType;
471 virtual const std::string& GetTestCaseName()
const = 0;
473 virtual TypeId GetTestCaseTypeId()
const = 0;
478 virtual void RegisterTests() = 0;
494 template <
class TestCase>
500 typedef typename TestCase::ParamType ParamType;
507 : test_case_name_(name), code_location_(code_location) {}
510 virtual const std::string& GetTestCaseName()
const {
return test_case_name_; }
512 virtual TypeId GetTestCaseTypeId()
const {
return GetTypeId<TestCase>(); }
519 void AddTestPattern(
const char* test_case_name,
520 const char* test_base_name,
528 int AddTestCaseInstantiation(
const std::string& instantiation_name,
529 GeneratorCreationFunc* func,
530 ParamNameGeneratorFunc* name_func,
531 const char* file,
int line) {
532 instantiations_.push_back(
533 InstantiationInfo(instantiation_name, func, name_func, file, line));
541 virtual void RegisterTests() {
542 for (
typename TestInfoContainer::iterator test_it = tests_.begin();
543 test_it != tests_.end(); ++test_it) {
545 for (
typename InstantiationContainer::iterator gen_it =
546 instantiations_.begin(); gen_it != instantiations_.end();
548 const std::string& instantiation_name = gen_it->name;
549 ParamGenerator<ParamType> generator((*gen_it->generator)());
550 ParamNameGeneratorFunc* name_func = gen_it->name_func;
551 const char* file = gen_it->file;
552 int line = gen_it->line;
554 std::string test_case_name;
555 if ( !instantiation_name.empty() )
556 test_case_name = instantiation_name +
"/";
557 test_case_name += test_info->test_case_base_name;
560 std::set<std::string> test_param_names;
563 param_it != generator.end(); ++param_it, ++i) {
566 std::string param_name = name_func(
569 GTEST_CHECK_(IsValidParamName(param_name))
570 <<
"Parameterized test name '" << param_name
571 <<
"' is invalid, in " << file
572 <<
" line " << line << std::endl;
574 GTEST_CHECK_(test_param_names.count(param_name) == 0)
575 <<
"Duplicate parameterized test name '" << param_name
576 <<
"', in " << file <<
" line " << line << std::endl;
578 test_param_names.insert(param_name);
580 test_name_stream << test_info->test_base_name <<
"/" << param_name;
581 MakeAndRegisterTestInfo(
582 test_case_name.c_str(),
583 test_name_stream.GetString().c_str(),
585 PrintToString(*param_it).c_str(),
588 TestCase::SetUpTestCase,
589 TestCase::TearDownTestCase,
590 test_info->test_meta_factory->CreateTestFactory(*param_it));
600 TestInfo(
const char* a_test_case_base_name,
601 const char* a_test_base_name,
603 test_case_base_name(a_test_case_base_name),
604 test_base_name(a_test_base_name),
605 test_meta_factory(a_test_meta_factory) {}
607 const std::string test_case_base_name;
608 const std::string test_base_name;
611 typedef ::std::vector<linked_ptr<TestInfo> > TestInfoContainer;
615 struct InstantiationInfo {
616 InstantiationInfo(
const std::string &name_in,
617 GeneratorCreationFunc* generator_in,
618 ParamNameGeneratorFunc* name_func_in,
622 generator(generator_in),
623 name_func(name_func_in),
628 GeneratorCreationFunc* generator;
629 ParamNameGeneratorFunc* name_func;
633 typedef ::std::vector<InstantiationInfo> InstantiationContainer;
635 static bool IsValidParamName(
const std::string& name) {
641 for (std::string::size_type index = 0; index < name.size(); ++index) {
642 if (!isalnum(name[index]) && name[index] !=
'_')
649 const std::string test_case_name_;
651 TestInfoContainer tests_;
652 InstantiationContainer instantiations_;
667 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
668 it != test_case_infos_.end(); ++it) {
675 template <
class TestCase>
677 const char* test_case_name,
680 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
681 it != test_case_infos_.end(); ++it) {
682 if ((*it)->GetTestCaseName() == test_case_name) {
683 if ((*it)->GetTestCaseTypeId() != GetTypeId<TestCase>()) {
687 ReportInvalidTestCaseType(test_case_name, code_location);
693 typed_test_info = CheckedDowncastToActualType<
699 if (typed_test_info == NULL) {
701 test_case_name, code_location);
702 test_case_infos_.push_back(typed_test_info);
704 return typed_test_info;
706 void RegisterTests() {
707 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
708 it != test_case_infos_.end(); ++it) {
709 (*it)->RegisterTests();
714 typedef ::std::vector<ParameterizedTestCaseInfoBase*> TestCaseInfoContainer;
716 TestCaseInfoContainer test_case_infos_;
724 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
Definition: gtest-param-util.h:390
Definition: gtest-death-test.h:43
Definition: gtest-param-util.h:203
Definition: gtest-param-util.h:495
Definition: gtest-param-util.h:288
Definition: gtest-param-util.h:466
Definition: gtest-param-util.h:55
Definition: gtest-message.h:89
Definition: gtest-internal.h:452
Definition: gtest-internal.h:492
Definition: gtest-linked_ptr.h:146
Definition: gtest-param-util.h:404
Definition: gtest-param-util.h:89
Definition: gtest-param-util.h:119
Definition: gtest-param-util.h:83
Definition: gtest-param-util.h:84
Definition: gtest-port.h:1196
Definition: gtest-param-util.h:663
Definition: gtest-param-util.h:65