Lab_1 0.1.1
Matrix Library
Loading...
Searching...
No Matches
gtest-param-util.h
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// Type and function utilities for implementing parameterized tests.
31
32// IWYU pragma: private, include "gtest/gtest.h"
33// IWYU pragma: friend gtest/.*
34// IWYU pragma: friend gmock/.*
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
55namespace testing {
56// Input to a parameterized test name generator, describing a test parameter.
57// Consists of the parameter value and the integer parameter index.
58template <class ParamType>
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// A builtin parameterized test name generator which returns the result of
67// testing::PrintToString.
69 template <class ParamType>
70 std::string operator()(const TestParamInfo<ParamType>& info) const {
71 return PrintToString(info.param);
72 }
73};
74
75namespace internal {
76
77// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
78// Utility Functions
79
80// Outputs a message explaining invalid registration of different
81// fixture class for the same test suite. This may happen when
82// TEST_P macro is used to define two tests with the same name
83// but in different namespaces.
84GTEST_API_ void ReportInvalidTestSuiteType(const char* test_suite_name,
85 CodeLocation code_location);
86
87template <typename>
88class ParamGeneratorInterface;
89template <typename>
90class ParamGenerator;
91
92// Interface for iterating over elements provided by an implementation
93// of ParamGeneratorInterface<T>.
94template <typename T>
96 public:
97 virtual ~ParamIteratorInterface() {}
98 // A pointer to the base generator instance.
99 // Used only for the purposes of iterator comparison
100 // to make sure that two iterators belong to the same generator.
101 virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0;
102 // Advances iterator to point to the next element
103 // provided by the generator. The caller is responsible
104 // for not calling Advance() on an iterator equal to
105 // BaseGenerator()->End().
106 virtual void Advance() = 0;
107 // Clones the iterator object. Used for implementing copy semantics
108 // of ParamIterator<T>.
109 virtual ParamIteratorInterface* Clone() const = 0;
110 // Dereferences the current iterator and provides (read-only) access
111 // to the pointed value. It is the caller's responsibility not to call
112 // Current() on an iterator equal to BaseGenerator()->End().
113 // Used for implementing ParamGenerator<T>::operator*().
114 virtual const T* Current() const = 0;
115 // Determines whether the given iterator and other point to the same
116 // element in the sequence generated by the generator.
117 // Used for implementing ParamGenerator<T>::operator==().
118 virtual bool Equals(const ParamIteratorInterface& other) const = 0;
119};
120
121// Class iterating over elements provided by an implementation of
122// ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>
123// and implements the const forward iterator concept.
124template <typename T>
126 public:
127 typedef T value_type;
128 typedef const T& reference;
129 typedef ptrdiff_t difference_type;
130
131 // ParamIterator assumes ownership of the impl_ pointer.
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 // Prefix version of operator++.
141 ParamIterator& operator++() {
142 impl_->Advance();
143 return *this;
144 }
145 // Postfix version of operator++.
146 ParamIterator operator++(int /*unused*/) {
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// ParamGeneratorInterface<T> is the binary interface to access generators
165// defined in other translation units.
166template <typename T>
168 public:
169 typedef T ParamType;
170
171 virtual ~ParamGeneratorInterface() {}
172
173 // Generator interface definition
174 virtual ParamIteratorInterface<T>* Begin() const = 0;
175 virtual ParamIteratorInterface<T>* End() const = 0;
176};
177
178// Wraps ParamGeneratorInterface<T> and provides general generator syntax
179// compatible with the STL Container concept.
180// This class implements copy initialization semantics and the contained
181// ParamGeneratorInterface<T> instance is shared among all copies
182// of the original object. This is possible because that instance is immutable.
183template <typename T>
185 public:
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// Generates values from a range of two comparable values. Can be used to
204// generate sequences of user-defined types that implement operator+() and
205// operator<().
206// This class is used in the Range() function.
207template <typename T, typename IncrementT>
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 // Having the same base generator guarantees that the other
245 // iterator is of the same type and we can downcast.
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)
257 base_(other.base_),
258 value_(other.value_),
259 index_(other.index_),
260 step_(other.step_) {}
261
262 // No implementation - assignment is unsupported.
263 void operator=(const Iterator& other);
264
265 const ParamGeneratorInterface<T>* const base_;
266 T value_;
267 int index_;
268 const IncrementT step_;
269 }; // class RangeGenerator::Iterator
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 // No implementation - assignment is unsupported.
279 void operator=(const RangeGenerator& other);
280
281 const T begin_;
282 const T end_;
283 const IncrementT step_;
284 // The index for the end() iterator. All the elements in the generated
285 // sequence are indexed (0-based) to aid iterator comparison.
286 const int end_index_;
287}; // class RangeGenerator
288
289// Generates values from a pair of STL-style iterators. Used in the
290// ValuesIn() function. The elements are copied from the source range
291// since the source can be located on the stack, and the generator
292// is likely to persist beyond that stack frame.
293template <typename T>
295 public:
296 template <typename ForwardIterator>
297 ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)
298 : container_(begin, end) {}
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 // We need to use cached value referenced by iterator_ because *iterator_
329 // can return a temporary object (and of type other then T), so just
330 // having "return &*iterator_;" doesn't work.
331 // value_ is updated here and not in Advance() because Advance()
332 // can advance iterator_ beyond the end of the range, and we cannot
333 // detect that fact. The client code, on the other hand, is
334 // responsible for not calling Current() on an out-of-range iterator.
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 // Having the same base generator guarantees that the other
341 // iterator is of the same type and we can downcast.
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 // The explicit constructor call suppresses a false warning
352 // emitted by gcc when supplied with the -Wextra option.
354 base_(other.base_),
355 iterator_(other.iterator_) {}
356
357 const ParamGeneratorInterface<T>* const base_;
358 typename ContainerType::const_iterator iterator_;
359 // A cached value of *iterator_. We keep it here to allow access by
360 // pointer in the wrapping iterator's operator->().
361 // value_ needs to be mutable to be accessed in Current().
362 // Use of std::unique_ptr helps manage cached value's lifetime,
363 // which is bound by the lifespan of the iterator itself.
364 mutable std::unique_ptr<const T> value_;
365 }; // class ValuesInIteratorRangeGenerator::Iterator
366
367 // No implementation - assignment is unsupported.
368 void operator=(const ValuesInIteratorRangeGenerator& other);
369
370 const ContainerType container_;
371}; // class ValuesInIteratorRangeGenerator
372
373// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
374//
375// Default parameterized test name generator, returns a string containing the
376// integer test parameter index.
377template <class ParamType>
378std::string DefaultParamName(const TestParamInfo<ParamType>& info) {
379 Message name_stream;
380 name_stream << info.index;
381 return name_stream.GetString();
382}
383
384template <typename T = int>
385void TestNotEmpty() {
386 static_assert(sizeof(T) == 0, "Empty arguments are not allowed.");
387}
388template <typename T = int>
389void TestNotEmpty(const T&) {}
390
391// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
392//
393// Stores a parameter value and later creates tests parameterized with that
394// value.
395template <class TestClass>
397 public:
398 typedef typename TestClass::ParamType ParamType;
399 explicit ParameterizedTestFactory(ParamType parameter)
400 : parameter_(parameter) {}
401 Test* CreateTest() override {
402 TestClass::SetParam(&parameter_);
403 return new TestClass();
404 }
405
406 private:
407 const ParamType parameter_;
408
410 ParameterizedTestFactory& operator=(const ParameterizedTestFactory&) = delete;
411};
412
413// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
414//
415// TestMetaFactoryBase is a base class for meta-factories that create
416// test factories for passing into MakeAndRegisterTestInfo function.
417template <class ParamType>
419 public:
420 virtual ~TestMetaFactoryBase() {}
421
422 virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0;
423};
424
425// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
426//
427// TestMetaFactory creates test factories for passing into
428// MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives
429// ownership of test factory pointer, same factory object cannot be passed
430// into that method twice. But ParameterizedTestSuiteInfo is going to call
431// it for each Test/Parameter value combination. Thus it needs meta factory
432// creator class.
433template <class TestSuite>
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// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
451//
452// ParameterizedTestSuiteInfoBase is a generic interface
453// to ParameterizedTestSuiteInfo classes. ParameterizedTestSuiteInfoBase
454// accumulates test information provided by TEST_P macro invocations
455// and generators provided by INSTANTIATE_TEST_SUITE_P macro invocations
456// and uses that information to register all resulting test instances
457// in RegisterTests method. The ParameterizeTestSuiteRegistry class holds
458// a collection of pointers to the ParameterizedTestSuiteInfo objects
459// and calls RegisterTests() on each of them when asked.
461 public:
463
464 // Base part of test suite name for display purposes.
465 virtual const std::string& GetTestSuiteName() const = 0;
466 // Test suite id to verify identity.
467 virtual TypeId GetTestSuiteTypeId() const = 0;
468 // UnitTest class invokes this method to register tests in this
469 // test suite right before running them in RUN_ALL_TESTS macro.
470 // This method should not be called more than once on any single
471 // instance of a ParameterizedTestSuiteInfoBase derived class.
472 virtual void RegisterTests() = 0;
473
474 protected:
476
477 private:
479 delete;
481 const ParameterizedTestSuiteInfoBase&) = delete;
482};
483
484// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
485//
486// Report a the name of a test_suit as safe to ignore
487// as the side effect of construction of this type.
488struct GTEST_API_ MarkAsIgnored {
489 explicit MarkAsIgnored(const char* test_suite);
490};
491
492GTEST_API_ void InsertSyntheticTestCase(const std::string& name,
493 CodeLocation location, bool has_test_p);
494
495// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
496//
497// ParameterizedTestSuiteInfo accumulates tests obtained from TEST_P
498// macro invocations for a particular test suite and generators
499// obtained from INSTANTIATE_TEST_SUITE_P macro invocations for that
500// test suite. It registers tests with all values generated by all
501// generators when asked.
502template <class TestSuite>
504 public:
505 // ParamType and GeneratorCreationFunc are private types but are required
506 // for declarations of public methods AddTestPattern() and
507 // AddTestSuiteInstantiation().
508 using ParamType = typename TestSuite::ParamType;
509 // A function that returns an instance of appropriate generator type.
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 // Test suite base name for display purposes.
518 const std::string& GetTestSuiteName() const override {
519 return test_suite_name_;
520 }
521 // Test suite id to verify identity.
522 TypeId GetTestSuiteTypeId() const override { return GetTypeId<TestSuite>(); }
523 // TEST_P macro uses AddTestPattern() to record information
524 // about a single test in a LocalTestInfo structure.
525 // test_suite_name is the base name of the test suite (without invocation
526 // prefix). test_base_name is the name of an individual test without
527 // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is
528 // test suite base name and DoBar is test base name.
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 // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information
536 // about a generator.
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; // Return value used only to run this method in namespace scope.
544 }
545 // UnitTest class invokes this method to register tests in this test suite
546 // right before running tests in RUN_ALL_TESTS macro.
547 // This method should not be called more than once on any single
548 // instance of a ParameterizedTestSuiteInfoBase derived class.
549 // UnitTest has a guard to prevent from calling this method more than once.
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, // No type parameter.
599 PrintToString(*param_it).c_str(), test_info->code_location,
600 GetTestSuiteTypeId(),
603 test_info->test_meta_factory->CreateTestFactory(*param_it));
604 } // for param_it
605 } // for gen_it
606 } // for test_it
607
608 if (!generated_instantiations) {
609 // There are no generaotrs, or they all generate nothing ...
610 InsertSyntheticTestCase(GetTestSuiteName(), code_location_,
611 !tests_.empty());
612 }
613 } // RegisterTests
614
615 private:
616 // LocalTestInfo structure keeps information about a single test registered
617 // with TEST_P macro.
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 // Records data received from INSTANTIATE_TEST_SUITE_P macros:
634 // <Instantiation name, Sequence generator creation function,
635 // Name generator function, Source file, Source line>
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 // Check for empty string
657 if (name.empty()) return false;
658
659 // Check for invalid characters
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
674 delete;
675}; // class ParameterizedTestSuiteInfo
676
677// Legacy API is deprecated but still available
678#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
679template <class TestCase>
681#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
682
683// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
684//
685// ParameterizedTestSuiteRegistry contains a map of
686// ParameterizedTestSuiteInfoBase classes accessed by test suite names. TEST_P
687// and INSTANTIATE_TEST_SUITE_P macros use it to locate their corresponding
688// ParameterizedTestSuiteInfo descriptors.
690 public:
693 for (auto& test_suite_info : test_suite_infos_) {
694 delete test_suite_info;
695 }
696 }
697
698 // Looks up or creates and returns a structure containing information about
699 // tests and instantiations of a particular test suite.
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 // Complain about incorrect usage of Google Test facilities
708 // and terminate the program since we cannot guaranty correct
709 // test suite setup and tear-down in this case.
710 ReportInvalidTestSuiteType(test_suite_name, code_location);
711 posix::Abort();
712 } else {
713 // At this point we are sure that the object we found is of the same
714 // type we are looking for, so we downcast it to that type
715 // without further checks.
716 typed_test_info = CheckedDowncastToActualType<
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// Legacy API is deprecated but still available
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 // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
743
744 private:
745 using TestSuiteInfoContainer = ::std::vector<ParameterizedTestSuiteInfoBase*>;
746
747 TestSuiteInfoContainer test_suite_infos_;
748
750 delete;
752 const ParameterizedTestSuiteRegistry&) = delete;
753};
754
755// Keep track of what type-parameterized test suite are defined and
756// where as well as which are intatiated. This allows susequently
757// identifying suits that are defined but never used.
759 public:
760 // Add a suite definition
761 void RegisterTestSuite(const char* test_suite_name,
762 CodeLocation code_location);
763
764 // Add an instantiation of a suit.
765 void RegisterInstantiation(const char* test_suite_name);
766
767 // For each suit repored as defined but not reported as instantiation,
768 // emit a test that reports that fact (configurably, as an error).
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} // namespace internal
784
785// Forward declarations of ValuesIn(), which is implemented in
786// include/gtest/gtest-param-test.h.
787template <class Container>
789 const Container& container);
790
791namespace internal {
792// Used in the Values() function to provide polymorphic capabilities.
793
794#ifdef _MSC_VER
795#pragma warning(push)
796#pragma warning(disable : 4100)
797#endif
798
799template <typename... Ts>
801 public:
802 explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {}
803
804 template <typename T>
805 operator ParamGenerator<T>() const { // NOLINT
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
822template <typename... T>
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 // Advance should not be called on beyond-of-range iterators
861 // so no component iterators must be beyond end of range, either.
862 void Advance() override {
863 assert(!AtEnd());
864 // Advance the last iterator.
865 ++std::get<sizeof...(T) - 1>(current_);
866 // if that reaches end, propagate that up.
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 // Having the same base generator guarantees that the other
878 // iterator is of the same type and we can downcast.
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 // We must report iterators equal if they both point beyond their
886 // respective ranges. That can happen in a variety of fashions,
887 // so we have to consult AtEnd().
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 // We are done. Nothing else to propagate.
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
939template <class... Gen>
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
953template <typename From, typename To>
955 public:
957 : generator_(std::move(gen)) {}
958
959 ParamIteratorInterface<To>* Begin() const override {
960 return new Iterator(this, generator_.begin(), generator_.end());
961 }
962 ParamIteratorInterface<To>* End() const override {
963 return new Iterator(this, generator_.end(), generator_.end());
964 }
965
966 private:
967 class Iterator : public ParamIteratorInterface<To> {
968 public:
969 Iterator(const ParamGeneratorInterface<To>* base, ParamIterator<From> it,
971 : base_(base), it_(it), end_(end) {
972 if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_));
973 }
974 ~Iterator() override {}
975
976 const ParamGeneratorInterface<To>* BaseGenerator() const override {
977 return base_;
978 }
979 void Advance() override {
980 ++it_;
981 if (it_ != end_) value_ = std::make_shared<To>(static_cast<To>(*it_));
982 }
983 ParamIteratorInterface<To>* Clone() const override {
984 return new Iterator(*this);
985 }
986 const To* Current() const override { return value_.get(); }
987 bool Equals(const ParamIteratorInterface<To>& other) const override {
988 // Having the same base generator guarantees that the other
989 // iterator is of the same type and we can downcast.
990 GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())
991 << "The program attempted to compare iterators "
992 << "from different generators." << std::endl;
993 const ParamIterator<From> other_it =
994 CheckedDowncastToActualType<const Iterator>(&other)->it_;
995 return it_ == other_it;
996 }
997
998 private:
999 Iterator(const Iterator& other) = default;
1000
1001 const ParamGeneratorInterface<To>* const base_;
1004 std::shared_ptr<To> value_;
1005 }; // class ParamGeneratorConverter::Iterator
1006
1007 ParamGenerator<From> generator_;
1008}; // class ParamGeneratorConverter
1009
1010template <class Gen>
1012 public:
1014 : generator_(std::move(g)) {}
1015
1016 template <typename T>
1017 operator ParamGenerator<T>() const { // NOLINT
1018 return ParamGenerator<T>(new ParamGeneratorConverter<Gen, T>(generator_));
1019 }
1020
1021 private:
1022 ParamGenerator<Gen> generator_;
1023};
1024
1025} // namespace internal
1026} // namespace testing
1027
1028#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_
Definition gtest-message.h:92
Definition gtest.h:242
Definition gtest-param-util.h:824
Definition gtest-param-util.h:940
Definition gtest-internal.h:1279
Definition gtest-param-util.h:1011
Definition gtest-param-util.h:954
Definition gtest-param-util.h:184
Definition gtest-param-util.h:167
Definition gtest-param-util.h:125
Definition gtest-param-util.h:95
Definition gtest-param-util.h:396
Definition gtest-param-util.h:460
Definition gtest-param-util.h:503
Definition gtest-param-util.h:689
Definition gtest-param-util.h:208
Definition gtest-internal.h:449
Definition gtest-param-util.h:418
Definition gtest-param-util.h:435
Definition googletest-port-test.cc:194
Definition gtest-param-util.h:800
Definition gtest-param-util.h:294
Definition gtest-param-util.h:68
Definition gtest-param-util.h:59
Definition gtest-internal.h:490
Definition gtest-internal.h:1215
Definition gtest-internal.h:1159
Definition gtest-param-util.h:488
Definition gtest-internal.h:513