media/webrtc/trunk/testing/gtest/test/gtest-param-test_test.cc

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 // Copyright 2008, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // Author: vladl@google.com (Vlad Losev)
michael@0 31 //
michael@0 32 // Tests for Google Test itself. This file verifies that the parameter
michael@0 33 // generators objects produce correct parameter sequences and that
michael@0 34 // Google Test runtime instantiates correct tests from those sequences.
michael@0 35
michael@0 36 #include "gtest/gtest.h"
michael@0 37
michael@0 38 #if GTEST_HAS_PARAM_TEST
michael@0 39
michael@0 40 # include <algorithm>
michael@0 41 # include <iostream>
michael@0 42 # include <list>
michael@0 43 # include <sstream>
michael@0 44 # include <string>
michael@0 45 # include <vector>
michael@0 46
michael@0 47 // To include gtest-internal-inl.h.
michael@0 48 # define GTEST_IMPLEMENTATION_ 1
michael@0 49 # include "src/gtest-internal-inl.h" // for UnitTestOptions
michael@0 50 # undef GTEST_IMPLEMENTATION_
michael@0 51
michael@0 52 # include "test/gtest-param-test_test.h"
michael@0 53
michael@0 54 using ::std::vector;
michael@0 55 using ::std::sort;
michael@0 56
michael@0 57 using ::testing::AddGlobalTestEnvironment;
michael@0 58 using ::testing::Bool;
michael@0 59 using ::testing::Message;
michael@0 60 using ::testing::Range;
michael@0 61 using ::testing::TestWithParam;
michael@0 62 using ::testing::Values;
michael@0 63 using ::testing::ValuesIn;
michael@0 64
michael@0 65 # if GTEST_HAS_COMBINE
michael@0 66 using ::testing::Combine;
michael@0 67 using ::std::tr1::get;
michael@0 68 using ::std::tr1::make_tuple;
michael@0 69 using ::std::tr1::tuple;
michael@0 70 # endif // GTEST_HAS_COMBINE
michael@0 71
michael@0 72 using ::testing::internal::ParamGenerator;
michael@0 73 using ::testing::internal::UnitTestOptions;
michael@0 74
michael@0 75 // Prints a value to a string.
michael@0 76 //
michael@0 77 // TODO(wan@google.com): remove PrintValue() when we move matchers and
michael@0 78 // EXPECT_THAT() from Google Mock to Google Test. At that time, we
michael@0 79 // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
michael@0 80 // EXPECT_THAT() and the matchers know how to print tuples.
michael@0 81 template <typename T>
michael@0 82 ::std::string PrintValue(const T& value) {
michael@0 83 ::std::stringstream stream;
michael@0 84 stream << value;
michael@0 85 return stream.str();
michael@0 86 }
michael@0 87
michael@0 88 # if GTEST_HAS_COMBINE
michael@0 89
michael@0 90 // These overloads allow printing tuples in our tests. We cannot
michael@0 91 // define an operator<< for tuples, as that definition needs to be in
michael@0 92 // the std namespace in order to be picked up by Google Test via
michael@0 93 // Argument-Dependent Lookup, yet defining anything in the std
michael@0 94 // namespace in non-STL code is undefined behavior.
michael@0 95
michael@0 96 template <typename T1, typename T2>
michael@0 97 ::std::string PrintValue(const tuple<T1, T2>& value) {
michael@0 98 ::std::stringstream stream;
michael@0 99 stream << "(" << get<0>(value) << ", " << get<1>(value) << ")";
michael@0 100 return stream.str();
michael@0 101 }
michael@0 102
michael@0 103 template <typename T1, typename T2, typename T3>
michael@0 104 ::std::string PrintValue(const tuple<T1, T2, T3>& value) {
michael@0 105 ::std::stringstream stream;
michael@0 106 stream << "(" << get<0>(value) << ", " << get<1>(value)
michael@0 107 << ", "<< get<2>(value) << ")";
michael@0 108 return stream.str();
michael@0 109 }
michael@0 110
michael@0 111 template <typename T1, typename T2, typename T3, typename T4, typename T5,
michael@0 112 typename T6, typename T7, typename T8, typename T9, typename T10>
michael@0 113 ::std::string PrintValue(
michael@0 114 const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) {
michael@0 115 ::std::stringstream stream;
michael@0 116 stream << "(" << get<0>(value) << ", " << get<1>(value)
michael@0 117 << ", "<< get<2>(value) << ", " << get<3>(value)
michael@0 118 << ", "<< get<4>(value) << ", " << get<5>(value)
michael@0 119 << ", "<< get<6>(value) << ", " << get<7>(value)
michael@0 120 << ", "<< get<8>(value) << ", " << get<9>(value) << ")";
michael@0 121 return stream.str();
michael@0 122 }
michael@0 123
michael@0 124 # endif // GTEST_HAS_COMBINE
michael@0 125
michael@0 126 // Verifies that a sequence generated by the generator and accessed
michael@0 127 // via the iterator object matches the expected one using Google Test
michael@0 128 // assertions.
michael@0 129 template <typename T, size_t N>
michael@0 130 void VerifyGenerator(const ParamGenerator<T>& generator,
michael@0 131 const T (&expected_values)[N]) {
michael@0 132 typename ParamGenerator<T>::iterator it = generator.begin();
michael@0 133 for (size_t i = 0; i < N; ++i) {
michael@0 134 ASSERT_FALSE(it == generator.end())
michael@0 135 << "At element " << i << " when accessing via an iterator "
michael@0 136 << "created with the copy constructor.\n";
michael@0 137 // We cannot use EXPECT_EQ() here as the values may be tuples,
michael@0 138 // which don't support <<.
michael@0 139 EXPECT_TRUE(expected_values[i] == *it)
michael@0 140 << "where i is " << i
michael@0 141 << ", expected_values[i] is " << PrintValue(expected_values[i])
michael@0 142 << ", *it is " << PrintValue(*it)
michael@0 143 << ", and 'it' is an iterator created with the copy constructor.\n";
michael@0 144 it++;
michael@0 145 }
michael@0 146 EXPECT_TRUE(it == generator.end())
michael@0 147 << "At the presumed end of sequence when accessing via an iterator "
michael@0 148 << "created with the copy constructor.\n";
michael@0 149
michael@0 150 // Test the iterator assignment. The following lines verify that
michael@0 151 // the sequence accessed via an iterator initialized via the
michael@0 152 // assignment operator (as opposed to a copy constructor) matches
michael@0 153 // just the same.
michael@0 154 it = generator.begin();
michael@0 155 for (size_t i = 0; i < N; ++i) {
michael@0 156 ASSERT_FALSE(it == generator.end())
michael@0 157 << "At element " << i << " when accessing via an iterator "
michael@0 158 << "created with the assignment operator.\n";
michael@0 159 EXPECT_TRUE(expected_values[i] == *it)
michael@0 160 << "where i is " << i
michael@0 161 << ", expected_values[i] is " << PrintValue(expected_values[i])
michael@0 162 << ", *it is " << PrintValue(*it)
michael@0 163 << ", and 'it' is an iterator created with the copy constructor.\n";
michael@0 164 it++;
michael@0 165 }
michael@0 166 EXPECT_TRUE(it == generator.end())
michael@0 167 << "At the presumed end of sequence when accessing via an iterator "
michael@0 168 << "created with the assignment operator.\n";
michael@0 169 }
michael@0 170
michael@0 171 template <typename T>
michael@0 172 void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
michael@0 173 typename ParamGenerator<T>::iterator it = generator.begin();
michael@0 174 EXPECT_TRUE(it == generator.end());
michael@0 175
michael@0 176 it = generator.begin();
michael@0 177 EXPECT_TRUE(it == generator.end());
michael@0 178 }
michael@0 179
michael@0 180 // Generator tests. They test that each of the provided generator functions
michael@0 181 // generates an expected sequence of values. The general test pattern
michael@0 182 // instantiates a generator using one of the generator functions,
michael@0 183 // checks the sequence produced by the generator using its iterator API,
michael@0 184 // and then resets the iterator back to the beginning of the sequence
michael@0 185 // and checks the sequence again.
michael@0 186
michael@0 187 // Tests that iterators produced by generator functions conform to the
michael@0 188 // ForwardIterator concept.
michael@0 189 TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
michael@0 190 const ParamGenerator<int> gen = Range(0, 10);
michael@0 191 ParamGenerator<int>::iterator it = gen.begin();
michael@0 192
michael@0 193 // Verifies that iterator initialization works as expected.
michael@0 194 ParamGenerator<int>::iterator it2 = it;
michael@0 195 EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
michael@0 196 << "element same as its source points to";
michael@0 197
michael@0 198 // Verifies that iterator assignment works as expected.
michael@0 199 it++;
michael@0 200 EXPECT_FALSE(*it == *it2);
michael@0 201 it2 = it;
michael@0 202 EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
michael@0 203 << "element same as its source points to";
michael@0 204
michael@0 205 // Verifies that prefix operator++() returns *this.
michael@0 206 EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
michael@0 207 << "refer to the original object";
michael@0 208
michael@0 209 // Verifies that the result of the postfix operator++ points to the value
michael@0 210 // pointed to by the original iterator.
michael@0 211 int original_value = *it; // Have to compute it outside of macro call to be
michael@0 212 // unaffected by the parameter evaluation order.
michael@0 213 EXPECT_EQ(original_value, *(it++));
michael@0 214
michael@0 215 // Verifies that prefix and postfix operator++() advance an iterator
michael@0 216 // all the same.
michael@0 217 it2 = it;
michael@0 218 it++;
michael@0 219 ++it2;
michael@0 220 EXPECT_TRUE(*it == *it2);
michael@0 221 }
michael@0 222
michael@0 223 // Tests that Range() generates the expected sequence.
michael@0 224 TEST(RangeTest, IntRangeWithDefaultStep) {
michael@0 225 const ParamGenerator<int> gen = Range(0, 3);
michael@0 226 const int expected_values[] = {0, 1, 2};
michael@0 227 VerifyGenerator(gen, expected_values);
michael@0 228 }
michael@0 229
michael@0 230 // Edge case. Tests that Range() generates the single element sequence
michael@0 231 // as expected when provided with range limits that are equal.
michael@0 232 TEST(RangeTest, IntRangeSingleValue) {
michael@0 233 const ParamGenerator<int> gen = Range(0, 1);
michael@0 234 const int expected_values[] = {0};
michael@0 235 VerifyGenerator(gen, expected_values);
michael@0 236 }
michael@0 237
michael@0 238 // Edge case. Tests that Range() with generates empty sequence when
michael@0 239 // supplied with an empty range.
michael@0 240 TEST(RangeTest, IntRangeEmpty) {
michael@0 241 const ParamGenerator<int> gen = Range(0, 0);
michael@0 242 VerifyGeneratorIsEmpty(gen);
michael@0 243 }
michael@0 244
michael@0 245 // Tests that Range() with custom step (greater then one) generates
michael@0 246 // the expected sequence.
michael@0 247 TEST(RangeTest, IntRangeWithCustomStep) {
michael@0 248 const ParamGenerator<int> gen = Range(0, 9, 3);
michael@0 249 const int expected_values[] = {0, 3, 6};
michael@0 250 VerifyGenerator(gen, expected_values);
michael@0 251 }
michael@0 252
michael@0 253 // Tests that Range() with custom step (greater then one) generates
michael@0 254 // the expected sequence when the last element does not fall on the
michael@0 255 // upper range limit. Sequences generated by Range() must not have
michael@0 256 // elements beyond the range limits.
michael@0 257 TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
michael@0 258 const ParamGenerator<int> gen = Range(0, 4, 3);
michael@0 259 const int expected_values[] = {0, 3};
michael@0 260 VerifyGenerator(gen, expected_values);
michael@0 261 }
michael@0 262
michael@0 263 // Verifies that Range works with user-defined types that define
michael@0 264 // copy constructor, operator=(), operator+(), and operator<().
michael@0 265 class DogAdder {
michael@0 266 public:
michael@0 267 explicit DogAdder(const char* a_value) : value_(a_value) {}
michael@0 268 DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
michael@0 269
michael@0 270 DogAdder operator=(const DogAdder& other) {
michael@0 271 if (this != &other)
michael@0 272 value_ = other.value_;
michael@0 273 return *this;
michael@0 274 }
michael@0 275 DogAdder operator+(const DogAdder& other) const {
michael@0 276 Message msg;
michael@0 277 msg << value_.c_str() << other.value_.c_str();
michael@0 278 return DogAdder(msg.GetString().c_str());
michael@0 279 }
michael@0 280 bool operator<(const DogAdder& other) const {
michael@0 281 return value_ < other.value_;
michael@0 282 }
michael@0 283 const ::testing::internal::String& value() const { return value_; }
michael@0 284
michael@0 285 private:
michael@0 286 ::testing::internal::String value_;
michael@0 287 };
michael@0 288
michael@0 289 TEST(RangeTest, WorksWithACustomType) {
michael@0 290 const ParamGenerator<DogAdder> gen =
michael@0 291 Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
michael@0 292 ParamGenerator<DogAdder>::iterator it = gen.begin();
michael@0 293
michael@0 294 ASSERT_FALSE(it == gen.end());
michael@0 295 EXPECT_STREQ("cat", it->value().c_str());
michael@0 296
michael@0 297 ASSERT_FALSE(++it == gen.end());
michael@0 298 EXPECT_STREQ("catdog", it->value().c_str());
michael@0 299
michael@0 300 EXPECT_TRUE(++it == gen.end());
michael@0 301 }
michael@0 302
michael@0 303 class IntWrapper {
michael@0 304 public:
michael@0 305 explicit IntWrapper(int a_value) : value_(a_value) {}
michael@0 306 IntWrapper(const IntWrapper& other) : value_(other.value_) {}
michael@0 307
michael@0 308 IntWrapper operator=(const IntWrapper& other) {
michael@0 309 value_ = other.value_;
michael@0 310 return *this;
michael@0 311 }
michael@0 312 // operator+() adds a different type.
michael@0 313 IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
michael@0 314 bool operator<(const IntWrapper& other) const {
michael@0 315 return value_ < other.value_;
michael@0 316 }
michael@0 317 int value() const { return value_; }
michael@0 318
michael@0 319 private:
michael@0 320 int value_;
michael@0 321 };
michael@0 322
michael@0 323 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
michael@0 324 const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
michael@0 325 ParamGenerator<IntWrapper>::iterator it = gen.begin();
michael@0 326
michael@0 327 ASSERT_FALSE(it == gen.end());
michael@0 328 EXPECT_EQ(0, it->value());
michael@0 329
michael@0 330 ASSERT_FALSE(++it == gen.end());
michael@0 331 EXPECT_EQ(1, it->value());
michael@0 332
michael@0 333 EXPECT_TRUE(++it == gen.end());
michael@0 334 }
michael@0 335
michael@0 336 // Tests that ValuesIn() with an array parameter generates
michael@0 337 // the expected sequence.
michael@0 338 TEST(ValuesInTest, ValuesInArray) {
michael@0 339 int array[] = {3, 5, 8};
michael@0 340 const ParamGenerator<int> gen = ValuesIn(array);
michael@0 341 VerifyGenerator(gen, array);
michael@0 342 }
michael@0 343
michael@0 344 // Tests that ValuesIn() with a const array parameter generates
michael@0 345 // the expected sequence.
michael@0 346 TEST(ValuesInTest, ValuesInConstArray) {
michael@0 347 const int array[] = {3, 5, 8};
michael@0 348 const ParamGenerator<int> gen = ValuesIn(array);
michael@0 349 VerifyGenerator(gen, array);
michael@0 350 }
michael@0 351
michael@0 352 // Edge case. Tests that ValuesIn() with an array parameter containing a
michael@0 353 // single element generates the single element sequence.
michael@0 354 TEST(ValuesInTest, ValuesInSingleElementArray) {
michael@0 355 int array[] = {42};
michael@0 356 const ParamGenerator<int> gen = ValuesIn(array);
michael@0 357 VerifyGenerator(gen, array);
michael@0 358 }
michael@0 359
michael@0 360 // Tests that ValuesIn() generates the expected sequence for an STL
michael@0 361 // container (vector).
michael@0 362 TEST(ValuesInTest, ValuesInVector) {
michael@0 363 typedef ::std::vector<int> ContainerType;
michael@0 364 ContainerType values;
michael@0 365 values.push_back(3);
michael@0 366 values.push_back(5);
michael@0 367 values.push_back(8);
michael@0 368 const ParamGenerator<int> gen = ValuesIn(values);
michael@0 369
michael@0 370 const int expected_values[] = {3, 5, 8};
michael@0 371 VerifyGenerator(gen, expected_values);
michael@0 372 }
michael@0 373
michael@0 374 // Tests that ValuesIn() generates the expected sequence.
michael@0 375 TEST(ValuesInTest, ValuesInIteratorRange) {
michael@0 376 typedef ::std::vector<int> ContainerType;
michael@0 377 ContainerType values;
michael@0 378 values.push_back(3);
michael@0 379 values.push_back(5);
michael@0 380 values.push_back(8);
michael@0 381 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
michael@0 382
michael@0 383 const int expected_values[] = {3, 5, 8};
michael@0 384 VerifyGenerator(gen, expected_values);
michael@0 385 }
michael@0 386
michael@0 387 // Edge case. Tests that ValuesIn() provided with an iterator range specifying a
michael@0 388 // single value generates a single-element sequence.
michael@0 389 TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
michael@0 390 typedef ::std::vector<int> ContainerType;
michael@0 391 ContainerType values;
michael@0 392 values.push_back(42);
michael@0 393 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
michael@0 394
michael@0 395 const int expected_values[] = {42};
michael@0 396 VerifyGenerator(gen, expected_values);
michael@0 397 }
michael@0 398
michael@0 399 // Edge case. Tests that ValuesIn() provided with an empty iterator range
michael@0 400 // generates an empty sequence.
michael@0 401 TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
michael@0 402 typedef ::std::vector<int> ContainerType;
michael@0 403 ContainerType values;
michael@0 404 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
michael@0 405
michael@0 406 VerifyGeneratorIsEmpty(gen);
michael@0 407 }
michael@0 408
michael@0 409 // Tests that the Values() generates the expected sequence.
michael@0 410 TEST(ValuesTest, ValuesWorks) {
michael@0 411 const ParamGenerator<int> gen = Values(3, 5, 8);
michael@0 412
michael@0 413 const int expected_values[] = {3, 5, 8};
michael@0 414 VerifyGenerator(gen, expected_values);
michael@0 415 }
michael@0 416
michael@0 417 // Tests that Values() generates the expected sequences from elements of
michael@0 418 // different types convertible to ParamGenerator's parameter type.
michael@0 419 TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
michael@0 420 const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
michael@0 421
michael@0 422 const double expected_values[] = {3.0, 5.0, 8.0};
michael@0 423 VerifyGenerator(gen, expected_values);
michael@0 424 }
michael@0 425
michael@0 426 TEST(ValuesTest, ValuesWorksForMaxLengthList) {
michael@0 427 const ParamGenerator<int> gen = Values(
michael@0 428 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
michael@0 429 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
michael@0 430 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
michael@0 431 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
michael@0 432 410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
michael@0 433
michael@0 434 const int expected_values[] = {
michael@0 435 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
michael@0 436 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
michael@0 437 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
michael@0 438 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
michael@0 439 410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
michael@0 440 VerifyGenerator(gen, expected_values);
michael@0 441 }
michael@0 442
michael@0 443 // Edge case test. Tests that single-parameter Values() generates the sequence
michael@0 444 // with the single value.
michael@0 445 TEST(ValuesTest, ValuesWithSingleParameter) {
michael@0 446 const ParamGenerator<int> gen = Values(42);
michael@0 447
michael@0 448 const int expected_values[] = {42};
michael@0 449 VerifyGenerator(gen, expected_values);
michael@0 450 }
michael@0 451
michael@0 452 // Tests that Bool() generates sequence (false, true).
michael@0 453 TEST(BoolTest, BoolWorks) {
michael@0 454 const ParamGenerator<bool> gen = Bool();
michael@0 455
michael@0 456 const bool expected_values[] = {false, true};
michael@0 457 VerifyGenerator(gen, expected_values);
michael@0 458 }
michael@0 459
michael@0 460 # if GTEST_HAS_COMBINE
michael@0 461
michael@0 462 // Tests that Combine() with two parameters generates the expected sequence.
michael@0 463 TEST(CombineTest, CombineWithTwoParameters) {
michael@0 464 const char* foo = "foo";
michael@0 465 const char* bar = "bar";
michael@0 466 const ParamGenerator<tuple<const char*, int> > gen =
michael@0 467 Combine(Values(foo, bar), Values(3, 4));
michael@0 468
michael@0 469 tuple<const char*, int> expected_values[] = {
michael@0 470 make_tuple(foo, 3), make_tuple(foo, 4),
michael@0 471 make_tuple(bar, 3), make_tuple(bar, 4)};
michael@0 472 VerifyGenerator(gen, expected_values);
michael@0 473 }
michael@0 474
michael@0 475 // Tests that Combine() with three parameters generates the expected sequence.
michael@0 476 TEST(CombineTest, CombineWithThreeParameters) {
michael@0 477 const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1),
michael@0 478 Values(3, 4),
michael@0 479 Values(5, 6));
michael@0 480 tuple<int, int, int> expected_values[] = {
michael@0 481 make_tuple(0, 3, 5), make_tuple(0, 3, 6),
michael@0 482 make_tuple(0, 4, 5), make_tuple(0, 4, 6),
michael@0 483 make_tuple(1, 3, 5), make_tuple(1, 3, 6),
michael@0 484 make_tuple(1, 4, 5), make_tuple(1, 4, 6)};
michael@0 485 VerifyGenerator(gen, expected_values);
michael@0 486 }
michael@0 487
michael@0 488 // Tests that the Combine() with the first parameter generating a single value
michael@0 489 // sequence generates a sequence with the number of elements equal to the
michael@0 490 // number of elements in the sequence generated by the second parameter.
michael@0 491 TEST(CombineTest, CombineWithFirstParameterSingleValue) {
michael@0 492 const ParamGenerator<tuple<int, int> > gen = Combine(Values(42),
michael@0 493 Values(0, 1));
michael@0 494
michael@0 495 tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)};
michael@0 496 VerifyGenerator(gen, expected_values);
michael@0 497 }
michael@0 498
michael@0 499 // Tests that the Combine() with the second parameter generating a single value
michael@0 500 // sequence generates a sequence with the number of elements equal to the
michael@0 501 // number of elements in the sequence generated by the first parameter.
michael@0 502 TEST(CombineTest, CombineWithSecondParameterSingleValue) {
michael@0 503 const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
michael@0 504 Values(42));
michael@0 505
michael@0 506 tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)};
michael@0 507 VerifyGenerator(gen, expected_values);
michael@0 508 }
michael@0 509
michael@0 510 // Tests that when the first parameter produces an empty sequence,
michael@0 511 // Combine() produces an empty sequence, too.
michael@0 512 TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
michael@0 513 const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0),
michael@0 514 Values(0, 1));
michael@0 515 VerifyGeneratorIsEmpty(gen);
michael@0 516 }
michael@0 517
michael@0 518 // Tests that when the second parameter produces an empty sequence,
michael@0 519 // Combine() produces an empty sequence, too.
michael@0 520 TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
michael@0 521 const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
michael@0 522 Range(1, 1));
michael@0 523 VerifyGeneratorIsEmpty(gen);
michael@0 524 }
michael@0 525
michael@0 526 // Edge case. Tests that combine works with the maximum number
michael@0 527 // of parameters supported by Google Test (currently 10).
michael@0 528 TEST(CombineTest, CombineWithMaxNumberOfParameters) {
michael@0 529 const char* foo = "foo";
michael@0 530 const char* bar = "bar";
michael@0 531 const ParamGenerator<tuple<const char*, int, int, int, int, int, int, int,
michael@0 532 int, int> > gen = Combine(Values(foo, bar),
michael@0 533 Values(1), Values(2),
michael@0 534 Values(3), Values(4),
michael@0 535 Values(5), Values(6),
michael@0 536 Values(7), Values(8),
michael@0 537 Values(9));
michael@0 538
michael@0 539 tuple<const char*, int, int, int, int, int, int, int, int, int>
michael@0 540 expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
michael@0 541 make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
michael@0 542 VerifyGenerator(gen, expected_values);
michael@0 543 }
michael@0 544
michael@0 545 # endif // GTEST_HAS_COMBINE
michael@0 546
michael@0 547 // Tests that an generator produces correct sequence after being
michael@0 548 // assigned from another generator.
michael@0 549 TEST(ParamGeneratorTest, AssignmentWorks) {
michael@0 550 ParamGenerator<int> gen = Values(1, 2);
michael@0 551 const ParamGenerator<int> gen2 = Values(3, 4);
michael@0 552 gen = gen2;
michael@0 553
michael@0 554 const int expected_values[] = {3, 4};
michael@0 555 VerifyGenerator(gen, expected_values);
michael@0 556 }
michael@0 557
michael@0 558 // This test verifies that the tests are expanded and run as specified:
michael@0 559 // one test per element from the sequence produced by the generator
michael@0 560 // specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's
michael@0 561 // fixture constructor, SetUp(), and TearDown() have run and have been
michael@0 562 // supplied with the correct parameters.
michael@0 563
michael@0 564 // The use of environment object allows detection of the case where no test
michael@0 565 // case functionality is run at all. In this case TestCaseTearDown will not
michael@0 566 // be able to detect missing tests, naturally.
michael@0 567 template <int kExpectedCalls>
michael@0 568 class TestGenerationEnvironment : public ::testing::Environment {
michael@0 569 public:
michael@0 570 static TestGenerationEnvironment* Instance() {
michael@0 571 static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
michael@0 572 return instance;
michael@0 573 }
michael@0 574
michael@0 575 void FixtureConstructorExecuted() { fixture_constructor_count_++; }
michael@0 576 void SetUpExecuted() { set_up_count_++; }
michael@0 577 void TearDownExecuted() { tear_down_count_++; }
michael@0 578 void TestBodyExecuted() { test_body_count_++; }
michael@0 579
michael@0 580 virtual void TearDown() {
michael@0 581 // If all MultipleTestGenerationTest tests have been de-selected
michael@0 582 // by the filter flag, the following checks make no sense.
michael@0 583 bool perform_check = false;
michael@0 584
michael@0 585 for (int i = 0; i < kExpectedCalls; ++i) {
michael@0 586 Message msg;
michael@0 587 msg << "TestsExpandedAndRun/" << i;
michael@0 588 if (UnitTestOptions::FilterMatchesTest(
michael@0 589 "TestExpansionModule/MultipleTestGenerationTest",
michael@0 590 msg.GetString().c_str())) {
michael@0 591 perform_check = true;
michael@0 592 }
michael@0 593 }
michael@0 594 if (perform_check) {
michael@0 595 EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
michael@0 596 << "Fixture constructor of ParamTestGenerationTest test case "
michael@0 597 << "has not been run as expected.";
michael@0 598 EXPECT_EQ(kExpectedCalls, set_up_count_)
michael@0 599 << "Fixture SetUp method of ParamTestGenerationTest test case "
michael@0 600 << "has not been run as expected.";
michael@0 601 EXPECT_EQ(kExpectedCalls, tear_down_count_)
michael@0 602 << "Fixture TearDown method of ParamTestGenerationTest test case "
michael@0 603 << "has not been run as expected.";
michael@0 604 EXPECT_EQ(kExpectedCalls, test_body_count_)
michael@0 605 << "Test in ParamTestGenerationTest test case "
michael@0 606 << "has not been run as expected.";
michael@0 607 }
michael@0 608 }
michael@0 609
michael@0 610 private:
michael@0 611 TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
michael@0 612 tear_down_count_(0), test_body_count_(0) {}
michael@0 613
michael@0 614 int fixture_constructor_count_;
michael@0 615 int set_up_count_;
michael@0 616 int tear_down_count_;
michael@0 617 int test_body_count_;
michael@0 618
michael@0 619 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
michael@0 620 };
michael@0 621
michael@0 622 const int test_generation_params[] = {36, 42, 72};
michael@0 623
michael@0 624 class TestGenerationTest : public TestWithParam<int> {
michael@0 625 public:
michael@0 626 enum {
michael@0 627 PARAMETER_COUNT =
michael@0 628 sizeof(test_generation_params)/sizeof(test_generation_params[0])
michael@0 629 };
michael@0 630
michael@0 631 typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
michael@0 632
michael@0 633 TestGenerationTest() {
michael@0 634 Environment::Instance()->FixtureConstructorExecuted();
michael@0 635 current_parameter_ = GetParam();
michael@0 636 }
michael@0 637 virtual void SetUp() {
michael@0 638 Environment::Instance()->SetUpExecuted();
michael@0 639 EXPECT_EQ(current_parameter_, GetParam());
michael@0 640 }
michael@0 641 virtual void TearDown() {
michael@0 642 Environment::Instance()->TearDownExecuted();
michael@0 643 EXPECT_EQ(current_parameter_, GetParam());
michael@0 644 }
michael@0 645
michael@0 646 static void SetUpTestCase() {
michael@0 647 bool all_tests_in_test_case_selected = true;
michael@0 648
michael@0 649 for (int i = 0; i < PARAMETER_COUNT; ++i) {
michael@0 650 Message test_name;
michael@0 651 test_name << "TestsExpandedAndRun/" << i;
michael@0 652 if ( !UnitTestOptions::FilterMatchesTest(
michael@0 653 "TestExpansionModule/MultipleTestGenerationTest",
michael@0 654 test_name.GetString())) {
michael@0 655 all_tests_in_test_case_selected = false;
michael@0 656 }
michael@0 657 }
michael@0 658 EXPECT_TRUE(all_tests_in_test_case_selected)
michael@0 659 << "When running the TestGenerationTest test case all of its tests\n"
michael@0 660 << "must be selected by the filter flag for the test case to pass.\n"
michael@0 661 << "If not all of them are enabled, we can't reliably conclude\n"
michael@0 662 << "that the correct number of tests have been generated.";
michael@0 663
michael@0 664 collected_parameters_.clear();
michael@0 665 }
michael@0 666
michael@0 667 static void TearDownTestCase() {
michael@0 668 vector<int> expected_values(test_generation_params,
michael@0 669 test_generation_params + PARAMETER_COUNT);
michael@0 670 // Test execution order is not guaranteed by Google Test,
michael@0 671 // so the order of values in collected_parameters_ can be
michael@0 672 // different and we have to sort to compare.
michael@0 673 sort(expected_values.begin(), expected_values.end());
michael@0 674 sort(collected_parameters_.begin(), collected_parameters_.end());
michael@0 675
michael@0 676 EXPECT_TRUE(collected_parameters_ == expected_values);
michael@0 677 }
michael@0 678
michael@0 679 protected:
michael@0 680 int current_parameter_;
michael@0 681 static vector<int> collected_parameters_;
michael@0 682
michael@0 683 private:
michael@0 684 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
michael@0 685 };
michael@0 686 vector<int> TestGenerationTest::collected_parameters_;
michael@0 687
michael@0 688 TEST_P(TestGenerationTest, TestsExpandedAndRun) {
michael@0 689 Environment::Instance()->TestBodyExecuted();
michael@0 690 EXPECT_EQ(current_parameter_, GetParam());
michael@0 691 collected_parameters_.push_back(GetParam());
michael@0 692 }
michael@0 693 INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest,
michael@0 694 ValuesIn(test_generation_params));
michael@0 695
michael@0 696 // This test verifies that the element sequence (third parameter of
michael@0 697 // INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at
michael@0 698 // the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS(). For
michael@0 699 // that, we declare param_value_ to be a static member of
michael@0 700 // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in
michael@0 701 // main(), just before invocation of InitGoogleTest(). After calling
michael@0 702 // InitGoogleTest(), we set the value to 2. If the sequence is evaluated
michael@0 703 // before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a
michael@0 704 // test with parameter other than 1, and the test body will fail the
michael@0 705 // assertion.
michael@0 706 class GeneratorEvaluationTest : public TestWithParam<int> {
michael@0 707 public:
michael@0 708 static int param_value() { return param_value_; }
michael@0 709 static void set_param_value(int param_value) { param_value_ = param_value; }
michael@0 710
michael@0 711 private:
michael@0 712 static int param_value_;
michael@0 713 };
michael@0 714 int GeneratorEvaluationTest::param_value_ = 0;
michael@0 715
michael@0 716 TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
michael@0 717 EXPECT_EQ(1, GetParam());
michael@0 718 }
michael@0 719 INSTANTIATE_TEST_CASE_P(GenEvalModule,
michael@0 720 GeneratorEvaluationTest,
michael@0 721 Values(GeneratorEvaluationTest::param_value()));
michael@0 722
michael@0 723 // Tests that generators defined in a different translation unit are
michael@0 724 // functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
michael@0 725 extern ParamGenerator<int> extern_gen;
michael@0 726 class ExternalGeneratorTest : public TestWithParam<int> {};
michael@0 727 TEST_P(ExternalGeneratorTest, ExternalGenerator) {
michael@0 728 // Sequence produced by extern_gen contains only a single value
michael@0 729 // which we verify here.
michael@0 730 EXPECT_EQ(GetParam(), 33);
michael@0 731 }
michael@0 732 INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule,
michael@0 733 ExternalGeneratorTest,
michael@0 734 extern_gen);
michael@0 735
michael@0 736 // Tests that a parameterized test case can be defined in one translation
michael@0 737 // unit and instantiated in another. This test will be instantiated in
michael@0 738 // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
michael@0 739 // defined in gtest-param-test_test.h.
michael@0 740 TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
michael@0 741 EXPECT_EQ(0, GetParam() % 33);
michael@0 742 }
michael@0 743
michael@0 744 // Tests that a parameterized test case can be instantiated with multiple
michael@0 745 // generators.
michael@0 746 class MultipleInstantiationTest : public TestWithParam<int> {};
michael@0 747 TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
michael@0 748 }
michael@0 749 INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
michael@0 750 INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
michael@0 751
michael@0 752 // Tests that a parameterized test case can be instantiated
michael@0 753 // in multiple translation units. This test will be instantiated
michael@0 754 // here and in gtest-param-test_test2.cc.
michael@0 755 // InstantiationInMultipleTranslationUnitsTest fixture class
michael@0 756 // is defined in gtest-param-test_test.h.
michael@0 757 TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) {
michael@0 758 EXPECT_EQ(0, GetParam() % 42);
michael@0 759 }
michael@0 760 INSTANTIATE_TEST_CASE_P(Sequence1,
michael@0 761 InstantiationInMultipleTranslaionUnitsTest,
michael@0 762 Values(42, 42*2));
michael@0 763
michael@0 764 // Tests that each iteration of parameterized test runs in a separate test
michael@0 765 // object.
michael@0 766 class SeparateInstanceTest : public TestWithParam<int> {
michael@0 767 public:
michael@0 768 SeparateInstanceTest() : count_(0) {}
michael@0 769
michael@0 770 static void TearDownTestCase() {
michael@0 771 EXPECT_GE(global_count_, 2)
michael@0 772 << "If some (but not all) SeparateInstanceTest tests have been "
michael@0 773 << "filtered out this test will fail. Make sure that all "
michael@0 774 << "GeneratorEvaluationTest are selected or de-selected together "
michael@0 775 << "by the test filter.";
michael@0 776 }
michael@0 777
michael@0 778 protected:
michael@0 779 int count_;
michael@0 780 static int global_count_;
michael@0 781 };
michael@0 782 int SeparateInstanceTest::global_count_ = 0;
michael@0 783
michael@0 784 TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
michael@0 785 EXPECT_EQ(0, count_++);
michael@0 786 global_count_++;
michael@0 787 }
michael@0 788 INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
michael@0 789
michael@0 790 // Tests that all instantiations of a test have named appropriately. Test
michael@0 791 // defined with TEST_P(TestCaseName, TestName) and instantiated with
michael@0 792 // INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named
michael@0 793 // SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the
michael@0 794 // sequence element used to instantiate the test.
michael@0 795 class NamingTest : public TestWithParam<int> {};
michael@0 796
michael@0 797 TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
michael@0 798 const ::testing::TestInfo* const test_info =
michael@0 799 ::testing::UnitTest::GetInstance()->current_test_info();
michael@0 800
michael@0 801 EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
michael@0 802
michael@0 803 Message index_stream;
michael@0 804 index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
michael@0 805 EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
michael@0 806
michael@0 807 EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
michael@0 808 }
michael@0 809
michael@0 810 INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
michael@0 811
michael@0 812 // Class that cannot be streamed into an ostream. It needs to be copyable
michael@0 813 // (and, in case of MSVC, also assignable) in order to be a test parameter
michael@0 814 // type. Its default copy constructor and assignment operator do exactly
michael@0 815 // what we need.
michael@0 816 class Unstreamable {
michael@0 817 public:
michael@0 818 explicit Unstreamable(int value) : value_(value) {}
michael@0 819
michael@0 820 private:
michael@0 821 int value_;
michael@0 822 };
michael@0 823
michael@0 824 class CommentTest : public TestWithParam<Unstreamable> {};
michael@0 825
michael@0 826 TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
michael@0 827 const ::testing::TestInfo* const test_info =
michael@0 828 ::testing::UnitTest::GetInstance()->current_test_info();
michael@0 829
michael@0 830 EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
michael@0 831 }
michael@0 832
michael@0 833 INSTANTIATE_TEST_CASE_P(InstantiationWithComments,
michael@0 834 CommentTest,
michael@0 835 Values(Unstreamable(1)));
michael@0 836
michael@0 837 // Verify that we can create a hierarchy of test fixtures, where the base
michael@0 838 // class fixture is not parameterized and the derived class is. In this case
michael@0 839 // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We
michael@0 840 // perform simple tests on both.
michael@0 841 class NonParameterizedBaseTest : public ::testing::Test {
michael@0 842 public:
michael@0 843 NonParameterizedBaseTest() : n_(17) { }
michael@0 844 protected:
michael@0 845 int n_;
michael@0 846 };
michael@0 847
michael@0 848 class ParameterizedDerivedTest : public NonParameterizedBaseTest,
michael@0 849 public ::testing::WithParamInterface<int> {
michael@0 850 protected:
michael@0 851 ParameterizedDerivedTest() : count_(0) { }
michael@0 852 int count_;
michael@0 853 static int global_count_;
michael@0 854 };
michael@0 855
michael@0 856 int ParameterizedDerivedTest::global_count_ = 0;
michael@0 857
michael@0 858 TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
michael@0 859 EXPECT_EQ(17, n_);
michael@0 860 }
michael@0 861
michael@0 862 TEST_P(ParameterizedDerivedTest, SeesSequence) {
michael@0 863 EXPECT_EQ(17, n_);
michael@0 864 EXPECT_EQ(0, count_++);
michael@0 865 EXPECT_EQ(GetParam(), global_count_++);
michael@0 866 }
michael@0 867
michael@0 868 INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5));
michael@0 869
michael@0 870 #endif // GTEST_HAS_PARAM_TEST
michael@0 871
michael@0 872 TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {
michael@0 873 #if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST
michael@0 874 FAIL() << "GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n"
michael@0 875 #endif
michael@0 876 }
michael@0 877
michael@0 878 int main(int argc, char **argv) {
michael@0 879 #if GTEST_HAS_PARAM_TEST
michael@0 880 // Used in TestGenerationTest test case.
michael@0 881 AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
michael@0 882 // Used in GeneratorEvaluationTest test case. Tests that the updated value
michael@0 883 // will be picked up for instantiating tests in GeneratorEvaluationTest.
michael@0 884 GeneratorEvaluationTest::set_param_value(1);
michael@0 885 #endif // GTEST_HAS_PARAM_TEST
michael@0 886
michael@0 887 ::testing::InitGoogleTest(&argc, argv);
michael@0 888
michael@0 889 #if GTEST_HAS_PARAM_TEST
michael@0 890 // Used in GeneratorEvaluationTest test case. Tests that value updated
michael@0 891 // here will NOT be used for instantiating tests in
michael@0 892 // GeneratorEvaluationTest.
michael@0 893 GeneratorEvaluationTest::set_param_value(2);
michael@0 894 #endif // GTEST_HAS_PARAM_TEST
michael@0 895
michael@0 896 return RUN_ALL_TESTS();
michael@0 897 }

mercurial