You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
895 lines
32 KiB
895 lines
32 KiB
// Copyright 2008, Google Inc.
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are
|
|
// met:
|
|
//
|
|
// * Redistributions of source code must retain the above copyright
|
|
// notice, this list of conditions and the following disclaimer.
|
|
// * Redistributions in binary form must reproduce the above
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
// in the documentation and/or other materials provided with the
|
|
// distribution.
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
// contributors may be used to endorse or promote products derived from
|
|
// this software without specific prior written permission.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
//
|
|
// Author: vladl@google.com (Vlad Losev)
|
|
//
|
|
// Tests for Google Test itself. This file verifies that the parameter
|
|
// generators objects produce correct parameter sequences and that
|
|
// Google Test runtime instantiates correct tests from those sequences.
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#if GTEST_HAS_PARAM_TEST
|
|
|
|
# include <algorithm>
|
|
# include <iostream>
|
|
# include <list>
|
|
# include <sstream>
|
|
# include <string>
|
|
# include <vector>
|
|
|
|
// To include gtest-internal-inl.h.
|
|
# define GTEST_IMPLEMENTATION_ 1
|
|
# include "src/gtest-internal-inl.h" // for UnitTestOptions
|
|
# undef GTEST_IMPLEMENTATION_
|
|
|
|
# include "test/gtest-param-test_test.h"
|
|
|
|
using ::std::vector;
|
|
using ::std::sort;
|
|
|
|
using ::testing::AddGlobalTestEnvironment;
|
|
using ::testing::Bool;
|
|
using ::testing::Message;
|
|
using ::testing::Range;
|
|
using ::testing::TestWithParam;
|
|
using ::testing::Values;
|
|
using ::testing::ValuesIn;
|
|
|
|
# if GTEST_HAS_COMBINE
|
|
using ::testing::Combine;
|
|
using ::std::tr1::get;
|
|
using ::std::tr1::make_tuple;
|
|
using ::std::tr1::tuple;
|
|
# endif // GTEST_HAS_COMBINE
|
|
|
|
using ::testing::internal::ParamGenerator;
|
|
using ::testing::internal::UnitTestOptions;
|
|
|
|
// Prints a value to a string.
|
|
//
|
|
// TODO(wan@google.com): remove PrintValue() when we move matchers and
|
|
// EXPECT_THAT() from Google Mock to Google Test. At that time, we
|
|
// can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
|
|
// EXPECT_THAT() and the matchers know how to print tuples.
|
|
template <typename T>
|
|
::std::string PrintValue(const T& value) {
|
|
::std::stringstream stream;
|
|
stream << value;
|
|
return stream.str();
|
|
}
|
|
|
|
# if GTEST_HAS_COMBINE
|
|
|
|
// These overloads allow printing tuples in our tests. We cannot
|
|
// define an operator<< for tuples, as that definition needs to be in
|
|
// the std namespace in order to be picked up by Google Test via
|
|
// Argument-Dependent Lookup, yet defining anything in the std
|
|
// namespace in non-STL code is undefined behavior.
|
|
|
|
template <typename T1, typename T2>
|
|
::std::string PrintValue(const tuple<T1, T2>& value) {
|
|
::std::stringstream stream;
|
|
stream << "(" << get<0>(value) << ", " << get<1>(value) << ")";
|
|
return stream.str();
|
|
}
|
|
|
|
template <typename T1, typename T2, typename T3>
|
|
::std::string PrintValue(const tuple<T1, T2, T3>& value) {
|
|
::std::stringstream stream;
|
|
stream << "(" << get<0>(value) << ", " << get<1>(value)
|
|
<< ", "<< get<2>(value) << ")";
|
|
return stream.str();
|
|
}
|
|
|
|
template <typename T1, typename T2, typename T3, typename T4, typename T5,
|
|
typename T6, typename T7, typename T8, typename T9, typename T10>
|
|
::std::string PrintValue(
|
|
const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) {
|
|
::std::stringstream stream;
|
|
stream << "(" << get<0>(value) << ", " << get<1>(value)
|
|
<< ", "<< get<2>(value) << ", " << get<3>(value)
|
|
<< ", "<< get<4>(value) << ", " << get<5>(value)
|
|
<< ", "<< get<6>(value) << ", " << get<7>(value)
|
|
<< ", "<< get<8>(value) << ", " << get<9>(value) << ")";
|
|
return stream.str();
|
|
}
|
|
|
|
# endif // GTEST_HAS_COMBINE
|
|
|
|
// Verifies that a sequence generated by the generator and accessed
|
|
// via the iterator object matches the expected one using Google Test
|
|
// assertions.
|
|
template <typename T, size_t N>
|
|
void VerifyGenerator(const ParamGenerator<T>& generator,
|
|
const T (&expected_values)[N]) {
|
|
typename ParamGenerator<T>::iterator it = generator.begin();
|
|
for (size_t i = 0; i < N; ++i) {
|
|
ASSERT_FALSE(it == generator.end())
|
|
<< "At element " << i << " when accessing via an iterator "
|
|
<< "created with the copy constructor.\n";
|
|
// We cannot use EXPECT_EQ() here as the values may be tuples,
|
|
// which don't support <<.
|
|
EXPECT_TRUE(expected_values[i] == *it)
|
|
<< "where i is " << i
|
|
<< ", expected_values[i] is " << PrintValue(expected_values[i])
|
|
<< ", *it is " << PrintValue(*it)
|
|
<< ", and 'it' is an iterator created with the copy constructor.\n";
|
|
it++;
|
|
}
|
|
EXPECT_TRUE(it == generator.end())
|
|
<< "At the presumed end of sequence when accessing via an iterator "
|
|
<< "created with the copy constructor.\n";
|
|
|
|
// Test the iterator assignment. The following lines verify that
|
|
// the sequence accessed via an iterator initialized via the
|
|
// assignment operator (as opposed to a copy constructor) matches
|
|
// just the same.
|
|
it = generator.begin();
|
|
for (size_t i = 0; i < N; ++i) {
|
|
ASSERT_FALSE(it == generator.end())
|
|
<< "At element " << i << " when accessing via an iterator "
|
|
<< "created with the assignment operator.\n";
|
|
EXPECT_TRUE(expected_values[i] == *it)
|
|
<< "where i is " << i
|
|
<< ", expected_values[i] is " << PrintValue(expected_values[i])
|
|
<< ", *it is " << PrintValue(*it)
|
|
<< ", and 'it' is an iterator created with the copy constructor.\n";
|
|
it++;
|
|
}
|
|
EXPECT_TRUE(it == generator.end())
|
|
<< "At the presumed end of sequence when accessing via an iterator "
|
|
<< "created with the assignment operator.\n";
|
|
}
|
|
|
|
template <typename T>
|
|
void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
|
|
typename ParamGenerator<T>::iterator it = generator.begin();
|
|
EXPECT_TRUE(it == generator.end());
|
|
|
|
it = generator.begin();
|
|
EXPECT_TRUE(it == generator.end());
|
|
}
|
|
|
|
// Generator tests. They test that each of the provided generator functions
|
|
// generates an expected sequence of values. The general test pattern
|
|
// instantiates a generator using one of the generator functions,
|
|
// checks the sequence produced by the generator using its iterator API,
|
|
// and then resets the iterator back to the beginning of the sequence
|
|
// and checks the sequence again.
|
|
|
|
// Tests that iterators produced by generator functions conform to the
|
|
// ForwardIterator concept.
|
|
TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
|
|
const ParamGenerator<int> gen = Range(0, 10);
|
|
ParamGenerator<int>::iterator it = gen.begin();
|
|
|
|
// Verifies that iterator initialization works as expected.
|
|
ParamGenerator<int>::iterator it2 = it;
|
|
EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
|
|
<< "element same as its source points to";
|
|
|
|
// Verifies that iterator assignment works as expected.
|
|
it++;
|
|
EXPECT_FALSE(*it == *it2);
|
|
it2 = it;
|
|
EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
|
|
<< "element same as its source points to";
|
|
|
|
// Verifies that prefix operator++() returns *this.
|
|
EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
|
|
<< "refer to the original object";
|
|
|
|
// Verifies that the result of the postfix operator++ points to the value
|
|
// pointed to by the original iterator.
|
|
int original_value = *it; // Have to compute it outside of macro call to be
|
|
// unaffected by the parameter evaluation order.
|
|
EXPECT_EQ(original_value, *(it++));
|
|
|
|
// Verifies that prefix and postfix operator++() advance an iterator
|
|
// all the same.
|
|
it2 = it;
|
|
it++;
|
|
++it2;
|
|
EXPECT_TRUE(*it == *it2);
|
|
}
|
|
|
|
// Tests that Range() generates the expected sequence.
|
|
TEST(RangeTest, IntRangeWithDefaultStep) {
|
|
const ParamGenerator<int> gen = Range(0, 3);
|
|
const int expected_values[] = {0, 1, 2};
|
|
VerifyGenerator(gen, expected_values);
|
|
}
|
|
|
|
// Edge case. Tests that Range() generates the single element sequence
|
|
// as expected when provided with range limits that are equal.
|
|
TEST(RangeTest, IntRangeSingleValue) {
|
|
const ParamGenerator<int> gen = Range(0, 1);
|
|
const int expected_values[] = {0};
|
|
VerifyGenerator(gen, expected_values);
|
|
}
|
|
|
|
// Edge case. Tests that Range() with generates empty sequence when
|
|
// supplied with an empty range.
|
|
TEST(RangeTest, IntRangeEmpty) {
|
|
const ParamGenerator<int> gen = Range(0, 0);
|
|
VerifyGeneratorIsEmpty(gen);
|
|
}
|
|
|
|
// Tests that Range() with custom step (greater then one) generates
|
|
// the expected sequence.
|
|
TEST(RangeTest, IntRangeWithCustomStep) {
|
|
const ParamGenerator<int> gen = Range(0, 9, 3);
|
|
const int expected_values[] = {0, 3, 6};
|
|
VerifyGenerator(gen, expected_values);
|
|
}
|
|
|
|
// Tests that Range() with custom step (greater then one) generates
|
|
// the expected sequence when the last element does not fall on the
|
|
// upper range limit. Sequences generated by Range() must not have
|
|
|