| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ |
| 37 |
#define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ |
| 38 |
|
| 39 |
#include <ctype.h> |
| 40 |
|
| 41 |
#include <cassert> |
| 42 |
#include <iterator> |
| 43 |
#include <memory> |
| 44 |
#include <set> |
| 45 |
#include <tuple> |
| 46 |
#include <type_traits> |
| 47 |
#include <utility> |
| 48 |
#include <vector> |
| 49 |
|
| 50 |
#include "gtest/gtest-printers.h" |
| 51 |
#include "gtest/gtest-test-part.h" |
| 52 |
#include "gtest/internal/gtest-internal.h" |
| 53 |
#include "gtest/internal/gtest-port.h" |
| 54 |
|
| 55 |
namespace testing { |
| 56 |
|
| 57 |
|
| 58 |
template <class ParamType> |
| 59 |
struct TestParamInfo { |
| 60 |
TestParamInfo(const ParamType& a_param, size_t an_index) |
| 61 |
: param(a_param), index(an_index) {} |
| 62 |
ParamType param; |
| 63 |
size_t index; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
struct PrintToStringParamName { |
| 69 |
template <class ParamType> |
| 70 |
std::string operator()(const TestParamInfo<ParamType>& info) const { |
| 71 |
return PrintToString(info.param); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
namespace internal { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name, |
| 85 |
CodeLocation code_location); |
| 86 |
|
| 87 |
template <typename> |
| 88 |
class ParamGeneratorInterface; |
| 89 |
template <typename> |
| 90 |
class ParamGenerator; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
template <typename T> |
| 95 |
class ParamIteratorInterface { |
| 96 |
public: |
| 97 |
virtual ~ParamIteratorInterface() {} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
virtual void Advance() = 0; |
| 107 |
|
| 108 |
|
| 109 |
virtual ParamIteratorInterface* Clone() const = 0; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
virtual const T* Current() const = 0; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
virtual bool Equals(const ParamIteratorInterface& other) const = 0; |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
template <typename T> |
| 125 |
class ParamIterator { |
| 126 |
public: |
| 127 |
typedef T value_type; |
| 128 |
typedef const T& reference; |
| 129 |
typedef ptrdiff_t difference_type; |
| 130 |
|
| 131 |
|
| 132 |
ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {} |
| 133 |
ParamIterator& operator=(const ParamIterator& other) { |
| 134 |
if (this != &other) impl_.reset(other.impl_->Clone()); |
| 135 |
return *this; |
| 136 |
} |
| 137 |
|
| 138 |
const T& operator*() const { return *impl_->Current(); } |
| 139 |
const T* operator->() const { return impl_->Current(); } |
| 140 |
|
| 141 |
ParamIterator& operator++() { |
| 142 |
impl_->Advance(); |
| 143 |
return *this; |
| 144 |
} |
| 145 |
|
| 146 |
ParamIterator operator++(int ) { |
| 147 |
ParamIteratorInterface<T>* clone = impl_->Clone(); |
| 148 |
impl_->Advance(); |
| 149 |
return ParamIterator(clone); |
| 150 |
} |
| 151 |
bool operator==(const ParamIterator& other) const { |
| 152 |
return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_); |
| 153 |
} |
| 154 |
bool operator!=(const ParamIterator& other) const { |
| 155 |
return !(*this == other); |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
friend class ParamGenerator<T>; |
| 160 |
explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {} |
| 161 |
std::unique_ptr<ParamIteratorInterface<T>> impl_; |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
template <typename T> |
| 167 |
class ParamGeneratorInterface { |
| 168 |
public: |
| 169 |
typedef T ParamType; |
| 170 |
|
| 171 |
virtual ~ParamGeneratorInterface() {} |
| 172 |
|
| 173 |
|
| 174 |
virtual ParamIteratorInterface<T>* Begin() const = 0; |
| 175 |
virtual ParamIteratorInterface<T>* End() const = 0; |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
template <typename T> |
| 184 |
class ParamGenerator { |
| 185 |
public: |
| 186 |
typedef ParamIterator<T> iterator; |
| 187 |
|
| 188 |
explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {} |
| 189 |
ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {} |
| 190 |
|
| 191 |
ParamGenerator& operator=(const ParamGenerator& other) { |
| 192 |
impl_ = other.impl_; |
| 193 |
return *this; |
| 194 |
} |
| 195 |
|
| 196 |
iterator begin() const { return iterator(impl_->Begin()); } |
| 197 |
iterator end() const { return iterator(impl_->End()); } |
| 198 |
|
| 199 |
private: |
| 200 |
std::shared_ptr<const ParamGeneratorInterface<T>> impl_; |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
template <typename T, typename IncrementT> |
| 208 |
class RangeGenerator : public ParamGeneratorInterface<T> { |
| 209 |
public: |
| 210 |
RangeGenerator(T begin, T end, IncrementT step) |
| 211 |
: begin_(begin), |
| 212 |
end_(end), |
| 213 |
step_(step), |
| 214 |
end_index_(CalculateEndIndex(begin, end, step)) {} |
| 215 |
~RangeGenerator() override {} |
| 216 |
|
| 217 |
ParamIteratorInterface<T>* Begin() const override { |
| 218 |
return new Iterator(this, begin_, 0, step_); |
| 219 |
} |
| 220 |
ParamIteratorInterface<T>* End() const override { |
| 221 |
return new Iterator(this, end_, end_index_, step_); |
| 222 |
} |
| 223 |
|
| 224 |
private: |
| 225 |
class Iterator : public ParamIteratorInterface<T> { |
| 226 |
public: |
| 227 |
Iterator(const ParamGeneratorInterface<T>* base, T value, int index, |
| 228 |
IncrementT step) |
| 229 |
: base_(base), value_(value), index_(index), step_(step) {} |
| 230 |
~Iterator() override {} |
| 231 |
|
| 232 |
const ParamGeneratorInterface<T>* BaseGenerator() const override { |
| 233 |
return base_; |
| 234 |
} |
| 235 |
void Advance() override { |
| 236 |
value_ = static_cast<T>(value_ + step_); |
| 237 |
index_++; |
| 238 |
} |
| 239 |
ParamIteratorInterface<T>* Clone() const override { |
| 240 |
return new Iterator(*this); |
| 241 |
} |
| 242 |
const T* Current() const override { return &value_; } |
| 243 |
bool Equals(const ParamIteratorInterface<T>& other) const override { |
| 244 |
|
| 245 |
|
| 246 |
GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) |
| 247 |
<< "The program attempted to compare iterators " |
| 248 |
<< "from different generators." << std::endl; |
| 249 |
const int other_index = |
| 250 |
CheckedDowncastToActualType<const Iterator>(&other)->index_; |
| 251 |
return index_ == other_index; |
| 252 |
} |
| 253 |
|
| 254 |
private: |
| 255 |
Iterator(const Iterator& other) |
| 256 |
: ParamIteratorInterface<T>(), |
| 257 |
base_(other.base_), |
| 258 |
value_(other.value_), |
| 259 |
index_(other.index_), |
| 260 |
step_(other.step_) {} |
| 261 |
|
| 262 |
|
| 263 |
void operator=(const Iterator& other); |
| 264 |
|
| 265 |
const ParamGeneratorInterface<T>* const base_; |
| 266 |
T value_; |
| 267 |
int index_; |
| 268 |
const IncrementT step_; |
| 269 |
}; |
| 270 |
|
| 271 |
static int CalculateEndIndex(const T& begin, const T& end, |
| 272 |
const IncrementT& step) { |
| 273 |
int end_index = 0; |
| 274 |
for (T i = begin; i < end; i = static_cast<T>(i + step)) end_index++; |
| 275 |
return end_index; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
void operator=(const RangeGenerator& other); |
| 280 |
|
| 281 |
const T begin_; |
| 282 |
const T end_; |
| 283 |
const IncrementT step_; |
| 284 |
|
| 285 |
|
| 286 |
const int end_index_; |
| 287 |
}; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
template <typename T> |
| 294 |
class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> { |
| 295 |
public: |
| 296 |
template <typename ForwardIterator> |
| 297 |
ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end) |
| 298 |
: container_(begin, end) {} |
| 299 |
~ValuesInIteratorRangeGenerator() override {} |
| 300 |
|
| 301 |
ParamIteratorInterface<T>* Begin() const override { |
| 302 |
return new Iterator(this, container_.begin()); |
| 303 |
} |
| 304 |
ParamIteratorInterface<T>* End() const override { |
| 305 |
return new Iterator(this, container_.end()); |
| 306 |
} |
| 307 |
|
| 308 |
private: |
| 309 |
typedef typename ::std::vector<T> ContainerType; |
| 310 |
|
| 311 |
class Iterator : public ParamIteratorInterface<T> { |
| 312 |
public: |
| 313 |
Iterator(const ParamGeneratorInterface<T>* base, |
| 314 |
typename ContainerType::const_iterator iterator) |
| 315 |
: base_(base), iterator_(iterator) {} |
| 316 |
~Iterator() override {} |
| 317 |
|
| 318 |
const ParamGeneratorInterface<T>* BaseGenerator() const override { |
| 319 |
return base_; |
| 320 |
} |
| 321 |
void Advance() override { |
| 322 |
++iterator_; |
| 323 |
value_.reset(); |
| 324 |
} |
| 325 |
ParamIteratorInterface<T>* Clone() const override { |
| 326 |
return new Iterator(*this); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
const T* Current() const override { |
| 336 |
if (value_.get() == nullptr) value_.reset(new T(*iterator_)); |
| 337 |
return value_.get(); |
| 338 |
} |
| 339 |
bool Equals(const ParamIteratorInterface<T>& other) const override { |
| 340 |
|
| 341 |
|
| 342 |
GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) |
| 343 |
<< "The program attempted to compare iterators " |
| 344 |
<< "from different generators." << std::endl; |
| 345 |
return iterator_ == |
| 346 |
CheckedDowncastToActualType<const Iterator>(&other)->iterator_; |
| 347 |
} |
| 348 |
|
| 349 |
private: |
| 350 |
Iterator(const Iterator& other) |
| 351 |
|
| 352 |
|
| 353 |
: ParamIteratorInterface<T>(), |
| 354 |
base_(other.base_), |
| 355 |
iterator_(other.iterator_) {} |
| 356 |
|
| 357 |
const ParamGeneratorInterface<T>* const base_; |
| 358 |
typename ContainerType::const_iterator iterator_; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
mutable std::unique_ptr<const T> value_; |
| 365 |
}; |
| 366 |
|
| 367 |
|
| 368 |
void operator=(const ValuesInIteratorRangeGenerator& other); |
| 369 |
|
| 370 |
const ContainerType container_; |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
template <class ParamType> |
| 378 |
std::string DefaultParamName(const TestParamInfo<ParamType>& info) { |
| 379 |
Message name_stream; |
| 380 |
name_stream << info.index; |
| 381 |
return name_stream.GetString(); |
| 382 |
} |
| 383 |
|
| 384 |
template <typename T = int> |
| 385 |
void TestNotEmpty() { |
| 386 |
static_assert(sizeof(T) == 0, "Empty arguments are not allowed."); |
| 387 |
} |
| 388 |
template <typename T = int> |
| 389 |
void TestNotEmpty(const T&) {} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
template <class TestClass> |
| 396 |
class ParameterizedTestFactory : public TestFactoryBase { |
| 397 |
public: |
| 398 |
typedef typename TestClass::ParamType ParamType; |
| 399 |
explicit ParameterizedTestFactory(ParamType parameter) |
| 400 |
: parameter_(parameter) {} |
| 401 |
Test* CreateTest() override { |
| 402 |
TestClass::SetParam(¶meter_); |
| 403 |
return new TestClass(); |
| 404 |
} |
| 405 |
|
| 406 |
private: |
| 407 |
const ParamType parameter_; |
| 408 |
|
| 409 |
ParameterizedTestFactory(const ParameterizedTestFactory&) = delete; |
| 410 |
ParameterizedTestFactory& operator=(const ParameterizedTestFactory&) = delete; |
| 411 |
}; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
template <class ParamType> |
| 418 |
class TestMetaFactoryBase { |
| 419 |
public: |
| 420 |
virtual ~TestMetaFactoryBase() {} |
| 421 |
|
| 422 |
virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0; |
| 423 |
}; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
template <class TestSuite> |
| 434 |
class TestMetaFactory |
| 435 |
: public TestMetaFactoryBase<typename TestSuite::ParamType> { |
| 436 |
public: |
| 437 |
using ParamType = typename TestSuite::ParamType; |
| 438 |
|
| 439 |
TestMetaFactory() {} |
| 440 |
|
| 441 |
TestFactoryBase* CreateTestFactory(ParamType parameter) override { |
| 442 |
return new ParameterizedTestFactory<TestSuite>(parameter); |
| 443 |
} |
| 444 |
|
| 445 |
private: |
| 446 |
TestMetaFactory(const TestMetaFactory&) = delete; |
| 447 |
TestMetaFactory& operator=(const TestMetaFactory&) = delete; |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
class ParameterizedTestSuiteInfoBase { |
| 461 |
public: |
| 462 |
virtual ~ParameterizedTestSuiteInfoBase() {} |
| 463 |
|
| 464 |
|
| 465 |
virtual const std::string& GetTestSuiteName() const = 0; |
| 466 |
|
| 467 |
virtual TypeId GetTestSuiteTypeId() const = 0; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
virtual void RegisterTests() = 0; |
| 473 |
|
| 474 |
protected: |
| 475 |
ParameterizedTestSuiteInfoBase() {} |
| 476 |
|
| 477 |
private: |
| 478 |
ParameterizedTestSuiteInfoBase(const ParameterizedTestSuiteInfoBase&) = |
| 479 |
delete; |
| 480 |
ParameterizedTestSuiteInfoBase& operator=( |
| 481 |
const ParameterizedTestSuiteInfoBase&) = delete; |
| 482 |
}; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
struct GTEST_API_ MarkAsIgnored { |
| 489 |
explicit MarkAsIgnored(const char* test_suite); |
| 490 |
}; |
| 491 |
|
| 492 |
GTEST_API_ void InsertSyntheticTestCase(const std::string& name, |
| 493 |
CodeLocation location, bool has_test_p); |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
template <class TestSuite> |
| 503 |
class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase { |
| 504 |
public: |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
using ParamType = typename TestSuite::ParamType; |
| 509 |
|
| 510 |
typedef ParamGenerator<ParamType>(GeneratorCreationFunc)(); |
| 511 |
using ParamNameGeneratorFunc = std::string(const TestParamInfo<ParamType>&); |
| 512 |
|
| 513 |
explicit ParameterizedTestSuiteInfo(const char* name, |
| 514 |
CodeLocation code_location) |
| 515 |
: test_suite_name_(name), code_location_(code_location) {} |
| 516 |
|
| 517 |
|
| 518 |
const std::string& GetTestSuiteName() const override { |
| 519 |
return test_suite_name_; |
| 520 |
} |
| 521 |
|
| 522 |
TypeId GetTestSuiteTypeId() const override { return GetTypeId<TestSuite>(); } |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
void AddTestPattern(const char* test_suite_name, const char* test_base_name, |
| 530 |
TestMetaFactoryBase<ParamType>* meta_factory, |
| 531 |
CodeLocation code_location) { |
| 532 |
tests_.push_back(std::shared_ptr<TestInfo>(new TestInfo( |
| 533 |
test_suite_name, test_base_name, meta_factory, code_location))); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
int AddTestSuiteInstantiation(const std::string& instantiation_name, |
| 538 |
GeneratorCreationFunc* func, |
| 539 |
ParamNameGeneratorFunc* name_func, |
| 540 |
const char* file, int line) { |
| 541 |
instantiations_.push_back( |
| 542 |
InstantiationInfo(instantiation_name, func, name_func, file, line)); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
void RegisterTests() override { |
| 551 |
bool generated_instantiations = false; |
| 552 |
|
| 553 |
for (typename TestInfoContainer::iterator test_it = tests_.begin(); |
| 554 |
test_it != tests_.end(); ++test_it) { |
| 555 |
std::shared_ptr<TestInfo> test_info = *test_it; |
| 556 |
for (typename InstantiationContainer::iterator gen_it = |
| 557 |
instantiations_.begin(); |
| 558 |
gen_it != instantiations_.end(); ++gen_it) { |
| 559 |
const std::string& instantiation_name = gen_it->name; |
| 560 |
ParamGenerator<ParamType> generator((*gen_it->generator)()); |
| 561 |
ParamNameGeneratorFunc* name_func = gen_it->name_func; |
| 562 |
const char* file = gen_it->file; |
| 563 |
int line = gen_it->line; |
| 564 |
|
| 565 |
std::string test_suite_name; |
| 566 |
if (!instantiation_name.empty()) |
| 567 |
test_suite_name = instantiation_name + "/"; |
| 568 |
test_suite_name += test_info->test_suite_base_name; |
| 569 |
|
| 570 |
size_t i = 0; |
| 571 |
std::set<std::string> test_param_names; |
| 572 |
for (typename ParamGenerator<ParamType>::iterator param_it = |
| 573 |
generator.begin(); |
| 574 |
param_it != generator.end(); ++param_it, ++i) { |
| 575 |
generated_instantiations = true; |
| 576 |
|
| 577 |
Message test_name_stream; |
| 578 |
|
| 579 |
std::string param_name = |
| 580 |
name_func(TestParamInfo<ParamType>(*param_it, i)); |
| 581 |
|
| 582 |
GTEST_CHECK_(IsValidParamName(param_name)) |
| 583 |
<< "Parameterized test name '" << param_name |
| 584 |
<< "' is invalid, in " << file << " line " << line << std::endl; |
| 585 |
|
| 586 |
GTEST_CHECK_(test_param_names.count(param_name) == 0) |
| 587 |
<< "Duplicate parameterized test name '" << param_name << "', in " |
| 588 |
<< file << " line " << line << std::endl; |
| 589 |
|
| 590 |
test_param_names.insert(param_name); |
| 591 |
|
| 592 |
if (!test_info->test_base_name.empty()) { |
| 593 |
test_name_stream << test_info->test_base_name << "/"; |
| 594 |
} |
| 595 |
test_name_stream << param_name; |
| 596 |
MakeAndRegisterTestInfo( |
| 597 |
test_suite_name.c_str(), test_name_stream.GetString().c_str(), |
| 598 |
nullptr, |
| 599 |
PrintToString(*param_it).c_str(), test_info->code_location, |
| 600 |
GetTestSuiteTypeId(), |
| 601 |
SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line), |
| 602 |
SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line), |
| 603 |
test_info->test_meta_factory->CreateTestFactory(*param_it)); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
if (!generated_instantiations) { |
| 609 |
|
| 610 |
InsertSyntheticTestCase(GetTestSuiteName(), code_location_, |
| 611 |
!tests_.empty()); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
private: |
| 616 |
|
| 617 |
|
| 618 |
struct TestInfo { |
| 619 |
TestInfo(const char* a_test_suite_base_name, const char* a_test_base_name, |
| 620 |
TestMetaFactoryBase<ParamType>* a_test_meta_factory, |
| 621 |
CodeLocation a_code_location) |
| 622 |
: test_suite_base_name(a_test_suite_base_name), |
| 623 |
test_base_name(a_test_base_name), |
| 624 |
test_meta_factory(a_test_meta_factory), |
| 625 |
code_location(a_code_location) {} |
| 626 |
|
| 627 |
const std::string test_suite_base_name; |
| 628 |
const std::string test_base_name; |
| 629 |
const std::unique_ptr<TestMetaFactoryBase<ParamType>> test_meta_factory; |
| 630 |
const CodeLocation code_location; |
| 631 |
}; |
| 632 |
using TestInfoContainer = ::std::vector<std::shared_ptr<TestInfo>>; |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
struct InstantiationInfo { |
| 637 |
InstantiationInfo(const std::string& name_in, |
| 638 |
GeneratorCreationFunc* generator_in, |
| 639 |
ParamNameGeneratorFunc* name_func_in, const char* file_in, |
| 640 |
int line_in) |
| 641 |
: name(name_in), |
| 642 |
generator(generator_in), |
| 643 |
name_func(name_func_in), |
| 644 |
file(file_in), |
| 645 |
line(line_in) {} |
| 646 |
|
| 647 |
std::string name; |
| 648 |
GeneratorCreationFunc* generator; |
| 649 |
ParamNameGeneratorFunc* name_func; |
| 650 |
const char* file; |
| 651 |
int line; |
| 652 |
}; |
| 653 |
typedef ::std::vector<InstantiationInfo> InstantiationContainer; |
| 654 |
|
| 655 |
static bool IsValidParamName(const std::string& name) { |
| 656 |
|
| 657 |
if (name.empty()) return false; |
| 658 |
|
| 659 |
|
| 660 |
for (std::string::size_type index = 0; index < name.size(); ++index) { |
| 661 |
if (!IsAlNum(name[index]) && name[index] != '_') return false; |
| 662 |
} |
| 663 |
|
| 664 |
return true; |
| 665 |
} |
| 666 |
|
| 667 |
const std::string test_suite_name_; |
| 668 |
CodeLocation code_location_; |
| 669 |
TestInfoContainer tests_; |
| 670 |
InstantiationContainer instantiations_; |
| 671 |
|
| 672 |
ParameterizedTestSuiteInfo(const ParameterizedTestSuiteInfo&) = delete; |
| 673 |
ParameterizedTestSuiteInfo& operator=(const ParameterizedTestSuiteInfo&) = |
| 674 |
delete; |
| 675 |
}; |
| 676 |
|
| 677 |
|
| 678 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 679 |
template <class TestCase> |
| 680 |
using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<TestCase>; |
| 681 |
#endif |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
class ParameterizedTestSuiteRegistry { |
| 690 |
public: |
| 691 |
ParameterizedTestSuiteRegistry() {} |
| 692 |
~ParameterizedTestSuiteRegistry() { |
| 693 |
for (auto& test_suite_info : test_suite_infos_) { |
| 694 |
delete test_suite_info; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
template <class TestSuite> |
| 701 |
ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder( |
| 702 |
const char* test_suite_name, CodeLocation code_location) { |
| 703 |
ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr; |
| 704 |
for (auto& test_suite_info : test_suite_infos_) { |
| 705 |
if (test_suite_info->GetTestSuiteName() == test_suite_name) { |
| 706 |
if (test_suite_info->GetTestSuiteTypeId() != GetTypeId<TestSuite>()) { |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
ReportInvalidTestSuiteType(test_suite_name, code_location); |
| 711 |
posix::Abort(); |
| 712 |
} else { |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
typed_test_info = CheckedDowncastToActualType< |
| 717 |
ParameterizedTestSuiteInfo<TestSuite>>(test_suite_info); |
| 718 |
} |
| 719 |
break; |
| 720 |
} |
| 721 |
} |
| 722 |
if (typed_test_info == nullptr) { |
| 723 |
typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>( |
| 724 |
test_suite_name, code_location); |
| 725 |
test_suite_infos_.push_back(typed_test_info); |
| 726 |
} |
| 727 |
return typed_test_info; |
| 728 |
} |
| 729 |
void RegisterTests() { |
| 730 |
for (auto& test_suite_info : test_suite_infos_) { |
| 731 |
test_suite_info->RegisterTests(); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 736 |
template <class TestCase> |
| 737 |
ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder( |
| 738 |
const char* test_case_name, CodeLocation code_location) { |
| 739 |
return GetTestSuitePatternHolder<TestCase>(test_case_name, code_location); |
| 740 |
} |
| 741 |
|
| 742 |
#endif |
| 743 |
|
| 744 |
private: |
| 745 |
using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>; |
| 746 |
|
| 747 |
TestSuiteInfoContainer test_suite_infos_; |
| 748 |
|
| 749 |
ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) = |
| 750 |
delete; |
| 751 |
ParameterizedTestSuiteRegistry& operator=( |
| 752 |
const ParameterizedTestSuiteRegistry&) = delete; |
| 753 |
}; |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
class TypeParameterizedTestSuiteRegistry { |
| 759 |
public: |
| 760 |
|
| 761 |
void RegisterTestSuite(const char* test_suite_name, |
| 762 |
CodeLocation code_location); |
| 763 |
|
| 764 |
|
| 765 |
void RegisterInstantiation(const char* test_suite_name); |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
void CheckForInstantiations(); |
| 770 |
|
| 771 |
private: |
| 772 |
struct TypeParameterizedTestSuiteInfo { |
| 773 |
explicit TypeParameterizedTestSuiteInfo(CodeLocation c) |
| 774 |
: code_location(c), instantiated(false) {} |
| 775 |
|
| 776 |
CodeLocation code_location; |
| 777 |
bool instantiated; |
| 778 |
}; |
| 779 |
|
| 780 |
std::map<std::string, TypeParameterizedTestSuiteInfo> suites_; |
| 781 |
}; |
| 782 |
|
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
template <class Container> |
| 788 |
internal::ParamGenerator<typename Container::value_type> ValuesIn( |
| 789 |
const Container& container); |
| 790 |
|
| 791 |
namespace internal { |
| 792 |
|
| 793 |
|
| 794 |
#ifdef _MSC_VER |
| 795 |
#pragma warning(push) |
| 796 |
#pragma warning(disable : 4100) |
| 797 |
#endif |
| 798 |
|
| 799 |
template <typename... Ts> |
| 800 |
class ValueArray { |
| 801 |
public: |
| 802 |
explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {} |
| 803 |
|
| 804 |
template <typename T> |
| 805 |
operator ParamGenerator<T>() const { |
| 806 |
return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>())); |
| 807 |
} |
| 808 |
|
| 809 |
private: |
| 810 |
template <typename T, size_t... I> |
| 811 |
std::vector<T> MakeVector(IndexSequence<I...>) const { |
| 812 |
return std::vector<T>{static_cast<T>(v_.template Get<I>())...}; |
| 813 |
} |
| 814 |
|
| 815 |
FlatTuple<Ts...> v_; |
| 816 |
}; |
| 817 |
|
| 818 |
#ifdef _MSC_VER |
| 819 |
#pragma warning(pop) |
| 820 |
#endif |
| 821 |
|
| 822 |
template <typename... T> |
| 823 |
class CartesianProductGenerator |
| 824 |
: public ParamGeneratorInterface<::std::tuple<T...>> { |
| 825 |
public: |
| 826 |
typedef ::std::tuple<T...> ParamType; |
| 827 |
|
| 828 |
CartesianProductGenerator(const std::tuple<ParamGenerator<T>...>& g) |
| 829 |
: generators_(g) {} |
| 830 |
~CartesianProductGenerator() override {} |
| 831 |
|
| 832 |
ParamIteratorInterface<ParamType>* Begin() const override { |
| 833 |
return new Iterator(this, generators_, false); |
| 834 |
} |
| 835 |
ParamIteratorInterface<ParamType>* End() const override { |
| 836 |
return new Iterator(this, generators_, true); |
| 837 |
} |
| 838 |
|
| 839 |
private: |
| 840 |
template <class I> |
| 841 |
class IteratorImpl; |
| 842 |
template <size_t... I> |
| 843 |
class IteratorImpl<IndexSequence<I...>> |
| 844 |
: public ParamIteratorInterface<ParamType> { |
| 845 |
public: |
| 846 |
IteratorImpl(const ParamGeneratorInterface<ParamType>* base, |
| 847 |
const std::tuple<ParamGenerator<T>...>& generators, |
| 848 |
bool is_end) |
| 849 |
: base_(base), |
| 850 |
begin_(std::get<I>(generators).begin()...), |
| 851 |
end_(std::get<I>(generators).end()...), |
| 852 |
current_(is_end ? end_ : begin_) { |
| 853 |
ComputeCurrentValue(); |
| 854 |
} |
| 855 |
~IteratorImpl() override {} |
| 856 |
|
| 857 |
const ParamGeneratorInterface<ParamType>* BaseGenerator() const override { |
| 858 |
return base_; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
void Advance() override { |
| 863 |
assert(!AtEnd()); |
| 864 |
|
| 865 |
++std::get<sizeof...(T) - 1>(current_); |
| 866 |
|
| 867 |
AdvanceIfEnd<sizeof...(T) - 1>(); |
| 868 |
ComputeCurrentValue(); |
| 869 |
} |
| 870 |
ParamIteratorInterface<ParamType>* Clone() const override { |
| 871 |
return new IteratorImpl(*this); |
| 872 |
} |
| 873 |
|
| 874 |
const ParamType* Current() const override { return current_value_.get(); } |
| 875 |
|
| 876 |
bool Equals(const ParamIteratorInterface<ParamType>& other) const override { |
| 877 |
|
| 878 |
|
| 879 |
GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) |
| 880 |
<< "The program attempted to compare iterators " |
| 881 |
<< "from different generators." << std::endl; |
| 882 |
const IteratorImpl* typed_other = |
| 883 |
CheckedDowncastToActualType<const IteratorImpl>(&other); |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
if (AtEnd() && typed_other->AtEnd()) return true; |
| 889 |
|
| 890 |
bool same = true; |
| 891 |
bool dummy[] = { |
| 892 |
(same = same && std::get<I>(current_) == |
| 893 |
std::get<I>(typed_other->current_))...}; |
| 894 |
(void)dummy; |
| 895 |
return same; |
| 896 |
} |
| 897 |
|
| 898 |
private: |
| 899 |
template <size_t ThisI> |
| 900 |
void AdvanceIfEnd() { |
| 901 |
if (std::get<ThisI>(current_) != std::get<ThisI>(end_)) return; |
| 902 |
|
| 903 |
bool last = ThisI == 0; |
| 904 |
if (last) { |
| 905 |
|
| 906 |
return; |
| 907 |
} |
| 908 |
|
| 909 |
constexpr size_t NextI = ThisI - (ThisI != 0); |
| 910 |
std::get<ThisI>(current_) = std::get<ThisI>(begin_); |
| 911 |
++std::get<NextI>(current_); |
| 912 |
AdvanceIfEnd<NextI>(); |
| 913 |
} |
| 914 |
|
| 915 |
void ComputeCurrentValue() { |
| 916 |
if (!AtEnd()) |
| 917 |
current_value_ = std::make_shared<ParamType>(*std::get<I>(current_)...); |
| 918 |
} |
| 919 |
bool AtEnd() const { |
| 920 |
bool at_end = false; |
| 921 |
bool dummy[] = { |
| 922 |
(at_end = at_end || std::get<I>(current_) == std::get<I>(end_))...}; |
| 923 |
(void)dummy; |
| 924 |
return at_end; |
| 925 |
} |
| 926 |
|
| 927 |
const ParamGeneratorInterface<ParamType>* const base_; |
| 928 |
std::tuple<typename ParamGenerator<T>::iterator...> begin_; |
| 929 |
std::tuple<typename ParamGenerator<T>::iterator...> end_; |
| 930 |
std::tuple<typename ParamGenerator<T>::iterator...> current_; |
| 931 |
std::shared_ptr<ParamType> current_value_; |
| 932 |
}; |
| 933 |
|
| 934 |
using Iterator = IteratorImpl<typename MakeIndexSequence<sizeof...(T)>::type>; |
| 935 |
|
| 936 |
std::tuple<ParamGenerator<T>...> generators_; |
| 937 |
}; |
| 938 |
|
| 939 |
template <class... Gen> |
| 940 |
class CartesianProductHolder { |
| 941 |
public: |
| 942 |
CartesianProductHolder(const Gen&... g) : generators_(g...) {} |
| 943 |
template <typename... T> |
| 944 |
operator ParamGenerator<::std::tuple<T...>>() const { |
| 945 |
return ParamGenerator<::std::tuple<T...>>( |
| 946 |
new CartesianProductGenerator<T...>(generators_)); |
| 947 |
} |
| 948 |
|
| 949 |
private: |
| 950 |
std::tuple<Gen...> generators_; |
| 951 |
}; |
| 952 |
|
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
#endif |