35 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
36 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
55 template <
class ParamType>
67 template <
class ParamType>
120 template <
typename T>
131 impl_.reset(other.impl_->Clone());
135 const T&
operator*()
const {
return *impl_->Current(); }
149 return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
152 return !(*
this == other);
158 std::unique_ptr<ParamIteratorInterface<T> > impl_;
163 template <
typename T>
197 std::shared_ptr<const ParamGeneratorInterface<T> > impl_;
204 template <
typename T,
typename IncrementT>
208 : begin_(begin), end_(
end),
209 step_(step), end_index_(CalculateEndIndex(begin,
end, step)) {}
213 return new Iterator(
this, begin_, 0, step_);
216 return new Iterator(
this, end_, end_index_, step_);
224 : base_(base), value_(value), index_(index), step_(step) {}
225 ~Iterator()
override {}
227 const ParamGeneratorInterface<T>* BaseGenerator()
const override {
230 void Advance()
override {
231 value_ =
static_cast<T
>(value_ + step_);
234 ParamIteratorInterface<T>* Clone()
const override {
235 return new Iterator(*
this);
237 const T* Current()
const override {
return &value_; }
238 bool Equals(
const ParamIteratorInterface<T>& other)
const override {
242 <<
"The program attempted to compare iterators "
243 <<
"from different generators." << std::endl;
244 const int other_index =
245 CheckedDowncastToActualType<const Iterator>(&other)->index_;
246 return index_ == other_index;
250 Iterator(
const Iterator& other)
251 : ParamIteratorInterface<T>(),
252 base_(other.base_), value_(other.value_), index_(other.index_),
253 step_(other.step_) {}
256 void operator=(
const Iterator& other);
258 const ParamGeneratorInterface<T>*
const base_;
261 const IncrementT step_;
264 static int CalculateEndIndex(
const T& begin,
266 const IncrementT& step) {
268 for (T i = begin; i < end; i = static_cast<T>(i + step))
278 const IncrementT step_;
281 const int end_index_;
289 template <
typename T>
292 template <
typename ForwardIterator>
294 : container_(begin,
end) {}
298 return new Iterator(
this, container_.begin());
301 return new Iterator(
this, container_.end());
305 typedef typename ::std::vector<T> ContainerType;
310 typename ContainerType::const_iterator iterator)
311 : base_(base), iterator_(iterator) {}
312 ~Iterator()
override {}
314 const ParamGeneratorInterface<T>* BaseGenerator()
const override {
317 void Advance()
override {
321 ParamIteratorInterface<T>* Clone()
const override {
322 return new Iterator(*
this);
331 const T* Current()
const override {
332 if (value_.get() ==
nullptr) value_.reset(
new T(*iterator_));
335 bool Equals(
const ParamIteratorInterface<T>& other)
const override {
339 <<
"The program attempted to compare iterators "
340 <<
"from different generators." << std::endl;
342 CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
346 Iterator(
const Iterator& other)
349 : ParamIteratorInterface<T>(),
351 iterator_(other.iterator_) {}
353 const ParamGeneratorInterface<T>*
const base_;
354 typename ContainerType::const_iterator iterator_;
360 mutable std::unique_ptr<const T> value_;
366 const ContainerType container_;
373 template <
class ParamType>
376 name_stream << info.
index;
380 template <
typename T =
int>
382 static_assert(
sizeof(T) == 0,
"Empty arguments are not allowed.");
384 template <
typename T =
int>
391 template <
class TestClass>
396 parameter_(parameter) {}
398 TestClass::SetParam(¶meter_);
399 return new TestClass();
412 template <
class ParamType>
428 template <
class TestSuite>
482 template <
class TestSuite>
495 : test_suite_name_(name), code_location_(code_location) {}
499 return test_suite_name_;
511 tests_.push_back(std::shared_ptr<TestInfo>(
512 new TestInfo(test_suite_name, test_base_name, meta_factory)));
517 GeneratorCreationFunc* func,
519 const char* file,
int line) {
520 instantiations_.push_back(
521 InstantiationInfo(instantiation_name, func, name_func, file, line));
530 for (
typename TestInfoContainer::iterator test_it = tests_.begin();
531 test_it != tests_.end(); ++test_it) {
532 std::shared_ptr<TestInfo> test_info = *test_it;
533 for (
typename InstantiationContainer::iterator gen_it =
534 instantiations_.begin(); gen_it != instantiations_.end();
536 const std::string& instantiation_name = gen_it->name;
539 const char* file = gen_it->file;
540 int line = gen_it->line;
543 if ( !instantiation_name.empty() )
544 test_suite_name = instantiation_name +
"/";
545 test_suite_name += test_info->test_suite_base_name;
548 std::set<std::string> test_param_names;
551 param_it != generator.
end(); ++param_it, ++i) {
558 <<
"Parameterized test name '" << param_name
559 <<
"' is invalid, in " << file
560 <<
" line " << line << std::endl;
563 <<
"Duplicate parameterized test name '" << param_name
564 <<
"', in " << file <<
" line " << line << std::endl;
566 test_param_names.insert(param_name);
568 test_name_stream << test_info->test_base_name <<
"/" << param_name;
570 test_suite_name.c_str(), test_name_stream.
GetString().c_str(),
576 test_info->test_meta_factory->CreateTestFactory(*param_it));
586 TestInfo(
const char* a_test_suite_base_name,
const char* a_test_base_name,
588 : test_suite_base_name(a_test_suite_base_name),
589 test_base_name(a_test_base_name),
590 test_meta_factory(a_test_meta_factory) {}
594 const std::unique_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory;
596 using TestInfoContainer = ::std::vector<std::shared_ptr<TestInfo> >;
600 struct InstantiationInfo {
602 GeneratorCreationFunc* generator_in,
607 generator(generator_in),
608 name_func(name_func_in),
613 GeneratorCreationFunc* generator;
618 typedef ::std::vector<InstantiationInfo> InstantiationContainer;
620 static bool IsValidParamName(
const std::string& name) {
626 for (std::string::size_type index = 0; index < name.size(); ++index) {
627 if (!isalnum(name[index]) && name[index] !=
'_')
635 CodeLocation code_location_;
636 TestInfoContainer tests_;
637 InstantiationContainer instantiations_;
643 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
644 template <
class TestCase>
658 for (
auto& test_suite_info : test_suite_infos_) {
659 delete test_suite_info;
665 template <
class TestSuite>
667 const char* test_suite_name,
CodeLocation code_location) {
669 for (
auto& test_suite_info : test_suite_infos_) {
670 if (test_suite_info->GetTestSuiteName() == test_suite_name) {
671 if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) {
687 if (typed_test_info ==
nullptr) {
689 test_suite_name, code_location);
690 test_suite_infos_.push_back(typed_test_info);
692 return typed_test_info;
695 for (
auto& test_suite_info : test_suite_infos_) {
696 test_suite_info->RegisterTests();
700 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
701 template <
class TestCase>
703 const char* test_case_name,
CodeLocation code_location) {
704 return GetTestSuitePatternHolder<TestCase>(test_case_name, code_location);
710 using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
712 TestSuiteInfoContainer test_suite_infos_;
721 template <
class Container>
722 internal::ParamGenerator<typename Container::value_type>
ValuesIn(
723 const Container& container);
728 template <
typename... Ts>
733 template <
typename T>
739 template <
typename T,
size_t... I>
741 return std::vector<T>{
static_cast<T
>(v_.template Get<I>())...};
747 template <
typename... T>
758 return new Iterator(
this, generators_,
false);
761 return new Iterator(
this, generators_,
true);
767 template <
size_t... I>
774 begin_(std::
get<I>(generators).begin()...),
775 end_(std::
get<I>(generators).
end()...),
776 current_(is_end ? end_ : begin_) {
777 ComputeCurrentValue();
779 ~IteratorImpl()
override {}
781 const ParamGeneratorInterface<ParamType>* BaseGenerator()
const override {
786 void Advance()
override {
789 ++
std::get<
sizeof...(T) - 1>(current_);
791 AdvanceIfEnd<
sizeof...(T) - 1>();
792 ComputeCurrentValue();
794 ParamIteratorInterface<ParamType>* Clone()
const override {
795 return new IteratorImpl(*
this);
798 const ParamType* Current()
const override {
return current_value_.get(); }
800 bool Equals(
const ParamIteratorInterface<ParamType>& other)
const override {
804 <<
"The program attempted to compare iterators "
805 <<
"from different generators." << std::endl;
806 const IteratorImpl* typed_other =
807 CheckedDowncastToActualType<const IteratorImpl>(&other);
812 if (AtEnd() && typed_other->AtEnd())
return true;
816 (same = same && std::get<I>(current_) ==
817 std::get<I>(typed_other->current_))...};
823 template <
size_t ThisI>
824 void AdvanceIfEnd() {
825 if (std::get<ThisI>(current_) != std::get<ThisI>(end_))
return;
827 bool last = ThisI == 0;
833 constexpr
size_t NextI = ThisI - (ThisI != 0);
834 std::get<ThisI>(current_) = std::get<ThisI>(begin_);
835 ++std::get<NextI>(current_);
836 AdvanceIfEnd<NextI>();
839 void ComputeCurrentValue() {
841 current_value_ = std::make_shared<ParamType>(*std::get<I>(current_)...);
846 (at_end = at_end || std::get<I>(current_) == std::get<I>(end_))...};
851 const ParamGeneratorInterface<ParamType>*
const base_;
852 std::tuple<typename ParamGenerator<T>::iterator...> begin_;
853 std::tuple<typename ParamGenerator<T>::iterator...> end_;
854 std::tuple<typename ParamGenerator<T>::iterator...> current_;
855 std::shared_ptr<ParamType> current_value_;
858 using Iterator = IteratorImpl<
typename MakeIndexSequence<
sizeof...(T)>::type>;
860 std::tuple<ParamGenerator<T>...> generators_;
863 template <
class... Gen>
867 template <
typename... T>
874 std::tuple<Gen...> generators_;
std::string GetString() const
~CartesianProductGenerator() override
ParamIteratorInterface< ParamType > * Begin() const override
ParamIteratorInterface< ParamType > * End() const override
::std::tuple< T... > ParamType
CartesianProductGenerator(const std::tuple< ParamGenerator< T >... > &g)
CartesianProductHolder(const Gen &... g)
ParamIterator< T > iterator
ParamGenerator(const ParamGenerator &other)
ParamGenerator & operator=(const ParamGenerator &other)
ParamGenerator(ParamGeneratorInterface< T > *impl)
virtual ~ParamGeneratorInterface()
virtual ParamIteratorInterface< T > * Begin() const =0
virtual ParamIteratorInterface< T > * End() const =0
const T & operator*() const
ptrdiff_t difference_type
bool operator!=(const ParamIterator &other) const
ParamIterator & operator=(const ParamIterator &other)
ParamIterator(const ParamIterator &other)
const T * operator->() const
ParamIterator & operator++()
bool operator==(const ParamIterator &other) const
ParamIterator operator++(int)
virtual const ParamGeneratorInterface< T > * BaseGenerator() const =0
virtual ParamIteratorInterface * Clone() const =0
virtual bool Equals(const ParamIteratorInterface &other) const =0
virtual ~ParamIteratorInterface()
virtual const T * Current() const =0
Test * CreateTest() override
ParameterizedTestFactory(ParamType parameter)
TestClass::ParamType ParamType
ParameterizedTestSuiteInfoBase()
virtual void RegisterTests()=0
virtual const std::string & GetTestSuiteName() const =0
virtual ~ParameterizedTestSuiteInfoBase()
virtual TypeId GetTestSuiteTypeId() const =0
void AddTestPattern(const char *test_suite_name, const char *test_base_name, TestMetaFactoryBase< ParamType > *meta_factory)
typename TestSuite::ParamType ParamType
int AddTestSuiteInstantiation(const std::string &instantiation_name, GeneratorCreationFunc *func, ParamNameGeneratorFunc *name_func, const char *file, int line)
std::string(const TestParamInfo< ParamType > &) ParamNameGeneratorFunc
const std::string & GetTestSuiteName() const override
ParameterizedTestSuiteInfo(const char *name, CodeLocation code_location)
void RegisterTests() override
TypeId GetTestSuiteTypeId() const override
ParameterizedTestCaseInfo< TestCase > * GetTestCasePatternHolder(const char *test_case_name, CodeLocation code_location)
ParameterizedTestSuiteInfo< TestSuite > * GetTestSuitePatternHolder(const char *test_suite_name, CodeLocation code_location)
~ParameterizedTestSuiteRegistry()
ParameterizedTestSuiteRegistry()
ParamIteratorInterface< T > * Begin() const override
RangeGenerator(T begin, T end, IncrementT step)
~RangeGenerator() override
ParamIteratorInterface< T > * End() const override
ParamIteratorInterface< T > * End() const override
ParamIteratorInterface< T > * Begin() const override
ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
~ValuesInIteratorRangeGenerator() override
#define GTEST_CHECK_(condition)
__host__ __device__ __forceinline__ T & get(array< T, m > &src)
GTEST_API_ void ReportInvalidTestSuiteType(const char *test_suite_name, CodeLocation code_location)
GTEST_API_ TestInfo * MakeAndRegisterTestInfo(const char *test_suite_name, const char *name, const char *type_param, const char *value_param, CodeLocation code_location, TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc, TearDownTestSuiteFunc tear_down_tc, TestFactoryBase *factory)
std::string DefaultParamName(const TestParamInfo< ParamType > &info)
Derived * CheckedDowncastToActualType(Base *base)
internal::ParamGenerator< typename ::testing::internal::IteratorTraits< ForwardIterator >::value_type > ValuesIn(ForwardIterator begin, ForwardIterator end)
::std::string PrintToString(const T &value)
std::string operator()(const TestParamInfo< ParamType > &info) const
TestParamInfo(const ParamType &a_param, size_t an_index)