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, OnTestSuiteStart) {
116 streamer_.OnTestSuiteStart(
TestSuite(
"FooTest",
"Bar",
nullptr,
nullptr));
120 TEST_F(StreamingListenerTest, OnTestSuiteEnd) {
122 streamer_.OnTestSuiteEnd(
TestSuite(
"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_
177 class UnitTestRecordPropertyTestHelper :
public Test {
266 #if GTEST_HAS_STREAM_REDIRECTION
271 #if GTEST_IS_THREADSAFE
272 using testing::internal::ThreadWithParam;
281 for (
size_t i = 0;
i < vector.size();
i++) {
282 os << vector[
i] <<
" ";
291 TEST(GetRandomSeedFromFlagTest, HandlesZero) {
297 TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) {
305 TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) {
315 TEST(GetNextRandomSeedTest, WorksForValidInput) {
328 static void ClearCurrentTestPartResults() {
329 TestResultAccessor::ClearTestPartResults(
335 TEST(GetTypeIdTest, ReturnsSameValueForSameType) {
336 EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>());
337 EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>());
340 class SubClassOfTest :
public Test {};
341 class AnotherSubClassOfTest :
public Test {};
343 TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) {
344 EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>());
345 EXPECT_NE(GetTypeId<int>(), GetTypeId<char>());
349 EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>());
354 TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) {
385 TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) {
389 TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
397 TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
411 class FormatEpochTimeInMillisAsIso8601Test :
public Test {
419 void SetUp()
override {
430 SetTimeZone("UTC+00");
433 void TearDown()
override {
434 SetTimeZone(saved_tz_);
435 free(
const_cast<char*
>(saved_tz_));
439 static void SetTimeZone(
const char* time_zone) {
443 #if _MSC_VER || GTEST_OS_WINDOWS_MINGW
453 #if GTEST_OS_LINUX_ANDROID && __ANDROID_API__ < 21
456 setenv(
"TZ",
"UTC", 1);
460 setenv((
"TZ"), time_zone, 1);
468 const char* saved_tz_;
471 const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec;
473 TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) {
478 TEST_F(FormatEpochTimeInMillisAsIso8601Test, IncludesMillisecondsAfterDot) {
480 "2011-10-31T18:52:42.234",
484 TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) {
489 TEST_F(FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) {
494 TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) {
500 # pragma option push -w-ccc -w-rch
505 TEST(NullLiteralTest, LHSAllowsNullLiterals) {
506 EXPECT_EQ(0,
static_cast<void*
>(
nullptr));
507 ASSERT_EQ(0,
static_cast<void*
>(
nullptr));
508 EXPECT_EQ(NULL,
static_cast<void*
>(
nullptr));
509 ASSERT_EQ(NULL,
static_cast<void*
>(
nullptr));
510 EXPECT_EQ(
nullptr,
static_cast<void*
>(
nullptr));
511 ASSERT_EQ(
nullptr,
static_cast<void*
>(
nullptr));
513 const int*
const p =
nullptr;
522 struct ConvertToAll {
523 template <
typename T>
529 struct ConvertToPointer {
531 operator T*()
const {
536 struct ConvertToAllButNoPointers {
537 template <
typename T,
545 inline bool operator==(MyType
const&, MyType
const&) {
return true; }
547 TEST(NullLiteralTest, ImplicitConversion) {
548 EXPECT_EQ(ConvertToPointer{},
static_cast<void*
>(
nullptr));
549 #if !defined(__GNUC__) || defined(__clang__)
551 EXPECT_EQ(ConvertToAll{},
static_cast<void*
>(
nullptr));
554 EXPECT_EQ(ConvertToAllButNoPointers{}, MyType{});
558 #pragma clang diagnostic push
559 #if __has_warning("-Wzero-as-null-pointer-constant")
560 #pragma clang diagnostic error "-Wzero-as-null-pointer-constant"
564 TEST(NullLiteralTest, NoConversionNoWarning) {
572 #pragma clang diagnostic pop
584 TEST(CodePointToUtf8Test, CanEncodeNul) {
589 TEST(CodePointToUtf8Test, CanEncodeAscii) {
598 TEST(CodePointToUtf8Test, CanEncode8To11Bits) {
612 TEST(CodePointToUtf8Test, CanEncode12To16Bits) {
622 #if !GTEST_WIDE_STRING_USES_UTF16_
629 TEST(CodePointToUtf8Test, CanEncode17To21Bits) {
641 TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) {
645 #endif // !GTEST_WIDE_STRING_USES_UTF16_
650 TEST(WideStringToUtf8Test, CanEncodeNul) {
656 TEST(WideStringToUtf8Test, CanEncodeAscii) {
665 TEST(WideStringToUtf8Test, CanEncode8To11Bits) {
671 const wchar_t s[] = { 0x576,
'\0' };
678 TEST(WideStringToUtf8Test, CanEncode12To16Bits) {
680 const wchar_t s1[] = { 0x8D3,
'\0' };
685 const wchar_t s2[] = { 0xC74D,
'\0' };
691 TEST(WideStringToUtf8Test, StopsOnNulCharacter) {
697 TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) {
701 #if !GTEST_WIDE_STRING_USES_UTF16_
705 TEST(WideStringToUtf8Test, CanEncode17To21Bits) {
716 TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) {
720 #else // !GTEST_WIDE_STRING_USES_UTF16_
723 TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) {
724 const wchar_t s[] = { 0xD801, 0xDC00,
'\0' };
730 TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) {
732 const wchar_t s1[] = { 0xD800,
'\0' };
735 const wchar_t s2[] = { 0xD800,
'M',
'\0' };
738 const wchar_t s3[] = { 0xDC00,
'P',
'Q',
'R',
'\0' };
741 #endif // !GTEST_WIDE_STRING_USES_UTF16_
744 #if !GTEST_WIDE_STRING_USES_UTF16_
745 TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) {
746 const wchar_t s[] = { 0x108634, 0xC74D,
'\n', 0x576, 0x8D3, 0x108634,
'\0'};
757 TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) {
758 const wchar_t s[] = { 0xC74D,
'\n', 0x576, 0x8D3,
'\0'};
760 "\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93",
763 #endif // !GTEST_WIDE_STRING_USES_UTF16_
767 TEST(RandomDeathTest, GeneratesCrashesOnInvalidRange) {
771 "Cannot generate a number in the range \\[0, 0\\)");
774 "Generation of a number in \\[0, 2147483649\\) was requested, "
775 "but this can only generate numbers in \\[0, 2147483648\\)");
781 for (
int i = 0;
i < 10;
i++) {
782 EXPECT_LT(random.Generate(kRange), kRange) <<
" for iteration " <<
i;
786 for (
int i = 0;
i < 10;
i++) {
787 EXPECT_LT(random2.Generate(kRange), kRange) <<
" for iteration " <<
i;
792 constexpr
int kSeed = 123;
793 constexpr
int kArraySize = 10;
798 for (
int i = 0;
i < kArraySize;
i++) {
799 values[
i] = random.Generate(kRange);
802 random.Reseed(kSeed);
803 for (
int i = 0;
i < kArraySize;
i++) {
812 static bool IsPositive(
int n) {
return n > 0; }
830 static int g_sum = 0;
831 static void Accumulate(
int n) { g_sum +=
n; }
873 "Invalid shuffle range start -1: must be in range \\[0, 3\\]");
876 "Invalid shuffle range start 4: must be in range \\[0, 3\\]");
879 "Invalid shuffle range finish 2: must be in range \\[3, 3\\]");
882 "Invalid shuffle range finish 4: must be in range \\[3, 3\\]");
885 class VectorShuffleTest :
public Test {
887 static const size_t kVectorSize = 20;
889 VectorShuffleTest() : random_(1) {
890 for (
int i = 0; i < static_cast<int>(kVectorSize);
i++) {
891 vector_.push_back(
i);
896 if (kVectorSize != vector.size()) {
900 bool found_in_vector[kVectorSize] = {
false };
901 for (
size_t i = 0;
i < vector.size();
i++) {
902 const int e = vector[
i];
903 if (e < 0 || e >=
static_cast<int>(kVectorSize) || found_in_vector[e]) {
906 found_in_vector[
e] =
true;
914 static bool VectorIsNotCorrupt(
const TestingVector& vector) {
915 return !VectorIsCorrupt(vector);
920 if (
i != vector[
static_cast<size_t>(
i)]) {
927 static bool RangeIsUnshuffled(
929 return !RangeIsShuffled(vector,
begin,
end);
933 return RangeIsShuffled(vector, 0,
static_cast<int>(vector.size()));
936 static bool VectorIsUnshuffled(
const TestingVector& vector) {
937 return !VectorIsShuffled(vector);
944 const size_t VectorShuffleTest::kVectorSize;
946 TEST_F(VectorShuffleTest, HandlesEmptyRange) {
953 ShuffleRange(&random_, kVectorSize/2, kVectorSize/2, &vector_);
958 ShuffleRange(&random_, kVectorSize - 1, kVectorSize - 1, &vector_);
963 ShuffleRange(&random_, kVectorSize, kVectorSize, &vector_);
968 TEST_F(VectorShuffleTest, HandlesRangeOfSizeOne) {
975 ShuffleRange(&random_, kVectorSize/2, kVectorSize/2 + 1, &vector_);
980 ShuffleRange(&random_, kVectorSize - 1, kVectorSize, &vector_);
988 TEST_F(VectorShuffleTest, ShufflesEntireVector) {
996 EXPECT_NE(
static_cast<int>(kVectorSize - 1), vector_[kVectorSize - 1]);
999 TEST_F(VectorShuffleTest, ShufflesStartOfVector) {
1000 const int kRangeSize = kVectorSize/2;
1007 static_cast<int>(kVectorSize));
1010 TEST_F(VectorShuffleTest, ShufflesEndOfVector) {
1011 const int kRangeSize = kVectorSize / 2;
1012 ShuffleRange(&random_, kRangeSize, kVectorSize, &vector_);
1015 EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize);
1017 static_cast<int>(kVectorSize));
1020 TEST_F(VectorShuffleTest, ShufflesMiddleOfVector) {
1021 const int kRangeSize =
static_cast<int>(kVectorSize) / 3;
1022 ShuffleRange(&random_, kRangeSize, 2*kRangeSize, &vector_);
1025 EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize);
1026 EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, 2*kRangeSize);
1027 EXPECT_PRED3(RangeIsUnshuffled, vector_, 2 * kRangeSize,
1028 static_cast<int>(kVectorSize));
1031 TEST_F(VectorShuffleTest, ShufflesRepeatably) {
1033 for (
size_t i = 0;
i < kVectorSize;
i++) {
1034 vector2.push_back(
static_cast<int>(
i));
1037 random_.Reseed(1234);
1039 random_.Reseed(1234);
1045 for (
size_t i = 0;
i < kVectorSize;
i++) {
1046 EXPECT_EQ(vector_[
i], vector2[
i]) <<
" where i is " <<
i;
1052 TEST(AssertHelperTest, AssertHelperIsSmall) {
1059 TEST(StringTest, EndsWithCaseInsensitive) {
1060 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"foobar",
"BAR"));
1061 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"foobaR",
"bar"));
1062 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"foobar",
""));
1063 EXPECT_TRUE(String::EndsWithCaseInsensitive(
"",
""));
1065 EXPECT_FALSE(String::EndsWithCaseInsensitive(
"Foobar",
"foo"));
1066 EXPECT_FALSE(String::EndsWithCaseInsensitive(
"foobar",
"Foo"));
1067 EXPECT_FALSE(String::EndsWithCaseInsensitive(
"",
"foo"));
1073 static const wchar_t*
const kNull =
nullptr;
1076 TEST(StringTest, CaseInsensitiveWideCStringEquals) {
1077 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
nullptr,
nullptr));
1082 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
L"foobar",
L"foobar"));
1083 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
L"foobar",
L"FOOBAR"));
1084 EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(
L"FOOBAR",
L"foobar"));
1087 #if GTEST_OS_WINDOWS
1090 TEST(StringTest, ShowWideCString) {
1092 String::ShowWideCString(NULL).
c_str());
1097 # if GTEST_OS_WINDOWS_MOBILE
1098 TEST(StringTest, AnsiAndUtf16Null) {
1099 EXPECT_EQ(NULL, String::AnsiToUtf16(NULL));
1100 EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL));
1103 TEST(StringTest, AnsiAndUtf16ConvertBasic) {
1104 const char* ansi = String::Utf16ToAnsi(
L"str");
1107 const WCHAR* utf16 = String::AnsiToUtf16(
"str");
1112 TEST(StringTest, AnsiAndUtf16ConvertPathChars) {
1113 const char* ansi = String::Utf16ToAnsi(
L".:\\ \"*?");
1116 const WCHAR* utf16 = String::AnsiToUtf16(
".:\\ \"*?");
1117 EXPECT_EQ(0, wcsncmp(
L".:\\ \"*?", utf16, 3));
1120 # endif // GTEST_OS_WINDOWS_MOBILE
1122 #endif // GTEST_OS_WINDOWS
1132 TEST(TestPropertyTest, ReplaceStringValue) {
1135 property.SetValue(
"2");
1142 static void AddFatalFailure() {
1143 FAIL() <<
"Expected fatal failure.";
1146 static void AddNonfatalFailure() {
1150 class ScopedFakeTestPartResultReporterTest :
public Test {
1156 static void AddFailure(FailureMode failure) {
1160 AddNonfatalFailure();
1167 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptsTestFailures) {
1171 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
1173 AddFailure(NONFATAL_FAILURE);
1182 TEST_F(ScopedFakeTestPartResultReporterTest, DeprecatedConstructor) {
1187 AddFailure(NONFATAL_FAILURE);
1192 #if GTEST_IS_THREADSAFE
1194 class ScopedFakeTestPartResultReporterWithThreadsTest
1195 :
public ScopedFakeTestPartResultReporterTest {
1197 static void AddFailureInOtherThread(FailureMode failure) {
1198 ThreadWithParam<FailureMode>
thread(&AddFailure, failure,
nullptr);
1203 TEST_F(ScopedFakeTestPartResultReporterWithThreadsTest,
1204 InterceptsTestFailuresInAllThreads) {
1208 ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, &
results);
1209 AddFailure(NONFATAL_FAILURE);
1211 AddFailureInOtherThread(NONFATAL_FAILURE);
1222 #endif // GTEST_IS_THREADSAFE
1228 typedef ScopedFakeTestPartResultReporterTest ExpectFatalFailureTest;
1230 TEST_F(ExpectFatalFailureTest, CatchesFatalFaliure) {
1234 TEST_F(ExpectFatalFailureTest, AcceptsStdStringObject) {
1239 TEST_F(ExpectFatalFailureTest, CatchesFatalFailureOnAllThreads) {
1243 "Expected fatal failure.");
1248 # pragma option push -w-ccc
1254 int NonVoidFunction() {
1260 TEST_F(ExpectFatalFailureTest, CanBeUsedInNonVoidFunction) {
1267 void DoesNotAbortHelper(
bool* aborted) {
1279 TEST_F(ExpectFatalFailureTest, DoesNotAbort) {
1280 bool aborted =
true;
1281 DoesNotAbortHelper(&aborted);
1289 static int global_var = 0;
1290 #define GTEST_USE_UNPROTECTED_COMMA_ global_var++, global_var++
1292 TEST_F(ExpectFatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) {
1293 #ifndef __BORLANDC__
1309 typedef ScopedFakeTestPartResultReporterTest ExpectNonfatalFailureTest;
1311 TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailure) {
1313 "Expected non-fatal failure.");
1316 TEST_F(ExpectNonfatalFailureTest, AcceptsStdStringObject) {
1321 TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailureOnAllThreads) {
1325 "Expected non-fatal failure.");
1331 TEST_F(ExpectNonfatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) {
1334 AddNonfatalFailure();
1339 AddNonfatalFailure();
1343 #if GTEST_IS_THREADSAFE
1345 typedef ScopedFakeTestPartResultReporterWithThreadsTest
1346 ExpectFailureWithThreadsTest;
1348 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailureOnAllThreads) {
1350 "Expected fatal failure.");
1353 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailureOnAllThreads) {
1355 AddFailureInOtherThread(NONFATAL_FAILURE),
"Expected non-fatal failure.");
1358 #endif // GTEST_IS_THREADSAFE
1362 TEST(TestPropertyTest, ConstructorWorks) {
1368 TEST(TestPropertyTest, SetValue) {
1371 property.SetValue(
"value_2");
1379 class TestResultTest :
public Test {
1381 typedef std::vector<TestPartResult> TPRVector;
1389 void SetUp()
override {
1411 TPRVector* results1 =
const_cast<TPRVector*
>(
1412 &TestResultAccessor::test_part_results(*r1));
1413 TPRVector* results2 =
const_cast<TPRVector*
>(
1414 &TestResultAccessor::test_part_results(*r2));
1419 results1->push_back(*pr1);
1422 results2->push_back(*pr1);
1423 results2->push_back(*pr2);
1426 void TearDown()
override {
1436 static void CompareTestPartResult(
const TestPartResult& expected,
1451 TEST_F(TestResultTest, total_part_count) {
1458 TEST_F(TestResultTest, Passed) {
1465 TEST_F(TestResultTest, Failed) {
1473 typedef TestResultTest TestResultDeathTest;
1475 TEST_F(TestResultDeathTest, GetTestPartResult) {
1483 TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) {
1489 TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) {
1492 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property);
1500 TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) {
1504 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1);
1505 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2);
1517 TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) {
1523 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1_1);
1524 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2_1);
1525 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1_2);
1526 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2_2);
1539 TEST(TestResultPropertyTest, GetTestProperty) {
1544 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_1);
1545 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_2);
1546 TestResultAccessor::RecordProperty(&
test_result,
"testcase", property_3);
1577 class GTestFlagSaverTest :
public Test {
1582 static void SetUpTestSuite() {
1607 static void TearDownTestSuite() {
1614 void VerifyAndModifyFlags() {
1665 TEST_F(GTestFlagSaverTest, ModifyGTestFlags) {
1666 VerifyAndModifyFlags();
1671 TEST_F(GTestFlagSaverTest, VerifyGTestFlags) {
1672 VerifyAndModifyFlags();
1678 static void SetEnv(
const char*
name,
const char*
value) {
1679 #if GTEST_OS_WINDOWS_MOBILE
1682 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
1686 static std::map<std::string, std::string*> added_env;
1691 if (added_env.find(
name) != added_env.end()) {
1692 prev_env = added_env[
name];
1700 putenv(
const_cast<char*
>(added_env[
name]->
c_str()));
1702 #elif GTEST_OS_WINDOWS // If we are on Windows proper.
1705 if (*
value ==
'\0') {
1710 #endif // GTEST_OS_WINDOWS_MOBILE
1713 #if !GTEST_OS_WINDOWS_MOBILE
1722 TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenVariableIsNotSet) {
1727 # if !defined(GTEST_GET_INT32_FROM_ENV_)
1731 TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueOverflows) {
1732 printf(
"(expecting 2 warnings)\n");
1743 TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueIsInvalid) {
1744 printf(
"(expecting 2 warnings)\n");
1753 # endif // !defined(GTEST_GET_INT32_FROM_ENV_)
1758 TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) {
1765 #endif // !GTEST_OS_WINDOWS_MOBILE
1771 TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) {
1782 TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) {
1783 printf(
"(expecting 2 warnings)\n");
1796 TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) {
1797 printf(
"(expecting 2 warnings)\n");
1810 TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) {
1822 #if !GTEST_OS_WINDOWS_MOBILE
1823 TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) {
1830 #endif // !GTEST_OS_WINDOWS_MOBILE
1834 TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) {
1843 TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) {
1852 TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) {
1862 void SetUp()
override {
1867 void TearDown()
override {
1868 SetEnv(index_var_,
"");
1869 SetEnv(total_var_,
"");
1872 const char* index_var_;
1873 const char* total_var_;
1878 TEST_F(ShouldShardTest, ReturnsFalseWhenNeitherEnvVarIsSet) {
1879 SetEnv(index_var_,
"");
1880 SetEnv(total_var_,
"");
1887 TEST_F(ShouldShardTest, ReturnsFalseWhenTotalShardIsOne) {
1888 SetEnv(index_var_,
"0");
1889 SetEnv(total_var_,
"1");
1897 #if !GTEST_OS_WINDOWS_MOBILE
1898 TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) {
1899 SetEnv(index_var_,
"4");
1900 SetEnv(total_var_,
"22");
1904 SetEnv(index_var_,
"8");
1905 SetEnv(total_var_,
"9");
1909 SetEnv(index_var_,
"0");
1910 SetEnv(total_var_,
"9");
1914 #endif // !GTEST_OS_WINDOWS_MOBILE
1918 typedef ShouldShardTest ShouldShardDeathTest;
1920 TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) {
1921 SetEnv(index_var_,
"4");
1922 SetEnv(total_var_,
"4");
1925 SetEnv(index_var_,
"4");
1926 SetEnv(total_var_,
"-2");
1929 SetEnv(index_var_,
"5");
1930 SetEnv(total_var_,
"");
1933 SetEnv(index_var_,
"");
1934 SetEnv(total_var_,
"5");
1940 TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereAreFiveShards) {
1942 const int num_tests = 17;
1943 const int num_shards = 5;
1946 for (
int test_id = 0; test_id < num_tests; test_id++) {
1947 int prev_selected_shard_index = -1;
1948 for (
int shard_index = 0; shard_index < num_shards; shard_index++) {
1950 if (prev_selected_shard_index < 0) {
1951 prev_selected_shard_index = shard_index;
1953 ADD_FAILURE() <<
"Shard " << prev_selected_shard_index <<
" and "
1954 << shard_index <<
" are both selected to run test " << test_id;
1962 for (
int shard_index = 0; shard_index < num_shards; shard_index++) {
1963 int num_tests_on_shard = 0;
1964 for (
int test_id = 0; test_id < num_tests; test_id++) {
1965 num_tests_on_shard +=
1968 EXPECT_GE(num_tests_on_shard, num_tests / num_shards);
1982 TEST(UnitTestTest, CanGetOriginalWorkingDir) {
1983 ASSERT_TRUE(UnitTest::GetInstance()->original_working_dir() !=
nullptr);
1984 EXPECT_STRNE(UnitTest::GetInstance()->original_working_dir(),
"");
1987 TEST(UnitTestTest, ReturnsPlausibleTimestamp) {
1988 EXPECT_LT(0, UnitTest::GetInstance()->start_timestamp());
1995 void ExpectNonFatalFailureRecordingPropertyWithReservedKey(
1999 <<
"' recorded unexpectedly.";
2002 void ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2004 const TestInfo* test_info = UnitTest::GetInstance()->current_test_info();
2006 ExpectNonFatalFailureRecordingPropertyWithReservedKey(*test_info->
result(),
2010 void ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2013 UnitTest::GetInstance()->current_test_suite();
2015 ExpectNonFatalFailureRecordingPropertyWithReservedKey(
2019 void ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2021 ExpectNonFatalFailureRecordingPropertyWithReservedKey(
2022 UnitTest::GetInstance()->ad_hoc_test_result(),
key);
2028 class UnitTestRecordPropertyTest :
2031 static void SetUpTestSuite() {
2032 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2034 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2036 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2038 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2040 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2042 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTestSuite(
2045 Test::RecordProperty(
"test_case_key_1",
"1");
2048 UnitTest::GetInstance()->current_test_suite();
2054 test_suite->ad_hoc_test_result().GetTestProperty(0).key());
2056 test_suite->ad_hoc_test_result().GetTestProperty(0).value());
2061 TEST_F(UnitTestRecordPropertyTest, OnePropertyFoundWhenAdded) {
2062 UnitTestRecordProperty(
"key_1",
"1");
2064 ASSERT_EQ(1, unit_test_.ad_hoc_test_result().test_property_count());
2067 unit_test_.ad_hoc_test_result().GetTestProperty(0).key());
2069 unit_test_.ad_hoc_test_result().GetTestProperty(0).value());
2073 TEST_F(UnitTestRecordPropertyTest, MultiplePropertiesFoundWhenAdded) {
2074 UnitTestRecordProperty(
"key_1",
"1");
2075 UnitTestRecordProperty(
"key_2",
"2");
2077 ASSERT_EQ(2, unit_test_.ad_hoc_test_result().test_property_count());
2080 unit_test_.ad_hoc_test_result().GetTestProperty(0).key());
2081 EXPECT_STREQ(
"1", unit_test_.ad_hoc_test_result().GetTestProperty(0).value());
2084 unit_test_.ad_hoc_test_result().GetTestProperty(1).key());
2085 EXPECT_STREQ(
"2", unit_test_.ad_hoc_test_result().GetTestProperty(1).value());
2089 TEST_F(UnitTestRecordPropertyTest, OverridesValuesForDuplicateKeys) {
2090 UnitTestRecordProperty(
"key_1",
"1");
2091 UnitTestRecordProperty(
"key_2",
"2");
2092 UnitTestRecordProperty(
"key_1",
"12");
2093 UnitTestRecordProperty(
"key_2",
"22");
2095 ASSERT_EQ(2, unit_test_.ad_hoc_test_result().test_property_count());
2098 unit_test_.ad_hoc_test_result().GetTestProperty(0).key());
2100 unit_test_.ad_hoc_test_result().GetTestProperty(0).value());
2103 unit_test_.ad_hoc_test_result().GetTestProperty(1).key());
2105 unit_test_.ad_hoc_test_result().GetTestProperty(1).value());
2108 TEST_F(UnitTestRecordPropertyTest,
2109 AddFailureInsideTestsWhenUsingTestSuiteReservedKeys) {
2110 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2112 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2114 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2116 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2118 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2120 ExpectNonFatalFailureRecordingPropertyWithReservedKeyForCurrentTest(
2124 TEST_F(UnitTestRecordPropertyTest,
2125 AddRecordWithReservedKeysGeneratesCorrectPropertyList) {
2127 Test::RecordProperty(
"name",
"1"),
2128 "'classname', 'name', 'status', 'time', 'type_param', 'value_param',"
2129 " 'file', and 'line' are reserved");
2132 class UnitTestRecordPropertyTestEnvironment :
public Environment {
2134 void TearDown()
override {
2135 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2137 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2139 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2141 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2143 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2145 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2147 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2149 ExpectNonFatalFailureRecordingPropertyWithReservedKeyOutsideOfTestSuite(
2167 bool IsEven(
int n) {
2168 return (
n % 2) == 0;
2172 struct IsEvenFunctor {
2173 bool operator()(
int n) {
return IsEven(
n); }
2184 msg << expr <<
" evaluates to " <<
n <<
", which is not even.";
2209 struct AssertIsEvenFunctor {
2211 return AssertIsEven(expr,
n);
2216 bool SumIsEven2(
int n1,
int n2) {
2217 return IsEven(n1 + n2);
2222 struct SumIsEven3Functor {
2223 bool operator()(
int n1,
int n2,
int n3) {
2224 return IsEven(n1 + n2 + n3);
2231 const char* e1,
const char* e2,
const char* e3,
const char* e4,
2232 int n1,
int n2,
int n3,
int n4) {
2233 const int sum = n1 + n2 + n3 + n4;
2239 msg << e1 <<
" + " << e2 <<
" + " << e3 <<
" + " << e4
2240 <<
" (" << n1 <<
" + " << n2 <<
" + " << n3 <<
" + " << n4
2241 <<
") evaluates to " <<
sum <<
", which is not even.";
2247 struct AssertSumIsEven5Functor {
2249 const char* e1,
const char* e2,
const char* e3,
const char* e4,
2250 const char* e5,
int n1,
int n2,
int n3,
int n4,
int n5) {
2251 const int sum = n1 + n2 + n3 + n4 + n5;
2257 msg << e1 <<
" + " << e2 <<
" + " << e3 <<
" + " << e4 <<
" + " << e5
2259 << n1 <<
" + " << n2 <<
" + " << n3 <<
" + " << n4 <<
" + " << n5
2260 <<
") evaluates to " <<
sum <<
", which is not even.";
2269 TEST(Pred1Test, WithoutFormat) {
2271 EXPECT_PRED1(IsEvenFunctor(), 2) <<
"This failure is UNEXPECTED!";
2276 EXPECT_PRED1(IsEven, 5) <<
"This failure is expected.";
2277 },
"This failure is expected.");
2279 "evaluates to false");
2283 TEST(Pred1Test, WithFormat) {
2287 <<
"This failure is UNEXPECTED!";
2292 "n evaluates to 5, which is not even.");
2295 },
"This failure is expected.");
2300 TEST(Pred1Test, SingleEvaluationOnFailure) {
2304 EXPECT_EQ(1,
n) <<
"The argument is not evaluated exactly once.";
2309 <<
"This failure is expected.";
2310 },
"This failure is expected.");
2311 EXPECT_EQ(2,
n) <<
"The argument is not evaluated exactly once.";
2318 TEST(PredTest, WithoutFormat) {
2320 ASSERT_PRED2(SumIsEven2, 2, 4) <<
"This failure is UNEXPECTED!";
2327 EXPECT_PRED2(SumIsEven2, n1, n2) <<
"This failure is expected.";
2328 },
"This failure is expected.");
2331 },
"evaluates to false");
2335 TEST(PredTest, WithFormat) {
2338 "This failure is UNEXPECTED!";
2348 },
"evaluates to 13, which is not even.");
2351 <<
"This failure is expected.";
2352 },
"This failure is expected.");
2357 TEST(PredTest, SingleEvaluationOnFailure) {
2362 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2363 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2371 n1++, n2++, n3++, n4++, n5++)
2372 <<
"This failure is UNEXPECTED!";
2373 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2374 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2375 EXPECT_EQ(1, n3) <<
"Argument 3 is not evaluated exactly once.";
2376 EXPECT_EQ(1, n4) <<
"Argument 4 is not evaluated exactly once.";
2377 EXPECT_EQ(1, n5) <<
"Argument 5 is not evaluated exactly once.";
2383 <<
"This failure is expected.";
2384 },
"This failure is expected.");
2385 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2386 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2387 EXPECT_EQ(1, n3) <<
"Argument 3 is not evaluated exactly once.";
2390 n1 = n2 = n3 = n4 = 0;
2393 },
"evaluates to 1, which is not even.");
2394 EXPECT_EQ(1, n1) <<
"Argument 1 is not evaluated exactly once.";
2395 EXPECT_EQ(1, n2) <<
"Argument 2 is not evaluated exactly once.";
2396 EXPECT_EQ(1, n3) <<
"Argument 3 is not evaluated exactly once.";
2397 EXPECT_EQ(1, n4) <<
"Argument 4 is not evaluated exactly once.";
2401 TEST(PredTest, ExpectPredEvalFailure) {
2402 std::set<int> set_a = {2, 1, 3, 4, 5};
2403 std::set<int> set_b = {0, 4, 8};
2404 const auto compare_sets = [] (std::set<int>, std::set<int>) {
return false; };
2407 "compare_sets(set_a, set_b) evaluates to false, where\nset_a evaluates "
2408 "to { 1, 2, 3, 4, 5 }\nset_b evaluates to { 0, 4, 8 }");
2414 bool IsPositive(
double x) {
2418 template <
typename T>
2419 bool IsNegative(
T x) {
2423 template <
typename T1,
typename T2>
2424 bool GreaterThan(
T1 x1,
T2 x2) {
2430 TEST(PredicateAssertionTest, AcceptsOverloadedFunction) {
2438 TEST(PredicateAssertionTest, AcceptsTemplateFunction) {
2459 template <
typename T>
2465 template <
typename T1,
typename T2>
2467 const T1& x1,
const T2& x2) {
2474 TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) {
2481 TEST(PredicateFormatAssertionTest, AcceptsTemplateFunction) {
2491 const char *
const p1 =
"good";
2495 const char p2[] =
"good";
2499 " \"bad\"\n \"good\"");
2503 TEST(StringAssertionTest, ASSERT_STREQ_Null) {
2504 ASSERT_STREQ(
static_cast<const char*
>(
nullptr),
nullptr);
2509 TEST(StringAssertionTest, ASSERT_STREQ_Null2) {
2523 "\"Hi\" vs \"Hi\"");
2550 TEST(StringAssertionTest, STREQ_Wide) {
2552 ASSERT_STREQ(
static_cast<const wchar_t*
>(
nullptr),
nullptr);
2574 },
"Expected failure");
2578 TEST(StringAssertionTest, STRNE_Wide) {
2582 EXPECT_STRNE(
static_cast<const wchar_t*
>(
nullptr),
nullptr);
2605 ASSERT_STRNE(
L"abc\x8119",
L"abc\x8120") <<
"This shouldn't happen";
2612 TEST(IsSubstringTest, ReturnsCorrectResultForCString) {
2623 TEST(IsSubstringTest, ReturnsCorrectResultForWideCString) {
2629 IsSubstring(
"",
"",
static_cast<const wchar_t*
>(
nullptr),
nullptr));
2635 TEST(IsSubstringTest, GeneratesCorrectMessageForCString) {
2637 " Actual: \"needle\"\n"
2638 "Expected: a substring of haystack_expr\n"
2639 "Which is: \"haystack\"",
2641 "needle",
"haystack").failure_message());
2646 TEST(IsSubstringTest, ReturnsCorrectResultsForStdString) {
2651 #if GTEST_HAS_STD_WSTRING
2654 TEST(IsSubstringTest, ReturnsCorrectResultForStdWstring) {
2661 TEST(IsSubstringTest, GeneratesCorrectMessageForWstring) {
2663 " Actual: L\"needle\"\n"
2664 "Expected: a substring of haystack_expr\n"
2665 "Which is: L\"haystack\"",
2667 "needle_expr",
"haystack_expr",
2671 #endif // GTEST_HAS_STD_WSTRING
2677 TEST(IsNotSubstringTest, ReturnsCorrectResultForCString) {
2684 TEST(IsNotSubstringTest, ReturnsCorrectResultForWideCString) {
2691 TEST(IsNotSubstringTest, GeneratesCorrectMessageForWideCString) {
2693 " Actual: L\"needle\"\n"
2694 "Expected: not a substring of haystack_expr\n"
2695 "Which is: L\"two needles\"",
2697 "needle_expr",
"haystack_expr",
2698 L"needle",
L"two needles").failure_message());
2703 TEST(IsNotSubstringTest, ReturnsCorrectResultsForStdString) {
2710 TEST(IsNotSubstringTest, GeneratesCorrectMessageForStdString) {
2712 " Actual: \"needle\"\n"
2713 "Expected: not a substring of haystack_expr\n"
2714 "Which is: \"two needles\"",
2716 "needle_expr",
"haystack_expr",
2717 ::
std::string(
"needle"),
"two needles").failure_message());
2720 #if GTEST_HAS_STD_WSTRING
2724 TEST(IsNotSubstringTest, ReturnsCorrectResultForStdWstring) {
2730 #endif // GTEST_HAS_STD_WSTRING
2734 template <
typename RawType>
2735 class FloatingPointTest :
public Test {
2739 RawType close_to_positive_zero;
2740 RawType close_to_negative_zero;
2741 RawType further_from_negative_zero;
2743 RawType close_to_one;
2744 RawType further_from_one;
2747 RawType close_to_infinity;
2748 RawType further_from_infinity;
2755 typedef typename Floating::Bits Bits;
2757 void SetUp()
override {
2758 const uint32_t max_ulps = Floating::kMaxUlps;
2761 const Bits zero_bits = Floating(0).
bits();
2764 values_.close_to_positive_zero = Floating::ReinterpretBits(
2765 zero_bits + max_ulps/2);
2766 values_.close_to_negative_zero = -Floating::ReinterpretBits(
2767 zero_bits + max_ulps - max_ulps/2);
2768 values_.further_from_negative_zero = -Floating::ReinterpretBits(
2769 zero_bits + max_ulps + 1 - max_ulps/2);
2772 const Bits one_bits = Floating(1).bits();
2775 values_.close_to_one = Floating::ReinterpretBits(one_bits + max_ulps);
2776 values_.further_from_one = Floating::ReinterpretBits(
2777 one_bits + max_ulps + 1);
2780 values_.infinity = Floating::Infinity();
2783 const Bits infinity_bits = Floating(values_.infinity).bits();
2786 values_.close_to_infinity = Floating::ReinterpretBits(
2787 infinity_bits - max_ulps);
2788 values_.further_from_infinity = Floating::ReinterpretBits(
2789 infinity_bits - max_ulps - 1);
2794 values_.nan1 = Floating::ReinterpretBits(Floating::kExponentBitMask
2795 | (
static_cast<Bits
>(1) << (Floating::kFractionBitCount - 1)) | 1);
2796 values_.nan2 = Floating::ReinterpretBits(Floating::kExponentBitMask
2797 | (
static_cast<Bits
>(1) << (Floating::kFractionBitCount - 1)) | 200);
2801 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
2804 static TestValues values_;
2807 template <
typename RawType>
2808 typename FloatingPointTest<RawType>::TestValues
2809 FloatingPointTest<RawType>::values_;
2812 typedef FloatingPointTest<float> FloatTest;
2815 TEST_F(FloatTest, Size) {
2820 TEST_F(FloatTest, Zeros) {
2833 TEST_F(FloatTest, AlmostZeros) {
2840 static const FloatTest::TestValues&
v = this->values_;
2848 v.further_from_negative_zero);
2849 },
"v.further_from_negative_zero");
2853 TEST_F(FloatTest, SmallDiff) {
2856 "values_.further_from_one");
2860 TEST_F(FloatTest, LargeDiff) {
2869 TEST_F(FloatTest, Infinity) {
2873 "-values_.infinity");
2889 static const FloatTest::TestValues&
v = this->values_;
2903 TEST_F(FloatTest, Reflexive) {
2910 TEST_F(FloatTest, Commutative) {
2924 "The difference between 1.0f and 1.5f is 0.5, "
2925 "which exceeds 0.25f");
2933 "The difference between 1.0f and 1.5f is 0.5, "
2934 "which exceeds 0.25f");
2938 TEST_F(FloatTest, FloatLESucceeds) {
2947 TEST_F(FloatTest, FloatLEFails) {
2950 "(2.0f) <= (1.0f)");
2955 },
"(values_.further_from_one) <= (1.0f)");
2959 },
"(values_.nan1) <= (values_.infinity)");
2962 },
"(-values_.infinity) <= (values_.nan1)");
2965 },
"(values_.nan1) <= (values_.nan1)");
2969 typedef FloatingPointTest<double> DoubleTest;
2972 TEST_F(DoubleTest, Size) {
2977 TEST_F(DoubleTest, Zeros) {
2990 TEST_F(DoubleTest, AlmostZeros) {
2997 static const DoubleTest::TestValues&
v = this->values_;
3005 v.further_from_negative_zero);
3006 },
"v.further_from_negative_zero");
3010 TEST_F(DoubleTest, SmallDiff) {
3013 "values_.further_from_one");
3017 TEST_F(DoubleTest, LargeDiff) {
3026 TEST_F(DoubleTest, Infinity) {
3030 "-values_.infinity");
3039 TEST_F(DoubleTest, NaN) {
3040 static const DoubleTest::TestValues&
v = this->values_;
3052 TEST_F(DoubleTest, Reflexive) {
3059 TEST_F(DoubleTest, Commutative) {
3073 "The difference between 1.0 and 1.5 is 0.5, "
3074 "which exceeds 0.25");
3078 EXPECT_NEAR(4.2934311416234112e+18, 4.2934311416234107e+18, 1.0),
3079 "The abs_error parameter 1.0 evaluates to 1 which is smaller than the "
3080 "minimum distance between doubles for numbers of this magnitude which is "
3089 "The difference between 1.0 and 1.5 is 0.5, "
3090 "which exceeds 0.25");
3094 TEST_F(DoubleTest, DoubleLESucceeds) {
3103 TEST_F(DoubleTest, DoubleLEFails) {
3111 },
"(values_.further_from_one) <= (1.0)");
3115 },
"(values_.nan1) <= (values_.infinity)");
3118 },
" (-values_.infinity) <= (values_.nan1)");
3121 },
"(values_.nan1) <= (values_.nan1)");
3131 FAIL() <<
"Unexpected failure: Disabled test should not be run.";
3142 TEST(DISABLED_TestSuite, TestShouldNotRun) {
3143 FAIL() <<
"Unexpected failure: Test in disabled test case should not be run.";
3148 TEST(DISABLED_TestSuite, DISABLED_TestShouldNotRun) {
3149 FAIL() <<
"Unexpected failure: Test in disabled test case should not be run.";
3154 class DisabledTestsTest :
public Test {
3156 static void SetUpTestSuite() {
3157 FAIL() <<
"Unexpected failure: All tests disabled in test case. "
3158 "SetUpTestSuite() should not be called.";
3161 static void TearDownTestSuite() {
3162 FAIL() <<
"Unexpected failure: All tests disabled in test case. "
3163 "TearDownTestSuite() should not be called.";
3167 TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) {
3168 FAIL() <<
"Unexpected failure: Disabled test should not be run.";
3171 TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) {
3172 FAIL() <<
"Unexpected failure: Disabled test should not be run.";
3177 template <
typename T>
3185 FAIL() <<
"Unexpected failure: Disabled typed test should not run.";
3188 template <
typename T>
3189 class DISABLED_TypedTest :
public Test {
3194 TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) {
3195 FAIL() <<
"Unexpected failure: Disabled typed test should not run.";
3200 template <
typename T>
3207 FAIL() <<
"Unexpected failure: "
3208 <<
"Disabled type-parameterized test should not run.";
3215 template <
typename T>
3216 class DISABLED_TypedTestP :
public Test {
3222 FAIL() <<
"Unexpected failure: "
3223 <<
"Disabled type-parameterized test should not run.";
3232 class SingleEvaluationTest :
public Test {
3237 static void CompareAndIncrementCharPtrs() {
3243 static void CompareAndIncrementInts() {
3248 SingleEvaluationTest() {
3255 static const char*
const s1_;
3256 static const char*
const s2_;
3257 static const char* p1_;
3258 static const char* p2_;
3264 const char*
const SingleEvaluationTest::s1_ =
"01234";
3265 const char*
const SingleEvaluationTest::s2_ =
"abcde";
3266 const char* SingleEvaluationTest::p1_;
3267 const char* SingleEvaluationTest::p2_;
3273 TEST_F(SingleEvaluationTest, FailedASSERT_STREQ) {
3281 TEST_F(SingleEvaluationTest, ASSERT_STR) {
3296 TEST_F(SingleEvaluationTest, FailedASSERT_NE) {
3298 "(a_++) != (b_++)");
3304 TEST_F(SingleEvaluationTest, OtherCases) {
3333 #if GTEST_HAS_EXCEPTIONS
3338 #define ERROR_DESC "class std::runtime_error"
3340 #define ERROR_DESC "std::runtime_error"
3343 #else // GTEST_HAS_RTTI
3345 #define ERROR_DESC "an std::exception-derived error"
3347 #endif // GTEST_HAS_RTTI
3349 void ThrowAnInteger() {
3352 void ThrowRuntimeError(
const char* what) {
3353 throw std::runtime_error(what);
3357 TEST_F(SingleEvaluationTest, ExceptionTests) {
3369 },
bool),
"throws a different type");
3375 ThrowRuntimeError(
"A description");
3376 },
bool),
"throws " ERROR_DESC
" with description \"A description\"");
3406 #endif // GTEST_HAS_EXCEPTIONS
3409 class NoFatalFailureTest :
public Test {
3412 void FailsNonFatal() {
3416 FAIL() <<
"some fatal failure";
3419 void DoAssertNoFatalFailureOnFails() {
3424 void DoExpectNoFatalFailureOnFails() {
3430 TEST_F(NoFatalFailureTest, NoFailure) {
3435 TEST_F(NoFatalFailureTest, NonFatalIsNoFailure) {
3438 "some non-fatal failure");
3441 "some non-fatal failure");
3444 TEST_F(NoFatalFailureTest, AssertNoFatalFailureOnFatalFailure) {
3448 DoAssertNoFatalFailureOnFails();
3451 EXPECT_EQ(TestPartResult::kFatalFailure,
3453 EXPECT_EQ(TestPartResult::kFatalFailure,
3461 TEST_F(NoFatalFailureTest, ExpectNoFatalFailureOnFatalFailure) {
3465 DoExpectNoFatalFailureOnFails();
3468 EXPECT_EQ(TestPartResult::kFatalFailure,
3470 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3472 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3482 TEST_F(NoFatalFailureTest, MessageIsStreamable) {
3489 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3491 EXPECT_EQ(TestPartResult::kNonFatalFailure,
3501 std::string EditsToString(
const std::vector<EditType>& edits) {
3503 for (
size_t i = 0;
i < edits.size(); ++
i) {
3504 static const char kEdits[] =
" +-/";
3505 out.append(1, kEdits[edits[
i]]);
3511 std::vector<size_t>
out;
3512 for (
size_t i = 0;
i <
str.size(); ++
i) {
3513 out.push_back(
static_cast<size_t>(
str[
i]));
3519 std::vector<std::string>
out;
3520 for (
size_t i = 0;
i <
str.size(); ++
i) {
3521 out.push_back(
str.substr(
i, 1));
3526 TEST(EditDistance, TestSuites) {
3531 const char* expected_edits;
3532 const char* expected_diff;
3534 static const Case kCases[] = {
3536 {__LINE__,
"A",
"A",
" ",
""},
3537 {__LINE__,
"ABCDE",
"ABCDE",
" ",
""},
3539 {__LINE__,
"X",
"XA",
" +",
"@@ +1,2 @@\n X\n+A\n"},
3540 {__LINE__,
"X",
"XABCD",
" ++++",
"@@ +1,5 @@\n X\n+A\n+B\n+C\n+D\n"},
3542 {__LINE__,
"XA",
"X",
" -",
"@@ -1,2 @@\n X\n-A\n"},
3543 {__LINE__,
"XABCD",
"X",
" ----",
"@@ -1,5 @@\n X\n-A\n-B\n-C\n-D\n"},
3545 {__LINE__,
"A",
"a",
"/",
"@@ -1,1 +1,1 @@\n-A\n+a\n"},
3546 {__LINE__,
"ABCD",
"abcd",
"////",
3547 "@@ -1,4 +1,4 @@\n-A\n-B\n-C\n-D\n+a\n+b\n+c\n+d\n"},
3549 {__LINE__,
"ABCDEFGH",
"ABXEGH1",
" -/ - +",
3550 "@@ -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"},
3551 {__LINE__,
"AAAABCCCC",
"ABABCDCDC",
"- / + / ",
3552 "@@ -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"},
3553 {__LINE__,
"ABCDE",
"BCDCD",
"- +/",
3554 "@@ -1,5 +1,5 @@\n-A\n B\n C\n D\n-E\n+C\n+D\n"},
3555 {__LINE__,
"ABCDEFGHIJKL",
"BCDCDEFGJKLJK",
"- ++ -- ++",
3556 "@@ -1,4 +1,5 @@\n-A\n B\n+C\n+D\n C\n D\n"
3557 "@@ -6,7 +7,7 @@\n F\n G\n-H\n-I\n J\n K\n L\n+J\n+K\n"},
3559 for (
const Case*
c = kCases;
c->left; ++
c) {
3562 CharsToIndices(
c->right))))
3563 <<
"Left <" <<
c->left <<
"> Right <" <<
c->right <<
"> Edits <"
3565 CharsToIndices(
c->left), CharsToIndices(
c->right))) <<
">";
3567 CharsToLines(
c->right)))
3568 <<
"Left <" <<
c->left <<
"> Right <" <<
c->right <<
"> Diff <"
3578 EqFailure(
"foo",
"bar", foo_val, bar_val,
false)
3579 .failure_message());
3581 "Expected equality of these values:\n"
3589 EqFailure(
"foo",
"6", foo_val, bar_val,
false)
3590 .failure_message());
3592 "Expected equality of these values:\n"
3599 EqFailure(
"5",
"bar", foo_val, bar_val,
false)
3600 .failure_message());
3602 "Expected equality of these values:\n"
3609 EqFailure(
"5",
"6", foo_val, bar_val,
false).failure_message());
3611 "Expected equality of these values:\n"
3619 true).failure_message());
3621 "Expected equality of these values:\n"
3623 " Which is: \"x\"\n"
3625 " Which is: \"y\"\n"
3630 TEST(AssertionTest, EqFailureWithDiff) {
3632 "1\\n2XXX\\n3\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12XXX\\n13\\n14\\n15");
3634 "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n11\\n12\\n13\\n14");
3636 EqFailure(
"left",
"right", left, right,
false).failure_message());
3638 "Expected equality of these values:\n"
3641 "1\\n2XXX\\n3\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12XXX\\n13\\n14\\n15\n"
3643 " Which is: 1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n11\\n12\\n13\\n14\n"
3644 "With diff:\n@@ -1,5 +1,6 @@\n 1\n-2XXX\n+2\n 3\n+4\n 5\n 6\n"
3645 "@@ -7,8 +8,6 @@\n 8\n 9\n-10\n 11\n-12XXX\n+12\n 13\n 14\n-15\n",
3664 # pragma option push -w-ccc -w-rch
3675 TEST(AssertionTest, AssertTrueWithAssertionResult) {
3677 #ifndef __BORLANDC__
3680 "Value of: ResultIsEven(3)\n"
3681 " Actual: false (3 is odd)\n"
3686 "Value of: ResultIsEvenNoExplanation(3)\n"
3687 " Actual: false (3 is odd)\n"
3701 TEST(AssertionTest, AssertFalseWithAssertionResult) {
3703 #ifndef __BORLANDC__
3706 "Value of: ResultIsEven(2)\n"
3707 " Actual: true (2 is even)\n"
3712 "Value of: ResultIsEvenNoExplanation(2)\n"
3725 TEST(ExpectTest, ASSERT_EQ_Double) {
3738 "Expected equality of these values:\n"
3745 TEST(AssertionTest, ASSERT_EQ_NULL) {
3747 const char*
p =
nullptr;
3759 TEST(ExpectTest, ASSERT_EQ_0) {
3774 "Expected: ('a') != ('a'), "
3775 "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)");
3783 "Expected: (2) <= (0), actual: 2 vs 0");
3790 "Expected: (2) < (2), actual: 2 vs 2");
3798 "Expected: (2) >= (3), actual: 2 vs 3");
3805 "Expected: (2) > (2), actual: 2 vs 2");
3808 #if GTEST_HAS_EXCEPTIONS
3810 void ThrowNothing() {}
3816 # ifndef __BORLANDC__
3821 "Expected: ThrowAnInteger() throws an exception of type bool.\n"
3822 " Actual: it throws a different type.");
3824 ASSERT_THROW(ThrowRuntimeError(
"A description"), std::logic_error),
3825 "Expected: ThrowRuntimeError(\"A description\") "
3826 "throws an exception of type std::logic_error.\n "
3827 "Actual: it throws " ERROR_DESC
" "
3828 "with description \"A description\".");
3833 "Expected: ThrowNothing() throws an exception of type bool.\n"
3834 " Actual: it throws nothing.");
3841 "Expected: ThrowAnInteger() doesn't throw an exception."
3842 "\n Actual: it throws.");
3844 "Expected: ThrowRuntimeError(\"A description\") "
3845 "doesn't throw an exception.\n "
3846 "Actual: it throws " ERROR_DESC
" "
3847 "with description \"A description\".");
3855 "Expected: ThrowNothing() throws an exception.\n"
3856 " Actual: it doesn't.");
3859 #endif // GTEST_HAS_EXCEPTIONS
3863 TEST(AssertionTest, AssertPrecedence) {
3865 bool false_value =
false;
3875 TEST(AssertionTest, NonFixtureSubroutine) {
3877 " x\n Which is: 2");
3883 explicit Uncopyable(
int a_value) :
value_(a_value) {}
3886 bool operator==(
const Uncopyable& rhs)
const {
3887 return value() == rhs.value();
3892 Uncopyable(
const Uncopyable&);
3897 ::std::ostream&
operator<<(::std::ostream& os,
const Uncopyable&
value) {
3898 return os <<
value.value();
3902 bool IsPositiveUncopyable(
const Uncopyable&
x) {
3903 return x.value() > 0;
3907 void TestAssertNonPositive() {
3912 void TestAssertEqualsUncopyable() {
3919 TEST(AssertionTest, AssertWorksWithUncopyableObject) {
3924 "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1");
3926 "Expected equality of these values:\n"
3927 " x\n Which is: 5\n y\n Which is: -1");
3931 TEST(AssertionTest, ExpectWorksWithUncopyableObject) {
3936 "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1");
3939 "Expected equality of these values:\n"
3940 " x\n Which is: 5\n y\n Which is: -1");
3948 TEST(AssertionTest, NamedEnum) {
3956 #if !defined(__SUNPRO_CC) && !defined(__HP_aCC)
3979 # endif // GTEST_OS_LINUX
3987 EXPECT_EQ(
static_cast<int>(kCaseA),
static_cast<int>(kCaseB));
3989 # endif // GTEST_OS_LINUX
3998 "(kCaseA) >= (kCaseB)");
4009 # ifndef __BORLANDC__
4013 " kCaseB\n Which is: ");
4022 #endif // !GTEST_OS_MAC && !defined(__SUNPRO_CC)
4024 #if GTEST_OS_WINDOWS
4026 static HRESULT UnexpectedHRESULTFailure() {
4027 return E_UNEXPECTED;
4030 static HRESULT OkHRESULTSuccess() {
4034 static HRESULT FalseHRESULTSuccess() {
4042 TEST(HRESULTAssertionTest, EXPECT_HRESULT_SUCCEEDED) {
4043 EXPECT_HRESULT_SUCCEEDED(S_OK);
4044 EXPECT_HRESULT_SUCCEEDED(S_FALSE);
4047 "Expected: (UnexpectedHRESULTFailure()) succeeds.\n"
4048 " Actual: 0x8000FFFF");
4051 TEST(HRESULTAssertionTest, ASSERT_HRESULT_SUCCEEDED) {
4052 ASSERT_HRESULT_SUCCEEDED(S_OK);
4053 ASSERT_HRESULT_SUCCEEDED(S_FALSE);
4056 "Expected: (UnexpectedHRESULTFailure()) succeeds.\n"
4057 " Actual: 0x8000FFFF");
4060 TEST(HRESULTAssertionTest, EXPECT_HRESULT_FAILED) {
4061 EXPECT_HRESULT_FAILED(E_UNEXPECTED);
4064 "Expected: (OkHRESULTSuccess()) fails.\n"
4067 "Expected: (FalseHRESULTSuccess()) fails.\n"
4071 TEST(HRESULTAssertionTest, ASSERT_HRESULT_FAILED) {
4072 ASSERT_HRESULT_FAILED(E_UNEXPECTED);
4074 # ifndef __BORLANDC__
4078 "Expected: (OkHRESULTSuccess()) fails.\n"
4083 "Expected: (FalseHRESULTSuccess()) fails.\n"
4088 TEST(HRESULTAssertionTest, Streaming) {
4089 EXPECT_HRESULT_SUCCEEDED(S_OK) <<
"unexpected failure";
4090 ASSERT_HRESULT_SUCCEEDED(S_OK) <<
"unexpected failure";
4091 EXPECT_HRESULT_FAILED(E_UNEXPECTED) <<
"unexpected failure";
4092 ASSERT_HRESULT_FAILED(E_UNEXPECTED) <<
"unexpected failure";
4095 EXPECT_HRESULT_SUCCEEDED(E_UNEXPECTED) <<
"expected failure",
4096 "expected failure");
4098 # ifndef __BORLANDC__
4102 ASSERT_HRESULT_SUCCEEDED(E_UNEXPECTED) <<
"expected failure",
4103 "expected failure");
4107 EXPECT_HRESULT_FAILED(S_OK) <<
"expected failure",
4108 "expected failure");
4111 ASSERT_HRESULT_FAILED(S_OK) <<
"expected failure",
4112 "expected failure");
4115 #endif // GTEST_OS_WINDOWS
4119 #pragma GCC diagnostic push
4120 #pragma GCC diagnostic ignored "-Wdangling-else"
4121 #pragma GCC diagnostic ignored "-Wempty-body"
4122 #pragma GCC diagnostic ignored "-Wpragmas"
4125 TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) {
4127 ASSERT_TRUE(
false) <<
"This should never be executed; "
4128 "It's a compilation test only.";
4144 #pragma GCC diagnostic pop
4147 #if GTEST_HAS_EXCEPTIONS
4150 TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
4162 TEST(ExpectThrowTest, DoesNotGenerateDuplicateCatchClauseWarning) {
4168 #pragma GCC diagnostic push
4169 #pragma GCC diagnostic ignored "-Wdangling-else"
4170 #pragma GCC diagnostic ignored "-Wempty-body"
4171 #pragma GCC diagnostic ignored "-Wpragmas"
4173 TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) {
4199 #pragma GCC diagnostic pop
4202 #endif // GTEST_HAS_EXCEPTIONS
4206 #pragma GCC diagnostic push
4207 #pragma GCC diagnostic ignored "-Wdangling-else"
4208 #pragma GCC diagnostic ignored "-Wempty-body"
4209 #pragma GCC diagnostic ignored "-Wpragmas"
4211 TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) {
4214 <<
"It's a compilation test only.";
4234 #pragma GCC diagnostic pop
4238 TEST(AssertionSyntaxTest, WorksWithSwitch) {
4248 EXPECT_FALSE(
false) <<
"EXPECT_FALSE failed in switch case";
4255 ASSERT_EQ(1, 1) <<
"ASSERT_EQ failed in default switch handler";
4263 #if GTEST_HAS_EXCEPTIONS
4265 void ThrowAString() {
4266 throw "std::string";
4271 TEST(AssertionSyntaxTest, WorksWithConst) {
4277 #endif // GTEST_HAS_EXCEPTIONS
4291 TEST(SuccessfulAssertionTest, EXPECT) {
4297 TEST(SuccessfulAssertionTest, EXPECT_STR) {
4309 TEST(SuccessfulAssertionTest, ASSERT_STR) {
4320 TEST(AssertionWithMessageTest, EXPECT) {
4321 EXPECT_EQ(1, 1) <<
"This should succeed.";
4323 "Expected failure #1");
4324 EXPECT_LE(1, 2) <<
"This should succeed.";
4326 "Expected failure #2.");
4327 EXPECT_GE(1, 0) <<
"This should succeed.";
4329 "Expected failure #3.");
4333 "Expected failure #4.");
4336 "Expected failure #5.");
4340 "Expected failure #6.");
4341 EXPECT_NEAR(1, 1.1, 0.2) <<
"This should succeed.";
4345 ASSERT_EQ(1, 1) <<
"This should succeed.";
4346 ASSERT_NE(1, 2) <<
"This should succeed.";
4347 ASSERT_LE(1, 2) <<
"This should succeed.";
4348 ASSERT_LT(1, 2) <<
"This should succeed.";
4349 ASSERT_GE(1, 0) <<
"This should succeed.";
4351 "Expected failure.");
4354 TEST(AssertionWithMessageTest, ASSERT_STR) {
4359 "Expected failure.");
4362 TEST(AssertionWithMessageTest, ASSERT_FLOATING) {
4373 ASSERT_FALSE(
true) <<
"Expected failure: " << 2 <<
" > " << 1
4374 <<
" evaluates to " <<
true;
4375 },
"Expected failure");
4379 TEST(AssertionWithMessageTest,
FAIL) {
4386 SUCCEED() <<
"Success == " << 1;
4395 ASSERT_TRUE(
false) <<
static_cast<const char*
>(
nullptr)
4396 <<
static_cast<char*
>(
nullptr);
4401 #if GTEST_OS_WINDOWS
4403 TEST(AssertionWithMessageTest, WideStringMessage) {
4405 EXPECT_TRUE(
false) <<
L"This failure is expected.\x8119";
4406 },
"This failure is expected.");
4409 <<
L"expected too.\x8120";
4410 },
"This failure is expected too.");
4412 #endif // GTEST_OS_WINDOWS
4418 "Intentional failure #1.");
4420 "Intentional failure #2.");
4431 TEST(ExpectTest, ExpectTrueWithAssertionResult) {
4434 "Value of: ResultIsEven(3)\n"
4435 " Actual: false (3 is odd)\n"
4439 "Value of: ResultIsEvenNoExplanation(3)\n"
4440 " Actual: false (3 is odd)\n"
4449 "Intentional failure #1.");
4451 "Intentional failure #2.");
4461 TEST(ExpectTest, ExpectFalseWithAssertionResult) {
4464 "Value of: ResultIsEven(2)\n"
4465 " Actual: true (2 is even)\n"
4469 "Value of: ResultIsEvenNoExplanation(2)\n"
4483 "Expected equality of these values:\n"
4494 TEST(ExpectTest, EXPECT_EQ_Double) {
4504 TEST(ExpectTest, EXPECT_EQ_NULL) {
4506 const char*
p =
nullptr;
4518 TEST(ExpectTest, EXPECT_EQ_0) {
4534 "Expected: ('a') != ('a'), "
4535 "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)");
4538 char*
const p0 =
nullptr;
4545 void* pv1 = (
void*)0x1234;
4546 char*
const p1 =
reinterpret_cast<char*
>(pv1);
4556 "Expected: (2) <= (0), actual: 2 vs 0");
4565 "Expected: (2) < (2), actual: 2 vs 2");
4575 "Expected: (2) >= (3), actual: 2 vs 3");
4584 "Expected: (2) > (2), actual: 2 vs 2");
4589 #if GTEST_HAS_EXCEPTIONS
4595 "Expected: ThrowAnInteger() throws an exception of "
4596 "type bool.\n Actual: it throws a different type.");
4599 "Expected: ThrowRuntimeError(\"A description\") "
4600 "throws an exception of type std::logic_error.\n "
4601 "Actual: it throws " ERROR_DESC
" "
4602 "with description \"A description\".");
4605 "Expected: ThrowNothing() throws an exception of type bool.\n"
4606 " Actual: it throws nothing.");
4613 "Expected: ThrowAnInteger() doesn't throw an "
4614 "exception.\n Actual: it throws.");
4616 "Expected: ThrowRuntimeError(\"A description\") "
4617 "doesn't throw an exception.\n "
4618 "Actual: it throws " ERROR_DESC
" "
4619 "with description \"A description\".");
4627 "Expected: ThrowNothing() throws an exception.\n"
4628 " Actual: it doesn't.");
4631 #endif // GTEST_HAS_EXCEPTIONS
4634 TEST(ExpectTest, ExpectPrecedence) {
4637 " true && false\n Which is: false");
4644 TEST(StreamableToStringTest, Scalar) {
4656 TEST(StreamableToStringTest, NullPointer) {
4662 TEST(StreamableToStringTest, CString) {
4667 TEST(StreamableToStringTest, NullCString) {
4675 TEST(StreamableTest,
string) {
4677 "This failure message is a std::string, and is expected.");
4684 TEST(StreamableTest, stringWithEmbeddedNUL) {
4685 static const char char_array_with_nul[] =
4686 "Here's a NUL\0 and some more string";
4687 static const std::string string_with_nul(char_array_with_nul,
4688 sizeof(char_array_with_nul)
4691 "Here's a NUL\\0 and some more string");
4695 TEST(StreamableTest, NULChar) {
4697 FAIL() <<
"A NUL" <<
'\0' <<
" and some more string";
4698 },
"A NUL\\0 and some more string");
4702 TEST(StreamableTest,
int) {
4712 TEST(StreamableTest, NullCharPtr) {
4718 TEST(StreamableTest, BasicIoManip) {
4720 FAIL() <<
"Line 1." << std::endl
4721 <<
"A NUL char " << std::ends << std::flush <<
" in line 2.";
4722 },
"Line 1.\nA NUL char \\0 in line 2.");
4727 void AddFailureHelper(
bool* aborted) {
4735 bool aborted =
true;
4737 "Intentional failure.");
4761 "Intentional failure.");
4780 SUCCEED() <<
"Explicit success.";
4794 bool false_value =
false;
4796 },
" false_value\n Which is: false\n true");
4807 TEST(EqAssertionTest, Time_T) {
4809 static_cast<time_t
>(0));
4811 static_cast<time_t
>(1234)),
4818 const char ch =
'b';
4820 " ch\n Which is: 'b'");
4822 " ch\n Which is: 'b'");
4826 TEST(EqAssertionTest, WideChar) {
4830 "Expected equality of these values:\n"
4832 " Which is: L'\0' (0, 0x0)\n"
4834 " Which is: L'x' (120, 0x78)");
4836 static wchar_t wchar;
4842 " wchar\n Which is: L'");
4846 TEST(EqAssertionTest, StdString) {
4862 char*
const p1 =
const_cast<char*
>(
"foo");
4871 " str3\n Which is: \"A \\0 in the middle\"");
4874 #if GTEST_HAS_STD_WSTRING
4877 TEST(EqAssertionTest, StdWideString) {
4885 const wchar_t kTestX8119[] = {
'T',
'e',
's',
't', 0x8119,
'\0' };
4890 const wchar_t kTestX8120[] = {
'T',
'e',
's',
't', 0x8120,
'\0' };
4898 wstr3.at(2) =
L'\0';
4909 #endif // GTEST_HAS_STD_WSTRING
4912 TEST(EqAssertionTest, CharPointer) {
4913 char*
const p0 =
nullptr;
4918 void* pv1 = (
void*)0x1234;
4919 void* pv2 = (
void*)0xABC0;
4920 char*
const p1 =
reinterpret_cast<char*
>(pv1);
4921 char*
const p2 =
reinterpret_cast<char*
>(pv2);
4929 reinterpret_cast<char*
>(0xABC0)),
4934 TEST(EqAssertionTest, WideCharPointer) {
4935 wchar_t*
const p0 =
nullptr;
4940 void* pv1 = (
void*)0x1234;
4941 void* pv2 = (
void*)0xABC0;
4942 wchar_t*
const p1 =
reinterpret_cast<wchar_t*
>(pv1);
4943 wchar_t*
const p2 =
reinterpret_cast<wchar_t*
>(pv2);
4950 void* pv3 = (
void*)0x1234;
4951 void* pv4 = (
void*)0xABC0;
4952 const wchar_t* p3 =
reinterpret_cast<const wchar_t*
>(pv3);
4953 const wchar_t* p4 =
reinterpret_cast<const wchar_t*
>(pv4);
4959 TEST(EqAssertionTest, OtherPointer) {
4960 ASSERT_EQ(
static_cast<const int*
>(
nullptr),
static_cast<const int*
>(
nullptr));
4962 reinterpret_cast<const int*
>(0x1234)),
4967 class UnprintableChar {
4969 explicit UnprintableChar(
char ch) : char_(
ch) {}
4971 bool operator==(
const UnprintableChar& rhs)
const {
4972 return char_ == rhs.char_;
4974 bool operator!=(
const UnprintableChar& rhs)
const {
4975 return char_ != rhs.char_;
4977 bool operator<(
const UnprintableChar& rhs)
const {
4978 return char_ < rhs.char_;
4980 bool operator<=(
const UnprintableChar& rhs)
const {
4981 return char_ <= rhs.char_;
4983 bool operator>(
const UnprintableChar& rhs)
const {
4984 return char_ > rhs.char_;
4986 bool operator>=(
const UnprintableChar& rhs)
const {
4987 return char_ >= rhs.char_;
4996 TEST(ComparisonAssertionTest, AcceptsUnprintableArgs) {
4997 const UnprintableChar
x(
'x'),
y(
'y');
5013 #ifndef __BORLANDC__
5016 "1-byte object <78>");
5018 "1-byte object <78>");
5021 "1-byte object <79>");
5023 "1-byte object <78>");
5025 "1-byte object <79>");
5037 int Bar()
const {
return 1; }
5052 class FRIEND_TEST_Test2 :
public Test {
5069 class TestLifeCycleTest :
public Test {
5073 TestLifeCycleTest() {
count_++; }
5077 ~TestLifeCycleTest()
override {
count_--; }
5089 TEST_F(TestLifeCycleTest, Test1) {
5096 TEST_F(TestLifeCycleTest, Test2) {
5107 TEST(AssertionResultTest, CopyConstructorWorksWhenNotOptimied) {
5117 EXPECT_EQ(
static_cast<bool>(r3),
static_cast<bool>(r1));
5123 TEST(AssertionResultTest, ConstructionWorks) {
5146 TEST(AssertionResultTest, NegationWorks) {
5156 TEST(AssertionResultTest, StreamingWorks) {
5158 r <<
"abc" <<
'd' << 0 <<
true;
5162 TEST(AssertionResultTest, CanStreamOstreamManipulators) {
5164 r <<
"Data" << std::endl << std::flush << std::ends <<
"Will be visible";
5170 TEST(AssertionResultTest, ConstructibleFromContextuallyConvertibleToBool) {
5171 struct ExplicitlyConvertibleToBool {
5172 explicit operator bool()
const {
return value; }
5175 ExplicitlyConvertibleToBool v1 = {
false};
5176 ExplicitlyConvertibleToBool v2 = {
true};
5185 TEST(AssertionResultTest, ConstructibleFromImplicitlyConvertible) {
5201 return os << val.x();
5204 const Base* pointer) {
5205 return os <<
"(" << pointer->x() <<
")";
5208 TEST(MessageTest, CanStreamUserTypeInGlobalNameSpace) {
5219 class MyTypeInUnnamedNameSpace :
public Base {
5221 explicit MyTypeInUnnamedNameSpace(
int an_x): Base(an_x) {}
5224 const MyTypeInUnnamedNameSpace& val) {
5225 return os << val.x();
5228 const MyTypeInUnnamedNameSpace* pointer) {
5229 return os <<
"(" << pointer->x() <<
")";
5233 TEST(MessageTest, CanStreamUserTypeInUnnamedNameSpace) {
5235 MyTypeInUnnamedNameSpace
a(1);
5244 class MyTypeInNameSpace1 :
public Base {
5249 const MyTypeInNameSpace1& val) {
5250 return os << val.x();
5253 const MyTypeInNameSpace1* pointer) {
5254 return os <<
"(" << pointer->x() <<
")";
5258 TEST(MessageTest, CanStreamUserTypeInUserNameSpace) {
5269 class MyTypeInNameSpace2 :
public ::Base {
5276 return os << val.x();
5280 return os <<
"(" << pointer->x() <<
")";
5283 TEST(MessageTest, CanStreamUserTypeInUserNameSpaceWithStreamOperatorInGlobal) {
5294 char*
const p1 =
nullptr;
5295 unsigned char*
const p2 =
nullptr;
5297 double* p4 =
nullptr;
5301 msg << p1 << p2 << p3 << p4 << p5 << p6;
5303 msg.GetString().c_str());
5309 const wchar_t* const_wstr =
nullptr;
5314 wchar_t* wstr =
nullptr;
5319 const_wstr =
L"abc\x8119";
5324 wstr =
const_cast<wchar_t*
>(const_wstr);
5335 class TestInfoTest :
public Test {
5343 if (strcmp(test_name, test_info->
name()) == 0)
5351 return test_info->
result();
5356 TEST_F(TestInfoTest, Names) {
5357 const TestInfo*
const test_info = GetTestInfo(
"Names");
5365 const TestInfo*
const test_info = GetTestInfo(
"result");
5368 ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
5371 ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
5374 #define VERIFY_CODE_LOCATION \
5375 const int expected_line = __LINE__ - 1; \
5376 const TestInfo* const test_info = GetUnitTestImpl()->current_test_info(); \
5377 ASSERT_TRUE(test_info); \
5378 EXPECT_STREQ(__FILE__, test_info->file()); \
5379 EXPECT_EQ(expected_line, test_info->line())
5385 class CodeLocationForTESTF :
public Test {
5392 class CodeLocationForTESTP :
public TestWithParam<int> {
5401 template <
typename T>
5402 class CodeLocationForTYPEDTEST :
public Test {
5411 template <
typename T>
5412 class CodeLocationForTYPEDTESTP :
public Test {
5425 #undef VERIFY_CODE_LOCATION
5429 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
5430 class SetUpTestCaseTest :
public Test {
5435 printf(
"Setting up the test case . . .\n");
5452 printf(
"Tearing down the test case . . .\n");
5485 TEST_F(SetUpTestCaseTest, Test2) {
5488 #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
5491 class SetUpTestSuiteTest :
public Test {
5496 printf(
"Setting up the test suite . . .\n");
5513 printf(
"Tearing down the test suite . . .\n");
5543 TEST_F(SetUpTestSuiteTest, TestSetupTestSuite1) {
5548 TEST_F(SetUpTestSuiteTest, TestSetupTestSuite2) {
5679 flags.recreate_environments_when_repeating =
5761 template <
typename CharType>
5763 CharType** array2) {
5764 ASSERT_EQ(size1, size2) <<
" Array sizes different.";
5766 for (
int i = 0;
i != size1;
i++) {
5773 EXPECT_EQ(expected.also_run_disabled_tests,
5787 EXPECT_EQ(expected.recreate_environments_when_repeating,
5799 template <
typename CharType>
5801 int argc2,
const CharType** argv2,
5802 const Flags& expected,
bool should_print_help) {
5806 # if GTEST_HAS_STREAM_REDIRECTION
5813 # if GTEST_HAS_STREAM_REDIRECTION
5828 # if GTEST_HAS_STREAM_REDIRECTION
5829 const char*
const expected_help_fragment =
5830 "This program contains tests written using";
5831 if (should_print_help) {
5835 expected_help_fragment, captured_stdout);
5837 # endif // GTEST_HAS_STREAM_REDIRECTION
5845 # define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \
5846 TestParsingFlags(sizeof(argv1)/sizeof(*argv1) - 1, argv1, \
5847 sizeof(argv2)/sizeof(*argv2) - 1, argv2, \
5848 expected, should_print_help)
5853 const char* argv[] = {
nullptr};
5855 const char* argv2[] = {
nullptr};
5861 TEST_F(ParseFlagsTest, NoFlag) {
5862 const char* argv[] = {
"foo.exe",
nullptr};
5864 const char* argv2[] = {
"foo.exe",
nullptr};
5870 TEST_F(ParseFlagsTest, FailFast) {
5871 const char* argv[] = {
"foo.exe",
"--gtest_fail_fast",
nullptr};
5873 const char* argv2[] = {
"foo.exe",
nullptr};
5879 TEST_F(ParseFlagsTest, FilterBad) {
5880 const char* argv[] = {
"foo.exe",
"--gtest_filter",
nullptr};
5882 const char* argv2[] = {
"foo.exe",
"--gtest_filter",
nullptr};
5888 TEST_F(ParseFlagsTest, FilterEmpty) {
5889 const char* argv[] = {
"foo.exe",
"--gtest_filter=",
nullptr};
5891 const char* argv2[] = {
"foo.exe",
nullptr};
5897 TEST_F(ParseFlagsTest, FilterNonEmpty) {
5898 const char* argv[] = {
"foo.exe",
"--gtest_filter=abc",
nullptr};
5900 const char* argv2[] = {
"foo.exe",
nullptr};
5906 TEST_F(ParseFlagsTest, BreakOnFailureWithoutValue) {
5907 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure",
nullptr};
5909 const char* argv2[] = {
"foo.exe",
nullptr};
5915 TEST_F(ParseFlagsTest, BreakOnFailureFalse_0) {
5916 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=0",
nullptr};
5918 const char* argv2[] = {
"foo.exe",
nullptr};
5924 TEST_F(ParseFlagsTest, BreakOnFailureFalse_f) {
5925 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=f",
nullptr};
5927 const char* argv2[] = {
"foo.exe",
nullptr};
5933 TEST_F(ParseFlagsTest, BreakOnFailureFalse_F) {
5934 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=F",
nullptr};
5936 const char* argv2[] = {
"foo.exe",
nullptr};
5943 TEST_F(ParseFlagsTest, BreakOnFailureTrue) {
5944 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure=1",
nullptr};
5946 const char* argv2[] = {
"foo.exe",
nullptr};
5952 TEST_F(ParseFlagsTest, CatchExceptions) {
5953 const char* argv[] = {
"foo.exe",
"--gtest_catch_exceptions",
nullptr};
5955 const char* argv2[] = {
"foo.exe",
nullptr};
5961 TEST_F(ParseFlagsTest, DeathTestUseFork) {
5962 const char* argv[] = {
"foo.exe",
"--gtest_death_test_use_fork",
nullptr};
5964 const char* argv2[] = {
"foo.exe",
nullptr};
5971 TEST_F(ParseFlagsTest, DuplicatedFlags) {
5972 const char* argv[] = {
"foo.exe",
"--gtest_filter=a",
"--gtest_filter=b",
5975 const char* argv2[] = {
"foo.exe",
nullptr};
5981 TEST_F(ParseFlagsTest, UnrecognizedFlag) {
5982 const char* argv[] = {
"foo.exe",
"--gtest_break_on_failure",
5984 "--gtest_filter=b",
nullptr};
5986 const char* argv2[] = {
"foo.exe",
"bar",
nullptr};
5989 flags.break_on_failure =
true;
5995 TEST_F(ParseFlagsTest, ListTestsFlag) {
5996 const char* argv[] = {
"foo.exe",
"--gtest_list_tests",
nullptr};
5998 const char* argv2[] = {
"foo.exe",
nullptr};
6004 TEST_F(ParseFlagsTest, ListTestsTrue) {
6005 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=1",
nullptr};
6007 const char* argv2[] = {
"foo.exe",
nullptr};
6013 TEST_F(ParseFlagsTest, ListTestsFalse) {
6014 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=0",
nullptr};
6016 const char* argv2[] = {
"foo.exe",
nullptr};
6022 TEST_F(ParseFlagsTest, ListTestsFalse_f) {
6023 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=f",
nullptr};
6025 const char* argv2[] = {
"foo.exe",
nullptr};
6031 TEST_F(ParseFlagsTest, ListTestsFalse_F) {
6032 const char* argv[] = {
"foo.exe",
"--gtest_list_tests=F",
nullptr};
6034 const char* argv2[] = {
"foo.exe",
nullptr};
6040 TEST_F(ParseFlagsTest, OutputEmpty) {
6041 const char* argv[] = {
"foo.exe",
"--gtest_output",
nullptr};
6043 const char* argv2[] = {
"foo.exe",
"--gtest_output",
nullptr};
6049 TEST_F(ParseFlagsTest, OutputXml) {
6050 const char* argv[] = {
"foo.exe",
"--gtest_output=xml",
nullptr};
6052 const char* argv2[] = {
"foo.exe",
nullptr};
6058 TEST_F(ParseFlagsTest, OutputXmlFile) {
6059 const char* argv[] = {
"foo.exe",
"--gtest_output=xml:file",
nullptr};
6061 const char* argv2[] = {
"foo.exe",
nullptr};
6067 TEST_F(ParseFlagsTest, OutputXmlDirectory) {
6068 const char* argv[] = {
"foo.exe",
"--gtest_output=xml:directory/path/",
6071 const char* argv2[] = {
"foo.exe",
nullptr};
6078 TEST_F(ParseFlagsTest, BriefFlag) {
6079 const char* argv[] = {
"foo.exe",
"--gtest_brief",
nullptr};
6081 const char* argv2[] = {
"foo.exe",
nullptr};
6087 TEST_F(ParseFlagsTest, BriefFlagTrue) {
6088 const char* argv[] = {
"foo.exe",
"--gtest_brief=1",
nullptr};
6090 const char* argv2[] = {
"foo.exe",
nullptr};
6096 TEST_F(ParseFlagsTest, BriefFlagFalse) {
6097 const char* argv[] = {
"foo.exe",
"--gtest_brief=0",
nullptr};
6099 const char* argv2[] = {
"foo.exe",
nullptr};
6105 TEST_F(ParseFlagsTest, PrintTimeFlag) {
6106 const char* argv[] = {
"foo.exe",
"--gtest_print_time",
nullptr};
6108 const char* argv2[] = {
"foo.exe",
nullptr};
6114 TEST_F(ParseFlagsTest, PrintTimeTrue) {
6115 const char* argv[] = {
"foo.exe",
"--gtest_print_time=1",
nullptr};
6117 const char* argv2[] = {
"foo.exe",
nullptr};
6123 TEST_F(ParseFlagsTest, PrintTimeFalse) {
6124 const char* argv[] = {
"foo.exe",
"--gtest_print_time=0",
nullptr};
6126 const char* argv2[] = {
"foo.exe",
nullptr};
6132 TEST_F(ParseFlagsTest, PrintTimeFalse_f) {
6133 const char* argv[] = {
"foo.exe",
"--gtest_print_time=f",
nullptr};
6135 const char* argv2[] = {
"foo.exe",
nullptr};
6141 TEST_F(ParseFlagsTest, PrintTimeFalse_F) {
6142 const char* argv[] = {
"foo.exe",
"--gtest_print_time=F",
nullptr};
6144 const char* argv2[] = {
"foo.exe",
nullptr};
6151 const char* argv[] = {
"foo.exe",
"--gtest_random_seed=1000",
nullptr};
6153 const char* argv2[] = {
"foo.exe",
nullptr};
6159 TEST_F(ParseFlagsTest, Repeat) {
6160 const char* argv[] = {
"foo.exe",
"--gtest_repeat=1000",
nullptr};
6162 const char* argv2[] = {
"foo.exe",
nullptr};
6169 const char* argv[] = {
6171 "--gtest_recreate_environments_when_repeating=0",
6175 const char* argv2[] = {
"foo.exe",
nullptr};
6183 const char* argv[] = {
"foo.exe",
"--gtest_also_run_disabled_tests",
nullptr};
6185 const char* argv2[] = {
"foo.exe",
nullptr};
6192 TEST_F(ParseFlagsTest, AlsoRunDisabledTestsTrue) {
6193 const char* argv[] = {
"foo.exe",
"--gtest_also_run_disabled_tests=1",
6196 const char* argv2[] = {
"foo.exe",
nullptr};
6203 TEST_F(ParseFlagsTest, AlsoRunDisabledTestsFalse) {
6204 const char* argv[] = {
"foo.exe",
"--gtest_also_run_disabled_tests=0",
6207 const char* argv2[] = {
"foo.exe",
nullptr};
6214 TEST_F(ParseFlagsTest, ShuffleWithoutValue) {
6215 const char* argv[] = {
"foo.exe",
"--gtest_shuffle",
nullptr};
6217 const char* argv2[] = {
"foo.exe",
nullptr};
6223 TEST_F(ParseFlagsTest, ShuffleFalse_0) {
6224 const char* argv[] = {
"foo.exe",
"--gtest_shuffle=0",
nullptr};
6226 const char* argv2[] = {
"foo.exe",
nullptr};
6232 TEST_F(ParseFlagsTest, ShuffleTrue) {
6233 const char* argv[] = {
"foo.exe",
"--gtest_shuffle=1",
nullptr};
6235 const char* argv2[] = {
"foo.exe",
nullptr};
6241 TEST_F(ParseFlagsTest, StackTraceDepth) {
6242 const char* argv[] = {
"foo.exe",
"--gtest_stack_trace_depth=5",
nullptr};
6244 const char* argv2[] = {
"foo.exe",
nullptr};
6249 TEST_F(ParseFlagsTest, StreamResultTo) {
6250 const char* argv[] = {
"foo.exe",
"--gtest_stream_result_to=localhost:1234",
6253 const char* argv2[] = {
"foo.exe",
nullptr};
6260 TEST_F(ParseFlagsTest, ThrowOnFailureWithoutValue) {
6261 const char* argv[] = {
"foo.exe",
"--gtest_throw_on_failure",
nullptr};
6263 const char* argv2[] = {
"foo.exe",
nullptr};
6269 TEST_F(ParseFlagsTest, ThrowOnFailureFalse_0) {
6270 const char* argv[] = {
"foo.exe",
"--gtest_throw_on_failure=0",
nullptr};
6272 const char* argv2[] = {
"foo.exe",
nullptr};
6279 TEST_F(ParseFlagsTest, ThrowOnFailureTrue) {
6280 const char* argv[] = {
"foo.exe",
"--gtest_throw_on_failure=1",
nullptr};
6282 const char* argv2[] = {
"foo.exe",
nullptr};
6287 # if GTEST_OS_WINDOWS
6289 TEST_F(ParseFlagsTest, WideStrings) {
6290 const wchar_t* argv[] = {
6292 L"--gtest_filter=Foo*",
6293 L"--gtest_list_tests=1",
6294 L"--gtest_break_on_failure",
6295 L"--non_gtest_flag",
6299 const wchar_t* argv2[] = {
6301 L"--non_gtest_flag",
6305 Flags expected_flags;
6306 expected_flags.break_on_failure =
true;
6307 expected_flags.filter =
"Foo*";
6308 expected_flags.list_tests =
true;
6312 # endif // GTEST_OS_WINDOWS
6314 #if GTEST_USE_OWN_FLAGFILE_FLAG_
6315 class FlagfileTest :
public ParseFlagsTest {
6317 void SetUp()
override {
6327 void TearDown()
override {
6351 const char* argv[] = {
"foo.exe", flagfile_flag.c_str(),
nullptr};
6353 const char* argv2[] = {
"foo.exe",
nullptr};
6359 TEST_F(FlagfileTest, FilterNonEmpty) {
6365 const char* argv[] = {
"foo.exe", flagfile_flag.c_str(),
nullptr};
6367 const char* argv2[] = {
"foo.exe",
nullptr};
6373 TEST_F(FlagfileTest, SeveralFlags) {
6381 const char* argv[] = {
"foo.exe", flagfile_flag.c_str(),
nullptr};
6383 const char* argv2[] = {
"foo.exe",
nullptr};
6385 Flags expected_flags;
6386 expected_flags.break_on_failure =
true;
6387 expected_flags.filter =
"abc";
6388 expected_flags.list_tests =
true;
6392 #endif // GTEST_USE_OWN_FLAGFILE_FLAG_
6395 class CurrentTestInfoTest :
public Test {
6404 <<
"There should be no tests running at this point.";
6413 <<
"There should be no tests running at this point.";
6419 TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestSuite) {
6423 <<
"There is a test running so we should have a valid TestInfo.";
6425 <<
"Expected the name of the currently running test suite.";
6427 <<
"Expected the name of the currently running test.";
6434 TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestSuite) {
6438 <<
"There is a test running so we should have a valid TestInfo.";
6440 <<
"Expected the name of the currently running test suite.";
6442 <<
"Expected the name of the currently running test.";
6467 TEST(NestedTestingNamespaceTest, Success) {
6468 EXPECT_EQ(1, 1) <<
"This shouldn't fail.";
6472 TEST(NestedTestingNamespaceTest, Failure) {
6474 "This failure is expected.");
6486 void SetUp()
override { Test::SetUp(); }
6492 TEST(StreamingAssertionsTest, Unconditional) {
6493 SUCCEED() <<
"expected success";
6495 "expected failure");
6497 "expected failure");
6502 # pragma option push -w-ccc -w-rch
6505 TEST(StreamingAssertionsTest, Truth) {
6509 "expected failure");
6511 "expected failure");
6514 TEST(StreamingAssertionsTest, Truth2) {
6518 "expected failure");
6520 "expected failure");
6528 TEST(StreamingAssertionsTest, IntegerEquals) {
6529 EXPECT_EQ(1, 1) <<
"unexpected failure";
6530 ASSERT_EQ(1, 1) <<
"unexpected failure";
6532 "expected failure");
6534 "expected failure");
6537 TEST(StreamingAssertionsTest, IntegerLessThan) {
6538 EXPECT_LT(1, 2) <<
"unexpected failure";
6539 ASSERT_LT(1, 2) <<
"unexpected failure";
6541 "expected failure");
6543 "expected failure");
6546 TEST(StreamingAssertionsTest, StringsEqual) {
6550 "expected failure");
6552 "expected failure");
6555 TEST(StreamingAssertionsTest, StringsNotEqual) {
6559 "expected failure");
6561 "expected failure");
6564 TEST(StreamingAssertionsTest, StringsEqualIgnoringCase) {
6568 "expected failure");
6570 "expected failure");
6573 TEST(StreamingAssertionsTest, StringNotEqualIgnoringCase) {
6577 "expected failure");
6579 "expected failure");
6582 TEST(StreamingAssertionsTest, FloatingPointEquals) {
6586 "expected failure");
6588 "expected failure");
6591 #if GTEST_HAS_EXCEPTIONS
6594 EXPECT_THROW(ThrowAnInteger(),
int) <<
"unexpected failure";
6595 ASSERT_THROW(ThrowAnInteger(),
int) <<
"unexpected failure";
6597 "expected failure",
"expected failure");
6599 "expected failure",
"expected failure");
6602 TEST(StreamingAssertionsTest, NoThrow) {
6606 "expected failure",
"expected failure");
6608 "expected failure",
"expected failure");
6611 TEST(StreamingAssertionsTest, AnyThrow) {
6615 "expected failure",
"expected failure");
6617 "expected failure",
"expected failure");
6620 #endif // GTEST_HAS_EXCEPTIONS
6624 TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsYes) {
6627 SetEnv(
"TERM",
"xterm");
6631 SetEnv(
"TERM",
"dumb");
6636 TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsAliasOfYes) {
6637 SetEnv(
"TERM",
"dumb");
6649 TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsNo) {
6652 SetEnv(
"TERM",
"xterm");
6656 SetEnv(
"TERM",
"dumb");
6661 TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsInvalid) {
6662 SetEnv(
"TERM",
"xterm");
6674 TEST(ColoredOutputTest, UsesColorsWhenStdoutIsTty) {
6677 SetEnv(
"TERM",
"xterm");
6682 TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) {
6685 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
6688 SetEnv(
"TERM",
"dumb");
6694 SetEnv(
"TERM",
"xterm");
6700 SetEnv(
"TERM",
"dumb");
6703 SetEnv(
"TERM",
"emacs");
6706 SetEnv(
"TERM",
"vt100");
6709 SetEnv(
"TERM",
"xterm-mono");
6712 SetEnv(
"TERM",
"xterm");
6715 SetEnv(
"TERM",
"xterm-color");
6718 SetEnv(
"TERM",
"xterm-256color");
6721 SetEnv(
"TERM",
"screen");
6724 SetEnv(
"TERM",
"screen-256color");
6727 SetEnv(
"TERM",
"tmux");
6730 SetEnv(
"TERM",
"tmux-256color");
6733 SetEnv(
"TERM",
"rxvt-unicode");
6736 SetEnv(
"TERM",
"rxvt-unicode-256color");
6739 SetEnv(
"TERM",
"linux");
6742 SetEnv(
"TERM",
"cygwin");
6744 #endif // GTEST_OS_WINDOWS
6751 StaticAssertTypeEq<const int, const int>();
6755 template <
typename T>
6761 TEST(StaticAssertTypeEqTest, WorksInClass) {
6769 TEST(StaticAssertTypeEqTest, CompilesForEqualTypes) {
6770 StaticAssertTypeEq<int, IntAlias>();
6771 StaticAssertTypeEq<int*, IntAlias*>();
6774 TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsNoFailure) {
6780 TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsOnlyFatalFailure) {
6782 const bool has_nonfatal_failure = HasNonfatalFailure();
6783 ClearCurrentTestPartResults();
6787 TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) {
6789 const bool has_nonfatal_failure = HasNonfatalFailure();
6790 ClearCurrentTestPartResults();
6794 TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) {
6797 const bool has_nonfatal_failure = HasNonfatalFailure();
6798 ClearCurrentTestPartResults();
6807 TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody) {
6811 TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody2) {
6814 ClearCurrentTestPartResults();
6818 TEST(HasFailureTest, ReturnsFalseWhenThereIsNoFailure) {
6822 TEST(HasFailureTest, ReturnsTrueWhenThereIsFatalFailure) {
6824 const bool has_failure = HasFailure();
6825 ClearCurrentTestPartResults();
6829 TEST(HasFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) {
6831 const bool has_failure = HasFailure();
6832 ClearCurrentTestPartResults();
6836 TEST(HasFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) {
6839 const bool has_failure = HasFailure();
6840 ClearCurrentTestPartResults();
6847 TEST(HasFailureTest, WorksOutsideOfTestBody) {
6851 TEST(HasFailureTest, WorksOutsideOfTestBody2) {
6854 ClearCurrentTestPartResults();
6881 TEST(TestEventListenersTest, ConstructionWorks) {
6884 EXPECT_TRUE(TestEventListenersAccessor::GetRepeater(&listeners) !=
nullptr);
6891 TEST(TestEventListenersTest, DestructionWorks) {
6892 bool default_result_printer_is_destroyed =
false;
6893 bool default_xml_printer_is_destroyed =
false;
6894 bool extra_listener_is_destroyed =
false;
6896 new TestListener(
nullptr, &default_result_printer_is_destroyed);
6898 new TestListener(
nullptr, &default_xml_printer_is_destroyed);
6900 new TestListener(
nullptr, &extra_listener_is_destroyed);
6904 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners,
6905 default_result_printer);
6906 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners,
6907 default_xml_printer);
6908 listeners.
Append(extra_listener);
6918 int on_start_counter = 0;
6919 bool is_destroyed =
false;
6923 listeners.
Append(listener);
6924 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6925 *UnitTest::GetInstance());
6965 std::vector<std::string>*
vector_;
6966 const char*
const id_;
6971 TEST(EventListenerTest, AppendKeepsOrder) {
6972 std::vector<std::string> vec;
6978 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
6979 *UnitTest::GetInstance());
6986 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramEnd(
6987 *UnitTest::GetInstance());
6994 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationStart(
6995 *UnitTest::GetInstance(), 0);
7002 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationEnd(
7003 *UnitTest::GetInstance(), 0);
7012 TEST(TestEventListenersTest, Release) {
7013 int on_start_counter = 0;
7014 bool is_destroyed =
false;
7021 listeners.
Append(listener);
7023 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7024 *UnitTest::GetInstance());
7033 TEST(EventListenerTest, SuppressEventForwarding) {
7034 int on_start_counter = 0;
7038 listeners.
Append(listener);
7039 ASSERT_TRUE(TestEventListenersAccessor::EventForwardingEnabled(listeners));
7040 TestEventListenersAccessor::SuppressEventForwarding(&listeners);
7041 ASSERT_FALSE(TestEventListenersAccessor::EventForwardingEnabled(listeners));
7042 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7043 *UnitTest::GetInstance());
7049 TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) {
7051 GTEST_CHECK_(TestEventListenersAccessor::EventForwardingEnabled(
7053 "expected failure");
7059 TEST(EventListenerTest, default_result_printer) {
7060 int on_start_counter = 0;
7061 bool is_destroyed =
false;
7065 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener);
7069 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7070 *UnitTest::GetInstance());
7076 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners,
nullptr);
7083 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7084 *UnitTest::GetInstance());
7090 TEST(EventListenerTest, RemovingDefaultResultPrinterWorks) {
7091 int on_start_counter = 0;
7092 bool is_destroyed =
false;
7099 TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener);
7106 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7107 *UnitTest::GetInstance());
7118 TEST(EventListenerTest, default_xml_generator) {
7119 int on_start_counter = 0;
7120 bool is_destroyed =
false;
7124 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener);
7128 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7129 *UnitTest::GetInstance());
7135 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners,
nullptr);
7142 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7143 *UnitTest::GetInstance());
7149 TEST(EventListenerTest, RemovingDefaultXmlGeneratorWorks) {
7150 int on_start_counter = 0;
7151 bool is_destroyed =
false;
7158 TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener);
7165 TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart(
7166 *UnitTest::GetInstance());
7184 "An expected failure");
7190 "An expected failure");
7192 "An expected failure");
7197 "An expected failure");
7202 "An expected failure");
7206 "An expected failure");
7208 "An expected failure");
7213 "An expected failure");
7217 "An expected failure");
7219 "An expected failure");
7249 struct IncompleteType;
7253 TEST(HasDebugStringAndShortDebugStringTest, ValueIsCompileTimeConstant) {
7280 TEST(HasDebugStringAndShortDebugStringTest,
7281 ValueIsTrueWhenTypeHasDebugStringAndShortDebugString) {
7288 TEST(HasDebugStringAndShortDebugStringTest,
7289 ValueIsFalseWhenTypeIsNotAProtocolMessage) {
7297 template <
typename T1,
typename T2>
7300 "GTEST_REMOVE_REFERENCE_AND_CONST_ failed.");
7303 TEST(RemoveReferenceToConstTest, Works) {
7304 TestGTestRemoveReferenceAndConst<int, int>();
7305 TestGTestRemoveReferenceAndConst<double, double&>();
7306 TestGTestRemoveReferenceAndConst<char, const char>();
7307 TestGTestRemoveReferenceAndConst<char, const char&>();
7308 TestGTestRemoveReferenceAndConst<const char*, const char*>();
7313 template <
typename T1,
typename T2>
7316 "GTEST_REFERENCE_TO_CONST_ failed.");
7319 TEST(GTestReferenceToConstTest, Works) {
7320 TestGTestReferenceToConst<const char&, char>();
7321 TestGTestReferenceToConst<const int&, const int>();
7322 TestGTestReferenceToConst<const double&, double>();
7323 TestGTestReferenceToConst<const std::string&, const std::string&>();
7331 TEST(IsContainerTestTest, WorksForNonContainer) {
7337 TEST(IsContainerTestTest, WorksForContainer) {
7351 struct const_iterator {
7355 const_iterator
begin()
const;
7356 const_iterator
end()
const;
7359 TEST(IsContainerTestTest, ConstOnlyContainer) {
7361 sizeof(IsContainerTest<ConstOnlyContainerWithPointerIterator>(0)));
7363 sizeof(IsContainerTest<ConstOnlyContainerWithClassIterator>(0)));
7383 TEST(ArrayEqTest, WorksForDegeneratedArrays) {
7388 TEST(ArrayEqTest, WorksForOneDimensionalArrays) {
7390 const int a[] = { 0, 1 };
7391 long b[] = { 0, 1 };
7400 TEST(ArrayEqTest, WorksForTwoDimensionalArrays) {
7401 const char a[][3] = {
"hi",
"lo" };
7402 const char b[][3] = {
"hi",
"lo" };
7403 const char c[][3] = {
"hi",
"li" };
7414 TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) {
7415 const char a[] =
"hello";
7420 TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) {
7421 int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
7422 const int b[2] = { 2, 3 };
7425 const int c[2] = { 6, 7 };
7431 TEST(CopyArrayTest, WorksForDegeneratedArrays) {
7437 TEST(CopyArrayTest, WorksForOneDimensionalArrays) {
7438 const char a[3] =
"hi";
7440 #ifndef __BORLANDC__ // C++Builder cannot compile some array size deductions.
7450 TEST(CopyArrayTest, WorksForTwoDimensionalArrays) {
7451 const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } };
7453 #ifndef __BORLANDC__ // C++Builder cannot compile some array size deductions.
7465 TEST(NativeArrayTest, ConstructorFromArrayWorks) {
7466 const int a[3] = { 0, 1, 2 };
7472 TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) {
7473 typedef int Array[2];
7487 TEST(NativeArrayTest, TypeMembersAreCorrect) {
7491 StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>();
7495 TEST(NativeArrayTest, MethodsWork) {
7496 const int a[3] = { 0, 1, 2 };
7515 const int b1[3] = { 0, 1, 1 };
7516 const int b2[4] = { 0, 1, 2, 3 };
7521 TEST(NativeArrayTest, WorksForTwoDimensionalArray) {
7522 const char a[2][3] = {
"hi",
"lo" };
7541 (std::is_base_of<IndexSequence<0, 1, 2>, MakeIndexSequence<3>>::
value));
7554 std::is_same<
char, ElemFromList<7,
int,
int,
int,
int,
int,
int,
int,
7555 char,
int,
int,
int,
int>::
type>::
value));
7562 FlatTuple<int, double, const char*> tuple = {};
7567 tuple = FlatTuple<int, double, const char*>(
7573 tuple.Get<1>() = 5.1;
7591 return i == static_cast<int>(s.size());
7595 EXPECT_EQ(tuple.Apply(AddIntToString),
"Hello5");
7603 EXPECT_EQ(tuple.Get<1>(),
"HelloHello");
7644 TEST(FlatTuple, ConstructorCalls) {
7649 { FlatTuple<ConstructionCounting> tuple; }
7661 FlatTuple<ConstructionCounting> tuple{
7674 FlatTuple<ConstructionCounting> tuple{
7689 FlatTuple<ConstructionCounting> tuple;
7691 tuple.Get<0>() =
elem;
7705 FlatTuple<ConstructionCounting> tuple;
7724 #define GTEST_FLAT_TUPLE_INT8 int, int, int, int, int, int, int, int,
7725 #define GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT8 GTEST_FLAT_TUPLE_INT8
7726 #define GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT16 GTEST_FLAT_TUPLE_INT16
7727 #define GTEST_FLAT_TUPLE_INT64 GTEST_FLAT_TUPLE_INT32 GTEST_FLAT_TUPLE_INT32
7728 #define GTEST_FLAT_TUPLE_INT128 GTEST_FLAT_TUPLE_INT64 GTEST_FLAT_TUPLE_INT64
7729 #define GTEST_FLAT_TUPLE_INT256 GTEST_FLAT_TUPLE_INT128 GTEST_FLAT_TUPLE_INT128
7733 FlatTuple<GTEST_FLAT_TUPLE_INT256 int> tuple;
7736 tuple.Get<99>() = 17;
7737 tuple.Get<256>() = 1000;
7745 TEST(SkipPrefixTest, SkipsWhenPrefixMatches) {
7746 const char*
const str =
"hello";
7748 const char*
p =
str;
7757 TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) {
7758 const char*
const str =
"world";
7760 const char*
p =
str;
7783 "DynamicUnitTestFixture",
"DynamicTest",
"TYPE",
"VALUE", __FILE__,
7788 for (
int i = 0;
i <
unittest->total_test_suite_count(); ++
i) {
7791 for (
int j = 0; j <
tests->total_test_count(); ++j) {
7800 FAIL() <<
"Didn't find the test!";
7805 TEST(PatternGlobbingTest, MatchesFilterLinearRuntime) {
7807 name.push_back(
'b');
7810 for (
int i = 0;
i < 100; ++
i) {
7819 TEST(PatternGlobbingTest, MatchesFilterWithMultiplePatterns) {
7828 TEST(PatternGlobbingTest, MatchesFilterEdgeCases) {