34 #include "gtest/gtest.h"
39 TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
65 #include <type_traits>
66 #include <unordered_set>
69 #include "gtest/gtest-spi.h"
70 #include "src/gtest-internal-inl.h"
75 #if GTEST_CAN_STREAM_RESULTS_
77 class StreamingListenerTest :
public Test {
79 class FakeSocketWriter :
public StreamingListener::AbstractSocketWriter {
87 StreamingListenerTest()
88 : fake_sock_writer_(
new FakeSocketWriter),
89 streamer_(fake_sock_writer_),
90 test_info_obj_(
"FooTest",
"Bar", nullptr, nullptr,
91 CodeLocation(__FILE__, __LINE__), nullptr, nullptr) {}
96 FakeSocketWriter*
const fake_sock_writer_;
97 StreamingListener streamer_;
102 TEST_F(StreamingListenerTest, OnTestProgramEnd) {
104 streamer_.OnTestProgramEnd(unit_test_);
108 TEST_F(StreamingListenerTest, OnTestIterationEnd) {
110 streamer_.OnTestIterationEnd(unit_test_, 42);
111 EXPECT_EQ(
"event=TestIterationEnd&passed=1&elapsed_time=0ms\n", *
output());
114 TEST_F(StreamingListenerTest, OnTestCaseStart) {
116 streamer_.OnTestCaseStart(
TestCase(
"FooTest",
"Bar",
nullptr,
nullptr));
120 TEST_F(StreamingListenerTest, OnTestCaseEnd) {
122 streamer_.OnTestCaseEnd(
TestCase(
"FooTest",
"Bar",
nullptr,
nullptr));
126 TEST_F(StreamingListenerTest, OnTestStart) {
128 streamer_.OnTestStart(test_info_obj_);
132 TEST_F(StreamingListenerTest, OnTestEnd) {
134 streamer_.OnTestEnd(test_info_obj_);
138 TEST_F(StreamingListenerTest, OnTestPartResult) {
145 "event=TestPartResult&file=foo.cc&line=42&message=failed%3D%0A%26%25\n",
149 #endif // GTEST_CAN_STREAM_RESULTS_
286 #if GTEST_HAS_STREAM_REDIRECTION
291 #if GTEST_IS_THREADSAFE
292 using testing::internal::ThreadWithParam;
301 for (
size_t i = 0;
i < vector.size();
i++) {
302 os << vector[
i] <<
" ";
311 TEST(GetRandomSeedFromFlagTest, HandlesZero) {
317 TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) {
325 TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) {
335 TEST(GetNextRandomSeedTest, WorksForValidInput) {
348 static void ClearCurrentTestPartResults() {
349 TestResultAccessor::ClearTestPartResults(
355 TEST(GetTypeIdTest, ReturnsSameValueForSameType) {
356 EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>());
357 EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>());
360 class SubClassOfTest :
public Test {};
361 class AnotherSubClassOfTest :
public Test {};
363 TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) {
364 EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>());
365 EXPECT_NE(GetTypeId<int>(), GetTypeId<char>());
369 EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>());
374 TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) {
405 TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) {
409 TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
417 TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
431 class FormatEpochTimeInMillisAsIso8601Test :
public Test {
439 void SetUp()
override {
450 SetTimeZone("UTC+00");
453 void TearDown()
override {
454 SetTimeZone(saved_tz_);
455 free(
const_cast<char*
>(saved_tz_));
459 static void SetTimeZone(
const char* time_zone) {
463 #if _MSC_VER || GTEST_OS_WINDOWS_MINGW
474 setenv((
"TZ"), time_zone, 1);
482 const char* saved_tz_;
485 const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec;
487 TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) {
492 TEST_F(FormatEpochTimeInMillisAsIso8601Test, MillisecondsDoNotAffectResult) {
494 "2011-10-31T18:52:42",
498 TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) {
503 TEST_F(FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) {
508 TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) {
514 # pragma option push -w-ccc -w-rch
519 TEST(NullLiteralTest, LHSAllowsNullLiterals) {
520 EXPECT_EQ(0,
static_cast<void*
>(
nullptr));
521 ASSERT_EQ(0,
static_cast<void*
>(
nullptr));
522 EXPECT_EQ(NULL,
static_cast<void*
>(
nullptr));
523 ASSERT_EQ(NULL,
static_cast<void*
>(
nullptr));
524 EXPECT_EQ(
nullptr,
static_cast<void*
>(
nullptr));
525 ASSERT_EQ(
nullptr,
static_cast<void*
>(
nullptr));
527 const int*
const p =
nullptr;
536 struct ConvertToAll {
537 template <
typename T>
543 struct ConvertToPointer {
545 operator T*()
const {
550 struct ConvertToAllButNoPointers {
551 template <
typename T,
559 inline bool operator==(MyType
const&, MyType
const&) {
return true; }
561 TEST(NullLiteralTest, ImplicitConversion) {
562 EXPECT_EQ(ConvertToPointer{},
static_cast<void*
>(
nullptr));
563 #if !defined(__GNUC__) || defined(__clang__)
565 EXPECT_EQ(ConvertToAll{},
static_cast<void*
>(
nullptr));
568 EXPECT_EQ(ConvertToAllButNoPointers{}, MyType{});
572 #pragma clang diagnostic push
573 #if __has_warning("-Wzero-as-null-pointer-constant")
574 #pragma clang diagnostic error "-Wzero-as-null-pointer-constant"
578 TEST(NullLiteralTest, NoConversionNoWarning) {
586 #pragma clang diagnostic pop
598 TEST(CodePointToUtf8Test, CanEncodeNul) {
603 TEST(CodePointToUtf8Test, CanEncodeAscii) {
612 TEST(CodePointToUtf8Test, CanEncode8To11Bits) {
626 TEST(CodePointToUtf8Test, CanEncode12To16Bits) {
636 #if !GTEST_WIDE_STRING_USES_UTF16_
643 TEST(CodePointToUtf8Test, CanEncode17To21Bits) {
655 TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) {
659 #endif // !GTEST_WIDE_STRING_USES_UTF16_
664 TEST(WideStringToUtf8Test, CanEncodeNul) {
670 TEST(WideStringToUtf8Test, CanEncodeAscii) {
679 TEST(WideStringToUtf8Test, CanEncode8To11Bits) {
685 const wchar_t s[] = { 0x576,
'\0' };
692 TEST(WideStringToUtf8Test, CanEncode12To16Bits) {
694 const wchar_t s1[] = { 0x8D3,
'\0' };
699 const wchar_t s2[] = { 0xC74D,
'\0' };
705 TEST(WideStringToUtf8Test, StopsOnNulCharacter) {
711 TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) {
715 #if !GTEST_WIDE_STRING_USES_UTF16_
719 TEST(WideStringToUtf8Test, CanEncode17To21Bits) {
730 TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) {
734 #else // !GTEST_WIDE_STRING_USES_UTF16_
737 TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) {
738 const wchar_t s[] = { 0xD801, 0xDC00,
'\0' };
744 TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) {
746 const wchar_t s1[] = { 0xD800,
'\0' };
749 const wchar_t s2[] = { 0xD800,
'M',
'\0' };
752 const wchar_t s3[] = { 0xDC00,
'P',
'Q',
'R',
'\0' };
755 #endif // !GTEST_WIDE_STRING_USES_UTF16_
758 #if !GTEST_WIDE_STRING_USES_UTF16_
759 TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) {
760 const wchar_t s[] = { 0x108634, 0xC74D,
'\n', 0x576, 0x8D3, 0x108634,
'\0'};
771 TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) {
772 const wchar_t s[] = { 0xC74D,
'\n', 0x576, 0x8D3,
'\0'};
774 "\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93",
777 #endif // !GTEST_WIDE_STRING_USES_UTF16_
781 TEST(RandomDeathTest, GeneratesCrashesOnInvalidRange) {
785 "Cannot generate a number in the range \\[0, 0\\)");
788 "Generation of a number in \\[0, 2147483649\\) was requested, "
789 "but this can only generate numbers in \\[0, 2147483648\\)");
793 const UInt32 kRange = 10000;
795 for (
int i = 0;
i < 10;
i++) {
796 EXPECT_LT(random.Generate(kRange), kRange) <<
" for iteration " <<
i;
800 for (
int i = 0;
i < 10;
i++) {
801 EXPECT_LT(random2.Generate(kRange), kRange) <<
" for iteration " <<
i;
806 const int kSeed = 123;
807 const int kArraySize = 10;
808 const UInt32 kRange = 10000;
812 for (
int i = 0;
i < kArraySize;
i++) {
813 values[
i] = random.Generate(kRange);
816 random.Reseed(kSeed);
817 for (
int i = 0;
i < kArraySize;
i++) {
826 static bool IsPositive(
int n) {
return n > 0; }
844 static int g_sum = 0;
845 static void Accumulate(
int n) { g_sum +=
n; }
887 "Invalid shuffle range start -1: must be in range \\[0, 3\\]");
890 "Invalid shuffle range start 4: must be in range \\[0, 3\\]");
893 "Invalid shuffle range finish 2: must be in range \\[3, 3\\]");
896 "Invalid shuffle range finish 4: must be in range \\[3, 3\\]");
899 class VectorShuffleTest :
public Test {
901 static const size_t kVectorSize = 20;
903 VectorShuffleTest() : random_(1) {
904 for (
int i = 0; i < static_cast<int>(kVectorSize);
i++) {
905 vector_.push_back(
i);
910 if (kVectorSize != vector.size()) {
914 bool found_in_vector[kVectorSize] = {
false };
915 for (
size_t i = 0;
i < vector.size();
i++) {
916 const int e = vector[
i];
917 if (e < 0 || e >=
static_cast<int>(kVectorSize) || found_in_vector[e]) {
920 found_in_vector[
e] =
true;
928 static bool VectorIsNotCorrupt(
const TestingVector& vector) {
929 return !VectorIsCorrupt(vector);
934 if (
i != vector[
static_cast<size_t>(
i)]) {
941 static bool RangeIsUnshuffled(
943 return !RangeIsShuffled(vector,
begin,
end);
947 return RangeIsShuffled(vector, 0,
static_cast<int>(vector.size()));
950 static bool VectorIsUnshuffled(
const TestingVector& vector) {
951 return !VectorIsShuffled(vector);
958 const size_t VectorShuffleTest::kVectorSize;
960 TEST_F(VectorShuffleTest, HandlesEmptyRange) {
967 ShuffleRange(&random_, kVectorSize/2, kVectorSize/2, &vector_);
972 ShuffleRange(&random_, kVectorSize - 1, kVectorSize - 1, &vector_);
977 ShuffleRange(&random_, kVectorSize, kVectorSize, &vector_);
982 TEST_F(VectorShuffleTest, HandlesRangeOfSizeOne) {
989 ShuffleRange(&random_, kVectorSize/2, kVectorSize/2 + 1, &vector_);
994 ShuffleRange(&random_, kVectorSize - 1, kVectorSize, &vector_);
1002 TEST_F(VectorShuffleTest, ShufflesEntireVector) {
1010 EXPECT_NE(
static_cast<int>(kVectorSize - 1), vector_[kVectorSize - 1]);
1013 TEST_F(VectorShuffleTest, ShufflesStartOfVector) {
1014 const int kRangeSize = kVectorSize/2;
1021 static_cast<int>(kVectorSize));
1024 TEST_F(VectorShuffleTest, ShufflesEndOfVector) {
1025 const int kRangeSize = kVectorSize / 2;
1026 ShuffleRange(&random_, kRangeSize, kVectorSize, &vector_);
1029 EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize);
1031 static_cast<int>(kVectorSize));
1034 TEST_F(VectorShuffleTest, ShufflesMiddleOfVector) {
1035 const int kRangeSize =
static_cast<int>(kVectorSize) / 3;
1036 ShuffleRange(&random_, kRangeSize, 2*kRangeSize, &vector_);
1039 EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize);
1040 EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, 2*kRangeSize);
1041 EXPECT_PRED3(RangeIsUnshuffled, vector_, 2 * kRangeSize,
1042 static_cast<int>(kVectorSize));
1045 TEST_F(VectorShuffleTest, ShufflesRepeatably) {
1047 for (
size_t i = 0;
i < kVectorSize;
i++) {
1048 vector2.push_back(
static_cast<int>(
i));
1051 random_.Reseed(1234);
1053 random_.Reseed(1234);
1059 for (
size_t i = 0;
i < kVectorSize;
i++) {
1060 EXPECT_EQ(vector_[
i], vector2[
i]) <<
" where i is " <<
i;
1066 TEST(AssertHelperTest, AssertHelperIsSmall) {
1073 TEST(StringTest, EndsWithCaseInsensitive) {
1074 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"foobar",
"BAR"));
1075 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"foobaR",
"bar"));
1076 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"foobar",
""));
1077 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"",
""));
1079 EXPECT_FALSE(String::EndsWithCaseInsensitive(
"Foobar",
"foo"));
1080 EXPECT_FALSE(String::EndsWithCaseInsensitive(
"foobar",
"Foo"));
1081 EXPECT_FALSE(String::EndsWithCaseInsensitive(
"",
"foo"));
1087 static const wchar_t*
const kNull =
nullptr;
1090 TEST(StringTest, CaseInsensitiveWideCStringEquals) {
1091 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
nullptr,
nullptr));
1096 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
L"foobar",
L"foobar"));
1097 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
L"foobar",
L"FOOBAR"));
1098 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
L"FOOBAR",
L"foobar"));
1101 #if GTEST_OS_WINDOWS
1104 TEST(StringTest, ShowWideCString) {
1106 String::ShowWideCString(NULL).
c_str());
1111 # if GTEST_OS_WINDOWS_MOBILE
1112 TEST(StringTest, AnsiAndUtf16Null) {
1113 EXPECT_EQ(NULL, String::AnsiToUtf16(NULL));
1114 EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL));
1117 TEST(StringTest, AnsiAndUtf16ConvertBasic) {
1118 const char* ansi = String::Utf16ToAnsi(
L"str");
1121 const WCHAR* utf16 = String::AnsiToUtf16(
"str");
1126 TEST(StringTest, AnsiAndUtf16ConvertPathChars) {
1127 const char* ansi = String::Utf16ToAnsi(
L".:\\ \"*?");
1130 const WCHAR* utf16 = String::AnsiToUtf16(
".:\\ \"*?");
1131 EXPECT_EQ(0, wcsncmp(
L".:\\ \"*?", utf16, 3));
1134 # endif // GTEST_OS_WINDOWS_MOBILE
1136 #endif // GTEST_OS_WINDOWS
1146 TEST(TestPropertyTest, ReplaceStringValue) {
1149 property.SetValue(
"2");
1156 static void AddFatalFailure() {
1157 FAIL() <<
"Expected fatal failure.";
1160 static void AddNonfatalFailure() {
1164 class ScopedFakeTestPartResultReporterTest :
public Test {
1170 static void AddFailure(FailureMode failure) {
1174 AddNonfatalFailure();
1181 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptsTestFailures) {
1185 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
1187 AddFailure(NONFATAL_FAILURE);
1196 TEST_F(ScopedFakeTestPartResultReporterTest, DeprecatedConstructor) {
1201 AddFailure(NONFATAL_FAILURE);
1206 #if GTEST_IS_THREADSAFE
1208 class ScopedFakeTestPartResultReporterWithThreadsTest
1209 :
public ScopedFakeTestPartResultReporterTest {
1211 static void AddFailureInOtherThread(FailureMode failure) {
1212 ThreadWithParam<FailureMode>
thread(&AddFailure, failure,
nullptr);
1217 TEST_F(ScopedFakeTestPartResultReporterWithThreadsTest,
1218 InterceptsTestFailuresInAllThreads) {
1222 ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, &
results);
1223 AddFailure(NONFATAL_FAILURE);
1225 AddFailureInOtherThread(NONFATAL_FAILURE);
1236 #endif // GTEST_IS_THREADSAFE
1242 typedef ScopedFakeTestPartResultReporterTest ExpectFatalFailureTest;
1244 TEST_F(ExpectFatalFailureTest, CatchesFatalFaliure) {
1248 TEST_F(ExpectFatalFailureTest, AcceptsStdStringObject) {
1253 TEST_F(ExpectFatalFailureTest, CatchesFatalFailureOnAllThreads) {
1257 "Expected fatal failure.");
1262 # pragma option push -w-ccc
1268 int NonVoidFunction() {
1274 TEST_F(ExpectFatalFailureTest, CanBeUsedInNonVoidFunction) {
1281 void DoesNotAbortHelper(
bool* aborted) {
1293 TEST_F(ExpectFatalFailureTest, DoesNotAbort) {
1294 bool aborted =
true;
1295 DoesNotAbortHelper(&aborted);
1303 static int global_var = 0;
1304 #define GTEST_USE_UNPROTECTED_COMMA_ global_var++, global_var++
1306 TEST_F(ExpectFatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) {
1307 #ifndef __BORLANDC__
1323 typedef ScopedFakeTestPartResultReporterTest ExpectNonfatalFailureTest;
1325 TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailure) {
1327 "Expected non-fatal failure.");
1330 TEST_F(ExpectNonfatalFailureTest, AcceptsStdStringObject) {
1335 TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailureOnAllThreads) {
1339 "Expected non-fatal failure.");
1345 TEST_F(ExpectNonfatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) {
1348 AddNonfatalFailure();
1353 AddNonfatalFailure();
1357 #if GTEST_IS_THREADSAFE
1359 typedef ScopedFakeTestPartResultReporterWithThreadsTest
1360 ExpectFailureWithThreadsTest;
1362 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailureOnAllThreads) {
1364 "Expected fatal failure.");
1367 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailureOnAllThreads) {
1369 AddFailureInOtherThread(NONFATAL_FAILURE),
"Expected non-fatal failure.");
1372 #endif // GTEST_IS_THREADSAFE
1376 TEST(TestPropertyTest, ConstructorWorks) {
1382 TEST(TestPropertyTest, SetValue) {
1385 property.SetValue(
"value_2");
1393 class TestResultTest :
public Test {
1395 typedef std::vector<TestPartResult> TPRVector;
1403 void SetUp()
override {
1425 TPRVector* results1 =
const_cast<TPRVector*
>(
1426 &TestResultAccessor::test_part_results(*r1));
1427 TPRVector* results2 =
const_cast<TPRVector*
>(
1428 &TestResultAccessor::test_part_results(*r2));
1433 results1->push_back(*pr1);
1436 results2->push_back(*pr1);
1437 results2->push_back(*pr2);
1440 void TearDown()
override {
1450 static void CompareTestPartResult(
const TestPartResult& expected,
1465 TEST_F(TestResultTest, total_part_count) {
1472 TEST_F(TestResultTest, Passed) {
1479 TEST_F(TestResultTest, Failed) {
1487 typedef TestResultTest TestResultDeathTest;
1489 TEST_F(TestResultDeathTest, GetTestPartResult) {
1497 TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) {
1503 TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) {
1506 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property);
1514 TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) {
1518 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1);
1519 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2);
1531 TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) {
1537 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1_1);
1538 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2_1);
1539 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1_2);
1540 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2_2);
1553 TEST(TestResultPropertyTest, GetTestProperty) {
1558 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1);
1559 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2);
1560 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_3);
1591 class GTestFlagSaverTest :
public Test {
1596 static void SetUpTestSuite() {
1618 static void TearDownTestSuite() {
1625 void VerifyAndModifyFlags() {
1655 GTEST_FLAG(stream_result_to) =
"localhost:1234";
1670 TEST_F(GTestFlagSaverTest, ModifyGTestFlags) {
1671 VerifyAndModifyFlags();
1676 TEST_F(GTestFlagSaverTest, VerifyGTestFlags) {
1677 VerifyAndModifyFlags();
1683 static void SetEnv(
const char*
name,
const char*
value) {
1684 #if GTEST_OS_WINDOWS_MOBILE
1687 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
1691 static std::map<std::string, std::string*> added_env;
1696 if (added_env.find(
name) != added_env.end()) {
1697 prev_env = added_env[
name];
1705 putenv(
const_cast<char*
>(added_env[
name]->
c_str()));
1707 #elif GTEST_OS_WINDOWS // If we are on Windows proper.
1710 if (*
value ==
'\0') {
1715 #endif // GTEST_OS_WINDOWS_MOBILE
1718 #if !GTEST_OS_WINDOWS_MOBILE
1727 TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenVariableIsNotSet) {
1732 # if !defined(GTEST_GET_INT32_FROM_ENV_)
1736 TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueOverflows) {
1737 printf(
"(expecting 2 warnings)\n");
1748 TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueIsInvalid) {
1749 printf(
"(expecting 2 warnings)\n");
1758 # endif // !defined(GTEST_GET_INT32_FROM_ENV_)
1763 TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) {
1770 #endif // !GTEST_OS_WINDOWS_MOBILE
1776 TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) {
1787 TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) {
1788 printf(
"(expecting 2 warnings)\n");
1801 TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) {
1802 printf(
"(expecting 2 warnings)\n");
1815 TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) {
1828 #if !GTEST_OS_WINDOWS_MOBILE
1829 TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) {
1836 #endif // !GTEST_OS_WINDOWS_MOBILE
1840 TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) {
1849 TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) {
1858 TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) {
1868 void SetUp()
override {
1873 void TearDown()
override {
1874 SetEnv(index_var_,
"");
1875 SetEnv(total_var_,
"");
1878 const char* index_var_;
1879 const char* total_var_;
1884 TEST_F(ShouldShardTest, ReturnsFalseWhenNeitherEnvVarIsSet) {
1885 SetEnv(index_var_,
"");
1886 SetEnv(total_var_,
"");
1893 TEST_F(ShouldShardTest, ReturnsFalseWhenTotalShardIsOne) {
1894 SetEnv(index_var_,
"0");
1895 SetEnv(total_var_,
"1");
1903 #if !GTEST_OS_WINDOWS_MOBILE
1904 TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) {
1905 SetEnv(index_var_,
"4");
1906 SetEnv(total_var_,
"22");
1910 SetEnv(index_var_,
"8");
1911 SetEnv(total_var_,
"9");
1915 SetEnv(index_var_,
"0");
1916 SetEnv(total_var_,
"9");
1920 #endif // !GTEST_OS_WINDOWS_MOBILE
1924 typedef ShouldShardTest ShouldShardDeathTest;
1926 TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) {
1927 SetEnv(index_var_,
"4");
1928 SetEnv(total_var_,
"4");
1931 SetEnv(index_var_,
"4");
1932 SetEnv(total_var_,
"-2");
1935 SetEnv(index_var_,
"5");
1936 SetEnv(total_var_,
"");
1939 SetEnv(index_var_,
"");
1940 SetEnv(total_var_,
"5");
1946 TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereAreFiveShards) {
1948 const int num_tests = 17;
1949 const int num_shards = 5;
1952 for (
int test_id = 0; test_id < num_tests; test_id++) {
1953 int prev_selected_shard_index = -1;
1954 for (
int shard_index = 0; shard_index < num_shards; shard_index++) {
1956 if (prev_selected_shard_index < 0) {
1957 prev_selected_shard_index = shard_index;
1959 ADD_FAILURE() <<
"Shard " << prev_selected_shard_index <<
" and "
1960 << shard_index <<
" are both selected to run test " << test_id;
1968 for (
int shard_index = 0; shard_index < num_shards; shard_index++) {
1969 int num_tests_on_shard = 0;
1970 for (
int test_id = 0; test_id < num_tests; test_id++) {
1971 num_tests_on_shard +=
1974 EXPECT_GE(num_tests_on_shard, num_tests / num_shards);
1988 TEST(UnitTestTest, CanGetOriginalWorkingDir) {
1989 ASSERT_TRUE(UnitTest::GetInstance()->original_working_dir() !=
nullptr);
1990 EXPECT_STRNE(UnitTest::GetInstance()->original_working_dir(),
"");
1993 TEST(UnitTestTest, ReturnsPlausibleTimestamp) {
1994 EXPECT_LT(0, UnitTest::GetInstance()->start_timestamp());
2001 void ExpectNonFatalFailureRecordingPropertyWithReservedKey(
2005 <<
"' recorded unexpectedly.";
2008 void ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2010 const TestInfo* test_info = UnitTest::GetInstance()->current_test_info();
2012 ExpectNonFatalFailureRecordingPropertyWithReservedKey(*test_info->
result(),
2016 void ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2019 UnitTest::GetInstance()->current_test_suite();
2021 ExpectNonFatalFailureRecordingPropertyWithReservedKey(
2025 void ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2027 ExpectNonFatalFailureRecordingPropertyWithReservedKey(
2028 UnitTest::GetInstance()->ad_hoc_test_result(),
key);
2034 class UnitTestRecordPropertyTest :
2037 static void SetUpTestSuite() {
2038 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2040 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2042 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2044 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2046 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2048 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2051 Test::RecordProperty(
"test_case_key_1",
"1");
2054 UnitTest::GetInstance()->current_test_suite();
2060 test_suite->ad_hoc_test_result().GetTestProperty(0).key());
2062 test_suite->ad_hoc_test_result().GetTestProperty(0).value());
2067 TEST_F(UnitTestRecordPropertyTest, OnePropertyFoundWhenAdded) {
2068 UnitTestRecordProperty(
"key_1",
"1");
2070 ASSERT_EQ(1, unit_test_.ad_hoc_test_result().test_property_count());
2073 unit_test_.ad_hoc_test_result().GetTestProperty(0).key());
2075 unit_test_.ad_hoc_test_result().GetTestProperty(0).value());
2079 TEST_F(UnitTestRecordPropertyTest, MultiplePropertiesFoundWhenAdded) {
2080 UnitTestRecordProperty(
"key_1",
"1");
2081 UnitTestRecordProperty(
"key_2",
"2");
2083 ASSERT_EQ(2, unit_test_.ad_hoc_test_result().test_property_count());
2086 unit_test_.ad_hoc_test_result().GetTestProperty(0).key());
2087 EXPECT_STREQ(
"1", unit_test_.ad_hoc_test_result().GetTestProperty(0).value());
2090 unit_test_.ad_hoc_test_result().GetTestProperty(1).key());
2091 EXPECT_STREQ(
"2", unit_test_.ad_hoc_test_result().GetTestProperty(1).value());
2095 TEST_F(UnitTestRecordPropertyTest, OverridesValuesForDuplicateKeys) {
2096 UnitTestRecordProperty(
"key_1",
"1");
2097 UnitTestRecordProperty(
"key_2",
"2");
2098 UnitTestRecordProperty(
"key_1",
"12");
2099 UnitTestRecordProperty(
"key_2",
"22");
2101 ASSERT_EQ(2, unit_test_.ad_hoc_test_result().test_property_count());
2104 unit_test_.ad_hoc_test_result().GetTestProperty(0).key());
2106 unit_test_.ad_hoc_test_result().GetTestProperty(0).value());
2109 unit_test_.ad_hoc_test_result().GetTestProperty(1).key());
2111 unit_test_.ad_hoc_test_result().GetTestProperty(1).value());
2114 TEST_F(UnitTestRecordPropertyTest,
2115 AddFailureInsideTestsWhenUsingTestSuiteReservedKeys) {
2116 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2118 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2120 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2122 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2124 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2126 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2130 TEST_F(UnitTestRecordPropertyTest,
2131 AddRecordWithReservedKeysGeneratesCorrectPropertyList) {
2133 Test::RecordProperty(
"name",
"1"),
2134 "'classname', 'name', 'status', 'time', 'type_param', 'value_param',"
2135 " 'file', and 'line' are reserved");
2138 class UnitTestRecordPropertyTestEnvironment :
public Environment {
2140 void TearDown()
override {
2141 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2143 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2145 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2147 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2149 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2151 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2153 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2155 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2173 bool IsEven(
int n) {
2174 return (
n % 2) == 0;
2178 struct IsEvenFunctor {
2179 bool operator()(
int n) {
return IsEven(
n); }
2190 msg << expr <<
" evaluates to " <<
n <<
", which is not even.";
2215 struct AssertIsEvenFunctor {
2217 return AssertIsEven(expr,
n);
2222 bool SumIsEven2(
int n1,
int n2) {
2223 return IsEven(n1 + n2);
2228 struct SumIsEven3Functor {
2229 bool operator()(
int n1,
int n2,
int n3) {
2230 return IsEven(n1 + n2 + n3);
2237 const char* e1,
const char* e2,
const char* e3,
const char* e4,
2238 int n1,
int n2,
int n3,
int n4) {
2239 const int sum = n1 + n2 + n3 + n4;
2245 msg << e1 <<
" + " << e2 <<
" + " << e3 <<
" + " << e4
2246 <<
" (" << n1 <<
" + " << n2 <<
" + " << n3 <<
" + " << n4
2247 <<
") evaluates to " <<
sum <<
", which is not even.";
2253 struct AssertSumIsEven5Functor {
2255 const char* e1,
const char* e2,
const char* e3,
const char* e4,
2256 const char* e5,
int n1,
int n2,
int n3,
int n4,
int n5) {
2257 const int sum = n1 + n2 + n3 + n4 + n5;
2263 msg << e1 <<
" + " << e2 <<
" + " << e3 <<
" + " << e4 <<
" + " << e5
2265 << n1 <<
" + " << n2 <<
" + " << n3 <<
" + " << n4 <<
" + " << n5
2266 <<
") evaluates to " <<
sum <<
", which is not even.";
2275 TEST(Pred1Test, WithoutFormat) {
2277 EXPECT_PRED1(IsEvenFunctor(), 2) <<
"This failure is UNEXPECTED!";
2282 EXPECT_PRED1(IsEven, 5) <<
"This failure is expected.";
2283 },
"This failure is expected.");
2285 "evaluates to false");
2289 TEST(Pred1Test, WithFormat) {
2293 <<
"This failure is UNEXPECTED!";
2298 "n evaluates to 5, which is not even.");
2301 },
"This failure is expected.");
2306 TEST(Pred1Test, SingleEvaluationOnFailure) {
2310 EXPECT_EQ(1,
n) <<
"The argument is not evaluated exactly once.";
2315 <<
"This failure is expected.";
2316 },
"This failure is expected.");
2317 EXPECT_EQ(2,
n) <<
"The argument is not evaluated exactly once.";
2324 TEST(PredTest, WithoutFormat) {
2326 ASSERT_PRED2(SumIsEven2, 2, 4) <<
"This failure is UNEXPECTED!";
2333 EXPECT_PRED2(SumIsEven2, n1, n2) <<
"This failure is expected.";
2334 },
"This failure is expected.");
2337 },
"evaluates to false");
2341 TEST(PredTest, WithFormat) {
2344 "This failure is UNEXPECTED!";
2354 },
"evaluates to 13, which is not even.");
2357 <<
"This failure is expected.";
2358 },
"This failure is expected.");
2363 TEST(PredTest, SingleEvaluationOnFailure) {
2368 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2369 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2377 n1++, n2++, n3++, n4++, n5++)
2378 <<
"This failure is UNEXPECTED!";
2379 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2380 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2381 EXPECT_EQ(1, n3) <<
"Argument 3 is not evaluated exactly once.";
2382 EXPECT_EQ(1, n4) <<
"Argument 4 is not evaluated exactly once.";
2383 EXPECT_EQ(1, n5) <<
"Argument 5 is not evaluated exactly once.";
2389 <<
"This failure is expected.";
2390 },
"This failure is expected.");
2391 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2392 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2393 EXPECT_EQ(1, n3) <<
"Argument 3 is not evaluated exactly once.";
2396 n1 = n2 = n3 = n4 = 0;
2399 },
"evaluates to 1, which is not even.");
2400 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2401 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2402 EXPECT_EQ(1, n3) <<
"Argument 3 is not evaluated exactly once.";
2403 EXPECT_EQ(1, n4) <<
"Argument 4 is not evaluated exactly once.";
2407 TEST(PredTest, ExpectPredEvalFailure) {
2408 std::set<int> set_a = {2, 1, 3, 4, 5};
2409 std::set<int> set_b = {0, 4, 8};
2410 const auto compare_sets = [] (std::set<int>, std::set<int>) {
return false; };
2413 "compare_sets(set_a, set_b) evaluates to false, where\nset_a evaluates "
2414 "to { 1, 2, 3, 4, 5 }\nset_b evaluates to { 0, 4, 8 }");
2420 bool IsPositive(
double x) {
2424 template <
typename T>
2425 bool IsNegative(
T x) {
2429 template <
typename T1,
typename T2>
2430 bool GreaterThan(
T1 x1,
T2 x2) {
2436 TEST(PredicateAssertionTest, AcceptsOverloadedFunction) {
2444 TEST(PredicateAssertionTest, AcceptsTemplateFunction) {
2465 template <
typename T>
2471 template <
typename T1,
typename T2>
2473 const T1& x1,
const T2& x2) {
2480 TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) {
2487 TEST(PredicateFormatAssertionTest, AcceptsTemplateFunction) {
2497 const char *
const p1 =
"good";
2501 const char p2[] =
"good";
2505 " \"bad\"\n \"good\"");
2509 TEST(StringAssertionTest, ASSERT_STREQ_Null) {
2510 ASSERT_STREQ(
static_cast<const char*
>(
nullptr),
nullptr);
2515 TEST(StringAssertionTest, ASSERT_STREQ_Null2) {
2529 "\"Hi\" vs \"Hi\"");
2556 TEST(StringAssertionTest, STREQ_Wide) {
2558 ASSERT_STREQ(
static_cast<const wchar_t*
>(
nullptr),
nullptr);
2580 },
"Expected failure");
2584 TEST(StringAssertionTest, STRNE_Wide) {
2588 EXPECT_STRNE(
static_cast<const wchar_t*
>(
nullptr),
nullptr);
2611 ASSERT_STRNE(
L"abc\x8119",
L"abc\x8120") <<
"This shouldn't happen";
2618 TEST(IsSubstringTest, ReturnsCorrectResultForCString) {
2629 TEST(IsSubstringTest, ReturnsCorrectResultForWideCString) {
2635 IsSubstring(
"",
"",
static_cast<const wchar_t*
>(
nullptr),
nullptr));
2641 TEST(IsSubstringTest, GeneratesCorrectMessageForCString) {
2643 " Actual: \"needle\"\n"
2644 "Expected: a substring of haystack_expr\n"
2645 "Which is: \"haystack\"",
2647 "needle",
"haystack").failure_message());
2652 TEST(IsSubstringTest, ReturnsCorrectResultsForStdString) {
2657 #if GTEST_HAS_STD_WSTRING
2660 TEST(IsSubstringTest, ReturnsCorrectResultForStdWstring) {
2667 TEST(IsSubstringTest, GeneratesCorrectMessageForWstring) {
2669 " Actual: L\"needle\"\n"
2670 "Expected: a substring of haystack_expr\n"
2671 "Which is: L\"haystack\"",
2673 "needle_expr",
"haystack_expr",
2677 #endif // GTEST_HAS_STD_WSTRING
2683 TEST(IsNotSubstringTest, ReturnsCorrectResultForCString) {
2690 TEST(IsNotSubstringTest, ReturnsCorrectResultForWideCString) {
2697 TEST(IsNotSubstringTest, GeneratesCorrectMessageForWideCString) {
2699 " Actual: L\"needle\"\n"
2700 "Expected: not a substring of haystack_expr\n"
2701 "Which is: L\"two needles\"",
2703 "needle_expr",
"haystack_expr",
2704 L"needle",
L"two needles").failure_message());
2709 TEST(IsNotSubstringTest, ReturnsCorrectResultsForStdString) {
2716 TEST(IsNotSubstringTest, GeneratesCorrectMessageForStdString) {
2718 " Actual: \"needle\"\n"
2719 "Expected: not a substring of haystack_expr\n"
2720 "Which is: \"two needles\"",
2722 "needle_expr",
"haystack_expr",
2723 ::
std::string(
"needle"),
"two needles").failure_message());
2726 #if GTEST_HAS_STD_WSTRING
2730 TEST(IsNotSubstringTest, ReturnsCorrectResultForStdWstring) {
2736 #endif // GTEST_HAS_STD_WSTRING
2740 template <
typename RawType>
2741 class FloatingPointTest :
public Test {
2745 RawType close_to_positive_zero;
2746 RawType close_to_negative_zero;
2747 RawType further_from_negative_zero;
2749 RawType close_to_one;
2750 RawType further_from_one;
2753 RawType close_to_infinity;
2754 RawType further_from_infinity;
2761 typedef typename Floating::Bits Bits;
2763 void SetUp()
override {
2764 const size_t max_ulps = Floating::kMaxUlps;
2767 const Bits zero_bits = Floating(0).
bits();
2770 values_.close_to_positive_zero = Floating::ReinterpretBits(
2771 zero_bits + max_ulps/2);
2772 values_.close_to_negative_zero = -Floating::ReinterpretBits(
2773 zero_bits + max_ulps - max_ulps/2);
2774 values_.further_from_negative_zero = -Floating::ReinterpretBits(
2775 zero_bits + max_ulps + 1 - max_ulps/2);
2778 const Bits one_bits = Floating(1).bits();
2781 values_.close_to_one = Floating::ReinterpretBits(one_bits + max_ulps);
2782 values_.further_from_one = Floating::ReinterpretBits(
2783 one_bits + max_ulps + 1);
2786 values_.infinity = Floating::Infinity();
2789 const Bits infinity_bits = Floating(values_.infinity).bits();
2792 values_.close_to_infinity = Floating::ReinterpretBits(
2793 infinity_bits - max_ulps);
2794 values_.further_from_infinity = Floating::ReinterpretBits(
2795 infinity_bits - max_ulps - 1);
2800 values_.nan1 = Floating::ReinterpretBits(Floating::kExponentBitMask
2801 | (
static_cast<Bits
>(1) << (Floating::kFractionBitCount - 1)) | 1);
2802 values_.nan2 = Floating::ReinterpretBits(Floating::kExponentBitMask
2803 | (
static_cast<Bits
>(1) << (Floating::kFractionBitCount - 1)) | 200);
2807 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
2810 static TestValues values_;
2813 template <
typename RawType>
2814 typename FloatingPointTest<RawType>::TestValues
2815 FloatingPointTest<RawType>::values_;
2818 typedef FloatingPointTest<float> FloatTest;
2821 TEST_F(FloatTest, Size) {
2826 TEST_F(FloatTest, Zeros) {
2839 TEST_F(FloatTest, AlmostZeros) {
2846 static const FloatTest::TestValues&
v = this->values_;
2854 v.further_from_negative_zero);
2855 },
"v.further_from_negative_zero");
2859 TEST_F(FloatTest, SmallDiff) {
2862 "values_.further_from_one");
2866 TEST_F(FloatTest, LargeDiff) {
2875 TEST_F(FloatTest, Infinity) {
2879 "-values_.infinity");
2895 static const FloatTest::TestValues&
v = this->values_;
2909 TEST_F(FloatTest, Reflexive) {
2916 TEST_F(FloatTest, Commutative) {
2930 "The difference between 1.0f and 1.5f is 0.5, "
2931 "which exceeds 0.25f");
2941 "The difference between 1.0f and 1.5f is 0.5, "
2942 "which exceeds 0.25f");
2948 TEST_F(FloatTest, FloatLESucceeds) {
2957 TEST_F(FloatTest, FloatLEFails) {
2960 "(2.0f) <= (1.0f)");
2965 },
"(values_.further_from_one) <= (1.0f)");
2969 },
"(values_.nan1) <= (values_.infinity)");
2972 },
"(-values_.infinity) <= (values_.nan1)");
2975 },
"(values_.nan1) <= (values_.nan1)");
2979 typedef FloatingPointTest<double> DoubleTest;
2982 TEST_F(DoubleTest, Size) {
2987 TEST_F(DoubleTest, Zeros) {
3000 TEST_F(DoubleTest, AlmostZeros) {
3007 static const DoubleTest::TestValues&
v = this->values_;
3015 v.further_from_negative_zero);
3016 },
"v.further_from_negative_zero");
3020 TEST_F(DoubleTest, SmallDiff) {
3023 "values_.further_from_one");
3027 TEST_F(DoubleTest, LargeDiff) {
3036 TEST_F(DoubleTest, Infinity) {
3040 "-values_.infinity");
3049 TEST_F(DoubleTest, NaN) {
3050 static const DoubleTest::TestValues&
v = this->values_;
3062 TEST_F(DoubleTest, Reflexive) {
3069 TEST_F(DoubleTest, Commutative) {
3083 "The difference between 1.0 and 1.5 is 0.5, "
3084 "which exceeds 0.25");
3094 "The difference between 1.0 and 1.5 is 0.5, "
3095 "which exceeds 0.25");
3101 TEST_F(DoubleTest, DoubleLESucceeds) {
3110 TEST_F(DoubleTest, DoubleLEFails) {
3118 },
"(values_.further_from_one) <= (1.0)");
3122 },
"(values_.nan1) <= (values_.infinity)");
3125 },
" (-values_.infinity) <= (values_.nan1)");
3128 },
"(values_.nan1) <= (values_.nan1)");
3138 FAIL() <<
"Unexpected failure: Disabled test should not be run.";
3149 TEST(DISABLED_TestSuite, TestShouldNotRun) {
3150 FAIL() <<
"Unexpected failure: Test in disabled test case should not be run.";
3155 TEST(DISABLED_TestSuite, DISABLED_TestShouldNotRun) {
3156 FAIL() <<
"Unexpected failure: Test in disabled test case should not be run.";
3161 class DisabledTestsTest :
public Test {
3163 static void SetUpTestSuite() {
3164 FAIL() <<
"Unexpected failure: All tests disabled in test case. "
3165 "SetUpTestSuite() should not be called.";
3168 static void TearDownTestSuite() {
3169 FAIL() <<
"Unexpected failure: All tests disabled in test case. "
3170 "TearDownTestSuite() should not be called.";
3174 TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) {
3175 FAIL() <<
"Unexpected failure: Disabled test should not be run.";
3178 TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) {
3179 FAIL() <<
"Unexpected failure: Disabled test should not be run.";
3184 #if GTEST_HAS_TYPED_TEST
3186 template <
typename T>
3194 FAIL() <<
"Unexpected failure: Disabled typed test should not run.";
3197 template <
typename T>
3198 class DISABLED_TypedTest :
public Test {
3203 TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) {
3204 FAIL() <<
"Unexpected failure: Disabled typed test should not run.";
3207 #endif // GTEST_HAS_TYPED_TEST
3211 #if GTEST_HAS_TYPED_TEST_P
3213 template <
typename T>
3220 FAIL() <<
"Unexpected failure: "
3221 <<
"Disabled type-parameterized test should not run.";
3228 template <
typename T>
3229 class DISABLED_TypedTestP :
public Test {
3235 FAIL() <<
"Unexpected failure: "
3236 <<
"Disabled type-parameterized test should not run.";
3243 #endif // GTEST_HAS_TYPED_TEST_P
3247 class SingleEvaluationTest :
public Test {
3252 static void CompareAndIncrementCharPtrs() {
3258 static void CompareAndIncrementInts() {
3263 SingleEvaluationTest() {
3270 static const char*
const s1_;
3271 static const char*
const s2_;
3272 static const char* p1_;
3273 static const char* p2_;
3279 const char*
const SingleEvaluationTest::s1_ =
"01234";
3280 const char*
const SingleEvaluationTest::s2_ =
"abcde";
3281 const char* SingleEvaluationTest::p1_;
3282 const char* SingleEvaluationTest::p2_;
3288 TEST_F(SingleEvaluationTest, FailedASSERT_STREQ) {
3296 TEST_F(SingleEvaluationTest, ASSERT_STR) {
3311 TEST_F(SingleEvaluationTest, FailedASSERT_NE) {
3313 "(a_++) != (b_++)");
3319 TEST_F(SingleEvaluationTest, OtherCases) {
3348 #if GTEST_HAS_EXCEPTIONS
3350 void ThrowAnInteger() {
3355 TEST_F(SingleEvaluationTest, ExceptionTests) {
3367 },
bool),
"throws a different type");
3397 #endif // GTEST_HAS_EXCEPTIONS
3400 class NoFatalFailureTest :
public Test {
3403 void FailsNonFatal() {
3407 FAIL() <<
"some fatal failure";
3410 void DoAssertNoFatalFailureOnFails() {
3415 void DoExpectNoFatalFailureOnFails() {
3421 TEST_F(NoFatalFailureTest, NoFailure) {
3426 TEST_F(NoFatalFailureTest, NonFatalIsNoFailure) {
3429 "some non-fatal failure");
3432 "some non-fatal failure");
3435 TEST_F(NoFatalFailureTest, AssertNoFatalFailureOnFatalFailure) {
3439 DoAssertNoFatalFailureOnFails();
3442 EXPECT_EQ(TestPartResult::kFatalFailure,
3444 EXPECT_EQ(TestPartResult::kFatalFailure,
3452 TEST_F(NoFatalFailureTest, ExpectNoFatalFailureOnFatalFailure) {
3456 DoExpectNoFatalFailureOnFails();
3459 EXPECT_EQ(TestPartResult::kFatalFailure,
3461 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3463 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3473 TEST_F(NoFatalFailureTest, MessageIsStreamable) {
3480 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3482 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3492 std::string EditsToString(
const std::vector<EditType>& edits) {
3494 for (
size_t i = 0;
i < edits.size(); ++
i) {
3495 static const char kEdits[] =
" +-/";
3496 out.append(1, kEdits[edits[
i]]);
3502 std::vector<size_t>
out;
3503 for (
size_t i = 0;
i <
str.size(); ++
i) {
3504 out.push_back(
static_cast<size_t>(
str[
i]));
3510 std::vector<std::string>
out;
3511 for (
size_t i = 0;
i <
str.size(); ++
i) {
3512 out.push_back(
str.substr(
i, 1));
3517 TEST(EditDistance, TestSuites) {
3522 const char* expected_edits;
3523 const char* expected_diff;
3525 static const Case kCases[] = {
3527 {__LINE__,
"A",
"A",
" ",
""},
3528 {__LINE__,
"ABCDE",
"ABCDE",
" ",
""},
3530 {__LINE__,
"X",
"XA",
" +",
"@@ +1,2 @@\n X\n+A\n"},
3531 {__LINE__,
"X",
"XABCD",
" ++++",
"@@ +1,5 @@\n X\n+A\n+B\n+C\n+D\n"},
3533 {__LINE__,
"XA",
"X",
" -",
"@@ -1,2 @@\n X\n-A\n"},
3534 {__LINE__,
"XABCD",
"X",
" ----",
"@@ -1,5 @@\n X\n-A\n-B\n-C\n-D\n"},
3536 {__LINE__,
"A",
"a",
"/",
"@@ -1,1 +1,1 @@\n-A\n+a\n"},
3537 {__LINE__,
"ABCD",
"abcd",
"////",
3538 "@@ -1,4 +1,4 @@\n-A\n-B\n-C\n-D\n+a\n+b\n+c\n+d\n"},
3540 {__LINE__,
"ABCDEFGH",
"ABXEGH1",
" -/ - +",
3541 "@@ -1,8 +1,7 @@\n A\n B\n-C\n-D\n+X\n E\n-F\n G\n H\n+1\n"},
3542 {__LINE__,
"AAAABCCCC",
"ABABCDCDC",
"- / + / ",
3543 "@@ -1,9 +1,9 @@\n-A\n A\n-A\n+B\n A\n B\n C\n+D\n C\n-C\n+D\n C\n"},
3544 {__LINE__,
"ABCDE",
"BCDCD",
"- +/",
3545 "@@ -1,5 +1,5 @@\n-A\n B\n C\n D\n-E\n+C\n+D\n"},
3546 {__LINE__,
"ABCDEFGHIJKL",
"BCDCDEFGJKLJK",
"- ++ -- ++",
3547 "@@ -1,4 +1,5 @@\n-A\n B\n+C\n+D\n C\n D\n"
3548 "@@ -6,7 +7,7 @@\n F\n G\n-H\n-I\n J\n K\n L\n+J\n+K\n"},
3550 for (
const Case*
c = kCases;
c->left; ++
c) {
3553 CharsToIndices(
c->right))))
3554 <<
"Left <" <<
c->left <<
"> Right <" <<
c->right <<
"> Edits <"
3556 CharsToIndices(
c->left), CharsToIndices(
c->right))) <<
">";
3558 CharsToLines(
c->right)))
3559 <<
"Left <" <<
c->left <<
"> Right <" <<
c->right <<
"> Diff <"
3569 EqFailure(
"foo",
"bar", foo_val, bar_val,
false)
3570 .failure_message());
3572 "Expected equality of these values:\n"
3580 EqFailure(
"foo",
"6", foo_val, bar_val,
false)
3581 .failure_message());
3583 "Expected equality of these values:\n"
3590 EqFailure(
"5",
"bar", foo_val, bar_val,
false)
3591 .failure_message());
3593 "Expected equality of these values:\n"
3600 EqFailure(
"5",
"6", foo_val, bar_val,
false).failure_message());
3602 "Expected equality of these values:\n"
3610 true).failure_message());
3612 "Expected equality of these values:\n"
3614 " Which is: \"x\"\n"
3616 " Which is: \"y\"\n"
3621 TEST(AssertionTest, EqFailureWithDiff) {
3623 "1\\n2XXX\\n3\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12XXX\\n13\\n14\\n15");
3625 "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n11\\n12\\n13\\n14");
3627 EqFailure(
"left",
"right", left, right,
false).failure_message());
3629 "Expected equality of these values:\n"
3632 "1\\n2XXX\\n3\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12XXX\\n13\\n14\\n15\n"
3634 " Which is: 1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n11\\n12\\n13\\n14\n"
3635 "With diff:\n@@ -1,5 +1,6 @@\n 1\n-2XXX\n+2\n 3\n+4\n 5\n 6\n"
3636 "@@ -7,8 +8,6 @@\n 8\n 9\n-10\n 11\n-12XXX\n+12\n 13\n 14\n-15\n",
3655 # pragma option push -w-ccc -w-rch
3666 TEST(AssertionTest, AssertTrueWithAssertionResult) {
3668 #ifndef __BORLANDC__
3671 "Value of: ResultIsEven(3)\n"
3672 " Actual: false (3 is odd)\n"
3677 "Value of: ResultIsEvenNoExplanation(3)\n"
3678 " Actual: false (3 is odd)\n"
3692 TEST(AssertionTest, AssertFalseWithAssertionResult) {
3694 #ifndef __BORLANDC__
3697 "Value of: ResultIsEven(2)\n"
3698 " Actual: true (2 is even)\n"
3703 "Value of: ResultIsEvenNoExplanation(2)\n"
3716 TEST(ExpectTest, ASSERT_EQ_Double) {
3729 "Expected equality of these values:\n"
3736 TEST(AssertionTest, ASSERT_EQ_NULL) {
3738 const char*
p =
nullptr;
3754 TEST(ExpectTest, ASSERT_EQ_0) {
3769 "Expected: ('a') != ('a'), "
3770 "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)");
3778 "Expected: (2) <= (0), actual: 2 vs 0");
3785 "Expected: (2) < (2), actual: 2 vs 2");
3793 "Expected: (2) >= (3), actual: 2 vs 3");
3800 "Expected: (2) > (2), actual: 2 vs 2");
3803 #if GTEST_HAS_EXCEPTIONS
3805 void ThrowNothing() {}
3811 # ifndef __BORLANDC__
3816 "Expected: ThrowAnInteger() throws an exception of type bool.\n"
3817 " Actual: it throws a different type.");
3822 "Expected: ThrowNothing() throws an exception of type bool.\n"
3823 " Actual: it throws nothing.");
3830 "Expected: ThrowAnInteger() doesn't throw an exception."
3831 "\n Actual: it throws.");
3839 "Expected: ThrowNothing() throws an exception.\n"
3840 " Actual: it doesn't.");
3843 #endif // GTEST_HAS_EXCEPTIONS
3847 TEST(AssertionTest, AssertPrecedence) {
3849 bool false_value =
false;
3859 TEST(AssertionTest, NonFixtureSubroutine) {
3861 " x\n Which is: 2");
3867 explicit Uncopyable(
int a_value) :
value_(a_value) {}
3870 bool operator==(
const Uncopyable& rhs)
const {
3871 return value() == rhs.value();
3876 Uncopyable(
const Uncopyable&);
3881 ::std::ostream&
operator<<(::std::ostream& os,
const Uncopyable&
value) {
3882 return os <<
value.value();
3886 bool IsPositiveUncopyable(
const Uncopyable&
x) {
3887 return x.value() > 0;
3891 void TestAssertNonPositive() {
3896 void TestAssertEqualsUncopyable() {
3903 TEST(AssertionTest, AssertWorksWithUncopyableObject) {
3908 "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1");
3910 "Expected equality of these values:\n"
3911 " x\n Which is: 5\n y\n Which is: -1");
3915 TEST(AssertionTest, ExpectWorksWithUncopyableObject) {
3920 "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1");
3923 "Expected equality of these values:\n"
3924 " x\n Which is: 5\n y\n Which is: -1");
3932 TEST(AssertionTest, NamedEnum) {
3940 #if !defined(__SUNPRO_CC) && !defined(__HP_aCC)
3963 # endif // GTEST_OS_LINUX
3971 EXPECT_EQ(
static_cast<int>(kCaseA),
static_cast<int>(kCaseB));
3973 # endif // GTEST_OS_LINUX
3982 "(kCaseA) >= (kCaseB)");
3993 # ifndef __BORLANDC__
3997 " kCaseB\n Which is: ");
4006 #endif // !GTEST_OS_MAC && !defined(__SUNPRO_CC)
4008 #if GTEST_OS_WINDOWS
4010 static HRESULT UnexpectedHRESULTFailure() {
4011 return E_UNEXPECTED;
4014 static HRESULT OkHRESULTSuccess() {
4018 static HRESULT FalseHRESULTSuccess() {
4026 TEST(HRESULTAssertionTest, EXPECT_HRESULT_SUCCEEDED) {
4027 EXPECT_HRESULT_SUCCEEDED(S_OK);
4028 EXPECT_HRESULT_SUCCEEDED(S_FALSE);
4031 "Expected: (UnexpectedHRESULTFailure()) succeeds.\n"
4032 " Actual: 0x8000FFFF");
4035 TEST(HRESULTAssertionTest, ASSERT_HRESULT_SUCCEEDED) {
4036 ASSERT_HRESULT_SUCCEEDED(S_OK);
4037 ASSERT_HRESULT_SUCCEEDED(S_FALSE);
4040 "Expected: (UnexpectedHRESULTFailure()) succeeds.\n"
4041 " Actual: 0x8000FFFF");
4044 TEST(HRESULTAssertionTest, EXPECT_HRESULT_FAILED) {
4045 EXPECT_HRESULT_FAILED(E_UNEXPECTED);
4048 "Expected: (OkHRESULTSuccess()) fails.\n"
4051 "Expected: (FalseHRESULTSuccess()) fails.\n"
4055 TEST(HRESULTAssertionTest, ASSERT_HRESULT_FAILED) {
4056 ASSERT_HRESULT_FAILED(E_UNEXPECTED);
4058 # ifndef __BORLANDC__
4062 "Expected: (OkHRESULTSuccess()) fails.\n"
4067 "Expected: (FalseHRESULTSuccess()) fails.\n"
4072 TEST(HRESULTAssertionTest, Streaming) {
4073 EXPECT_HRESULT_SUCCEEDED(S_OK) <<
"unexpected failure";
4074 ASSERT_HRESULT_SUCCEEDED(S_OK) <<
"unexpected failure";
4075 EXPECT_HRESULT_FAILED(E_UNEXPECTED) <<
"unexpected failure";
4076 ASSERT_HRESULT_FAILED(E_UNEXPECTED) <<
"unexpected failure";
4079 EXPECT_HRESULT_SUCCEEDED(E_UNEXPECTED) <<
"expected failure",
4080 "expected failure");
4082 # ifndef __BORLANDC__
4086 ASSERT_HRESULT_SUCCEEDED(E_UNEXPECTED) <<
"expected failure",
4087 "expected failure");
4091 EXPECT_HRESULT_FAILED(S_OK) <<
"expected failure",
4092 "expected failure");
4095 ASSERT_HRESULT_FAILED(S_OK) <<
"expected failure",
4096 "expected failure");
4099 #endif // GTEST_OS_WINDOWS
4103 # pragma option push -w-ccc -w-rch
4107 TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) {
4109 ASSERT_TRUE(
false) <<
"This should never be executed; "
4110 "It's a compilation test only.";
4126 #if GTEST_HAS_EXCEPTIONS
4129 TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
4141 TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) {
4166 #endif // GTEST_HAS_EXCEPTIONS
4168 TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) {
4171 <<
"It's a compilation test only.";
4192 TEST(AssertionSyntaxTest, WorksWithSwitch) {
4202 EXPECT_FALSE(
false) <<
"EXPECT_FALSE failed in switch case";
4209 ASSERT_EQ(1, 1) <<
"ASSERT_EQ failed in default switch handler";
4217 #if GTEST_HAS_EXCEPTIONS
4219 void ThrowAString() {
4220 throw "std::string";
4225 TEST(AssertionSyntaxTest, WorksWithConst) {
4231 #endif // GTEST_HAS_EXCEPTIONS
4245 TEST(SuccessfulAssertionTest, EXPECT) {
4251 TEST(SuccessfulAssertionTest, EXPECT_STR) {
4263 TEST(SuccessfulAssertionTest, ASSERT_STR) {
4274 TEST(AssertionWithMessageTest, EXPECT) {
4275 EXPECT_EQ(1, 1) <<
"This should succeed.";
4277 "Expected failure #1");
4278 EXPECT_LE(1, 2) <<
"This should succeed.";
4280 "Expected failure #2.");
4281 EXPECT_GE(1, 0) <<
"This should succeed.";
4283 "Expected failure #3.");
4287 "Expected failure #4.");
4290 "Expected failure #5.");
4294 "Expected failure #6.");
4295 EXPECT_NEAR(1, 1.1, 0.2) <<
"This should succeed.";
4299 ASSERT_EQ(1, 1) <<
"This should succeed.";
4300 ASSERT_NE(1, 2) <<
"This should succeed.";
4301 ASSERT_LE(1, 2) <<
"This should succeed.";
4302 ASSERT_LT(1, 2) <<
"This should succeed.";
4303 ASSERT_GE(1, 0) <<
"This should succeed.";
4305 "Expected failure.");
4308 TEST(AssertionWithMessageTest, ASSERT_STR) {
4313 "Expected failure.");
4316 TEST(AssertionWithMessageTest, ASSERT_FLOATING) {
4329 ASSERT_FALSE(
true) <<
"Expected failure: " << 2 <<
" > " << 1
4330 <<
" evaluates to " <<
true;
4331 },
"Expected failure");
4335 TEST(AssertionWithMessageTest,
FAIL) {
4342 SUCCEED() <<
"Success == " << 1;
4351 ASSERT_TRUE(
false) <<
static_cast<const char*
>(
nullptr)
4352 <<
static_cast<char*
>(
nullptr);
4357 #if GTEST_OS_WINDOWS
4359 TEST(AssertionWithMessageTest, WideStringMessage) {
4361 EXPECT_TRUE(
false) <<
L"This failure is expected.\x8119";
4362 },
"This failure is expected.");
4365 <<
L"expected too.\x8120";
4366 },
"This failure is expected too.");
4368 #endif // GTEST_OS_WINDOWS
4374 "Intentional failure #1.");
4376 "Intentional failure #2.");
4387 TEST(ExpectTest, ExpectTrueWithAssertionResult) {
4390 "Value of: ResultIsEven(3)\n"
4391 " Actual: false (3 is odd)\n"
4395 "Value of: ResultIsEvenNoExplanation(3)\n"
4396 " Actual: false (3 is odd)\n"
4405 "Intentional failure #1.");
4407 "Intentional failure #2.");
4417 TEST(ExpectTest, ExpectFalseWithAssertionResult) {
4420 "Value of: ResultIsEven(2)\n"
4421 " Actual: true (2 is even)\n"
4425 "Value of: ResultIsEvenNoExplanation(2)\n"
4439 "Expected equality of these values:\n"
4450 TEST(ExpectTest, EXPECT_EQ_Double) {
4460 TEST(ExpectTest, EXPECT_EQ_NULL) {
4462 const char*
p =
nullptr;
4478 TEST(ExpectTest, EXPECT_EQ_0) {
4494 "Expected: ('a') != ('a'), "
4495 "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)");
4498 char*
const p0 =
nullptr;
4505 void* pv1 = (
void*)0x1234;
4506 char*
const p1 =
reinterpret_cast<char*
>(pv1);
4516 "Expected: (2) <= (0), actual: 2 vs 0");
4525 "Expected: (2) < (2), actual: 2 vs 2");
4535 "Expected: (2) >= (3), actual: 2 vs 3");
4544 "Expected: (2) > (2), actual: 2 vs 2");
4549 #if GTEST_HAS_EXCEPTIONS
4555 "Expected: ThrowAnInteger() throws an exception of "
4556 "type bool.\n Actual: it throws a different type.");
4559 "Expected: ThrowNothing() throws an exception of type bool.\n"
4560 " Actual: it throws nothing.");
4567 "Expected: ThrowAnInteger() doesn't throw an "
4568 "exception.\n Actual: it throws.");
4576 "Expected: ThrowNothing() throws an exception.\n"
4577 " Actual: it doesn't.");
4580 #endif // GTEST_HAS_EXCEPTIONS
4583 TEST(ExpectTest, ExpectPrecedence) {
4586 " true && false\n Which is: false");
4593 TEST(StreamableToStringTest, Scalar) {
4605 TEST(StreamableToStringTest, NullPointer) {
4611 TEST(StreamableToStringTest, CString) {
4616 TEST(StreamableToStringTest, NullCString) {
4624 TEST(StreamableTest,
string) {
4626 "This failure message is a std::string, and is expected.");
4633 TEST(StreamableTest, stringWithEmbeddedNUL) {
4634 static const char char_array_with_nul[] =
4635 "Here's a NUL\0 and some more string";
4636 static const std::string string_with_nul(char_array_with_nul,
4637 sizeof(char_array_with_nul)
4640 "Here's a NUL\\0 and some more string");
4644 TEST(StreamableTest, NULChar) {
4646 FAIL() <<
"A NUL" <<
'\0' <<
" and some more string";
4647 },
"A NUL\\0 and some more string");
4651 TEST(StreamableTest,
int) {
4661 TEST(StreamableTest, NullCharPtr) {
4667 TEST(StreamableTest, BasicIoManip) {
4669 FAIL() <<
"Line 1." << std::endl
4670 <<
"A NUL char " << std::ends << std::flush <<
" in line 2.";
4671 },
"Line 1.\nA NUL char \\0 in line 2.");
4676 void AddFailureHelper(
bool* aborted) {
4684 bool aborted =
true;
4686 "Intentional failure.");
4710 "Intentional failure.");
4729 SUCCEED() <<
"Explicit success.";
4743 bool false_value =
false;
4745 },
" false_value\n Which is: false\n true");
4756 TEST(EqAssertionTest, Time_T) {
4758 static_cast<time_t
>(0));
4760 static_cast<time_t
>(1234)),
4767 const char ch =
'b';
4769 " ch\n Which is: 'b'");
4771 " ch\n Which is: 'b'");
4775 TEST(EqAssertionTest, WideChar) {
4779 "Expected equality of these values:\n"
4781 " Which is: L'\0' (0, 0x0)\n"
4783 " Which is: L'x' (120, 0x78)");
4785 static wchar_t wchar;
4791 " wchar\n Which is: L'");
4795 TEST(EqAssertionTest, StdString) {
4811 char*
const p1 =
const_cast<char*
>(
"foo");
4820 " str3\n Which is: \"A \\0 in the middle\"");
4823 #if GTEST_HAS_STD_WSTRING
4826 TEST(EqAssertionTest, StdWideString) {
4834 const wchar_t kTestX8119[] = {
'T',
'e',
's',
't', 0x8119,
'\0' };
4839 const wchar_t kTestX8120[] = {
'T',
'e',
's',
't', 0x8120,
'\0' };
4847 wstr3.at(2) =
L'\0';
4858 #endif // GTEST_HAS_STD_WSTRING
4861 TEST(EqAssertionTest, CharPointer) {
4862 char*
const p0 =
nullptr;
4867 void* pv1 = (
void*)0x1234;
4868 void* pv2 = (
void*)0xABC0;
4869 char*
const p1 =
reinterpret_cast<char*
>(pv1);
4870 char*
const p2 =
reinterpret_cast<char*
>(pv2);
4878 reinterpret_cast<char*
>(0xABC0)),
4883 TEST(EqAssertionTest, WideCharPointer) {
4884 wchar_t*
const p0 =
nullptr;
4889 void* pv1 = (
void*)0x1234;
4890 void* pv2 = (
void*)0xABC0;
4891 wchar_t*
const p1 =
reinterpret_cast<wchar_t*
>(pv1);
4892 wchar_t*
const p2 =
reinterpret_cast<wchar_t*
>(pv2);
4899 void* pv3 = (
void*)0x1234;
4900 void* pv4 = (
void*)0xABC0;
4901 const wchar_t* p3 =
reinterpret_cast<const wchar_t*
>(pv3);
4902 const wchar_t* p4 =
reinterpret_cast<const wchar_t*
>(pv4);
4908 TEST(EqAssertionTest, OtherPointer) {
4909 ASSERT_EQ(
static_cast<const int*
>(
nullptr),
static_cast<const int*
>(
nullptr));
4911 reinterpret_cast<const int*
>(0x1234)),
4916 class UnprintableChar {
4918 explicit UnprintableChar(
char ch) : char_(
ch) {}
4920 bool operator==(
const UnprintableChar& rhs)
const {
4921 return char_ == rhs.char_;
4923 bool operator!=(
const UnprintableChar& rhs)
const {
4924 return char_ != rhs.char_;
4926 bool operator<(
const UnprintableChar& rhs)
const {
4927 return char_ < rhs.char_;
4929 bool operator<=(
const UnprintableChar& rhs)
const {
4930 return char_ <= rhs.char_;
4932 bool operator>(
const UnprintableChar& rhs)
const {
4933 return char_ > rhs.char_;
4935 bool operator>=(
const UnprintableChar& rhs)
const {
4936 return char_ >= rhs.char_;
4945 TEST(ComparisonAssertionTest, AcceptsUnprintableArgs) {
4946 const UnprintableChar
x(
'x'),
y(
'y');
4962 #ifndef __BORLANDC__
4965 "1-byte object <78>");
4967 "1-byte object <78>");
4970 "1-byte object <79>");
4972 "1-byte object <78>");
4974 "1-byte object <79>");
4986 int Bar()
const {
return 1; }
5001 class FRIEND_TEST_Test2 :
public Test {
5018 class TestLifeCycleTest :
public Test {
5022 TestLifeCycleTest() {
count_++; }
5026 ~TestLifeCycleTest()
override {
count_--; }
5038 TEST_F(TestLifeCycleTest, Test1) {
5045 TEST_F(TestLifeCycleTest, Test2) {
5056 TEST(AssertionResultTest, CopyConstructorWorksWhenNotOptimied) {
5066 EXPECT_EQ(
static_cast<bool>(r3),
static_cast<bool>(r1));
5072 TEST(AssertionResultTest, ConstructionWorks) {
5095 TEST(AssertionResultTest, NegationWorks) {
5105 TEST(AssertionResultTest, StreamingWorks) {
5107 r <<
"abc" <<
'd' << 0 <<
true;
5111 TEST(AssertionResultTest, CanStreamOstreamManipulators) {
5113 r <<
"Data" << std::endl << std::flush << std::ends <<
"Will be visible";
5119 TEST(AssertionResultTest, ConstructibleFromContextuallyConvertibleToBool) {
5120 struct ExplicitlyConvertibleToBool {
5121 explicit operator bool()
const {
return value; }
5124 ExplicitlyConvertibleToBool v1 = {
false};
5125 ExplicitlyConvertibleToBool v2 = {
true};
5134 TEST(AssertionResultTest, ConstructibleFromImplicitlyConvertible) {
5150 return os << val.x();
5153 const Base* pointer) {
5154 return os <<
"(" << pointer->x() <<
")";
5157 TEST(MessageTest, CanStreamUserTypeInGlobalNameSpace) {
5168 class MyTypeInUnnamedNameSpace :
public Base {
5170 explicit MyTypeInUnnamedNameSpace(
int an_x): Base(an_x) {}
5173 const MyTypeInUnnamedNameSpace& val) {
5174 return os << val.x();
5177 const MyTypeInUnnamedNameSpace* pointer) {
5178 return os <<
"(" << pointer->x() <<
")";
5182 TEST(MessageTest, CanStreamUserTypeInUnnamedNameSpace) {
5184 MyTypeInUnnamedNameSpace
a(1);
5199 return os << val.x();
5203 return os <<
"(" << pointer->x() <<
")";
5207 TEST(MessageTest, CanStreamUserTypeInUserNameSpace) {
5225 return os << val.x();
5229 return os <<
"(" << pointer->x() <<
")";
5232 TEST(MessageTest, CanStreamUserTypeInUserNameSpaceWithStreamOperatorInGlobal) {
5243 char*
const p1 =
nullptr;
5244 unsigned char*
const p2 =
nullptr;
5246 double* p4 =
nullptr;
5250 msg << p1 << p2 << p3 << p4 << p5 << p6;
5252 msg.GetString().c_str());
5258 const wchar_t* const_wstr =
nullptr;
5263 wchar_t* wstr =
nullptr;
5268 const_wstr =
L"abc\x8119";
5273 wstr =
const_cast<wchar_t*
>(const_wstr);
5292 if (strcmp(test_name, test_info->
name()) == 0)
5300 return test_info->
result();
5306 const TestInfo*
const test_info = GetTestInfo(
"Names");
5314 const TestInfo*
const test_info = GetTestInfo(
"result");
5317 ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
5320 ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
5323 #define VERIFY_CODE_LOCATION \
5324 const int expected_line = __LINE__ - 1; \
5325 const TestInfo* const test_info = GetUnitTestImpl()->current_test_info(); \
5326 ASSERT_TRUE(test_info); \
5327 EXPECT_STREQ(__FILE__, test_info->file()); \
5328 EXPECT_EQ(expected_line, test_info->line())
5350 template <
typename T>
5360 template <
typename T>
5374 #undef VERIFY_CODE_LOCATION
5378 #ifndef REMOVE_LEGACY_TEST_CASEAPI
5384 printf(
"Setting up the test case . . .\n");
5401 printf(
"Tearing down the test case . . .\n");
5437 #endif // REMOVE_LEGACY_TEST_CASEAPI
5445 printf(
"Setting up the test suite . . .\n");
5462 printf(
"Tearing down the test suite . . .\n");
5674 template <
typename CharType>
5676 CharType** array2) {
5677 ASSERT_EQ(size1, size2) <<
" Array sizes different.";
5679 for (
int i = 0;
i != size1;
i++) {
5707 template <
typename CharType>
5709 int argc2,
const CharType** argv2,
5710 const Flags& expected,
bool should_print_help) {
5714 # if GTEST_HAS_STREAM_REDIRECTION
5721 # if GTEST_HAS_STREAM_REDIRECTION
5736 # if GTEST_HAS_STREAM_REDIRECTION
5737 const char*
const expected_help_fragment =
5738 "This program contains tests written using";
5739 if (should_print_help) {
5743 expected_help_fragment, captured_stdout);
5745 # endif // GTEST_HAS_STREAM_REDIRECTION
5753 # define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \
5754 TestParsingFlags(sizeof(argv1)/sizeof(*argv1) - 1, argv1, \
5755 sizeof(argv2)/sizeof(*argv2) - 1, argv2, \
5756 expected, should_print_help)
5761 const char* argv[] = {
nullptr};
5763 const char* argv2[] = {
nullptr};
5770 const char* argv[] = {
"foo.exe",
nullptr};
5772 const char* argv2[] = {
"foo.exe",
nullptr};
5779 const char* argv[] = {
"foo.exe",
"--gtest_filter",
nullptr};
5781 const char* argv2[] = {
"foo.exe",
"--gtest_filter",
nullptr};
5788 const char* argv[] = {
"foo.exe",
"--gtest_filter=",
nullptr};
5790 const char* argv2[] = {
"foo.exe",
nullptr};
5797 const char* argv[] = {
"foo.exe",
"--gtest_filter=abc",
nullptr};
5799 const char* argv2[] = {
"foo.exe",
nullptr};
5806 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure",
nullptr};
5808 const char* argv2[] = {
"foo.exe",
nullptr};
5815 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=0",
nullptr};
5817 const char* argv2[] = {
"foo.exe",
nullptr};
5824 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=f",
nullptr};
5826 const char* argv2[] = {
"foo.exe",
nullptr};
5833 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=F",
nullptr};
5835 const char* argv2[] = {
"foo.exe",
nullptr};
5843 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=1",
nullptr};
5845 const char* argv2[] = {
"foo.exe",
nullptr};
5852 const char* argv[] = {
"foo.exe",
"--gtest_catch_exceptions",
nullptr};
5854 const char* argv2[] = {
"foo.exe",
nullptr};
5861 const char* argv[] = {
"foo.exe",
"--gtest_death_test_use_fork",
nullptr};
5863 const char* argv2[] = {
"foo.exe",
nullptr};
5871 const char* argv[] = {
"foo.exe",
"--gtest_filter=a",
"--gtest_filter=b",
5874 const char* argv2[] = {
"foo.exe",
nullptr};
5881 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure",
5883 "--gtest_filter=b",
nullptr};
5885 const char* argv2[] = {
"foo.exe",
"bar",
nullptr};
5888 flags.break_on_failure =
true;
5895 const char* argv[] = {
"foo.exe",
"--gtest_list_tests",
nullptr};
5897 const char* argv2[] = {
"foo.exe",
nullptr};
5904 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=1",
nullptr};
5906 const char* argv2[] = {
"foo.exe",
nullptr};
5913 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=0",
nullptr};
5915 const char* argv2[] = {
"foo.exe",
nullptr};
5922 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=f",
nullptr};
5924 const char* argv2[] = {
"foo.exe",
nullptr};
5931 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=F",
nullptr};
5933 const char* argv2[] = {
"foo.exe",
nullptr};
5940 const char* argv[] = {
"foo.exe",
"--gtest_output",
nullptr};
5942 const char* argv2[] = {
"foo.exe",
"--gtest_output",
nullptr};
5949 const char* argv[] = {
"foo.exe",
"--gtest_output=xml",
nullptr};
5951 const char* argv2[] = {
"foo.exe",
nullptr};
5958 const char* argv[] = {
"foo.exe",
"--gtest_output=xml:file",
nullptr};
5960 const char* argv2[] = {
"foo.exe",
nullptr};
5967 const char* argv[] = {
"foo.exe",
"--gtest_output=xml:directory/path/",
5970 const char* argv2[] = {
"foo.exe",
nullptr};
5978 const char* argv[] = {
"foo.exe",
"--gtest_print_time",
nullptr};
5980 const char* argv2[] = {
"foo.exe",
nullptr};
5987 const char* argv[] = {
"foo.exe",
"--gtest_print_time=1",
nullptr};
5989 const char* argv2[] = {
"foo.exe",
nullptr};
5996 const char* argv[] = {
"foo.exe",
"--gtest_print_time=0",
nullptr};
5998 const char* argv2[] = {
"foo.exe",
nullptr};
6005 const char* argv[] = {
"foo.exe",
"--gtest_print_time=f",
nullptr};
6007 const char* argv2[] = {
"foo.exe",
nullptr};
6014 const char* argv[] = {
"foo.exe",
"--gtest_print_time=F",
nullptr};
6016 const char* argv2[] = {
"foo.exe",
nullptr};
6023 const char* argv[] = {
"foo.exe",
"--gtest_random_seed=1000",
nullptr};
6025 const char* argv2[] = {
"foo.exe",
nullptr};
6032 const char* argv[] = {
"foo.exe",
"--gtest_repeat=1000",
nullptr};
6034 const char* argv2[] = {
"foo.exe",
nullptr};
6041 const char* argv[] = {
"foo.exe",
"--gtest_also_run_disabled_tests",
nullptr};
6043 const char* argv2[] = {
"foo.exe",
nullptr};
6051 const char* argv[] = {
"foo.exe",
"--gtest_also_run_disabled_tests=1",
6054 const char* argv2[] = {
"foo.exe",
nullptr};
6062 const char* argv[] = {
"foo.exe",
"--gtest_also_run_disabled_tests=0",
6065 const char* argv2[] = {
"foo.exe",
nullptr};
6073 const char* argv[] = {
"foo.exe",
"--gtest_shuffle",
nullptr};
6075 const char* argv2[] = {
"foo.exe",
nullptr};
6082 const char* argv[] = {
"foo.exe",
"--gtest_shuffle=0",
nullptr};
6084 const char* argv2[] = {
"foo.exe",
nullptr};
6091 const char* argv[] = {
"foo.exe",
"--gtest_shuffle=1",
nullptr};
6093 const char* argv2[] = {
"foo.exe",
nullptr};
6100 const char* argv[] = {
"foo.exe",
"--gtest_stack_trace_depth=5",
nullptr};
6102 const char* argv2[] = {
"foo.exe",
nullptr};
6108 const char* argv[] = {
"foo.exe",
"--gtest_stream_result_to=localhost:1234",
6111 const char* argv2[] = {
"foo.exe",
nullptr};
6119 const char* argv[] = {
"foo.exe",
"--gtest_throw_on_failure",
nullptr};
6121 const char* argv2[] = {
"foo.exe",
nullptr};
6128 const char* argv[] = {
"foo.exe",
"--gtest_throw_on_failure=0",
nullptr};
6130 const char* argv2[] = {
"foo.exe",
nullptr};
6138 const char* argv[] = {
"foo.exe",
"--gtest_throw_on_failure=1",
nullptr};
6140 const char* argv2[] = {
"foo.exe",
nullptr};
6145 # if GTEST_OS_WINDOWS
6147 TEST_F(ParseFlagsTest, WideStrings) {
6148 const wchar_t* argv[] = {
6150 L"--gtest_filter=Foo*",
6151 L"--gtest_list_tests=1",
6152 L"--gtest_break_on_failure",
6153 L"--non_gtest_flag",
6157 const wchar_t* argv2[] = {
6159 L"--non_gtest_flag",
6163 Flags expected_flags;
6164 expected_flags.break_on_failure =
true;
6165 expected_flags.filter =
"Foo*";
6166 expected_flags.list_tests =
true;
6170 # endif // GTEST_OS_WINDOWS
6172 #if GTEST_USE_OWN_FLAGFILE_FLAG_
6173 class FlagfileTest :
public ParseFlagsTest {
6175 virtual void SetUp() {
6185 virtual void TearDown() {
6209 const char* argv[] = {
"foo.exe", flagfile_flag.c_str(),
nullptr};
6211 const char* argv2[] = {
"foo.exe",
nullptr};
6217 TEST_F(FlagfileTest, FilterNonEmpty) {
6223 const char* argv[] = {
"foo.exe", flagfile_flag.c_str(),
nullptr};
6225 const char* argv2[] = {
"foo.exe",
nullptr};
6231 TEST_F(FlagfileTest, SeveralFlags) {
6239 const char* argv[] = {
"foo.exe", flagfile_flag.c_str(),
nullptr};
6241 const char* argv2[] = {
"foo.exe",
nullptr};
6243 Flags expected_flags;
6244 expected_flags.break_on_failure =
true;
6245 expected_flags.filter =
"abc";
6246 expected_flags.list_tests =
true;
6250 #endif // GTEST_USE_OWN_FLAGFILE_FLAG_
6262 <<
"There should be no tests running at this point.";
6271 <<
"There should be no tests running at this point.";
6281 <<
"There is a test running so we should have a valid TestInfo.";
6283 <<
"Expected the name of the currently running test case.";
6285 <<
"Expected the name of the currently running test.";
6296 <<
"There is a test running so we should have a valid TestInfo.";
6298 <<
"Expected the name of the currently running test case.";
6300 <<
"Expected the name of the currently running test.";
6325 TEST(NestedTestingNamespaceTest, Success) {
6326 EXPECT_EQ(1, 1) <<
"This shouldn't fail.";
6330 TEST(NestedTestingNamespaceTest, Failure) {
6332 "This failure is expected.");
6344 void SetUp()
override { Test::SetUp(); }
6350 TEST(StreamingAssertionsTest, Unconditional) {
6351 SUCCEED() <<
"expected success";
6353 "expected failure");
6355 "expected failure");
6360 # pragma option push -w-ccc -w-rch
6363 TEST(StreamingAssertionsTest, Truth) {
6367 "expected failure");
6369 "expected failure");
6372 TEST(StreamingAssertionsTest, Truth2) {
6376 "expected failure");
6378 "expected failure");
6386 TEST(StreamingAssertionsTest, IntegerEquals) {
6387 EXPECT_EQ(1, 1) <<
"unexpected failure";
6388 ASSERT_EQ(1, 1) <<
"unexpected failure";
6390 "expected failure");
6392 "expected failure");
6395 TEST(StreamingAssertionsTest, IntegerLessThan) {
6396 EXPECT_LT(1, 2) <<
"unexpected failure";
6397 ASSERT_LT(1, 2) <<
"unexpected failure";
6399 "expected failure");
6401 "expected failure");
6404 TEST(StreamingAssertionsTest, StringsEqual) {
6408 "expected failure");
6410 "expected failure");
6413 TEST(StreamingAssertionsTest, StringsNotEqual) {
6417 "expected failure");
6419 "expected failure");
6422 TEST(StreamingAssertionsTest, StringsEqualIgnoringCase) {
6426 "expected failure");
6428 "expected failure");
6431 TEST(StreamingAssertionsTest, StringNotEqualIgnoringCase) {
6435 "expected failure");
6437 "expected failure");
6440 TEST(StreamingAssertionsTest, FloatingPointEquals) {
6444 "expected failure");
6446 "expected failure");
6449 #if GTEST_HAS_EXCEPTIONS
6452 EXPECT_THROW(ThrowAnInteger(),
int) <<
"unexpected failure";
6453 ASSERT_THROW(ThrowAnInteger(),
int) <<
"unexpected failure";
6455 "expected failure",
"expected failure");
6457 "expected failure",
"expected failure");
6460 TEST(StreamingAssertionsTest, NoThrow) {
6464 "expected failure",
"expected failure");
6466 "expected failure",
"expected failure");
6469 TEST(StreamingAssertionsTest, AnyThrow) {
6473 "expected failure",
"expected failure");
6475 "expected failure",
"expected failure");
6478 #endif // GTEST_HAS_EXCEPTIONS
6482 TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsYes) {
6485 SetEnv(
"TERM",
"xterm");
6489 SetEnv(
"TERM",
"dumb");
6494 TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsAliasOfYes) {
6495 SetEnv(
"TERM",
"dumb");
6507 TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsNo) {
6510 SetEnv(
"TERM",
"xterm");
6514 SetEnv(
"TERM",
"dumb");
6519 TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsInvalid) {
6520 SetEnv(
"TERM",
"xterm");
6532 TEST(ColoredOutputTest, UsesColorsWhenStdoutIsTty) {
6535 SetEnv(
"TERM",
"xterm");
6540 TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) {
6543 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
6546 SetEnv(
"TERM",
"dumb");
6552 SetEnv(
"TERM",
"xterm");
6558 SetEnv(
"TERM",
"dumb");
6561 SetEnv(
"TERM",
"emacs");
6564 SetEnv(
"TERM",
"vt100");
6567 SetEnv(
"TERM",
"xterm-mono");
6570 SetEnv(
"TERM",
"xterm");
6573 SetEnv(
"TERM",
"xterm-color");
6576 SetEnv(
"TERM",
"xterm-256color");
6579 SetEnv(
"TERM",
"screen");
6582 SetEnv(
"TERM",
"screen-256color");
6585 SetEnv(
"TERM",
"tmux");
6588 SetEnv(
"TERM",
"tmux-256color");
6591 SetEnv(
"TERM",
"rxvt-unicode");
6594 SetEnv(
"TERM",
"rxvt-unicode-256color");
6597 SetEnv(
"TERM",
"linux");
6600 SetEnv(
"TERM",
"cygwin");
6602 #endif // GTEST_OS_WINDOWS
6609 StaticAssertTypeEq<const int, const int>();
6613 template <
typename T>
6619 TEST(StaticAssertTypeEqTest, WorksInClass) {
6627 TEST(StaticAssertTypeEqTest, CompilesForEqualTypes) {
6628 StaticAssertTypeEq<int, IntAlias>();
6629 StaticAssertTypeEq<int*, IntAlias*>();
6632 TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsNoFailure) {
6638 TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsOnlyFatalFailure) {
6640 const bool has_nonfatal_failure = HasNonfatalFailure();
6641 ClearCurrentTestPartResults();
6645 TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) {
6647 const bool has_nonfatal_failure = HasNonfatalFailure();
6648 ClearCurrentTestPartResults();
6652 TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) {
6655 const bool has_nonfatal_failure = HasNonfatalFailure();
6656 ClearCurrentTestPartResults();
6665 TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody) {
6669 TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody2) {
6672 ClearCurrentTestPartResults();
6676 TEST(HasFailureTest, ReturnsFalseWhenThereIsNoFailure) {
6680 TEST(HasFailureTest, ReturnsTrueWhenThereIsFatalFailure) {
6682 const bool has_failure = HasFailure();
6683 ClearCurrentTestPartResults();
6687 TEST(HasFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) {
6689 const bool has_failure = HasFailure();
6690 ClearCurrentTestPartResults();
6694 TEST(HasFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) {
6697 const bool has_failure = HasFailure();
6698 ClearCurrentTestPartResults();
6705 TEST(HasFailureTest, WorksOutsideOfTestBody) {
6709 TEST(HasFailureTest, WorksOutsideOfTestBody2) {
6712 ClearCurrentTestPartResults();
6739 TEST(TestEventListenersTest, ConstructionWorks) {
6742 EXPECT_TRUE(TestEventListenersAccessor::GetRepeater(&listeners) !=
nullptr);
6749 TEST(TestEventListenersTest, DestructionWorks) {
6750 bool default_result_printer_is_destroyed =
false;
6751 bool default_xml_printer_is_destroyed =
false;
6752 bool extra_listener_is_destroyed =
false;
6754 new TestListener(
nullptr, &default_result_printer_is_destroyed);
6756 new TestListener(
nullptr, &default_xml_printer_is_destroyed);
6758 new TestListener(
nullptr, &extra_listener_is_destroyed);
6762 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners,
6763 default_result_printer);
6764 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners,
6765 default_xml_printer);
6766 listeners.
Append(extra_listener);
6776 int on_start_counter = 0;
6777 bool is_destroyed =
false;
6781 listeners.
Append(listener);
6782 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6783 *UnitTest::GetInstance());
6829 TEST(EventListenerTest, AppendKeepsOrder) {
6830 std::vector<std::string> vec;
6836 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6837 *UnitTest::GetInstance());
6844 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramEnd(
6845 *UnitTest::GetInstance());
6852 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationStart(
6853 *UnitTest::GetInstance(), 0);
6860 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationEnd(
6861 *UnitTest::GetInstance(), 0);
6870 TEST(TestEventListenersTest, Release) {
6871 int on_start_counter = 0;
6872 bool is_destroyed =
false;
6879 listeners.
Append(listener);
6881 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6882 *UnitTest::GetInstance());
6891 TEST(EventListenerTest, SuppressEventForwarding) {
6892 int on_start_counter = 0;
6896 listeners.
Append(listener);
6897 ASSERT_TRUE(TestEventListenersAccessor::EventForwardingEnabled(listeners));
6898 TestEventListenersAccessor::SuppressEventForwarding(&listeners);
6899 ASSERT_FALSE(TestEventListenersAccessor::EventForwardingEnabled(listeners));
6900 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6901 *UnitTest::GetInstance());
6907 TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) {
6909 GTEST_CHECK_(TestEventListenersAccessor::EventForwardingEnabled(
6911 "expected failure");
6917 TEST(EventListenerTest, default_result_printer) {
6918 int on_start_counter = 0;
6919 bool is_destroyed =
false;
6923 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener);
6927 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6928 *UnitTest::GetInstance());
6934 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners,
nullptr);
6941 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6942 *UnitTest::GetInstance());
6948 TEST(EventListenerTest, RemovingDefaultResultPrinterWorks) {
6949 int on_start_counter = 0;
6950 bool is_destroyed =
false;
6957 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener);
6964 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6965 *UnitTest::GetInstance());
6976 TEST(EventListenerTest, default_xml_generator) {
6977 int on_start_counter = 0;
6978 bool is_destroyed =
false;
6982 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener);
6986 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6987 *UnitTest::GetInstance());
6993 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners,
nullptr);
7000 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7001 *UnitTest::GetInstance());
7007 TEST(EventListenerTest, RemovingDefaultXmlGeneratorWorks) {
7008 int on_start_counter = 0;
7009 bool is_destroyed =
false;
7016 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener);
7023 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7024 *UnitTest::GetInstance());
7042 "An expected failure");
7048 "An expected failure");
7050 "An expected failure");
7055 "An expected failure");
7060 "An expected failure");
7064 "An expected failure");
7066 "An expected failure");
7071 "An expected failure");
7075 "An expected failure");
7077 "An expected failure");
7087 TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) {
7095 TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) {
7101 TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) {
7114 TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) {
7120 TEST(RemoveReferenceTest, RemovesReference) {
7127 template <
typename T1,
typename T2>
7132 TEST(RemoveReferenceTest, MacroVersion) {
7133 TestGTestRemoveReference<int, int>();
7134 TestGTestRemoveReference<const char, const char&>();
7139 template <
typename T1,
typename T2>
7144 TEST(RemoveReferenceToConstTest, Works) {
7145 TestGTestRemoveReferenceAndConst<int, int>();
7146 TestGTestRemoveReferenceAndConst<double, double&>();
7147 TestGTestRemoveReferenceAndConst<char, const char>();
7148 TestGTestRemoveReferenceAndConst<char, const char&>();
7149 TestGTestRemoveReferenceAndConst<const char*, const char*>();
7154 template <
typename T1,
typename T2>
7159 TEST(GTestReferenceToConstTest, Works) {
7160 TestGTestReferenceToConst<const char&, char>();
7161 TestGTestReferenceToConst<const int&, const int>();
7162 TestGTestReferenceToConst<const double&, double>();
7163 TestGTestReferenceToConst<const std::string&, const std::string&>();
7171 TEST(IsContainerTestTest, WorksForNonContainer) {
7177 TEST(IsContainerTestTest, WorksForContainer) {
7199 TEST(IsContainerTestTest, ConstOnlyContainer) {
7201 sizeof(IsContainerTest<ConstOnlyContainerWithPointerIterator>(0)));
7203 sizeof(IsContainerTest<ConstOnlyContainerWithClassIterator>(0)));
7223 TEST(ArrayEqTest, WorksForDegeneratedArrays) {
7228 TEST(ArrayEqTest, WorksForOneDimensionalArrays) {
7230 const int a[] = { 0, 1 };
7231 long b[] = { 0, 1 };
7240 TEST(ArrayEqTest, WorksForTwoDimensionalArrays) {
7241 const char a[][3] = {
"hi",
"lo" };
7242 const char b[][3] = {
"hi",
"lo" };
7243 const char c[][3] = {
"hi",
"li" };
7254 TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) {
7255 const char a[] =
"hello";
7260 TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) {
7261 int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
7262 const int b[2] = { 2, 3 };
7265 const int c[2] = { 6, 7 };
7271 TEST(CopyArrayTest, WorksForDegeneratedArrays) {
7277 TEST(CopyArrayTest, WorksForOneDimensionalArrays) {
7278 const char a[3] =
"hi";
7280 #ifndef __BORLANDC__ // C++Builder cannot compile some array size deductions.
7290 TEST(CopyArrayTest, WorksForTwoDimensionalArrays) {
7291 const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } };
7293 #ifndef __BORLANDC__ // C++Builder cannot compile some array size deductions.
7305 TEST(NativeArrayTest, ConstructorFromArrayWorks) {
7306 const int a[3] = { 0, 1, 2 };
7312 TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) {
7313 typedef int Array[2];
7327 TEST(NativeArrayTest, TypeMembersAreCorrect) {
7331 StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>();
7335 TEST(NativeArrayTest, MethodsWork) {
7336 const int a[3] = { 0, 1, 2 };
7355 const int b1[3] = { 0, 1, 1 };
7356 const int b2[4] = { 0, 1, 2, 3 };
7361 TEST(NativeArrayTest, WorksForTwoDimensionalArray) {
7362 const char a[2][3] = {
"hi",
"lo" };
7381 (std::is_base_of<IndexSequence<0, 1, 2>, MakeIndexSequence<3>>::
value));
7391 (std::is_same<
double,
7399 int,
int,
int,
int,
int,
int,
int,
char,
int,
int,
7407 FlatTuple<int, double, const char*> tuple = {};
7412 tuple = FlatTuple<int, double, const char*>(7, 3.2,
"Foo");
7417 tuple.Get<1>() = 5.1;
7427 #define GTEST_FLAT_TUPLE_INT8 int, int, int, int, int, int, int, int,
7428 #define GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT8 GTEST_FLAT_TUPLE_INT8
7429 #define GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT16
7430 #define GTEST_FLAT_TUPLE_INT64 GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT32
7431 #define GTEST_FLAT_TUPLE_INT128 GTEST_FLAT_TUPLE_INT64 GTEST_FLAT_TUPLE_INT64
7432 #define GTEST_FLAT_TUPLE_INT256 GTEST_FLAT_TUPLE_INT128 GTEST_FLAT_TUPLE_INT128
7436 FlatTuple<GTEST_FLAT_TUPLE_INT256 int> tuple;
7439 tuple.Get<99>() = 17;
7440 tuple.Get<256>() = 1000;
7448 TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
7449 const char*
const str =
"hello";
7451 const char*
p =
str;
7460 TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
7461 const char*
const str =
"world";
7463 const char*
p =
str;
7477 FAIL() <<
"A failure happened inside SetUpTestSuite().";
7501 "DynamicUnitTestFixture",
"DynamicTest",
"TYPE",
"VALUE", __FILE__,
7506 for (
int i = 0;
i <
unittest->total_test_suite_count(); ++
i) {
7509 for (
int j = 0; j <
tests->total_test_count(); ++j) {
7518 FAIL() <<
"Didn't find the test!";