aboutsummaryrefslogtreecommitdiffstats
path: root/test/gtest_unittest.cc
diff options
context:
space:
mode:
authorzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-05-10 17:11:58 +0000
committerzhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925>2010-05-10 17:11:58 +0000
commit2ccea88c99d1ae23383d1b8eb3680a4a4d2edd66 (patch)
tree1e633f4afd1e86e367d7d10cb0467fc910d03464 /test/gtest_unittest.cc
parentcdc0aae155e9069eeebcb191570b8cb1b258ecd8 (diff)
downloadgoogletest-2ccea88c99d1ae23383d1b8eb3680a4a4d2edd66.tar.gz
googletest-2ccea88c99d1ae23383d1b8eb3680a4a4d2edd66.tar.bz2
googletest-2ccea88c99d1ae23383d1b8eb3680a4a4d2edd66.zip
Moves the universal printer from gmock to gtest and refactors the cmake script for reusing in gmock (by Vlad Losev).
Diffstat (limited to 'test/gtest_unittest.cc')
-rw-r--r--test/gtest_unittest.cc339
1 files changed, 337 insertions, 2 deletions
diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc
index adc0fffa..a92809f7 100644
--- a/test/gtest_unittest.cc
+++ b/test/gtest_unittest.cc
@@ -132,23 +132,28 @@ using testing::Message;
using testing::ScopedFakeTestPartResultReporter;
using testing::StaticAssertTypeEq;
using testing::Test;
-using testing::TestEventListeners;
using testing::TestCase;
+using testing::TestEventListeners;
using testing::TestPartResult;
using testing::TestPartResultArray;
using testing::TestProperty;
using testing::TestResult;
using testing::UnitTest;
using testing::kMaxStackTraceDepth;
+using testing::internal::AddReference;
using testing::internal::AlwaysFalse;
using testing::internal::AlwaysTrue;
using testing::internal::AppendUserMessage;
+using testing::internal::ArrayAwareFind;
+using testing::internal::ArrayEq;
using testing::internal::CodePointToUtf8;
+using testing::internal::CompileAssertTypesEqual;
+using testing::internal::CopyArray;
using testing::internal::CountIf;
using testing::internal::EqFailure;
using testing::internal::FloatingPoint;
-using testing::internal::FormatTimeInMillisAsSeconds;
using testing::internal::ForEach;
+using testing::internal::FormatTimeInMillisAsSeconds;
using testing::internal::GTestFlagSaver;
using testing::internal::GetCurrentOsStackTraceExceptTop;
using testing::internal::GetElementOr;
@@ -157,9 +162,17 @@ using testing::internal::GetRandomSeedFromFlag;
using testing::internal::GetTestTypeId;
using testing::internal::GetTypeId;
using testing::internal::GetUnitTestImpl;
+using testing::internal::ImplicitlyConvertible;
using testing::internal::Int32;
using testing::internal::Int32FromEnvOrDie;
+using testing::internal::IsAProtocolMessage;
+using testing::internal::IsContainer;
+using testing::internal::IsContainerTest;
+using testing::internal::IsNotContainer;
+using testing::internal::NativeArray;
using testing::internal::ParseInt32Flag;
+using testing::internal::RemoveConst;
+using testing::internal::RemoveReference;
using testing::internal::ShouldRunTestOnShard;
using testing::internal::ShouldShard;
using testing::internal::ShouldUseColor;
@@ -171,7 +184,9 @@ using testing::internal::TestEventListenersAccessor;
using testing::internal::TestResultAccessor;
using testing::internal::UInt32;
using testing::internal::WideStringToUtf8;
+using testing::internal::kCopy;
using testing::internal::kMaxRandomSeed;
+using testing::internal::kReference;
using testing::internal::kTestTypeIdInGoogleTest;
using testing::internal::scoped_ptr;
@@ -184,6 +199,10 @@ using testing::internal::GetCapturedStdout;
using testing::internal::ThreadWithParam;
#endif
+#if GTEST_HAS_PROTOBUF_
+using ::testing::internal::TestMessage;
+#endif // GTEST_HAS_PROTOBUF_
+
class TestingVector : public std::vector<int> {
};
@@ -6725,3 +6744,319 @@ GTEST_TEST(AlternativeNameTest, Works) { // GTEST_TEST is the same as TEST.
EXPECT_FATAL_FAILURE(GTEST_FAIL() << "An expected failure",
"An expected failure");
}
+
+// Tests for internal utilities necessary for implementation of the universal
+// printing.
+// TODO(vladl@google.com): Find a better home for them.
+
+class ConversionHelperBase {};
+class ConversionHelperDerived : public ConversionHelperBase {};
+
+// Tests that IsAProtocolMessage<T>::value is a compile-time constant.
+TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) {
+ GTEST_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value,
+ const_true);
+ GTEST_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false);
+}
+
+// Tests that IsAProtocolMessage<T>::value is true when T is
+// ProtocolMessage or a sub-class of it.
+TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) {
+ EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value);
+ EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value);
+#if GTEST_HAS_PROTOBUF_
+ EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value);
+#endif // GTEST_HAS_PROTOBUF_
+}
+
+// Tests that IsAProtocolMessage<T>::value is false when T is neither
+// ProtocolMessage nor a sub-class of it.
+TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) {
+ EXPECT_FALSE(IsAProtocolMessage<int>::value);
+ EXPECT_FALSE(IsAProtocolMessage<const ConversionHelperBase>::value);
+}
+
+// Tests that CompileAssertTypesEqual compiles when the type arguments are
+// equal.
+TEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) {
+ CompileAssertTypesEqual<void, void>();
+ CompileAssertTypesEqual<int*, int*>();
+}
+
+// Tests that RemoveReference does not affect non-reference types.
+TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) {
+ CompileAssertTypesEqual<int, RemoveReference<int>::type>();
+ CompileAssertTypesEqual<const char, RemoveReference<const char>::type>();
+}
+
+// Tests that RemoveReference removes reference from reference types.
+TEST(RemoveReferenceTest, RemovesReference) {
+ CompileAssertTypesEqual<int, RemoveReference<int&>::type>();
+ CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>();
+}
+
+// Tests GTEST_REMOVE_REFERENCE_.
+
+template <typename T1, typename T2>
+void TestGTestRemoveReference() {
+ CompileAssertTypesEqual<T1, GTEST_REMOVE_REFERENCE_(T2)>();
+}
+
+TEST(RemoveReferenceTest, MacroVersion) {
+ TestGTestRemoveReference<int, int>();
+ TestGTestRemoveReference<const char, const char&>();
+}
+
+
+// Tests that RemoveConst does not affect non-const types.
+TEST(RemoveConstTest, DoesNotAffectNonConstType) {
+ CompileAssertTypesEqual<int, RemoveConst<int>::type>();
+ CompileAssertTypesEqual<char&, RemoveConst<char&>::type>();
+}
+
+// Tests that RemoveConst removes const from const types.
+TEST(RemoveConstTest, RemovesConst) {
+ CompileAssertTypesEqual<int, RemoveConst<const int>::type>();
+ CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>();
+ CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>();
+}
+
+// Tests GTEST_REMOVE_CONST_.
+
+template <typename T1, typename T2>
+void TestGTestRemoveConst() {
+ CompileAssertTypesEqual<T1, GTEST_REMOVE_CONST_(T2)>();
+}
+
+TEST(RemoveConstTest, MacroVersion) {
+ TestGTestRemoveConst<int, int>();
+ TestGTestRemoveConst<double&, double&>();
+ TestGTestRemoveConst<char, const char>();
+}
+
+// Tests that AddReference does not affect reference types.
+TEST(AddReferenceTest, DoesNotAffectReferenceType) {
+ CompileAssertTypesEqual<int&, AddReference<int&>::type>();
+ CompileAssertTypesEqual<const char&, AddReference<const char&>::type>();
+}
+
+// Tests that AddReference adds reference to non-reference types.
+TEST(AddReferenceTest, AddsReference) {
+ CompileAssertTypesEqual<int&, AddReference<int>::type>();
+ CompileAssertTypesEqual<const char&, AddReference<const char>::type>();
+}
+
+// Tests GTEST_ADD_REFERENCE_.
+
+template <typename T1, typename T2>
+void TestGTestAddReference() {
+ CompileAssertTypesEqual<T1, GTEST_ADD_REFERENCE_(T2)>();
+}
+
+TEST(AddReferenceTest, MacroVersion) {
+ TestGTestAddReference<int&, int>();
+ TestGTestAddReference<const char&, const char&>();
+}
+
+// Tests GTEST_REFERENCE_TO_CONST_.
+
+template <typename T1, typename T2>
+void TestGTestReferenceToConst() {
+ CompileAssertTypesEqual<T1, GTEST_REFERENCE_TO_CONST_(T2)>();
+}
+
+TEST(GTestReferenceToConstTest, Works) {
+ TestGTestReferenceToConst<const char&, char>();
+ TestGTestReferenceToConst<const int&, const int>();
+ TestGTestReferenceToConst<const double&, double>();
+ TestGTestReferenceToConst<const String&, const String&>();
+}
+
+// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant.
+TEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) {
+ GTEST_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true);
+ GTEST_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value),
+ const_false);
+}
+
+// Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can
+// be implicitly converted to T2.
+TEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) {
+ EXPECT_TRUE((ImplicitlyConvertible<int, double>::value));
+ EXPECT_TRUE((ImplicitlyConvertible<double, int>::value));
+ EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value));
+ EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value));
+ EXPECT_TRUE((ImplicitlyConvertible<ConversionHelperDerived&,
+ const ConversionHelperBase&>::value));
+ EXPECT_TRUE((ImplicitlyConvertible<const ConversionHelperBase,
+ ConversionHelperBase>::value));
+}
+
+// Tests that ImplicitlyConvertible<T1, T2>::value is false when T1
+// cannot be implicitly converted to T2.
+TEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) {
+ EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value));
+ EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value));
+ EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value));
+ EXPECT_FALSE((ImplicitlyConvertible<ConversionHelperBase&,
+ ConversionHelperDerived&>::value));
+}
+
+// Tests IsContainerTest.
+
+class NonContainer {};
+
+TEST(IsContainerTestTest, WorksForNonContainer) {
+ EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0)));
+ EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0)));
+ EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0)));
+}
+
+TEST(IsContainerTestTest, WorksForContainer) {
+ EXPECT_EQ(sizeof(IsContainer),
+ sizeof(IsContainerTest<std::vector<bool> >(0)));
+ EXPECT_EQ(sizeof(IsContainer),
+ sizeof(IsContainerTest<std::map<int, double> >(0)));
+}
+
+// Tests ArrayEq().
+
+TEST(ArrayEqTest, WorksForDegeneratedArrays) {
+ EXPECT_TRUE(ArrayEq(5, 5L));
+ EXPECT_FALSE(ArrayEq('a', 0));
+}
+
+TEST(ArrayEqTest, WorksForOneDimensionalArrays) {
+ const int a[] = { 0, 1 };
+ long b[] = { 0, 1 };
+ EXPECT_TRUE(ArrayEq(a, b));
+ EXPECT_TRUE(ArrayEq(a, 2, b));
+
+ b[0] = 2;
+ EXPECT_FALSE(ArrayEq(a, b));
+ EXPECT_FALSE(ArrayEq(a, 1, b));
+}
+
+TEST(ArrayEqTest, WorksForTwoDimensionalArrays) {
+ const char a[][3] = { "hi", "lo" };
+ const char b[][3] = { "hi", "lo" };
+ const char c[][3] = { "hi", "li" };
+
+ EXPECT_TRUE(ArrayEq(a, b));
+ EXPECT_TRUE(ArrayEq(a, 2, b));
+
+ EXPECT_FALSE(ArrayEq(a, c));
+ EXPECT_FALSE(ArrayEq(a, 2, c));
+}
+
+// Tests ArrayAwareFind().
+
+TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) {
+ const char a[] = "hello";
+ EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o'));
+ EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x'));
+}
+
+TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) {
+ int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
+ const int b[2] = { 2, 3 };
+ EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b));
+
+ const int c[2] = { 6, 7 };
+ EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c));
+}
+
+// Tests CopyArray().
+
+TEST(CopyArrayTest, WorksForDegeneratedArrays) {
+ int n = 0;
+ CopyArray('a', &n);
+ EXPECT_EQ('a', n);
+}
+
+TEST(CopyArrayTest, WorksForOneDimensionalArrays) {
+ const char a[3] = "hi";
+ int b[3];
+ CopyArray(a, &b);
+ EXPECT_TRUE(ArrayEq(a, b));
+
+ int c[3];
+ CopyArray(a, 3, c);
+ EXPECT_TRUE(ArrayEq(a, c));
+}
+
+TEST(CopyArrayTest, WorksForTwoDimensionalArrays) {
+ const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } };
+ int b[2][3];
+ CopyArray(a, &b);
+ EXPECT_TRUE(ArrayEq(a, b));
+
+ int c[2][3];
+ CopyArray(a, 2, c);
+ EXPECT_TRUE(ArrayEq(a, c));
+}
+
+// Tests NativeArray.
+
+TEST(NativeArrayTest, ConstructorFromArrayWorks) {
+ const int a[3] = { 0, 1, 2 };
+ NativeArray<int> na(a, 3, kReference);
+ EXPECT_EQ(3U, na.size());
+ EXPECT_EQ(a, na.begin());
+}
+
+TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) {
+ typedef int Array[2];
+ Array* a = new Array[1];
+ (*a)[0] = 0;
+ (*a)[1] = 1;
+ NativeArray<int> na(*a, 2, kCopy);
+ EXPECT_NE(*a, na.begin());
+ delete[] a;
+ EXPECT_EQ(0, na.begin()[0]);
+ EXPECT_EQ(1, na.begin()[1]);
+
+ // We rely on the heap checker to verify that na deletes the copy of
+ // array.
+}
+
+TEST(NativeArrayTest, TypeMembersAreCorrect) {
+ StaticAssertTypeEq<char, NativeArray<char>::value_type>();
+ StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>();
+
+ StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>();
+ StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>();
+}
+
+TEST(NativeArrayTest, MethodsWork) {
+ const int a[3] = { 0, 1, 2 };
+ NativeArray<int> na(a, 3, kCopy);
+ ASSERT_EQ(3U, na.size());
+ EXPECT_EQ(3, na.end() - na.begin());
+
+ NativeArray<int>::const_iterator it = na.begin();
+ EXPECT_EQ(0, *it);
+ ++it;
+ EXPECT_EQ(1, *it);
+ it++;
+ EXPECT_EQ(2, *it);
+ ++it;
+ EXPECT_EQ(na.end(), it);
+
+ EXPECT_TRUE(na == na);
+
+ NativeArray<int> na2(a, 3, kReference);
+ EXPECT_TRUE(na == na2);
+
+ const int b1[3] = { 0, 1, 1 };
+ const int b2[4] = { 0, 1, 2, 3 };
+ EXPECT_FALSE(na == NativeArray<int>(b1, 3, kReference));
+ EXPECT_FALSE(na == NativeArray<int>(b2, 4, kCopy));
+}
+
+TEST(NativeArrayTest, WorksForTwoDimensionalArray) {
+ const char a[2][3] = { "hi", "lo" };
+ NativeArray<char[3]> na(a, 2, kReference);
+ ASSERT_EQ(2U, na.size());
+ EXPECT_EQ(a, na.begin());
+}