| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 55 | namespace testing { | ||
| 56 | // Input to a parameterized test name generator, describing a test parameter. | ||
| 57 | // Consists of the parameter value and the integer parameter index. | ||
| 58 | template <class ParamType> | ||
| 59 | struct TestParamInfo { | ||
| 60 | 32 | TestParamInfo(const ParamType& a_param, size_t an_index) | |
| 61 | 32 | : 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. | ||
| 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 | // 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. | ||
| 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 | // Interface for iterating over elements provided by an implementation | ||
| 93 | // of ParamGeneratorInterface<T>. | ||
| 94 | template <typename T> | ||
| 95 | class ParamIteratorInterface { | ||
| 96 | public: | ||
| 97 | 58 | 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. | ||
| 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 | // 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 | 96 | const T& operator*() const { return *impl_->Current(); } | |
| 139 | const T* operator->() const { return impl_->Current(); } | ||
| 140 | // Prefix version of operator++. | ||
| 141 | 32 | ParamIterator& operator++() { | |
| 142 | 32 | impl_->Advance(); | |
| 143 | 32 | 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 | 41 | bool operator==(const ParamIterator& other) const { | |
| 152 |
3/4✓ Branch 8 taken 24 times.
✗ Branch 9 not taken.
✓ Branch 26 taken 5 times.
✓ Branch 27 taken 19 times.
|
41 | return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_); |
| 153 | } | ||
| 154 | 41 | bool operator!=(const ParamIterator& other) const { | |
| 155 | 41 | return !(*this == other); | |
| 156 | } | ||
| 157 | |||
| 158 | private: | ||
| 159 | friend class ParamGenerator<T>; | ||
| 160 | 50 | 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. | ||
| 166 | template <typename T> | ||
| 167 | class ParamGeneratorInterface { | ||
| 168 | public: | ||
| 169 | typedef T ParamType; | ||
| 170 | |||
| 171 | 10 | 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. | ||
| 183 | template <typename T> | ||
| 184 | class ParamGenerator { | ||
| 185 | public: | ||
| 186 | typedef ParamIterator<T> iterator; | ||
| 187 | |||
| 188 | 9 | 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 | 9 | iterator begin() const { return iterator(impl_->Begin()); } | |
| 197 | 41 | 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. | ||
| 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 | // 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) | ||
| 256 | : ParamIteratorInterface<T>(), | ||
| 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. | ||
| 293 | template <typename T> | ||
| 294 | class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> { | ||
| 295 | public: | ||
| 296 | template <typename ForwardIterator> | ||
| 297 | 9 | ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end) | |
| 298 |
1/1✓ Branch 10 taken 5 times.
|
27 | : container_(begin, end) {} |
| 299 | 20 | ~ValuesInIteratorRangeGenerator() override {} | |
| 300 | |||
| 301 | 9 | ParamIteratorInterface<T>* Begin() const override { | |
| 302 | 9 | return new Iterator(this, container_.begin()); | |
| 303 | } | ||
| 304 | 41 | ParamIteratorInterface<T>* End() const override { | |
| 305 | 41 | 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 | 50 | Iterator(const ParamGeneratorInterface<T>* base, | |
| 314 | typename ContainerType::const_iterator iterator) | ||
| 315 | 50 | : base_(base), iterator_(iterator) {} | |
| 316 | 116 | ~Iterator() override {} | |
| 317 | |||
| 318 | 82 | const ParamGeneratorInterface<T>* BaseGenerator() const override { | |
| 319 | 82 | return base_; | |
| 320 | } | ||
| 321 | 32 | void Advance() override { | |
| 322 | 32 | ++iterator_; | |
| 323 | 32 | value_.reset(); | |
| 324 | 32 | } | |
| 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 | 96 | const T* Current() const override { | |
| 336 |
3/5✓ Branch 4 taken 19 times.
✓ Branch 5 taken 38 times.
✓ Branch 19 taken 3 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
96 | if (value_.get() == nullptr) value_.reset(new T(*iterator_)); |
| 337 | 96 | return value_.get(); | |
| 338 | } | ||
| 339 | 41 | 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 |
1/3✗ Branch 15 not taken.
✓ Branch 16 taken 24 times.
✗ Branch 19 not taken.
|
41 | GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) |
| 343 | << "The program attempted to compare iterators " | ||
| 344 | ✗ | << "from different generators." << std::endl; | |
| 345 | 41 | return iterator_ == | |
| 346 | 82 | 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. | ||
| 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 | // 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. | ||
| 377 | template <class ParamType> | ||
| 378 | 32 | std::string DefaultParamName(const TestParamInfo<ParamType>& info) { | |
| 379 |
1/1✓ Branch 2 taken 19 times.
|
32 | Message name_stream; |
| 380 |
1/1✓ Branch 6 taken 19 times.
|
32 | name_stream << info.index; |
| 381 |
1/1✓ Branch 2 taken 19 times.
|
64 | return name_stream.GetString(); |
| 382 | 32 | } | |
| 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 | // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | ||
| 392 | // | ||
| 393 | // Stores a parameter value and later creates tests parameterized with that | ||
| 394 | // value. | ||
| 395 | template <class TestClass> | ||
| 396 | class ParameterizedTestFactory : public TestFactoryBase { | ||
| 397 | public: | ||
| 398 | typedef typename TestClass::ParamType ParamType; | ||
| 399 | 32 | explicit ParameterizedTestFactory(ParamType parameter) | |
| 400 |
1/1✓ Branch 9 taken 3 times.
|
32 | : parameter_(parameter) {} |
| 401 | 32 | Test* CreateTest() override { | |
| 402 | 32 | TestClass::SetParam(¶meter_); | |
| 403 |
1/3✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
32 | 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 | // 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. | ||
| 417 | template <class ParamType> | ||
| 418 | class TestMetaFactoryBase { | ||
| 419 | public: | ||
| 420 | 10 | 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. | ||
| 433 | template <class TestSuite> | ||
| 434 | class TestMetaFactory | ||
| 435 | : public TestMetaFactoryBase<typename TestSuite::ParamType> { | ||
| 436 | public: | ||
| 437 | using ParamType = typename TestSuite::ParamType; | ||
| 438 | |||
| 439 | 9 | TestMetaFactory() {} | |
| 440 | |||
| 441 | 32 | TestFactoryBase* CreateTestFactory(ParamType parameter) override { | |
| 442 |
2/4✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
32 | 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. | ||
| 460 | class ParameterizedTestSuiteInfoBase { | ||
| 461 | public: | ||
| 462 | 10 | virtual ~ParameterizedTestSuiteInfoBase() {} | |
| 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: | ||
| 475 | 5 | ParameterizedTestSuiteInfoBase() {} | |
| 476 | |||
| 477 | private: | ||
| 478 | ParameterizedTestSuiteInfoBase(const ParameterizedTestSuiteInfoBase&) = | ||
| 479 | delete; | ||
| 480 | ParameterizedTestSuiteInfoBase& operator=( | ||
| 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. | ||
| 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 | // 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. | ||
| 502 | template <class TestSuite> | ||
| 503 | class ParameterizedTestSuiteInfo : public ParameterizedTestSuiteInfoBase { | ||
| 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 | 9 | explicit ParameterizedTestSuiteInfo(const char* name, | |
| 514 | CodeLocation code_location) | ||
| 515 |
2/2✓ Branch 10 taken 5 times.
✓ Branch 17 taken 5 times.
|
27 | : test_suite_name_(name), code_location_(code_location) {} |
| 516 | |||
| 517 | // Test suite base name for display purposes. | ||
| 518 | 25 | const std::string& GetTestSuiteName() const override { | |
| 519 | 25 | return test_suite_name_; | |
| 520 | } | ||
| 521 | // Test suite id to verify identity. | ||
| 522 | 41 | 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 | 9 | void AddTestPattern(const char* test_suite_name, const char* test_base_name, | |
| 530 | TestMetaFactoryBase<ParamType>* meta_factory, | ||
| 531 | CodeLocation code_location) { | ||
| 532 |
5/7✓ Branch 5 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 13 taken 5 times.
✓ Branch 16 taken 5 times.
✓ Branch 20 taken 5 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
|
9 | tests_.push_back(std::shared_ptr<TestInfo>(new TestInfo( |
| 533 | test_suite_name, test_base_name, meta_factory, code_location))); | ||
| 534 | 9 | } | |
| 535 | // INSTANTIATE_TEST_SUITE_P macro uses AddGenerator() to record information | ||
| 536 | // about a generator. | ||
| 537 | 9 | int AddTestSuiteInstantiation(const std::string& instantiation_name, | |
| 538 | GeneratorCreationFunc* func, | ||
| 539 | ParamNameGeneratorFunc* name_func, | ||
| 540 | const char* file, int line) { | ||
| 541 |
1/1✓ Branch 4 taken 5 times.
|
18 | instantiations_.push_back( |
| 542 |
1/1✓ Branch 2 taken 5 times.
|
18 | InstantiationInfo(instantiation_name, func, name_func, file, line)); |
| 543 | 9 | 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 | 9 | void RegisterTests() override { | |
| 551 | 9 | bool generated_instantiations = false; | |
| 552 | |||
| 553 | 18 | for (typename TestInfoContainer::iterator test_it = tests_.begin(); | |
| 554 |
2/2✓ Branch 9 taken 5 times.
✓ Branch 10 taken 5 times.
|
18 | test_it != tests_.end(); ++test_it) { |
| 555 | 9 | std::shared_ptr<TestInfo> test_info = *test_it; | |
| 556 | 27 | for (typename InstantiationContainer::iterator gen_it = | |
| 557 | 9 | instantiations_.begin(); | |
| 558 |
2/2✓ Branch 9 taken 5 times.
✓ Branch 10 taken 5 times.
|
18 | gen_it != instantiations_.end(); ++gen_it) { |
| 559 | 9 | const std::string& instantiation_name = gen_it->name; | |
| 560 |
1/1✓ Branch 5 taken 5 times.
|
9 | ParamGenerator<ParamType> generator((*gen_it->generator)()); |
| 561 | 9 | ParamNameGeneratorFunc* name_func = gen_it->name_func; | |
| 562 | 9 | const char* file = gen_it->file; | |
| 563 | 9 | int line = gen_it->line; | |
| 564 | |||
| 565 | 9 | std::string test_suite_name; | |
| 566 |
1/2✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
9 | if (!instantiation_name.empty()) |
| 567 |
1/1✓ Branch 3 taken 5 times.
|
9 | test_suite_name = instantiation_name + "/"; |
| 568 |
1/1✓ Branch 4 taken 5 times.
|
9 | test_suite_name += test_info->test_suite_base_name; |
| 569 | |||
| 570 | 9 | size_t i = 0; | |
| 571 | 9 | std::set<std::string> test_param_names; | |
| 572 |
1/1✓ Branch 2 taken 5 times.
|
82 | for (typename ParamGenerator<ParamType>::iterator param_it = |
| 573 | generator.begin(); | ||
| 574 |
5/5✓ Branch 1 taken 19 times.
✓ Branch 5 taken 24 times.
✓ Branch 9 taken 24 times.
✓ Branch 13 taken 19 times.
✓ Branch 14 taken 5 times.
|
73 | param_it != generator.end(); ++param_it, ++i) { |
| 575 | 32 | generated_instantiations = true; | |
| 576 | |||
| 577 |
1/1✓ Branch 2 taken 19 times.
|
32 | Message test_name_stream; |
| 578 | |||
| 579 |
1/1✓ Branch 2 taken 19 times.
|
64 | std::string param_name = |
| 580 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 5 taken 3 times.
|
64 | name_func(TestParamInfo<ParamType>(*param_it, i)); |
| 581 | |||
| 582 |
2/4✓ Branch 2 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
✗ Branch 8 not taken.
|
32 | GTEST_CHECK_(IsValidParamName(param_name)) |
| 583 | << "Parameterized test name '" << param_name | ||
| 584 | ✗ | << "' is invalid, in " << file << " line " << line << std::endl; | |
| 585 | |||
| 586 |
3/5✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 10 not taken.
|
32 | 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 |
1/1✓ Branch 1 taken 19 times.
|
32 | test_param_names.insert(param_name); |
| 591 | |||
| 592 |
1/2✓ Branch 5 taken 19 times.
✗ Branch 6 not taken.
|
32 | if (!test_info->test_base_name.empty()) { |
| 593 |
2/2✓ Branch 5 taken 19 times.
✓ Branch 9 taken 19 times.
|
32 | test_name_stream << test_info->test_base_name << "/"; |
| 594 | } | ||
| 595 |
1/1✓ Branch 1 taken 19 times.
|
32 | test_name_stream << param_name; |
| 596 |
8/8✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✓ Branch 7 taken 16 times.
✓ Branch 10 taken 16 times.
✓ Branch 14 taken 19 times.
✓ Branch 17 taken 3 times.
✓ Branch 20 taken 16 times.
✓ Branch 27 taken 3 times.
|
192 | MakeAndRegisterTestInfo( |
| 597 |
1/1✓ Branch 2 taken 19 times.
|
64 | test_suite_name.c_str(), test_name_stream.GetString().c_str(), |
| 598 | nullptr, // No type parameter. | ||
| 599 |
2/2✓ Branch 6 taken 19 times.
✓ Branch 9 taken 19 times.
|
96 | PrintToString(*param_it).c_str(), test_info->code_location, |
| 600 | 32 | GetTestSuiteTypeId(), | |
| 601 | SuiteApiResolver<TestSuite>::GetSetUpCaseOrSuite(file, line), | ||
| 602 | SuiteApiResolver<TestSuite>::GetTearDownCaseOrSuite(file, line), | ||
| 603 |
1/1✓ Branch 12 taken 16 times.
|
32 | test_info->test_meta_factory->CreateTestFactory(*param_it)); |
| 604 | } // for param_it | ||
| 605 | } // for gen_it | ||
| 606 | } // for test_it | ||
| 607 | |||
| 608 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
9 | if (!generated_instantiations) { |
| 609 | // There are no generaotrs, or they all generate nothing ... | ||
| 610 | ✗ | InsertSyntheticTestCase(GetTestSuiteName(), code_location_, | |
| 611 | ✗ | !tests_.empty()); | |
| 612 | } | ||
| 613 | 9 | } // RegisterTests | |
| 614 | |||
| 615 | private: | ||
| 616 | // LocalTestInfo structure keeps information about a single test registered | ||
| 617 | // with TEST_P macro. | ||
| 618 | struct TestInfo { | ||
| 619 | 9 | 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 |
1/1✓ Branch 4 taken 5 times.
|
27 | : test_suite_base_name(a_test_suite_base_name), |
| 623 |
1/1✓ Branch 5 taken 5 times.
|
27 | test_base_name(a_test_base_name), |
| 624 | 9 | test_meta_factory(a_test_meta_factory), | |
| 625 |
1/1✓ Branch 4 taken 5 times.
|
9 | 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 | 9 | InstantiationInfo(const std::string& name_in, | |
| 638 | GeneratorCreationFunc* generator_in, | ||
| 639 | ParamNameGeneratorFunc* name_func_in, const char* file_in, | ||
| 640 | int line_in) | ||
| 641 | 9 | : name(name_in), | |
| 642 | 9 | generator(generator_in), | |
| 643 | 9 | name_func(name_func_in), | |
| 644 | 9 | file(file_in), | |
| 645 | 9 | 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 | 32 | static bool IsValidParamName(const std::string& name) { | |
| 656 | // Check for empty string | ||
| 657 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
|
32 | if (name.empty()) return false; |
| 658 | |||
| 659 | // Check for invalid characters | ||
| 660 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 3 taken 19 times.
|
64 | for (std::string::size_type index = 0; index < name.size(); ++index) { |
| 661 |
2/6✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 19 times.
|
32 | if (!IsAlNum(name[index]) && name[index] != '_') return false; |
| 662 | } | ||
| 663 | |||
| 664 | 32 | 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 | }; // class ParameterizedTestSuiteInfo | ||
| 676 | |||
| 677 | // Legacy API is deprecated but still available | ||
| 678 | #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ | ||
| 679 | template <class TestCase> | ||
| 680 | using ParameterizedTestCaseInfo = ParameterizedTestSuiteInfo<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. | ||
| 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 | // 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 | 18 | ParameterizedTestSuiteInfo<TestSuite>* GetTestSuitePatternHolder( | |
| 702 | const char* test_suite_name, CodeLocation code_location) { | ||
| 703 | 18 | ParameterizedTestSuiteInfo<TestSuite>* typed_test_info = nullptr; | |
| 704 |
2/2✓ Branch 12 taken 13 times.
✓ Branch 13 taken 5 times.
|
30 | for (auto& test_suite_info : test_suite_infos_) { |
| 705 |
3/3✓ Branch 11 taken 13 times.
✓ Branch 15 taken 5 times.
✓ Branch 16 taken 8 times.
|
21 | if (test_suite_info->GetTestSuiteName() == test_suite_name) { |
| 706 |
2/3✓ Branch 11 taken 5 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 5 times.
|
9 | 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< | ||
| 717 |
1/1✓ Branch 2 taken 5 times.
|
9 | ParameterizedTestSuiteInfo<TestSuite>>(test_suite_info); |
| 718 | } | ||
| 719 | 9 | break; | |
| 720 | } | ||
| 721 | } | ||
| 722 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
18 | if (typed_test_info == nullptr) { |
| 723 |
2/4✓ Branch 4 taken 5 times.
✓ Branch 7 taken 5 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
9 | typed_test_info = new ParameterizedTestSuiteInfo<TestSuite>( |
| 724 | test_suite_name, code_location); | ||
| 725 |
1/1✓ Branch 5 taken 5 times.
|
9 | test_suite_infos_.push_back(typed_test_info); |
| 726 | } | ||
| 727 | 18 | 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 | |||
| 749 | ParameterizedTestSuiteRegistry(const ParameterizedTestSuiteRegistry&) = | ||
| 750 | delete; | ||
| 751 | ParameterizedTestSuiteRegistry& operator=( | ||
| 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. | ||
| 758 | class TypeParameterizedTestSuiteRegistry { | ||
| 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. | ||
| 787 | template <class Container> | ||
| 788 | internal::ParamGenerator<typename Container::value_type> ValuesIn( | ||
| 789 | const Container& container); | ||
| 790 | |||
| 791 | namespace 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 | |||
| 799 | template <typename... Ts> | ||
| 800 | class ValueArray { | ||
| 801 | public: | ||
| 802 | 9 | explicit ValueArray(Ts... v) : v_(FlatTupleConstructTag{}, std::move(v)...) {} | |
| 803 | |||
| 804 | template <typename T> | ||
| 805 | 9 | operator ParamGenerator<T>() const { // NOLINT | |
| 806 |
2/2✓ Branch 2 taken 5 times.
✓ Branch 7 taken 5 times.
|
9 | return ValuesIn(MakeVector<T>(MakeIndexSequence<sizeof...(Ts)>())); |
| 807 | } | ||
| 808 | |||
| 809 | private: | ||
| 810 | template <typename T, size_t... I> | ||
| 811 | 7 | std::vector<T> MakeVector(IndexSequence<I...>) const { | |
| 812 |
5/7✓ Branch 11 taken 1 times.
✓ Branch 15 taken 3 times.
✓ Branch 16 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✓ Branch 27 taken 1 times.
✓ Branch 39 taken 1 times.
|
33 | return std::vector<T>{static_cast<T>(v_.template Get<I>())...}; |
| 813 | 8 | } | |
| 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 | // 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 | |||
| 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 | } // namespace internal | ||
| 954 | } // namespace testing | ||
| 955 | |||
| 956 | #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ | ||
| 957 |