vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 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 | // |
| 32 | // Tests for Google Test itself. This file verifies that the parameter |
| 33 | // generators objects produce correct parameter sequences and that |
| 34 | // Google Test runtime instantiates correct tests from those sequences. |
| 35 | |
| 36 | #include <gtest/gtest.h> |
| 37 | |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 38 | #if GTEST_HAS_PARAM_TEST |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 39 | |
| 40 | #include <algorithm> |
| 41 | #include <iostream> |
| 42 | #include <list> |
| 43 | #include <vector> |
| 44 | |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 45 | // To include gtest-internal-inl.h. |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 46 | #define GTEST_IMPLEMENTATION_ 1 |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 47 | #include "src/gtest-internal-inl.h" // for UnitTestOptions |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 48 | #undef GTEST_IMPLEMENTATION_ |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 49 | |
| 50 | #include "test/gtest-param-test_test.h" |
| 51 | |
| 52 | using ::std::vector; |
| 53 | using ::std::sort; |
| 54 | |
| 55 | using ::testing::AddGlobalTestEnvironment; |
| 56 | using ::testing::Bool; |
| 57 | using ::testing::Message; |
| 58 | using ::testing::Range; |
| 59 | using ::testing::TestWithParam; |
| 60 | using ::testing::Values; |
| 61 | using ::testing::ValuesIn; |
| 62 | |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 63 | #if GTEST_HAS_COMBINE |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 64 | using ::testing::Combine; |
| 65 | using ::std::tr1::get; |
| 66 | using ::std::tr1::make_tuple; |
| 67 | using ::std::tr1::tuple; |
| 68 | #endif // GTEST_HAS_COMBINE |
| 69 | |
| 70 | using ::testing::internal::ParamGenerator; |
| 71 | using ::testing::internal::UnitTestOptions; |
| 72 | |
| 73 | // Verifies that a sequence generated by the generator and accessed |
| 74 | // via the iterator object matches the expected one using Google Test |
| 75 | // assertions. |
| 76 | template <typename T, size_t N> |
| 77 | void VerifyGenerator(const ParamGenerator<T>& generator, |
| 78 | const T (&expected_values)[N]) { |
| 79 | typename ParamGenerator<T>::iterator it = generator.begin(); |
| 80 | for (size_t i = 0; i < N; ++i) { |
| 81 | ASSERT_FALSE(it == generator.end()) |
| 82 | << "At element " << i << " when accessing via an iterator " |
| 83 | << "created with the copy constructor." << std::endl; |
| 84 | EXPECT_EQ(expected_values[i], *it) |
| 85 | << "At element " << i << " when accessing via an iterator " |
| 86 | << "created with the copy constructor." << std::endl; |
| 87 | it++; |
| 88 | } |
| 89 | EXPECT_TRUE(it == generator.end()) |
| 90 | << "At the presumed end of sequence when accessing via an iterator " |
| 91 | << "created with the copy constructor." << std::endl; |
| 92 | |
| 93 | // Test the iterator assignment. The following lines verify that |
| 94 | // the sequence accessed via an iterator initialized via the |
| 95 | // assignment operator (as opposed to a copy constructor) matches |
| 96 | // just the same. |
| 97 | it = generator.begin(); |
| 98 | for (size_t i = 0; i < N; ++i) { |
| 99 | ASSERT_FALSE(it == generator.end()) |
| 100 | << "At element " << i << " when accessing via an iterator " |
| 101 | << "created with the assignment operator." << std::endl; |
| 102 | EXPECT_EQ(expected_values[i], *it) |
| 103 | << "At element " << i << " when accessing via an iterator " |
| 104 | << "created with the assignment operator." << std::endl; |
| 105 | it++; |
| 106 | } |
| 107 | EXPECT_TRUE(it == generator.end()) |
| 108 | << "At the presumed end of sequence when accessing via an iterator " |
| 109 | << "created with the assignment operator." << std::endl; |
| 110 | } |
| 111 | |
| 112 | template <typename T> |
| 113 | void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) { |
| 114 | typename ParamGenerator<T>::iterator it = generator.begin(); |
| 115 | EXPECT_TRUE(it == generator.end()); |
| 116 | |
| 117 | it = generator.begin(); |
| 118 | EXPECT_TRUE(it == generator.end()); |
| 119 | } |
| 120 | |
| 121 | // Generator tests. They test that each of the provided generator functions |
| 122 | // generates an expected sequence of values. The general test pattern |
| 123 | // instantiates a generator using one of the generator functions, |
| 124 | // checks the sequence produced by the generator using its iterator API, |
| 125 | // and then resets the iterator back to the beginning of the sequence |
| 126 | // and checks the sequence again. |
| 127 | |
| 128 | // Tests that iterators produced by generator functions conform to the |
| 129 | // ForwardIterator concept. |
| 130 | TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) { |
| 131 | const ParamGenerator<int> gen = Range(0, 10); |
| 132 | ParamGenerator<int>::iterator it = gen.begin(); |
| 133 | |
| 134 | // Verifies that iterator initialization works as expected. |
| 135 | ParamGenerator<int>::iterator it2 = it; |
| 136 | EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the " |
| 137 | << "element same as its source points to"; |
| 138 | |
| 139 | // Verifies that iterator assignment works as expected. |
| 140 | it++; |
| 141 | EXPECT_FALSE(*it == *it2); |
| 142 | it2 = it; |
| 143 | EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the " |
| 144 | << "element same as its source points to"; |
| 145 | |
| 146 | // Verifies that prefix operator++() returns *this. |
| 147 | EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be " |
| 148 | << "refer to the original object"; |
| 149 | |
| 150 | // Verifies that the result of the postfix operator++ points to the value |
| 151 | // pointed to by the original iterator. |
| 152 | int original_value = *it; // Have to compute it outside of macro call to be |
| 153 | // unaffected by the parameter evaluation order. |
| 154 | EXPECT_EQ(original_value, *(it++)); |
| 155 | |
| 156 | // Verifies that prefix and postfix operator++() advance an iterator |
| 157 | // all the same. |
| 158 | it2 = it; |
| 159 | it++; |
| 160 | ++it2; |
| 161 | EXPECT_TRUE(*it == *it2); |
| 162 | } |
| 163 | |
| 164 | // Tests that Range() generates the expected sequence. |
| 165 | TEST(RangeTest, IntRangeWithDefaultStep) { |
| 166 | const ParamGenerator<int> gen = Range(0, 3); |
| 167 | const int expected_values[] = {0, 1, 2}; |
| 168 | VerifyGenerator(gen, expected_values); |
| 169 | } |
| 170 | |
| 171 | // Edge case. Tests that Range() generates the single element sequence |
| 172 | // as expected when provided with range limits that are equal. |
| 173 | TEST(RangeTest, IntRangeSingleValue) { |
| 174 | const ParamGenerator<int> gen = Range(0, 1); |
| 175 | const int expected_values[] = {0}; |
| 176 | VerifyGenerator(gen, expected_values); |
| 177 | } |
| 178 | |
| 179 | // Edge case. Tests that Range() with generates empty sequence when |
| 180 | // supplied with an empty range. |
| 181 | TEST(RangeTest, IntRangeEmpty) { |
| 182 | const ParamGenerator<int> gen = Range(0, 0); |
| 183 | VerifyGeneratorIsEmpty(gen); |
| 184 | } |
| 185 | |
| 186 | // Tests that Range() with custom step (greater then one) generates |
| 187 | // the expected sequence. |
| 188 | TEST(RangeTest, IntRangeWithCustomStep) { |
| 189 | const ParamGenerator<int> gen = Range(0, 9, 3); |
| 190 | const int expected_values[] = {0, 3, 6}; |
| 191 | VerifyGenerator(gen, expected_values); |
| 192 | } |
| 193 | |
| 194 | // Tests that Range() with custom step (greater then one) generates |
| 195 | // the expected sequence when the last element does not fall on the |
| 196 | // upper range limit. Sequences generated by Range() must not have |
| 197 | // elements beyond the range limits. |
| 198 | TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) { |
| 199 | const ParamGenerator<int> gen = Range(0, 4, 3); |
| 200 | const int expected_values[] = {0, 3}; |
| 201 | VerifyGenerator(gen, expected_values); |
| 202 | } |
| 203 | |
| 204 | // Verifies that Range works with user-defined types that define |
| 205 | // copy constructor, operator=(), operator+(), and operator<(). |
| 206 | class DogAdder { |
| 207 | public: |
| 208 | explicit DogAdder(const char* value) : value_(value) {} |
| 209 | DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} |
| 210 | |
| 211 | DogAdder operator=(const DogAdder& other) { |
| 212 | if (this != &other) |
| 213 | value_ = other.value_; |
| 214 | return *this; |
| 215 | } |
| 216 | DogAdder operator+(const DogAdder& other) const { |
| 217 | Message msg; |
| 218 | msg << value_.c_str() << other.value_.c_str(); |
| 219 | return DogAdder(msg.GetString().c_str()); |
| 220 | } |
| 221 | bool operator<(const DogAdder& other) const { |
| 222 | return value_ < other.value_; |
| 223 | } |
| 224 | const ::testing::internal::String& value() const { return value_; } |
| 225 | |
| 226 | private: |
| 227 | ::testing::internal::String value_; |
| 228 | }; |
| 229 | |
| 230 | TEST(RangeTest, WorksWithACustomType) { |
| 231 | const ParamGenerator<DogAdder> gen = |
| 232 | Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog")); |
| 233 | ParamGenerator<DogAdder>::iterator it = gen.begin(); |
| 234 | |
| 235 | ASSERT_FALSE(it == gen.end()); |
| 236 | EXPECT_STREQ("cat", it->value().c_str()); |
| 237 | |
| 238 | ASSERT_FALSE(++it == gen.end()); |
| 239 | EXPECT_STREQ("catdog", it->value().c_str()); |
| 240 | |
| 241 | EXPECT_TRUE(++it == gen.end()); |
| 242 | } |
| 243 | |
| 244 | class IntWrapper { |
| 245 | public: |
| 246 | explicit IntWrapper(int value) : value_(value) {} |
| 247 | IntWrapper(const IntWrapper& other) : value_(other.value_) {} |
| 248 | |
| 249 | IntWrapper operator=(const IntWrapper& other) { |
| 250 | value_ = other.value_; |
| 251 | return *this; |
| 252 | } |
| 253 | // operator+() adds a different type. |
| 254 | IntWrapper operator+(int other) const { return IntWrapper(value_ + other); } |
| 255 | bool operator<(const IntWrapper& other) const { |
| 256 | return value_ < other.value_; |
| 257 | } |
| 258 | int value() const { return value_; } |
| 259 | |
| 260 | private: |
| 261 | int value_; |
| 262 | }; |
| 263 | |
| 264 | TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) { |
| 265 | const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2)); |
| 266 | ParamGenerator<IntWrapper>::iterator it = gen.begin(); |
| 267 | |
| 268 | ASSERT_FALSE(it == gen.end()); |
| 269 | EXPECT_EQ(0, it->value()); |
| 270 | |
| 271 | ASSERT_FALSE(++it == gen.end()); |
| 272 | EXPECT_EQ(1, it->value()); |
| 273 | |
| 274 | EXPECT_TRUE(++it == gen.end()); |
| 275 | } |
| 276 | |
| 277 | // Tests that ValuesIn() with an array parameter generates |
| 278 | // the expected sequence. |
| 279 | TEST(ValuesInTest, ValuesInArray) { |
| 280 | int array[] = {3, 5, 8}; |
| 281 | const ParamGenerator<int> gen = ValuesIn(array); |
| 282 | VerifyGenerator(gen, array); |
| 283 | } |
| 284 | |
| 285 | // Tests that ValuesIn() with a const array parameter generates |
| 286 | // the expected sequence. |
| 287 | TEST(ValuesInTest, ValuesInConstArray) { |
| 288 | const int array[] = {3, 5, 8}; |
| 289 | const ParamGenerator<int> gen = ValuesIn(array); |
| 290 | VerifyGenerator(gen, array); |
| 291 | } |
| 292 | |
| 293 | // Edge case. Tests that ValuesIn() with an array parameter containing a |
| 294 | // single element generates the single element sequence. |
| 295 | TEST(ValuesInTest, ValuesInSingleElementArray) { |
| 296 | int array[] = {42}; |
| 297 | const ParamGenerator<int> gen = ValuesIn(array); |
| 298 | VerifyGenerator(gen, array); |
| 299 | } |
| 300 | |
| 301 | // Tests that ValuesIn() generates the expected sequence for an STL |
| 302 | // container (vector). |
| 303 | TEST(ValuesInTest, ValuesInVector) { |
| 304 | typedef ::std::vector<int> ContainerType; |
| 305 | ContainerType values; |
| 306 | values.push_back(3); |
| 307 | values.push_back(5); |
| 308 | values.push_back(8); |
| 309 | const ParamGenerator<int> gen = ValuesIn(values); |
| 310 | |
| 311 | const int expected_values[] = {3, 5, 8}; |
| 312 | VerifyGenerator(gen, expected_values); |
| 313 | } |
| 314 | |
| 315 | // Tests that ValuesIn() generates the expected sequence. |
| 316 | TEST(ValuesInTest, ValuesInIteratorRange) { |
| 317 | typedef ::std::vector<int> ContainerType; |
| 318 | ContainerType values; |
| 319 | values.push_back(3); |
| 320 | values.push_back(5); |
| 321 | values.push_back(8); |
| 322 | const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end()); |
| 323 | |
| 324 | const int expected_values[] = {3, 5, 8}; |
| 325 | VerifyGenerator(gen, expected_values); |
| 326 | } |
| 327 | |
| 328 | // Edge case. Tests that ValuesIn() provided with an iterator range specifying a |
| 329 | // single value generates a single-element sequence. |
| 330 | TEST(ValuesInTest, ValuesInSingleElementIteratorRange) { |
| 331 | typedef ::std::vector<int> ContainerType; |
| 332 | ContainerType values; |
| 333 | values.push_back(42); |
| 334 | const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end()); |
| 335 | |
| 336 | const int expected_values[] = {42}; |
| 337 | VerifyGenerator(gen, expected_values); |
| 338 | } |
| 339 | |
| 340 | // Edge case. Tests that ValuesIn() provided with an empty iterator range |
| 341 | // generates an empty sequence. |
| 342 | TEST(ValuesInTest, ValuesInEmptyIteratorRange) { |
| 343 | typedef ::std::vector<int> ContainerType; |
| 344 | ContainerType values; |
| 345 | const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end()); |
| 346 | |
| 347 | VerifyGeneratorIsEmpty(gen); |
| 348 | } |
| 349 | |
| 350 | // Tests that the Values() generates the expected sequence. |
| 351 | TEST(ValuesTest, ValuesWorks) { |
| 352 | const ParamGenerator<int> gen = Values(3, 5, 8); |
| 353 | |
| 354 | const int expected_values[] = {3, 5, 8}; |
| 355 | VerifyGenerator(gen, expected_values); |
| 356 | } |
| 357 | |
| 358 | // Tests that Values() generates the expected sequences from elements of |
| 359 | // different types convertible to ParamGenerator's parameter type. |
| 360 | TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) { |
| 361 | const ParamGenerator<double> gen = Values(3, 5.0f, 8.0); |
| 362 | |
| 363 | const double expected_values[] = {3.0, 5.0, 8.0}; |
| 364 | VerifyGenerator(gen, expected_values); |
| 365 | } |
| 366 | |
| 367 | TEST(ValuesTest, ValuesWorksForMaxLengthList) { |
| 368 | const ParamGenerator<int> gen = Values( |
| 369 | 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, |
| 370 | 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, |
| 371 | 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, |
| 372 | 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, |
| 373 | 410, 420, 430, 440, 450, 460, 470, 480, 490, 500); |
| 374 | |
| 375 | const int expected_values[] = { |
| 376 | 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, |
| 377 | 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, |
| 378 | 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, |
| 379 | 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, |
| 380 | 410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; |
| 381 | VerifyGenerator(gen, expected_values); |
| 382 | } |
| 383 | |
| 384 | // Edge case test. Tests that single-parameter Values() generates the sequence |
| 385 | // with the single value. |
| 386 | TEST(ValuesTest, ValuesWithSingleParameter) { |
| 387 | const ParamGenerator<int> gen = Values(42); |
| 388 | |
| 389 | const int expected_values[] = {42}; |
| 390 | VerifyGenerator(gen, expected_values); |
| 391 | } |
| 392 | |
| 393 | // Tests that Bool() generates sequence (false, true). |
| 394 | TEST(BoolTest, BoolWorks) { |
| 395 | const ParamGenerator<bool> gen = Bool(); |
| 396 | |
| 397 | const bool expected_values[] = {false, true}; |
| 398 | VerifyGenerator(gen, expected_values); |
| 399 | } |
| 400 | |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 401 | #if GTEST_HAS_COMBINE |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 402 | |
| 403 | template <typename T1, typename T2> |
| 404 | ::std::ostream& operator<<(::std::ostream& stream, const tuple<T1, T2>& value) { |
| 405 | stream << "(" << get<0>(value) << ", " << get<1>(value) << ")"; |
| 406 | return stream; |
| 407 | } |
| 408 | |
| 409 | template <typename T1, typename T2, typename T3> |
| 410 | ::std::ostream& operator<<(::std::ostream& stream, |
| 411 | const tuple<T1, T2, T3>& value) { |
| 412 | stream << "(" << get<0>(value) << ", " << get<1>(value) |
| 413 | << ", "<< get<2>(value) << ")"; |
| 414 | return stream; |
| 415 | } |
| 416 | |
| 417 | template <typename T1, typename T2, typename T3, typename T4, typename T5, |
| 418 | typename T6, typename T7, typename T8, typename T9, typename T10> |
| 419 | ::std::ostream& operator<<( |
| 420 | ::std::ostream& stream, |
| 421 | const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) { |
| 422 | stream << "(" << get<0>(value) << ", " << get<1>(value) |
| 423 | << ", "<< get<2>(value) << ", " << get<3>(value) |
| 424 | << ", "<< get<4>(value) << ", " << get<5>(value) |
| 425 | << ", "<< get<6>(value) << ", " << get<7>(value) |
| 426 | << ", "<< get<8>(value) << ", " << get<9>(value) << ")"; |
| 427 | return stream; |
| 428 | } |
| 429 | |
| 430 | // Tests that Combine() with two parameters generates the expected sequence. |
| 431 | TEST(CombineTest, CombineWithTwoParameters) { |
| 432 | const char* foo = "foo"; |
| 433 | const char* bar = "bar"; |
| 434 | const ParamGenerator<tuple<const char*, int> > gen = |
| 435 | Combine(Values(foo, bar), Values(3, 4)); |
| 436 | |
| 437 | tuple<const char*, int> expected_values[] = { |
| 438 | make_tuple(foo, 3), make_tuple(foo, 4), |
| 439 | make_tuple(bar, 3), make_tuple(bar, 4)}; |
| 440 | VerifyGenerator(gen, expected_values); |
| 441 | } |
| 442 | |
| 443 | // Tests that Combine() with three parameters generates the expected sequence. |
| 444 | TEST(CombineTest, CombineWithThreeParameters) { |
| 445 | const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1), |
| 446 | Values(3, 4), |
| 447 | Values(5, 6)); |
| 448 | tuple<int, int, int> expected_values[] = { |
| 449 | make_tuple(0, 3, 5), make_tuple(0, 3, 6), |
| 450 | make_tuple(0, 4, 5), make_tuple(0, 4, 6), |
| 451 | make_tuple(1, 3, 5), make_tuple(1, 3, 6), |
| 452 | make_tuple(1, 4, 5), make_tuple(1, 4, 6)}; |
| 453 | VerifyGenerator(gen, expected_values); |
| 454 | } |
| 455 | |
| 456 | // Tests that the Combine() with the first parameter generating a single value |
| 457 | // sequence generates a sequence with the number of elements equal to the |
| 458 | // number of elements in the sequence generated by the second parameter. |
| 459 | TEST(CombineTest, CombineWithFirstParameterSingleValue) { |
| 460 | const ParamGenerator<tuple<int, int> > gen = Combine(Values(42), |
| 461 | Values(0, 1)); |
| 462 | |
| 463 | tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)}; |
| 464 | VerifyGenerator(gen, expected_values); |
| 465 | } |
| 466 | |
| 467 | // Tests that the Combine() with the second parameter generating a single value |
| 468 | // sequence generates a sequence with the number of elements equal to the |
| 469 | // number of elements in the sequence generated by the first parameter. |
| 470 | TEST(CombineTest, CombineWithSecondParameterSingleValue) { |
| 471 | const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1), |
| 472 | Values(42)); |
| 473 | |
| 474 | tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)}; |
| 475 | VerifyGenerator(gen, expected_values); |
| 476 | } |
| 477 | |
| 478 | // Tests that when the first parameter produces an empty sequence, |
| 479 | // Combine() produces an empty sequence, too. |
| 480 | TEST(CombineTest, CombineWithFirstParameterEmptyRange) { |
| 481 | const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0), |
| 482 | Values(0, 1)); |
| 483 | VerifyGeneratorIsEmpty(gen); |
| 484 | } |
| 485 | |
| 486 | // Tests that when the second parameter produces an empty sequence, |
| 487 | // Combine() produces an empty sequence, too. |
| 488 | TEST(CombineTest, CombineWithSecondParameterEmptyRange) { |
| 489 | const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1), |
| 490 | Range(1, 1)); |
| 491 | VerifyGeneratorIsEmpty(gen); |
| 492 | } |
| 493 | |
| 494 | // Edge case. Tests that combine works with the maximum number |
| 495 | // of parameters supported by Google Test (currently 10). |
| 496 | TEST(CombineTest, CombineWithMaxNumberOfParameters) { |
| 497 | const char* foo = "foo"; |
| 498 | const char* bar = "bar"; |
| 499 | const ParamGenerator<tuple<const char*, int, int, int, int, int, int, int, |
| 500 | int, int> > gen = Combine(Values(foo, bar), |
| 501 | Values(1), Values(2), |
| 502 | Values(3), Values(4), |
| 503 | Values(5), Values(6), |
| 504 | Values(7), Values(8), |
| 505 | Values(9)); |
| 506 | |
| 507 | tuple<const char*, int, int, int, int, int, int, int, int, int> |
| 508 | expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9), |
| 509 | make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)}; |
| 510 | VerifyGenerator(gen, expected_values); |
| 511 | } |
| 512 | |
| 513 | #endif // GTEST_HAS_COMBINE |
| 514 | |
| 515 | // Tests that an generator produces correct sequence after being |
| 516 | // assigned from another generator. |
| 517 | TEST(ParamGeneratorTest, AssignmentWorks) { |
| 518 | ParamGenerator<int> gen = Values(1, 2); |
| 519 | const ParamGenerator<int> gen2 = Values(3, 4); |
| 520 | gen = gen2; |
| 521 | |
| 522 | const int expected_values[] = {3, 4}; |
| 523 | VerifyGenerator(gen, expected_values); |
| 524 | } |
| 525 | |
| 526 | // This test verifies that the tests are expanded and run as specified: |
| 527 | // one test per element from the sequence produced by the generator |
| 528 | // specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's |
| 529 | // fixture constructor, SetUp(), and TearDown() have run and have been |
| 530 | // supplied with the correct parameters. |
| 531 | |
| 532 | // The use of environment object allows detection of the case where no test |
| 533 | // case functionality is run at all. In this case TestCaseTearDown will not |
| 534 | // be able to detect missing tests, naturally. |
| 535 | template <int kExpectedCalls> |
| 536 | class TestGenerationEnvironment : public ::testing::Environment { |
| 537 | public: |
| 538 | static TestGenerationEnvironment* Instance() { |
| 539 | static TestGenerationEnvironment* instance = new TestGenerationEnvironment; |
| 540 | return instance; |
| 541 | } |
| 542 | |
| 543 | void FixtureConstructorExecuted() { fixture_constructor_count_++; } |
| 544 | void SetUpExecuted() { set_up_count_++; } |
| 545 | void TearDownExecuted() { tear_down_count_++; } |
| 546 | void TestBodyExecuted() { test_body_count_++; } |
| 547 | |
| 548 | virtual void TearDown() { |
| 549 | // If all MultipleTestGenerationTest tests have been de-selected |
| 550 | // by the filter flag, the following checks make no sense. |
| 551 | bool perform_check = false; |
| 552 | |
| 553 | for (int i = 0; i < kExpectedCalls; ++i) { |
| 554 | Message msg; |
| 555 | msg << "TestsExpandedAndRun/" << i; |
| 556 | if (UnitTestOptions::FilterMatchesTest( |
| 557 | "TestExpansionModule/MultipleTestGenerationTest", |
| 558 | msg.GetString().c_str())) { |
| 559 | perform_check = true; |
| 560 | } |
| 561 | } |
| 562 | if (perform_check) { |
| 563 | EXPECT_EQ(kExpectedCalls, fixture_constructor_count_) |
| 564 | << "Fixture constructor of ParamTestGenerationTest test case " |
| 565 | << "has not been run as expected."; |
| 566 | EXPECT_EQ(kExpectedCalls, set_up_count_) |
| 567 | << "Fixture SetUp method of ParamTestGenerationTest test case " |
| 568 | << "has not been run as expected."; |
| 569 | EXPECT_EQ(kExpectedCalls, tear_down_count_) |
| 570 | << "Fixture TearDown method of ParamTestGenerationTest test case " |
| 571 | << "has not been run as expected."; |
| 572 | EXPECT_EQ(kExpectedCalls, test_body_count_) |
| 573 | << "Test in ParamTestGenerationTest test case " |
| 574 | << "has not been run as expected."; |
| 575 | } |
| 576 | } |
| 577 | private: |
| 578 | TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0), |
| 579 | tear_down_count_(0), test_body_count_(0) {} |
| 580 | |
| 581 | int fixture_constructor_count_; |
| 582 | int set_up_count_; |
| 583 | int tear_down_count_; |
| 584 | int test_body_count_; |
| 585 | |
| 586 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment); |
| 587 | }; |
| 588 | |
| 589 | const int test_generation_params[] = {36, 42, 72}; |
| 590 | |
| 591 | class TestGenerationTest : public TestWithParam<int> { |
| 592 | public: |
| 593 | enum { |
| 594 | PARAMETER_COUNT = |
| 595 | sizeof(test_generation_params)/sizeof(test_generation_params[0]) |
| 596 | }; |
| 597 | |
| 598 | typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment; |
| 599 | |
| 600 | TestGenerationTest() { |
| 601 | Environment::Instance()->FixtureConstructorExecuted(); |
| 602 | current_parameter_ = GetParam(); |
| 603 | } |
| 604 | virtual void SetUp() { |
| 605 | Environment::Instance()->SetUpExecuted(); |
| 606 | EXPECT_EQ(current_parameter_, GetParam()); |
| 607 | } |
| 608 | virtual void TearDown() { |
| 609 | Environment::Instance()->TearDownExecuted(); |
| 610 | EXPECT_EQ(current_parameter_, GetParam()); |
| 611 | } |
| 612 | |
| 613 | static void SetUpTestCase() { |
| 614 | bool all_tests_in_test_case_selected = true; |
| 615 | |
| 616 | for (int i = 0; i < PARAMETER_COUNT; ++i) { |
| 617 | Message test_name; |
| 618 | test_name << "TestsExpandedAndRun/" << i; |
| 619 | if ( !UnitTestOptions::FilterMatchesTest( |
| 620 | "TestExpansionModule/MultipleTestGenerationTest", |
| 621 | test_name.GetString())) { |
| 622 | all_tests_in_test_case_selected = false; |
| 623 | } |
| 624 | } |
| 625 | EXPECT_TRUE(all_tests_in_test_case_selected) |
| 626 | << "When running the TestGenerationTest test case all of its tests\n" |
| 627 | << "must be selected by the filter flag for the test case to pass.\n" |
| 628 | << "If not all of them are enabled, we can't reliably conclude\n" |
| 629 | << "that the correct number of tests have been generated."; |
| 630 | |
| 631 | collected_parameters_.clear(); |
| 632 | } |
| 633 | |
| 634 | static void TearDownTestCase() { |
| 635 | vector<int> expected_values(test_generation_params, |
| 636 | test_generation_params + PARAMETER_COUNT); |
| 637 | // Test execution order is not guaranteed by Google Test, |
| 638 | // so the order of values in collected_parameters_ can be |
| 639 | // different and we have to sort to compare. |
| 640 | sort(expected_values.begin(), expected_values.end()); |
| 641 | sort(collected_parameters_.begin(), collected_parameters_.end()); |
| 642 | |
| 643 | EXPECT_TRUE(collected_parameters_ == expected_values); |
| 644 | } |
| 645 | protected: |
| 646 | int current_parameter_; |
| 647 | static vector<int> collected_parameters_; |
| 648 | |
| 649 | private: |
| 650 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest); |
| 651 | }; |
| 652 | vector<int> TestGenerationTest::collected_parameters_; |
| 653 | |
| 654 | TEST_P(TestGenerationTest, TestsExpandedAndRun) { |
| 655 | Environment::Instance()->TestBodyExecuted(); |
| 656 | EXPECT_EQ(current_parameter_, GetParam()); |
| 657 | collected_parameters_.push_back(GetParam()); |
| 658 | } |
| 659 | INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest, |
| 660 | ValuesIn(test_generation_params)); |
| 661 | |
| 662 | // This test verifies that the element sequence (third parameter of |
| 663 | // INSTANTIATE_TEST_CASE_P) is evaluated in RUN_ALL_TESTS and not at the call |
| 664 | // site of INSTANTIATE_TEST_CASE_P. |
| 665 | // For that, we declare param_value_ to be a static member of |
| 666 | // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in main(), |
| 667 | // just before invocation of RUN_ALL_TESTS. If the sequence is evaluated |
| 668 | // before that moment, INSTANTIATE_TEST_CASE_P will create a test with |
| 669 | // parameter 0, and the test body will fail the assertion. |
| 670 | class GeneratorEvaluationTest : public TestWithParam<int> { |
| 671 | public: |
| 672 | static int param_value() { return param_value_; } |
| 673 | static void set_param_value(int param_value) { param_value_ = param_value; } |
| 674 | |
| 675 | private: |
| 676 | static int param_value_; |
| 677 | }; |
| 678 | int GeneratorEvaluationTest::param_value_ = 0; |
| 679 | |
| 680 | TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) { |
| 681 | EXPECT_EQ(1, GetParam()); |
| 682 | } |
| 683 | INSTANTIATE_TEST_CASE_P(GenEvalModule, |
| 684 | GeneratorEvaluationTest, |
| 685 | Values(GeneratorEvaluationTest::param_value())); |
| 686 | |
| 687 | // Tests that generators defined in a different translation unit are |
| 688 | // functional. Generator extern_gen is defined in gtest-param-test_test2.cc. |
| 689 | extern ParamGenerator<int> extern_gen; |
| 690 | class ExternalGeneratorTest : public TestWithParam<int> {}; |
| 691 | TEST_P(ExternalGeneratorTest, ExternalGenerator) { |
| 692 | // Sequence produced by extern_gen contains only a single value |
| 693 | // which we verify here. |
| 694 | EXPECT_EQ(GetParam(), 33); |
| 695 | } |
| 696 | INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule, |
| 697 | ExternalGeneratorTest, |
| 698 | extern_gen); |
| 699 | |
| 700 | // Tests that a parameterized test case can be defined in one translation |
| 701 | // unit and instantiated in another. This test will be instantiated in |
| 702 | // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is |
| 703 | // defined in gtest-param-test_test.h. |
| 704 | TEST_P(ExternalInstantiationTest, IsMultipleOf33) { |
| 705 | EXPECT_EQ(0, GetParam() % 33); |
| 706 | } |
| 707 | |
| 708 | // Tests that a parameterized test case can be instantiated with multiple |
| 709 | // generators. |
| 710 | class MultipleInstantiationTest : public TestWithParam<int> {}; |
| 711 | TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) { |
| 712 | } |
| 713 | INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2)); |
| 714 | INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5)); |
| 715 | |
| 716 | // Tests that a parameterized test case can be instantiated |
| 717 | // in multiple translation units. This test will be instantiated |
| 718 | // here and in gtest-param-test_test2.cc. |
| 719 | // InstantiationInMultipleTranslationUnitsTest fixture class |
| 720 | // is defined in gtest-param-test_test.h. |
| 721 | TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) { |
| 722 | EXPECT_EQ(0, GetParam() % 42); |
| 723 | } |
| 724 | INSTANTIATE_TEST_CASE_P(Sequence1, |
| 725 | InstantiationInMultipleTranslaionUnitsTest, |
| 726 | Values(42, 42*2)); |
| 727 | |
| 728 | // Tests that each iteration of parameterized test runs in a separate test |
| 729 | // object. |
| 730 | class SeparateInstanceTest : public TestWithParam<int> { |
| 731 | public: |
| 732 | SeparateInstanceTest() : count_(0) {} |
| 733 | |
| 734 | static void TearDownTestCase() { |
| 735 | EXPECT_GE(global_count_, 2) |
| 736 | << "If some (but not all) SeparateInstanceTest tests have been " |
| 737 | << "filtered out this test will fail. Make sure that all " |
| 738 | << "GeneratorEvaluationTest are selected or de-selected together " |
| 739 | << "by the test filter."; |
| 740 | } |
| 741 | |
| 742 | protected: |
| 743 | int count_; |
| 744 | static int global_count_; |
| 745 | }; |
| 746 | int SeparateInstanceTest::global_count_ = 0; |
| 747 | |
| 748 | TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) { |
| 749 | EXPECT_EQ(0, count_++); |
| 750 | global_count_++; |
| 751 | } |
| 752 | INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4)); |
| 753 | |
| 754 | // Tests that all instantiations of a test have named appropriately. Test |
| 755 | // defined with TEST_P(TestCaseName, TestName) and instantiated with |
| 756 | // INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named |
| 757 | // SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the |
| 758 | // sequence element used to instantiate the test. |
| 759 | class NamingTest : public TestWithParam<int> {}; |
| 760 | |
| 761 | TEST_P(NamingTest, TestsAreNamedAppropriately) { |
| 762 | const ::testing::TestInfo* const test_info = |
| 763 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 764 | |
| 765 | EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name()); |
| 766 | |
| 767 | Message msg; |
| 768 | msg << "TestsAreNamedAppropriately/" << GetParam(); |
| 769 | EXPECT_STREQ(msg.GetString().c_str(), test_info->name()); |
| 770 | } |
| 771 | |
| 772 | INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5)); |
| 773 | |
| 774 | #endif // GTEST_HAS_PARAM_TEST |
| 775 | |
| 776 | TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) { |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 777 | #if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 778 | FAIL() << "GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n" |
| 779 | #endif |
| 780 | } |
| 781 | |
| 782 | int main(int argc, char **argv) { |
zhanyong.wan | 0af0709 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 783 | #if GTEST_HAS_PARAM_TEST |
vladlosev | 3d70421 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 784 | // Used in TestGenerationTest test case. |
| 785 | AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); |
| 786 | // Used in GeneratorEvaluationTest test case. |
| 787 | GeneratorEvaluationTest::set_param_value(1); |
| 788 | #endif // GTEST_HAS_PARAM_TEST |
| 789 | |
| 790 | testing::InitGoogleTest(&argc, argv); |
| 791 | return RUN_ALL_TESTS(); |
| 792 | } |