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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-param-test_test.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,897 @@
     1.4 +// Copyright 2008, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Author: vladl@google.com (Vlad Losev)
    1.34 +//
    1.35 +// Tests for Google Test itself. This file verifies that the parameter
    1.36 +// generators objects produce correct parameter sequences and that
    1.37 +// Google Test runtime instantiates correct tests from those sequences.
    1.38 +
    1.39 +#include "gtest/gtest.h"
    1.40 +
    1.41 +#if GTEST_HAS_PARAM_TEST
    1.42 +
    1.43 +# include <algorithm>
    1.44 +# include <iostream>
    1.45 +# include <list>
    1.46 +# include <sstream>
    1.47 +# include <string>
    1.48 +# include <vector>
    1.49 +
    1.50 +// To include gtest-internal-inl.h.
    1.51 +# define GTEST_IMPLEMENTATION_ 1
    1.52 +# include "src/gtest-internal-inl.h"  // for UnitTestOptions
    1.53 +# undef GTEST_IMPLEMENTATION_
    1.54 +
    1.55 +# include "test/gtest-param-test_test.h"
    1.56 +
    1.57 +using ::std::vector;
    1.58 +using ::std::sort;
    1.59 +
    1.60 +using ::testing::AddGlobalTestEnvironment;
    1.61 +using ::testing::Bool;
    1.62 +using ::testing::Message;
    1.63 +using ::testing::Range;
    1.64 +using ::testing::TestWithParam;
    1.65 +using ::testing::Values;
    1.66 +using ::testing::ValuesIn;
    1.67 +
    1.68 +# if GTEST_HAS_COMBINE
    1.69 +using ::testing::Combine;
    1.70 +using ::std::tr1::get;
    1.71 +using ::std::tr1::make_tuple;
    1.72 +using ::std::tr1::tuple;
    1.73 +# endif  // GTEST_HAS_COMBINE
    1.74 +
    1.75 +using ::testing::internal::ParamGenerator;
    1.76 +using ::testing::internal::UnitTestOptions;
    1.77 +
    1.78 +// Prints a value to a string.
    1.79 +//
    1.80 +// TODO(wan@google.com): remove PrintValue() when we move matchers and
    1.81 +// EXPECT_THAT() from Google Mock to Google Test.  At that time, we
    1.82 +// can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
    1.83 +// EXPECT_THAT() and the matchers know how to print tuples.
    1.84 +template <typename T>
    1.85 +::std::string PrintValue(const T& value) {
    1.86 +  ::std::stringstream stream;
    1.87 +  stream << value;
    1.88 +  return stream.str();
    1.89 +}
    1.90 +
    1.91 +# if GTEST_HAS_COMBINE
    1.92 +
    1.93 +// These overloads allow printing tuples in our tests.  We cannot
    1.94 +// define an operator<< for tuples, as that definition needs to be in
    1.95 +// the std namespace in order to be picked up by Google Test via
    1.96 +// Argument-Dependent Lookup, yet defining anything in the std
    1.97 +// namespace in non-STL code is undefined behavior.
    1.98 +
    1.99 +template <typename T1, typename T2>
   1.100 +::std::string PrintValue(const tuple<T1, T2>& value) {
   1.101 +  ::std::stringstream stream;
   1.102 +  stream << "(" << get<0>(value) << ", " << get<1>(value) << ")";
   1.103 +  return stream.str();
   1.104 +}
   1.105 +
   1.106 +template <typename T1, typename T2, typename T3>
   1.107 +::std::string PrintValue(const tuple<T1, T2, T3>& value) {
   1.108 +  ::std::stringstream stream;
   1.109 +  stream << "(" << get<0>(value) << ", " << get<1>(value)
   1.110 +         << ", "<< get<2>(value) << ")";
   1.111 +  return stream.str();
   1.112 +}
   1.113 +
   1.114 +template <typename T1, typename T2, typename T3, typename T4, typename T5,
   1.115 +          typename T6, typename T7, typename T8, typename T9, typename T10>
   1.116 +::std::string PrintValue(
   1.117 +    const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) {
   1.118 +  ::std::stringstream stream;
   1.119 +  stream << "(" << get<0>(value) << ", " << get<1>(value)
   1.120 +         << ", "<< get<2>(value) << ", " << get<3>(value)
   1.121 +         << ", "<< get<4>(value) << ", " << get<5>(value)
   1.122 +         << ", "<< get<6>(value) << ", " << get<7>(value)
   1.123 +         << ", "<< get<8>(value) << ", " << get<9>(value) << ")";
   1.124 +  return stream.str();
   1.125 +}
   1.126 +
   1.127 +# endif  // GTEST_HAS_COMBINE
   1.128 +
   1.129 +// Verifies that a sequence generated by the generator and accessed
   1.130 +// via the iterator object matches the expected one using Google Test
   1.131 +// assertions.
   1.132 +template <typename T, size_t N>
   1.133 +void VerifyGenerator(const ParamGenerator<T>& generator,
   1.134 +                     const T (&expected_values)[N]) {
   1.135 +  typename ParamGenerator<T>::iterator it = generator.begin();
   1.136 +  for (size_t i = 0; i < N; ++i) {
   1.137 +    ASSERT_FALSE(it == generator.end())
   1.138 +        << "At element " << i << " when accessing via an iterator "
   1.139 +        << "created with the copy constructor.\n";
   1.140 +    // We cannot use EXPECT_EQ() here as the values may be tuples,
   1.141 +    // which don't support <<.
   1.142 +    EXPECT_TRUE(expected_values[i] == *it)
   1.143 +        << "where i is " << i
   1.144 +        << ", expected_values[i] is " << PrintValue(expected_values[i])
   1.145 +        << ", *it is " << PrintValue(*it)
   1.146 +        << ", and 'it' is an iterator created with the copy constructor.\n";
   1.147 +    it++;
   1.148 +  }
   1.149 +  EXPECT_TRUE(it == generator.end())
   1.150 +        << "At the presumed end of sequence when accessing via an iterator "
   1.151 +        << "created with the copy constructor.\n";
   1.152 +
   1.153 +  // Test the iterator assignment. The following lines verify that
   1.154 +  // the sequence accessed via an iterator initialized via the
   1.155 +  // assignment operator (as opposed to a copy constructor) matches
   1.156 +  // just the same.
   1.157 +  it = generator.begin();
   1.158 +  for (size_t i = 0; i < N; ++i) {
   1.159 +    ASSERT_FALSE(it == generator.end())
   1.160 +        << "At element " << i << " when accessing via an iterator "
   1.161 +        << "created with the assignment operator.\n";
   1.162 +    EXPECT_TRUE(expected_values[i] == *it)
   1.163 +        << "where i is " << i
   1.164 +        << ", expected_values[i] is " << PrintValue(expected_values[i])
   1.165 +        << ", *it is " << PrintValue(*it)
   1.166 +        << ", and 'it' is an iterator created with the copy constructor.\n";
   1.167 +    it++;
   1.168 +  }
   1.169 +  EXPECT_TRUE(it == generator.end())
   1.170 +        << "At the presumed end of sequence when accessing via an iterator "
   1.171 +        << "created with the assignment operator.\n";
   1.172 +}
   1.173 +
   1.174 +template <typename T>
   1.175 +void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
   1.176 +  typename ParamGenerator<T>::iterator it = generator.begin();
   1.177 +  EXPECT_TRUE(it == generator.end());
   1.178 +
   1.179 +  it = generator.begin();
   1.180 +  EXPECT_TRUE(it == generator.end());
   1.181 +}
   1.182 +
   1.183 +// Generator tests. They test that each of the provided generator functions
   1.184 +// generates an expected sequence of values. The general test pattern
   1.185 +// instantiates a generator using one of the generator functions,
   1.186 +// checks the sequence produced by the generator using its iterator API,
   1.187 +// and then resets the iterator back to the beginning of the sequence
   1.188 +// and checks the sequence again.
   1.189 +
   1.190 +// Tests that iterators produced by generator functions conform to the
   1.191 +// ForwardIterator concept.
   1.192 +TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
   1.193 +  const ParamGenerator<int> gen = Range(0, 10);
   1.194 +  ParamGenerator<int>::iterator it = gen.begin();
   1.195 +
   1.196 +  // Verifies that iterator initialization works as expected.
   1.197 +  ParamGenerator<int>::iterator it2 = it;
   1.198 +  EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
   1.199 +                           << "element same as its source points to";
   1.200 +
   1.201 +  // Verifies that iterator assignment works as expected.
   1.202 +  it++;
   1.203 +  EXPECT_FALSE(*it == *it2);
   1.204 +  it2 = it;
   1.205 +  EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
   1.206 +                           << "element same as its source points to";
   1.207 +
   1.208 +  // Verifies that prefix operator++() returns *this.
   1.209 +  EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
   1.210 +                          << "refer to the original object";
   1.211 +
   1.212 +  // Verifies that the result of the postfix operator++ points to the value
   1.213 +  // pointed to by the original iterator.
   1.214 +  int original_value = *it;  // Have to compute it outside of macro call to be
   1.215 +                             // unaffected by the parameter evaluation order.
   1.216 +  EXPECT_EQ(original_value, *(it++));
   1.217 +
   1.218 +  // Verifies that prefix and postfix operator++() advance an iterator
   1.219 +  // all the same.
   1.220 +  it2 = it;
   1.221 +  it++;
   1.222 +  ++it2;
   1.223 +  EXPECT_TRUE(*it == *it2);
   1.224 +}
   1.225 +
   1.226 +// Tests that Range() generates the expected sequence.
   1.227 +TEST(RangeTest, IntRangeWithDefaultStep) {
   1.228 +  const ParamGenerator<int> gen = Range(0, 3);
   1.229 +  const int expected_values[] = {0, 1, 2};
   1.230 +  VerifyGenerator(gen, expected_values);
   1.231 +}
   1.232 +
   1.233 +// Edge case. Tests that Range() generates the single element sequence
   1.234 +// as expected when provided with range limits that are equal.
   1.235 +TEST(RangeTest, IntRangeSingleValue) {
   1.236 +  const ParamGenerator<int> gen = Range(0, 1);
   1.237 +  const int expected_values[] = {0};
   1.238 +  VerifyGenerator(gen, expected_values);
   1.239 +}
   1.240 +
   1.241 +// Edge case. Tests that Range() with generates empty sequence when
   1.242 +// supplied with an empty range.
   1.243 +TEST(RangeTest, IntRangeEmpty) {
   1.244 +  const ParamGenerator<int> gen = Range(0, 0);
   1.245 +  VerifyGeneratorIsEmpty(gen);
   1.246 +}
   1.247 +
   1.248 +// Tests that Range() with custom step (greater then one) generates
   1.249 +// the expected sequence.
   1.250 +TEST(RangeTest, IntRangeWithCustomStep) {
   1.251 +  const ParamGenerator<int> gen = Range(0, 9, 3);
   1.252 +  const int expected_values[] = {0, 3, 6};
   1.253 +  VerifyGenerator(gen, expected_values);
   1.254 +}
   1.255 +
   1.256 +// Tests that Range() with custom step (greater then one) generates
   1.257 +// the expected sequence when the last element does not fall on the
   1.258 +// upper range limit. Sequences generated by Range() must not have
   1.259 +// elements beyond the range limits.
   1.260 +TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
   1.261 +  const ParamGenerator<int> gen = Range(0, 4, 3);
   1.262 +  const int expected_values[] = {0, 3};
   1.263 +  VerifyGenerator(gen, expected_values);
   1.264 +}
   1.265 +
   1.266 +// Verifies that Range works with user-defined types that define
   1.267 +// copy constructor, operator=(), operator+(), and operator<().
   1.268 +class DogAdder {
   1.269 + public:
   1.270 +  explicit DogAdder(const char* a_value) : value_(a_value) {}
   1.271 +  DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
   1.272 +
   1.273 +  DogAdder operator=(const DogAdder& other) {
   1.274 +    if (this != &other)
   1.275 +      value_ = other.value_;
   1.276 +    return *this;
   1.277 +  }
   1.278 +  DogAdder operator+(const DogAdder& other) const {
   1.279 +    Message msg;
   1.280 +    msg << value_.c_str() << other.value_.c_str();
   1.281 +    return DogAdder(msg.GetString().c_str());
   1.282 +  }
   1.283 +  bool operator<(const DogAdder& other) const {
   1.284 +    return value_ < other.value_;
   1.285 +  }
   1.286 +  const ::testing::internal::String& value() const { return value_; }
   1.287 +
   1.288 + private:
   1.289 +  ::testing::internal::String value_;
   1.290 +};
   1.291 +
   1.292 +TEST(RangeTest, WorksWithACustomType) {
   1.293 +  const ParamGenerator<DogAdder> gen =
   1.294 +      Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
   1.295 +  ParamGenerator<DogAdder>::iterator it = gen.begin();
   1.296 +
   1.297 +  ASSERT_FALSE(it == gen.end());
   1.298 +  EXPECT_STREQ("cat", it->value().c_str());
   1.299 +
   1.300 +  ASSERT_FALSE(++it == gen.end());
   1.301 +  EXPECT_STREQ("catdog", it->value().c_str());
   1.302 +
   1.303 +  EXPECT_TRUE(++it == gen.end());
   1.304 +}
   1.305 +
   1.306 +class IntWrapper {
   1.307 + public:
   1.308 +  explicit IntWrapper(int a_value) : value_(a_value) {}
   1.309 +  IntWrapper(const IntWrapper& other) : value_(other.value_) {}
   1.310 +
   1.311 +  IntWrapper operator=(const IntWrapper& other) {
   1.312 +    value_ = other.value_;
   1.313 +    return *this;
   1.314 +  }
   1.315 +  // operator+() adds a different type.
   1.316 +  IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
   1.317 +  bool operator<(const IntWrapper& other) const {
   1.318 +    return value_ < other.value_;
   1.319 +  }
   1.320 +  int value() const { return value_; }
   1.321 +
   1.322 + private:
   1.323 +  int value_;
   1.324 +};
   1.325 +
   1.326 +TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
   1.327 +  const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
   1.328 +  ParamGenerator<IntWrapper>::iterator it = gen.begin();
   1.329 +
   1.330 +  ASSERT_FALSE(it == gen.end());
   1.331 +  EXPECT_EQ(0, it->value());
   1.332 +
   1.333 +  ASSERT_FALSE(++it == gen.end());
   1.334 +  EXPECT_EQ(1, it->value());
   1.335 +
   1.336 +  EXPECT_TRUE(++it == gen.end());
   1.337 +}
   1.338 +
   1.339 +// Tests that ValuesIn() with an array parameter generates
   1.340 +// the expected sequence.
   1.341 +TEST(ValuesInTest, ValuesInArray) {
   1.342 +  int array[] = {3, 5, 8};
   1.343 +  const ParamGenerator<int> gen = ValuesIn(array);
   1.344 +  VerifyGenerator(gen, array);
   1.345 +}
   1.346 +
   1.347 +// Tests that ValuesIn() with a const array parameter generates
   1.348 +// the expected sequence.
   1.349 +TEST(ValuesInTest, ValuesInConstArray) {
   1.350 +  const int array[] = {3, 5, 8};
   1.351 +  const ParamGenerator<int> gen = ValuesIn(array);
   1.352 +  VerifyGenerator(gen, array);
   1.353 +}
   1.354 +
   1.355 +// Edge case. Tests that ValuesIn() with an array parameter containing a
   1.356 +// single element generates the single element sequence.
   1.357 +TEST(ValuesInTest, ValuesInSingleElementArray) {
   1.358 +  int array[] = {42};
   1.359 +  const ParamGenerator<int> gen = ValuesIn(array);
   1.360 +  VerifyGenerator(gen, array);
   1.361 +}
   1.362 +
   1.363 +// Tests that ValuesIn() generates the expected sequence for an STL
   1.364 +// container (vector).
   1.365 +TEST(ValuesInTest, ValuesInVector) {
   1.366 +  typedef ::std::vector<int> ContainerType;
   1.367 +  ContainerType values;
   1.368 +  values.push_back(3);
   1.369 +  values.push_back(5);
   1.370 +  values.push_back(8);
   1.371 +  const ParamGenerator<int> gen = ValuesIn(values);
   1.372 +
   1.373 +  const int expected_values[] = {3, 5, 8};
   1.374 +  VerifyGenerator(gen, expected_values);
   1.375 +}
   1.376 +
   1.377 +// Tests that ValuesIn() generates the expected sequence.
   1.378 +TEST(ValuesInTest, ValuesInIteratorRange) {
   1.379 +  typedef ::std::vector<int> ContainerType;
   1.380 +  ContainerType values;
   1.381 +  values.push_back(3);
   1.382 +  values.push_back(5);
   1.383 +  values.push_back(8);
   1.384 +  const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
   1.385 +
   1.386 +  const int expected_values[] = {3, 5, 8};
   1.387 +  VerifyGenerator(gen, expected_values);
   1.388 +}
   1.389 +
   1.390 +// Edge case. Tests that ValuesIn() provided with an iterator range specifying a
   1.391 +// single value generates a single-element sequence.
   1.392 +TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
   1.393 +  typedef ::std::vector<int> ContainerType;
   1.394 +  ContainerType values;
   1.395 +  values.push_back(42);
   1.396 +  const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
   1.397 +
   1.398 +  const int expected_values[] = {42};
   1.399 +  VerifyGenerator(gen, expected_values);
   1.400 +}
   1.401 +
   1.402 +// Edge case. Tests that ValuesIn() provided with an empty iterator range
   1.403 +// generates an empty sequence.
   1.404 +TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
   1.405 +  typedef ::std::vector<int> ContainerType;
   1.406 +  ContainerType values;
   1.407 +  const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
   1.408 +
   1.409 +  VerifyGeneratorIsEmpty(gen);
   1.410 +}
   1.411 +
   1.412 +// Tests that the Values() generates the expected sequence.
   1.413 +TEST(ValuesTest, ValuesWorks) {
   1.414 +  const ParamGenerator<int> gen = Values(3, 5, 8);
   1.415 +
   1.416 +  const int expected_values[] = {3, 5, 8};
   1.417 +  VerifyGenerator(gen, expected_values);
   1.418 +}
   1.419 +
   1.420 +// Tests that Values() generates the expected sequences from elements of
   1.421 +// different types convertible to ParamGenerator's parameter type.
   1.422 +TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
   1.423 +  const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
   1.424 +
   1.425 +  const double expected_values[] = {3.0, 5.0, 8.0};
   1.426 +  VerifyGenerator(gen, expected_values);
   1.427 +}
   1.428 +
   1.429 +TEST(ValuesTest, ValuesWorksForMaxLengthList) {
   1.430 +  const ParamGenerator<int> gen = Values(
   1.431 +      10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
   1.432 +      110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
   1.433 +      210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
   1.434 +      310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
   1.435 +      410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
   1.436 +
   1.437 +  const int expected_values[] = {
   1.438 +      10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
   1.439 +      110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
   1.440 +      210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
   1.441 +      310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
   1.442 +      410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
   1.443 +  VerifyGenerator(gen, expected_values);
   1.444 +}
   1.445 +
   1.446 +// Edge case test. Tests that single-parameter Values() generates the sequence
   1.447 +// with the single value.
   1.448 +TEST(ValuesTest, ValuesWithSingleParameter) {
   1.449 +  const ParamGenerator<int> gen = Values(42);
   1.450 +
   1.451 +  const int expected_values[] = {42};
   1.452 +  VerifyGenerator(gen, expected_values);
   1.453 +}
   1.454 +
   1.455 +// Tests that Bool() generates sequence (false, true).
   1.456 +TEST(BoolTest, BoolWorks) {
   1.457 +  const ParamGenerator<bool> gen = Bool();
   1.458 +
   1.459 +  const bool expected_values[] = {false, true};
   1.460 +  VerifyGenerator(gen, expected_values);
   1.461 +}
   1.462 +
   1.463 +# if GTEST_HAS_COMBINE
   1.464 +
   1.465 +// Tests that Combine() with two parameters generates the expected sequence.
   1.466 +TEST(CombineTest, CombineWithTwoParameters) {
   1.467 +  const char* foo = "foo";
   1.468 +  const char* bar = "bar";
   1.469 +  const ParamGenerator<tuple<const char*, int> > gen =
   1.470 +      Combine(Values(foo, bar), Values(3, 4));
   1.471 +
   1.472 +  tuple<const char*, int> expected_values[] = {
   1.473 +    make_tuple(foo, 3), make_tuple(foo, 4),
   1.474 +    make_tuple(bar, 3), make_tuple(bar, 4)};
   1.475 +  VerifyGenerator(gen, expected_values);
   1.476 +}
   1.477 +
   1.478 +// Tests that Combine() with three parameters generates the expected sequence.
   1.479 +TEST(CombineTest, CombineWithThreeParameters) {
   1.480 +  const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1),
   1.481 +                                                            Values(3, 4),
   1.482 +                                                            Values(5, 6));
   1.483 +  tuple<int, int, int> expected_values[] = {
   1.484 +    make_tuple(0, 3, 5), make_tuple(0, 3, 6),
   1.485 +    make_tuple(0, 4, 5), make_tuple(0, 4, 6),
   1.486 +    make_tuple(1, 3, 5), make_tuple(1, 3, 6),
   1.487 +    make_tuple(1, 4, 5), make_tuple(1, 4, 6)};
   1.488 +  VerifyGenerator(gen, expected_values);
   1.489 +}
   1.490 +
   1.491 +// Tests that the Combine() with the first parameter generating a single value
   1.492 +// sequence generates a sequence with the number of elements equal to the
   1.493 +// number of elements in the sequence generated by the second parameter.
   1.494 +TEST(CombineTest, CombineWithFirstParameterSingleValue) {
   1.495 +  const ParamGenerator<tuple<int, int> > gen = Combine(Values(42),
   1.496 +                                                       Values(0, 1));
   1.497 +
   1.498 +  tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)};
   1.499 +  VerifyGenerator(gen, expected_values);
   1.500 +}
   1.501 +
   1.502 +// Tests that the Combine() with the second parameter generating a single value
   1.503 +// sequence generates a sequence with the number of elements equal to the
   1.504 +// number of elements in the sequence generated by the first parameter.
   1.505 +TEST(CombineTest, CombineWithSecondParameterSingleValue) {
   1.506 +  const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
   1.507 +                                                       Values(42));
   1.508 +
   1.509 +  tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)};
   1.510 +  VerifyGenerator(gen, expected_values);
   1.511 +}
   1.512 +
   1.513 +// Tests that when the first parameter produces an empty sequence,
   1.514 +// Combine() produces an empty sequence, too.
   1.515 +TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
   1.516 +  const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0),
   1.517 +                                                       Values(0, 1));
   1.518 +  VerifyGeneratorIsEmpty(gen);
   1.519 +}
   1.520 +
   1.521 +// Tests that when the second parameter produces an empty sequence,
   1.522 +// Combine() produces an empty sequence, too.
   1.523 +TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
   1.524 +  const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
   1.525 +                                                       Range(1, 1));
   1.526 +  VerifyGeneratorIsEmpty(gen);
   1.527 +}
   1.528 +
   1.529 +// Edge case. Tests that combine works with the maximum number
   1.530 +// of parameters supported by Google Test (currently 10).
   1.531 +TEST(CombineTest, CombineWithMaxNumberOfParameters) {
   1.532 +  const char* foo = "foo";
   1.533 +  const char* bar = "bar";
   1.534 +  const ParamGenerator<tuple<const char*, int, int, int, int, int, int, int,
   1.535 +                             int, int> > gen = Combine(Values(foo, bar),
   1.536 +                                                       Values(1), Values(2),
   1.537 +                                                       Values(3), Values(4),
   1.538 +                                                       Values(5), Values(6),
   1.539 +                                                       Values(7), Values(8),
   1.540 +                                                       Values(9));
   1.541 +
   1.542 +  tuple<const char*, int, int, int, int, int, int, int, int, int>
   1.543 +      expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
   1.544 +                           make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
   1.545 +  VerifyGenerator(gen, expected_values);
   1.546 +}
   1.547 +
   1.548 +# endif  // GTEST_HAS_COMBINE
   1.549 +
   1.550 +// Tests that an generator produces correct sequence after being
   1.551 +// assigned from another generator.
   1.552 +TEST(ParamGeneratorTest, AssignmentWorks) {
   1.553 +  ParamGenerator<int> gen = Values(1, 2);
   1.554 +  const ParamGenerator<int> gen2 = Values(3, 4);
   1.555 +  gen = gen2;
   1.556 +
   1.557 +  const int expected_values[] = {3, 4};
   1.558 +  VerifyGenerator(gen, expected_values);
   1.559 +}
   1.560 +
   1.561 +// This test verifies that the tests are expanded and run as specified:
   1.562 +// one test per element from the sequence produced by the generator
   1.563 +// specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's
   1.564 +// fixture constructor, SetUp(), and TearDown() have run and have been
   1.565 +// supplied with the correct parameters.
   1.566 +
   1.567 +// The use of environment object allows detection of the case where no test
   1.568 +// case functionality is run at all. In this case TestCaseTearDown will not
   1.569 +// be able to detect missing tests, naturally.
   1.570 +template <int kExpectedCalls>
   1.571 +class TestGenerationEnvironment : public ::testing::Environment {
   1.572 + public:
   1.573 +  static TestGenerationEnvironment* Instance() {
   1.574 +    static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
   1.575 +    return instance;
   1.576 +  }
   1.577 +
   1.578 +  void FixtureConstructorExecuted() { fixture_constructor_count_++; }
   1.579 +  void SetUpExecuted() { set_up_count_++; }
   1.580 +  void TearDownExecuted() { tear_down_count_++; }
   1.581 +  void TestBodyExecuted() { test_body_count_++; }
   1.582 +
   1.583 +  virtual void TearDown() {
   1.584 +    // If all MultipleTestGenerationTest tests have been de-selected
   1.585 +    // by the filter flag, the following checks make no sense.
   1.586 +    bool perform_check = false;
   1.587 +
   1.588 +    for (int i = 0; i < kExpectedCalls; ++i) {
   1.589 +      Message msg;
   1.590 +      msg << "TestsExpandedAndRun/" << i;
   1.591 +      if (UnitTestOptions::FilterMatchesTest(
   1.592 +             "TestExpansionModule/MultipleTestGenerationTest",
   1.593 +              msg.GetString().c_str())) {
   1.594 +        perform_check = true;
   1.595 +      }
   1.596 +    }
   1.597 +    if (perform_check) {
   1.598 +      EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
   1.599 +          << "Fixture constructor of ParamTestGenerationTest test case "
   1.600 +          << "has not been run as expected.";
   1.601 +      EXPECT_EQ(kExpectedCalls, set_up_count_)
   1.602 +          << "Fixture SetUp method of ParamTestGenerationTest test case "
   1.603 +          << "has not been run as expected.";
   1.604 +      EXPECT_EQ(kExpectedCalls, tear_down_count_)
   1.605 +          << "Fixture TearDown method of ParamTestGenerationTest test case "
   1.606 +          << "has not been run as expected.";
   1.607 +      EXPECT_EQ(kExpectedCalls, test_body_count_)
   1.608 +          << "Test in ParamTestGenerationTest test case "
   1.609 +          << "has not been run as expected.";
   1.610 +    }
   1.611 +  }
   1.612 +
   1.613 + private:
   1.614 +  TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
   1.615 +                                tear_down_count_(0), test_body_count_(0) {}
   1.616 +
   1.617 +  int fixture_constructor_count_;
   1.618 +  int set_up_count_;
   1.619 +  int tear_down_count_;
   1.620 +  int test_body_count_;
   1.621 +
   1.622 +  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
   1.623 +};
   1.624 +
   1.625 +const int test_generation_params[] = {36, 42, 72};
   1.626 +
   1.627 +class TestGenerationTest : public TestWithParam<int> {
   1.628 + public:
   1.629 +  enum {
   1.630 +    PARAMETER_COUNT =
   1.631 +        sizeof(test_generation_params)/sizeof(test_generation_params[0])
   1.632 +  };
   1.633 +
   1.634 +  typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
   1.635 +
   1.636 +  TestGenerationTest() {
   1.637 +    Environment::Instance()->FixtureConstructorExecuted();
   1.638 +    current_parameter_ = GetParam();
   1.639 +  }
   1.640 +  virtual void SetUp() {
   1.641 +    Environment::Instance()->SetUpExecuted();
   1.642 +    EXPECT_EQ(current_parameter_, GetParam());
   1.643 +  }
   1.644 +  virtual void TearDown() {
   1.645 +    Environment::Instance()->TearDownExecuted();
   1.646 +    EXPECT_EQ(current_parameter_, GetParam());
   1.647 +  }
   1.648 +
   1.649 +  static void SetUpTestCase() {
   1.650 +    bool all_tests_in_test_case_selected = true;
   1.651 +
   1.652 +    for (int i = 0; i < PARAMETER_COUNT; ++i) {
   1.653 +      Message test_name;
   1.654 +      test_name << "TestsExpandedAndRun/" << i;
   1.655 +      if ( !UnitTestOptions::FilterMatchesTest(
   1.656 +                "TestExpansionModule/MultipleTestGenerationTest",
   1.657 +                test_name.GetString())) {
   1.658 +        all_tests_in_test_case_selected = false;
   1.659 +      }
   1.660 +    }
   1.661 +    EXPECT_TRUE(all_tests_in_test_case_selected)
   1.662 +        << "When running the TestGenerationTest test case all of its tests\n"
   1.663 +        << "must be selected by the filter flag for the test case to pass.\n"
   1.664 +        << "If not all of them are enabled, we can't reliably conclude\n"
   1.665 +        << "that the correct number of tests have been generated.";
   1.666 +
   1.667 +    collected_parameters_.clear();
   1.668 +  }
   1.669 +
   1.670 +  static void TearDownTestCase() {
   1.671 +    vector<int> expected_values(test_generation_params,
   1.672 +                                test_generation_params + PARAMETER_COUNT);
   1.673 +    // Test execution order is not guaranteed by Google Test,
   1.674 +    // so the order of values in collected_parameters_ can be
   1.675 +    // different and we have to sort to compare.
   1.676 +    sort(expected_values.begin(), expected_values.end());
   1.677 +    sort(collected_parameters_.begin(), collected_parameters_.end());
   1.678 +
   1.679 +    EXPECT_TRUE(collected_parameters_ == expected_values);
   1.680 +  }
   1.681 +
   1.682 + protected:
   1.683 +  int current_parameter_;
   1.684 +  static vector<int> collected_parameters_;
   1.685 +
   1.686 + private:
   1.687 +  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
   1.688 +};
   1.689 +vector<int> TestGenerationTest::collected_parameters_;
   1.690 +
   1.691 +TEST_P(TestGenerationTest, TestsExpandedAndRun) {
   1.692 +  Environment::Instance()->TestBodyExecuted();
   1.693 +  EXPECT_EQ(current_parameter_, GetParam());
   1.694 +  collected_parameters_.push_back(GetParam());
   1.695 +}
   1.696 +INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest,
   1.697 +                        ValuesIn(test_generation_params));
   1.698 +
   1.699 +// This test verifies that the element sequence (third parameter of
   1.700 +// INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at
   1.701 +// the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS().  For
   1.702 +// that, we declare param_value_ to be a static member of
   1.703 +// GeneratorEvaluationTest and initialize it to 0.  We set it to 1 in
   1.704 +// main(), just before invocation of InitGoogleTest().  After calling
   1.705 +// InitGoogleTest(), we set the value to 2.  If the sequence is evaluated
   1.706 +// before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a
   1.707 +// test with parameter other than 1, and the test body will fail the
   1.708 +// assertion.
   1.709 +class GeneratorEvaluationTest : public TestWithParam<int> {
   1.710 + public:
   1.711 +  static int param_value() { return param_value_; }
   1.712 +  static void set_param_value(int param_value) { param_value_ = param_value; }
   1.713 +
   1.714 + private:
   1.715 +  static int param_value_;
   1.716 +};
   1.717 +int GeneratorEvaluationTest::param_value_ = 0;
   1.718 +
   1.719 +TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
   1.720 +  EXPECT_EQ(1, GetParam());
   1.721 +}
   1.722 +INSTANTIATE_TEST_CASE_P(GenEvalModule,
   1.723 +                        GeneratorEvaluationTest,
   1.724 +                        Values(GeneratorEvaluationTest::param_value()));
   1.725 +
   1.726 +// Tests that generators defined in a different translation unit are
   1.727 +// functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
   1.728 +extern ParamGenerator<int> extern_gen;
   1.729 +class ExternalGeneratorTest : public TestWithParam<int> {};
   1.730 +TEST_P(ExternalGeneratorTest, ExternalGenerator) {
   1.731 +  // Sequence produced by extern_gen contains only a single value
   1.732 +  // which we verify here.
   1.733 +  EXPECT_EQ(GetParam(), 33);
   1.734 +}
   1.735 +INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule,
   1.736 +                        ExternalGeneratorTest,
   1.737 +                        extern_gen);
   1.738 +
   1.739 +// Tests that a parameterized test case can be defined in one translation
   1.740 +// unit and instantiated in another. This test will be instantiated in
   1.741 +// gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
   1.742 +// defined in gtest-param-test_test.h.
   1.743 +TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
   1.744 +  EXPECT_EQ(0, GetParam() % 33);
   1.745 +}
   1.746 +
   1.747 +// Tests that a parameterized test case can be instantiated with multiple
   1.748 +// generators.
   1.749 +class MultipleInstantiationTest : public TestWithParam<int> {};
   1.750 +TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
   1.751 +}
   1.752 +INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
   1.753 +INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
   1.754 +
   1.755 +// Tests that a parameterized test case can be instantiated
   1.756 +// in multiple translation units. This test will be instantiated
   1.757 +// here and in gtest-param-test_test2.cc.
   1.758 +// InstantiationInMultipleTranslationUnitsTest fixture class
   1.759 +// is defined in gtest-param-test_test.h.
   1.760 +TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) {
   1.761 +  EXPECT_EQ(0, GetParam() % 42);
   1.762 +}
   1.763 +INSTANTIATE_TEST_CASE_P(Sequence1,
   1.764 +                        InstantiationInMultipleTranslaionUnitsTest,
   1.765 +                        Values(42, 42*2));
   1.766 +
   1.767 +// Tests that each iteration of parameterized test runs in a separate test
   1.768 +// object.
   1.769 +class SeparateInstanceTest : public TestWithParam<int> {
   1.770 + public:
   1.771 +  SeparateInstanceTest() : count_(0) {}
   1.772 +
   1.773 +  static void TearDownTestCase() {
   1.774 +    EXPECT_GE(global_count_, 2)
   1.775 +        << "If some (but not all) SeparateInstanceTest tests have been "
   1.776 +        << "filtered out this test will fail. Make sure that all "
   1.777 +        << "GeneratorEvaluationTest are selected or de-selected together "
   1.778 +        << "by the test filter.";
   1.779 +  }
   1.780 +
   1.781 + protected:
   1.782 +  int count_;
   1.783 +  static int global_count_;
   1.784 +};
   1.785 +int SeparateInstanceTest::global_count_ = 0;
   1.786 +
   1.787 +TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
   1.788 +  EXPECT_EQ(0, count_++);
   1.789 +  global_count_++;
   1.790 +}
   1.791 +INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
   1.792 +
   1.793 +// Tests that all instantiations of a test have named appropriately. Test
   1.794 +// defined with TEST_P(TestCaseName, TestName) and instantiated with
   1.795 +// INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named
   1.796 +// SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the
   1.797 +// sequence element used to instantiate the test.
   1.798 +class NamingTest : public TestWithParam<int> {};
   1.799 +
   1.800 +TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
   1.801 +  const ::testing::TestInfo* const test_info =
   1.802 +     ::testing::UnitTest::GetInstance()->current_test_info();
   1.803 +
   1.804 +  EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
   1.805 +
   1.806 +  Message index_stream;
   1.807 +  index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
   1.808 +  EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
   1.809 +
   1.810 +  EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
   1.811 +}
   1.812 +
   1.813 +INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
   1.814 +
   1.815 +// Class that cannot be streamed into an ostream.  It needs to be copyable
   1.816 +// (and, in case of MSVC, also assignable) in order to be a test parameter
   1.817 +// type.  Its default copy constructor and assignment operator do exactly
   1.818 +// what we need.
   1.819 +class Unstreamable {
   1.820 + public:
   1.821 +  explicit Unstreamable(int value) : value_(value) {}
   1.822 +
   1.823 + private:
   1.824 +  int value_;
   1.825 +};
   1.826 +
   1.827 +class CommentTest : public TestWithParam<Unstreamable> {};
   1.828 +
   1.829 +TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
   1.830 +  const ::testing::TestInfo* const test_info =
   1.831 +     ::testing::UnitTest::GetInstance()->current_test_info();
   1.832 +
   1.833 +  EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
   1.834 +}
   1.835 +
   1.836 +INSTANTIATE_TEST_CASE_P(InstantiationWithComments,
   1.837 +                        CommentTest,
   1.838 +                        Values(Unstreamable(1)));
   1.839 +
   1.840 +// Verify that we can create a hierarchy of test fixtures, where the base
   1.841 +// class fixture is not parameterized and the derived class is. In this case
   1.842 +// ParameterizedDerivedTest inherits from NonParameterizedBaseTest.  We
   1.843 +// perform simple tests on both.
   1.844 +class NonParameterizedBaseTest : public ::testing::Test {
   1.845 + public:
   1.846 +  NonParameterizedBaseTest() : n_(17) { }
   1.847 + protected:
   1.848 +  int n_;
   1.849 +};
   1.850 +
   1.851 +class ParameterizedDerivedTest : public NonParameterizedBaseTest,
   1.852 +                                 public ::testing::WithParamInterface<int> {
   1.853 + protected:
   1.854 +  ParameterizedDerivedTest() : count_(0) { }
   1.855 +  int count_;
   1.856 +  static int global_count_;
   1.857 +};
   1.858 +
   1.859 +int ParameterizedDerivedTest::global_count_ = 0;
   1.860 +
   1.861 +TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
   1.862 +  EXPECT_EQ(17, n_);
   1.863 +}
   1.864 +
   1.865 +TEST_P(ParameterizedDerivedTest, SeesSequence) {
   1.866 +  EXPECT_EQ(17, n_);
   1.867 +  EXPECT_EQ(0, count_++);
   1.868 +  EXPECT_EQ(GetParam(), global_count_++);
   1.869 +}
   1.870 +
   1.871 +INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5));
   1.872 +
   1.873 +#endif  // GTEST_HAS_PARAM_TEST
   1.874 +
   1.875 +TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {
   1.876 +#if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST
   1.877 +  FAIL() << "GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n"
   1.878 +#endif
   1.879 +}
   1.880 +
   1.881 +int main(int argc, char **argv) {
   1.882 +#if GTEST_HAS_PARAM_TEST
   1.883 +  // Used in TestGenerationTest test case.
   1.884 +  AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
   1.885 +  // Used in GeneratorEvaluationTest test case. Tests that the updated value
   1.886 +  // will be picked up for instantiating tests in GeneratorEvaluationTest.
   1.887 +  GeneratorEvaluationTest::set_param_value(1);
   1.888 +#endif  // GTEST_HAS_PARAM_TEST
   1.889 +
   1.890 +  ::testing::InitGoogleTest(&argc, argv);
   1.891 +
   1.892 +#if GTEST_HAS_PARAM_TEST
   1.893 +  // Used in GeneratorEvaluationTest test case. Tests that value updated
   1.894 +  // here will NOT be used for instantiating tests in
   1.895 +  // GeneratorEvaluationTest.
   1.896 +  GeneratorEvaluationTest::set_param_value(2);
   1.897 +#endif  // GTEST_HAS_PARAM_TEST
   1.898 +
   1.899 +  return RUN_ALL_TESTS();
   1.900 +}

mercurial