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