/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/include/gtest/internal/gtest-param-util.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 619 lines · 368 code · 71 blank · 180 comment · 31 complexity · 4e317938bded7d106d5899cfd93c8c61 MD5 · raw file

  1. // Copyright 2008 Google Inc.
  2. // All Rights Reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  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
  13. // distribution.
  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.
  17. //
  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.
  29. //
  30. // Author: vladl@google.com (Vlad Losev)
  31. // Type and function utilities for implementing parameterized tests.
  32. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
  33. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
  34. #include <iterator>
  35. #include <utility>
  36. #include <vector>
  37. // scripts/fuse_gtest.py depends on gtest's own header being #included
  38. // *unconditionally*. Therefore these #includes cannot be moved
  39. // inside #if GTEST_HAS_PARAM_TEST.
  40. #include <gtest/internal/gtest-internal.h>
  41. #include <gtest/internal/gtest-linked_ptr.h>
  42. #include <gtest/internal/gtest-port.h>
  43. #if GTEST_HAS_PARAM_TEST
  44. namespace testing {
  45. namespace internal {
  46. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  47. //
  48. // Outputs a message explaining invalid registration of different
  49. // fixture class for the same test case. This may happen when
  50. // TEST_P macro is used to define two tests with the same name
  51. // but in different namespaces.
  52. GTEST_API_ void ReportInvalidTestCaseType(const char* test_case_name,
  53. const char* file, int line);
  54. template <typename> class ParamGeneratorInterface;
  55. template <typename> class ParamGenerator;
  56. // Interface for iterating over elements provided by an implementation
  57. // of ParamGeneratorInterface<T>.
  58. template <typename T>
  59. class ParamIteratorInterface {
  60. public:
  61. virtual ~ParamIteratorInterface() {}
  62. // A pointer to the base generator instance.
  63. // Used only for the purposes of iterator comparison
  64. // to make sure that two iterators belong to the same generator.
  65. virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
  66. // Advances iterator to point to the next element
  67. // provided by the generator. The caller is responsible
  68. // for not calling Advance() on an iterator equal to
  69. // BaseGenerator()->End().
  70. virtual void Advance() = 0;
  71. // Clones the iterator object. Used for implementing copy semantics
  72. // of ParamIterator<T>.
  73. virtual ParamIteratorInterface* Clone() const = 0;
  74. // Dereferences the current iterator and provides (read-only) access
  75. // to the pointed value. It is the caller's responsibility not to call
  76. // Current() on an iterator equal to BaseGenerator()->End().
  77. // Used for implementing ParamGenerator<T>::operator*().
  78. virtual const T* Current() const = 0;
  79. // Determines whether the given iterator and other point to the same
  80. // element in the sequence generated by the generator.
  81. // Used for implementing ParamGenerator<T>::operator==().
  82. virtual bool Equals(const ParamIteratorInterface& other) const = 0;
  83. };
  84. // Class iterating over elements provided by an implementation of
  85. // ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
  86. // and implements the const forward iterator concept.
  87. template <typename T>
  88. class ParamIterator {
  89. public:
  90. typedef T value_type;
  91. typedef const T& reference;
  92. typedef ptrdiff_t difference_type;
  93. // ParamIterator assumes ownership of the impl_ pointer.
  94. ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {}
  95. ParamIterator& operator=(const ParamIterator& other) {
  96. if (this != &other)
  97. impl_.reset(other.impl_->Clone());
  98. return *this;
  99. }
  100. const T& operator*() const { return *impl_->Current(); }
  101. const T* operator->() const { return impl_->Current(); }
  102. // Prefix version of operator++.
  103. ParamIterator& operator++() {
  104. impl_->Advance();
  105. return *this;
  106. }
  107. // Postfix version of operator++.
  108. ParamIterator operator++(int /*unused*/) {
  109. ParamIteratorInterface<T>* clone = impl_->Clone();
  110. impl_->Advance();
  111. return ParamIterator(clone);
  112. }
  113. bool operator==(const ParamIterator& other) const {
  114. return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);
  115. }
  116. bool operator!=(const ParamIterator& other) const {
  117. return !(*this == other);
  118. }
  119. private:
  120. friend class ParamGenerator<T>;
  121. explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {}
  122. scoped_ptr<ParamIteratorInterface<T> > impl_;
  123. };
  124. // ParamGeneratorInterface<T> is the binary interface to access generators
  125. // defined in other translation units.
  126. template <typename T>
  127. class ParamGeneratorInterface {
  128. public:
  129. typedef T ParamType;
  130. virtual ~ParamGeneratorInterface() {}
  131. // Generator interface definition
  132. virtual ParamIteratorInterface<T>* Begin() const = 0;
  133. virtual ParamIteratorInterface<T>* End() const = 0;
  134. };
  135. // Wraps ParamGeneratorInterface<T> and provides general generator syntax
  136. // compatible with the STL Container concept.
  137. // This class implements copy initialization semantics and the contained
  138. // ParamGeneratorInterface<T> instance is shared among all copies
  139. // of the original object. This is possible because that instance is immutable.
  140. template<typename T>
  141. class ParamGenerator {
  142. public:
  143. typedef ParamIterator<T> iterator;
  144. explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {}
  145. ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {}
  146. ParamGenerator& operator=(const ParamGenerator& other) {
  147. impl_ = other.impl_;
  148. return *this;
  149. }
  150. iterator begin() const { return iterator(impl_->Begin()); }
  151. iterator end() const { return iterator(impl_->End()); }
  152. private:
  153. ::testing::internal::linked_ptr<const ParamGeneratorInterface<T> > impl_;
  154. };
  155. // Generates values from a range of two comparable values. Can be used to
  156. // generate sequences of user-defined types that implement operator+() and
  157. // operator<().
  158. // This class is used in the Range() function.
  159. template <typename T, typename IncrementT>
  160. class RangeGenerator : public ParamGeneratorInterface<T> {
  161. public:
  162. RangeGenerator(T begin, T end, IncrementT step)
  163. : begin_(begin), end_(end),
  164. step_(step), end_index_(CalculateEndIndex(begin, end, step)) {}
  165. virtual ~RangeGenerator() {}
  166. virtual ParamIteratorInterface<T>* Begin() const {
  167. return new Iterator(this, begin_, 0, step_);
  168. }
  169. virtual ParamIteratorInterface<T>* End() const {
  170. return new Iterator(this, end_, end_index_, step_);
  171. }
  172. private:
  173. class Iterator : public ParamIteratorInterface<T> {
  174. public:
  175. Iterator(const ParamGeneratorInterface<T>* base, T value, int index,
  176. IncrementT step)
  177. : base_(base), value_(value), index_(index), step_(step) {}
  178. virtual ~Iterator() {}
  179. virtual const ParamGeneratorInterface<T>* BaseGenerator() const {
  180. return base_;
  181. }
  182. virtual void Advance() {
  183. value_ = value_ + step_;
  184. index_++;
  185. }
  186. virtual ParamIteratorInterface<T>* Clone() const {
  187. return new Iterator(*this);
  188. }
  189. virtual const T* Current() const { return &value_; }
  190. virtual bool Equals(const ParamIteratorInterface<T>& other) const {
  191. // Having the same base generator guarantees that the other
  192. // iterator is of the same type and we can downcast.
  193. GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
  194. << "The program attempted to compare iterators "
  195. << "from different generators." << std::endl;
  196. const int other_index =
  197. CheckedDowncastToActualType<const Iterator>(&other)->index_;
  198. return index_ == other_index;
  199. }
  200. private:
  201. Iterator(const Iterator& other)
  202. : ParamIteratorInterface<T>(),
  203. base_(other.base_), value_(other.value_), index_(other.index_),
  204. step_(other.step_) {}
  205. // No implementation - assignment is unsupported.
  206. void operator=(const Iterator& other);
  207. const ParamGeneratorInterface<T>* const base_;
  208. T value_;
  209. int index_;
  210. const IncrementT step_;
  211. }; // class RangeGenerator::Iterator
  212. static int CalculateEndIndex(const T& begin,
  213. const T& end,
  214. const IncrementT& step) {
  215. int end_index = 0;
  216. for (T i = begin; i < end; i = i + step)
  217. end_index++;
  218. return end_index;
  219. }
  220. // No implementation - assignment is unsupported.
  221. void operator=(const RangeGenerator& other);
  222. const T begin_;
  223. const T end_;
  224. const IncrementT step_;
  225. // The index for the end() iterator. All the elements in the generated
  226. // sequence are indexed (0-based) to aid iterator comparison.
  227. const int end_index_;
  228. }; // class RangeGenerator
  229. // Generates values from a pair of STL-style iterators. Used in the
  230. // ValuesIn() function. The elements are copied from the source range
  231. // since the source can be located on the stack, and the generator
  232. // is likely to persist beyond that stack frame.
  233. template <typename T>
  234. class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> {
  235. public:
  236. template <typename ForwardIterator>
  237. ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
  238. : container_(begin, end) {}
  239. virtual ~ValuesInIteratorRangeGenerator() {}
  240. virtual ParamIteratorInterface<T>* Begin() const {
  241. return new Iterator(this, container_.begin());
  242. }
  243. virtual ParamIteratorInterface<T>* End() const {
  244. return new Iterator(this, container_.end());
  245. }
  246. private:
  247. typedef typename ::std::vector<T> ContainerType;
  248. class Iterator : public ParamIteratorInterface<T> {
  249. public:
  250. Iterator(const ParamGeneratorInterface<T>* base,
  251. typename ContainerType::const_iterator iterator)
  252. : base_(base), iterator_(iterator) {}
  253. virtual ~Iterator() {}
  254. virtual const ParamGeneratorInterface<T>* BaseGenerator() const {
  255. return base_;
  256. }
  257. virtual void Advance() {
  258. ++iterator_;
  259. value_.reset();
  260. }
  261. virtual ParamIteratorInterface<T>* Clone() const {
  262. return new Iterator(*this);
  263. }
  264. // We need to use cached value referenced by iterator_ because *iterator_
  265. // can return a temporary object (and of type other then T), so just
  266. // having "return &*iterator_;" doesn't work.
  267. // value_ is updated here and not in Advance() because Advance()
  268. // can advance iterator_ beyond the end of the range, and we cannot
  269. // detect that fact. The client code, on the other hand, is
  270. // responsible for not calling Current() on an out-of-range iterator.
  271. virtual const T* Current() const {
  272. if (value_.get() == NULL)
  273. value_.reset(new T(*iterator_));
  274. return value_.get();
  275. }
  276. virtual bool Equals(const ParamIteratorInterface<T>& other) const {
  277. // Having the same base generator guarantees that the other
  278. // iterator is of the same type and we can downcast.
  279. GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
  280. << "The program attempted to compare iterators "
  281. << "from different generators." << std::endl;
  282. return iterator_ ==
  283. CheckedDowncastToActualType<const Iterator>(&other)->iterator_;
  284. }
  285. private:
  286. Iterator(const Iterator& other)
  287. // The explicit constructor call suppresses a false warning
  288. // emitted by gcc when supplied with the -Wextra option.
  289. : ParamIteratorInterface<T>(),
  290. base_(other.base_),
  291. iterator_(other.iterator_) {}
  292. const ParamGeneratorInterface<T>* const base_;
  293. typename ContainerType::const_iterator iterator_;
  294. // A cached value of *iterator_. We keep it here to allow access by
  295. // pointer in the wrapping iterator's operator->().
  296. // value_ needs to be mutable to be accessed in Current().
  297. // Use of scoped_ptr helps manage cached value's lifetime,
  298. // which is bound by the lifespan of the iterator itself.
  299. mutable scoped_ptr<const T> value_;
  300. }; // class ValuesInIteratorRangeGenerator::Iterator
  301. // No implementation - assignment is unsupported.
  302. void operator=(const ValuesInIteratorRangeGenerator& other);
  303. const ContainerType container_;
  304. }; // class ValuesInIteratorRangeGenerator
  305. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  306. //
  307. // Stores a parameter value and later creates tests parameterized with that
  308. // value.
  309. template <class TestClass>
  310. class ParameterizedTestFactory : public TestFactoryBase {
  311. public:
  312. typedef typename TestClass::ParamType ParamType;
  313. explicit ParameterizedTestFactory(ParamType parameter) :
  314. parameter_(parameter) {}
  315. virtual Test* CreateTest() {
  316. TestClass::SetParam(&parameter_);
  317. return new TestClass();
  318. }
  319. private:
  320. const ParamType parameter_;
  321. GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory);
  322. };
  323. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  324. //
  325. // TestMetaFactoryBase is a base class for meta-factories that create
  326. // test factories for passing into MakeAndRegisterTestInfo function.
  327. template <class ParamType>
  328. class TestMetaFactoryBase {
  329. public:
  330. virtual ~TestMetaFactoryBase() {}
  331. virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
  332. };
  333. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  334. //
  335. // TestMetaFactory creates test factories for passing into
  336. // MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
  337. // ownership of test factory pointer, same factory object cannot be passed
  338. // into that method twice. But ParameterizedTestCaseInfo is going to call
  339. // it for each Test/Parameter value combination. Thus it needs meta factory
  340. // creator class.
  341. template <class TestCase>
  342. class TestMetaFactory
  343. : public TestMetaFactoryBase<typename TestCase::ParamType> {
  344. public:
  345. typedef typename TestCase::ParamType ParamType;
  346. TestMetaFactory() {}
  347. virtual TestFactoryBase* CreateTestFactory(ParamType parameter) {
  348. return new ParameterizedTestFactory<TestCase>(parameter);
  349. }
  350. private:
  351. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory);
  352. };
  353. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  354. //
  355. // ParameterizedTestCaseInfoBase is a generic interface
  356. // to ParameterizedTestCaseInfo classes. ParameterizedTestCaseInfoBase
  357. // accumulates test information provided by TEST_P macro invocations
  358. // and generators provided by INSTANTIATE_TEST_CASE_P macro invocations
  359. // and uses that information to register all resulting test instances
  360. // in RegisterTests method. The ParameterizeTestCaseRegistry class holds
  361. // a collection of pointers to the ParameterizedTestCaseInfo objects
  362. // and calls RegisterTests() on each of them when asked.
  363. class ParameterizedTestCaseInfoBase {
  364. public:
  365. virtual ~ParameterizedTestCaseInfoBase() {}
  366. // Base part of test case name for display purposes.
  367. virtual const String& GetTestCaseName() const = 0;
  368. // Test case id to verify identity.
  369. virtual TypeId GetTestCaseTypeId() const = 0;
  370. // UnitTest class invokes this method to register tests in this
  371. // test case right before running them in RUN_ALL_TESTS macro.
  372. // This method should not be called more then once on any single
  373. // instance of a ParameterizedTestCaseInfoBase derived class.
  374. virtual void RegisterTests() = 0;
  375. protected:
  376. ParameterizedTestCaseInfoBase() {}
  377. private:
  378. GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfoBase);
  379. };
  380. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  381. //
  382. // ParameterizedTestCaseInfo accumulates tests obtained from TEST_P
  383. // macro invocations for a particular test case and generators
  384. // obtained from INSTANTIATE_TEST_CASE_P macro invocations for that
  385. // test case. It registers tests with all values generated by all
  386. // generators when asked.
  387. template <class TestCase>
  388. class ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase {
  389. public:
  390. // ParamType and GeneratorCreationFunc are private types but are required
  391. // for declarations of public methods AddTestPattern() and
  392. // AddTestCaseInstantiation().
  393. typedef typename TestCase::ParamType ParamType;
  394. // A function that returns an instance of appropriate generator type.
  395. typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();
  396. explicit ParameterizedTestCaseInfo(const char* name)
  397. : test_case_name_(name) {}
  398. // Test case base name for display purposes.
  399. virtual const String& GetTestCaseName() const { return test_case_name_; }
  400. // Test case id to verify identity.
  401. virtual TypeId GetTestCaseTypeId() const { return GetTypeId<TestCase>(); }
  402. // TEST_P macro uses AddTestPattern() to record information
  403. // about a single test in a LocalTestInfo structure.
  404. // test_case_name is the base name of the test case (without invocation
  405. // prefix). test_base_name is the name of an individual test without
  406. // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
  407. // test case base name and DoBar is test base name.
  408. void AddTestPattern(const char* test_case_name,
  409. const char* test_base_name,
  410. TestMetaFactoryBase<ParamType>* meta_factory) {
  411. tests_.push_back(linked_ptr<TestInfo>(new TestInfo(test_case_name,
  412. test_base_name,
  413. meta_factory)));
  414. }
  415. // INSTANTIATE_TEST_CASE_P macro uses AddGenerator() to record information
  416. // about a generator.
  417. int AddTestCaseInstantiation(const char* instantiation_name,
  418. GeneratorCreationFunc* func,
  419. const char* /* file */,
  420. int /* line */) {
  421. instantiations_.push_back(::std::make_pair(instantiation_name, func));
  422. return 0; // Return value used only to run this method in namespace scope.
  423. }
  424. // UnitTest class invokes this method to register tests in this test case
  425. // test cases right before running tests in RUN_ALL_TESTS macro.
  426. // This method should not be called more then once on any single
  427. // instance of a ParameterizedTestCaseInfoBase derived class.
  428. // UnitTest has a guard to prevent from calling this method more then once.
  429. virtual void RegisterTests() {
  430. for (typename TestInfoContainer::iterator test_it = tests_.begin();
  431. test_it != tests_.end(); ++test_it) {
  432. linked_ptr<TestInfo> test_info = *test_it;
  433. for (typename InstantiationContainer::iterator gen_it =
  434. instantiations_.begin(); gen_it != instantiations_.end();
  435. ++gen_it) {
  436. const String& instantiation_name = gen_it->first;
  437. ParamGenerator<ParamType> generator((*gen_it->second)());
  438. Message test_case_name_stream;
  439. if ( !instantiation_name.empty() )
  440. test_case_name_stream << instantiation_name.c_str() << "/";
  441. test_case_name_stream << test_info->test_case_base_name.c_str();
  442. int i = 0;
  443. for (typename ParamGenerator<ParamType>::iterator param_it =
  444. generator.begin();
  445. param_it != generator.end(); ++param_it, ++i) {
  446. Message test_name_stream;
  447. test_name_stream << test_info->test_base_name.c_str() << "/" << i;
  448. ::testing::internal::MakeAndRegisterTestInfo(
  449. test_case_name_stream.GetString().c_str(),
  450. test_name_stream.GetString().c_str(),
  451. "", // test_case_comment
  452. "", // comment; TODO(vladl@google.com): provide parameter value
  453. // representation.
  454. GetTestCaseTypeId(),
  455. TestCase::SetUpTestCase,
  456. TestCase::TearDownTestCase,
  457. test_info->test_meta_factory->CreateTestFactory(*param_it));
  458. } // for param_it
  459. } // for gen_it
  460. } // for test_it
  461. } // RegisterTests
  462. private:
  463. // LocalTestInfo structure keeps information about a single test registered
  464. // with TEST_P macro.
  465. struct TestInfo {
  466. TestInfo(const char* a_test_case_base_name,
  467. const char* a_test_base_name,
  468. TestMetaFactoryBase<ParamType>* a_test_meta_factory) :
  469. test_case_base_name(a_test_case_base_name),
  470. test_base_name(a_test_base_name),
  471. test_meta_factory(a_test_meta_factory) {}
  472. const String test_case_base_name;
  473. const String test_base_name;
  474. const scoped_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory;
  475. };
  476. typedef ::std::vector<linked_ptr<TestInfo> > TestInfoContainer;
  477. // Keeps pairs of <Instantiation name, Sequence generator creation function>
  478. // received from INSTANTIATE_TEST_CASE_P macros.
  479. typedef ::std::vector<std::pair<String, GeneratorCreationFunc*> >
  480. InstantiationContainer;
  481. const String test_case_name_;
  482. TestInfoContainer tests_;
  483. InstantiationContainer instantiations_;
  484. GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfo);
  485. }; // class ParameterizedTestCaseInfo
  486. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  487. //
  488. // ParameterizedTestCaseRegistry contains a map of ParameterizedTestCaseInfoBase
  489. // classes accessed by test case names. TEST_P and INSTANTIATE_TEST_CASE_P
  490. // macros use it to locate their corresponding ParameterizedTestCaseInfo
  491. // descriptors.
  492. class ParameterizedTestCaseRegistry {
  493. public:
  494. ParameterizedTestCaseRegistry() {}
  495. ~ParameterizedTestCaseRegistry() {
  496. for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
  497. it != test_case_infos_.end(); ++it) {
  498. delete *it;
  499. }
  500. }
  501. // Looks up or creates and returns a structure containing information about
  502. // tests and instantiations of a particular test case.
  503. template <class TestCase>
  504. ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder(
  505. const char* test_case_name,
  506. const char* file,
  507. int line) {
  508. ParameterizedTestCaseInfo<TestCase>* typed_test_info = NULL;
  509. for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
  510. it != test_case_infos_.end(); ++it) {
  511. if ((*it)->GetTestCaseName() == test_case_name) {
  512. if ((*it)->GetTestCaseTypeId() != GetTypeId<TestCase>()) {
  513. // Complain about incorrect usage of Google Test facilities
  514. // and terminate the program since we cannot guaranty correct
  515. // test case setup and tear-down in this case.
  516. ReportInvalidTestCaseType(test_case_name, file, line);
  517. abort();
  518. } else {
  519. // At this point we are sure that the object we found is of the same
  520. // type we are looking for, so we downcast it to that type
  521. // without further checks.
  522. typed_test_info = CheckedDowncastToActualType<
  523. ParameterizedTestCaseInfo<TestCase> >(*it);
  524. }
  525. break;
  526. }
  527. }
  528. if (typed_test_info == NULL) {
  529. typed_test_info = new ParameterizedTestCaseInfo<TestCase>(test_case_name);
  530. test_case_infos_.push_back(typed_test_info);
  531. }
  532. return typed_test_info;
  533. }
  534. void RegisterTests() {
  535. for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();
  536. it != test_case_infos_.end(); ++it) {
  537. (*it)->RegisterTests();
  538. }
  539. }
  540. private:
  541. typedef ::std::vector<ParameterizedTestCaseInfoBase*> TestCaseInfoContainer;
  542. TestCaseInfoContainer test_case_infos_;
  543. GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry);
  544. };
  545. } // namespace internal
  546. } // namespace testing
  547. #endif // GTEST_HAS_PARAM_TEST
  548. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_