37 #endif // GTEST_OS_MAC
54 TEST(IsXDigitTest, WorksForNarrowAscii) {
67 TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
72 TEST(IsXDigitTest, WorksForWideAscii) {
85 TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
104 class Derived :
public Base {
109 TEST(ImplicitCastTest, ConvertsPointers) {
111 EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
114 TEST(ImplicitCastTest, CanUseInheritance) {
116 Base
base = ::testing::internal::ImplicitCast_<Base>(derived);
132 TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
133 bool converted =
false;
135 Base
base = ::testing::internal::ImplicitCast_<Base>(castable);
142 operator Base()
const {
151 TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
152 bool converted =
false;
154 Base
base = ::testing::internal::ImplicitCast_<Base>(const_castable);
166 operator Base()
const {
176 TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
177 bool converted =
false;
178 bool const_converted =
false;
180 Base
base = ::testing::internal::ImplicitCast_<Base>(castable);
185 const_converted =
false;
187 base = ::testing::internal::ImplicitCast_<Base>(const_castable);
194 To(
bool* converted) { *converted =
true; }
197 TEST(ImplicitCastTest, CanUseImplicitConstructor) {
198 bool converted =
false;
199 To to = ::testing::internal::ImplicitCast_<To>(&converted);
204 TEST(IteratorTraitsTest, WorksForSTLContainerIterators) {
211 TEST(IteratorTraitsTest, WorksForPointerToNonConst) {
216 TEST(IteratorTraitsTest, WorksForPointerToConst) {
222 TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
224 GTEST_CHECK_(
false) <<
"This should never be executed; "
225 "It's a compilation test only.";
238 TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
252 TEST(FormatFileLocationTest, FormatsFileLocation) {
257 TEST(FormatFileLocationTest, FormatsUnknownFile) {
263 TEST(FormatFileLocationTest, FormatsUknownLine) {
267 TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
272 TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
276 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
281 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
285 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
289 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA
290 void* ThreadFunc(
void*
data) {
297 TEST(GetThreadCountTest, ReturnsCorrectValue) {
306 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
308 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
309 ASSERT_EQ(0, pthread_attr_destroy(&attr));
320 for (
int i = 0;
i < 5; ++
i) {
324 SleepMilliseconds(100);
330 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
333 #endif // GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA
335 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
336 const bool a_false_condition =
false;
339 "googletest-port-test\\.cc\\(\\d+\\):"
340 #elif GTEST_USES_POSIX_RE
341 "googletest-port-test\\.cc:[0-9]+"
343 "googletest-port-test\\.cc:\\d+"
345 ".*a_false_condition.*Extra info.*";
351 #if GTEST_HAS_DEATH_TEST
353 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
356 ::std::cerr <<
"Success\n";
358 ::testing::ExitedWithCode(0),
"Success");
361 #endif // GTEST_HAS_DEATH_TEST
366 TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
368 # if GTEST_HAS_POSIX_RE
377 #endif // !GTEST_USES_PCRE
380 #if GTEST_USES_POSIX_RE
382 # if GTEST_HAS_TYPED_TEST
384 template <
typename Str>
389 typedef testing::Types<
391 # if GTEST_HAS_GLOBAL_STRING
393 # endif // GTEST_HAS_GLOBAL_STRING
394 const char*> StringTypes;
399 TYPED_TEST(RETest, ImplicitConstructorWorks) {
400 const RE empty(TypeParam(
""));
403 const RE simple(TypeParam(
"hello"));
406 const RE normal(TypeParam(
".*(\\w+)"));
413 const RE invalid(TypeParam(
"?"));
414 },
"\"?\" is not a valid POSIX Extended regular expression.");
419 const RE empty(TypeParam(
""));
423 const RE re(TypeParam(
"a.*z"));
432 const RE empty(TypeParam(
""));
436 const RE re(TypeParam(
"a.*z"));
444 # endif // GTEST_HAS_TYPED_TEST
446 #elif GTEST_USES_SIMPLE_RE
448 TEST(IsInSetTest, NulCharIsNotInAnySet) {
454 TEST(IsInSetTest, WorksForNonNulChars) {
462 TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
471 TEST(IsAsciiDigitTest, IsTrueForDigit) {
478 TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
486 TEST(IsAsciiPunctTest, IsTrueForPunct) {
487 for (
const char*
p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *
p;
p++) {
492 TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
500 TEST(IsRepeatTest, IsTrueForRepeatChar) {
506 TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
514 TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
523 TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
531 TEST(IsAsciiWordCharTest, IsTrueForLetter) {
538 TEST(IsAsciiWordCharTest, IsTrueForDigit) {
545 TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
549 TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
554 TEST(IsValidEscapeTest, IsFalseForDigit) {
559 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
564 TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
569 TEST(IsValidEscapeTest, IsTrueForPunct) {
580 TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
589 TEST(AtomMatchesCharTest, EscapedPunct) {
601 TEST(AtomMatchesCharTest, Escaped_d) {
610 TEST(AtomMatchesCharTest, Escaped_D) {
619 TEST(AtomMatchesCharTest, Escaped_s) {
630 TEST(AtomMatchesCharTest, Escaped_S) {
639 TEST(AtomMatchesCharTest, Escaped_w) {
651 TEST(AtomMatchesCharTest, Escaped_W) {
662 TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
681 TEST(AtomMatchesCharTest, UnescapedDot) {
690 TEST(AtomMatchesCharTest, UnescapedChar) {
700 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
702 "NULL is not a valid simple regular expression");
705 "Syntax error at index 1 in simple regular expression \"a\\\": ");
707 "'\\' cannot appear at the end");
709 "'\\' cannot appear at the end");
711 "invalid escape sequence \"\\h\"");
713 "'^' can only appear at the beginning");
715 "'^' can only appear at the beginning");
717 "'$' can only appear at the end");
719 "'$' can only appear at the end");
721 "'(' is unsupported");
723 "')' is unsupported");
725 "'[' is unsupported");
727 "'{' is unsupported");
729 "'?' can only follow a repeatable token");
731 "'*' can only follow a repeatable token");
733 "'+' can only follow a repeatable token");
736 TEST(ValidateRegexTest, ReturnsTrueForValid) {
744 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
747 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
748 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
750 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
753 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
755 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
756 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
759 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
760 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
763 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
765 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
767 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
770 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
771 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
773 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
776 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
778 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
781 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
786 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
793 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
801 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
810 TEST(MatchRegexAtHeadTest,
811 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
821 TEST(MatchRegexAtHeadTest, MatchesSequentially) {
827 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
831 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
840 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
842 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
845 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
846 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
848 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
851 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
852 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
853 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
857 TEST(RETest, ImplicitConstructorWorks) {
861 const RE simple(
"hello");
866 TEST(RETest, RejectsInvalidRegex) {
868 const RE normal(
NULL);
869 },
"NULL is not a valid simple regular expression");
872 const RE normal(
".*(\\w+");
873 },
"'(' is unsupported");
876 const RE invalid(
"^?");
877 },
"'?' can only follow a repeatable token");
881 TEST(RETest, FullMatchWorks) {
897 TEST(RETest, PartialMatchWorks) {
910 #endif // GTEST_USES_POSIX_RE
912 #if !GTEST_OS_WINDOWS_MOBILE
914 TEST(CaptureTest, CapturesStdout) {
920 fprintf(
stdout,
"def%cghi",
'\0');
924 TEST(CaptureTest, CapturesStderr) {
926 fprintf(stderr,
"jkl");
930 fprintf(stderr,
"jkl%cmno",
'\0');
935 TEST(CaptureTest, CapturesStdoutAndStderr) {
939 fprintf(stderr,
"stu");
944 TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
947 "Only one stdout capturer can exist at a time");
954 #endif // !GTEST_OS_WINDOWS_MOBILE
956 TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
964 TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
979 TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
984 TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
990 thread_local_string.
set(
"foo");
994 TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
1001 thread_local_string.
set(
"foo");
1005 #if GTEST_IS_THREADSAFE
1009 TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
1011 ThreadWithParam<int*> thread(&AddTwo, &
i,
nullptr);
1016 TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1027 TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1033 class AtomicCounterWithMutex {
1035 explicit AtomicCounterWithMutex(
Mutex* mutex) :
1045 #if GTEST_HAS_PTHREAD
1049 pthread_mutex_t memory_barrier_mutex;
1051 pthread_mutex_init(&memory_barrier_mutex,
nullptr));
1054 SleepMilliseconds(random_.Generate(30));
1058 #elif GTEST_OS_WINDOWS
1060 volatile LONG
dummy = 0;
1061 ::InterlockedIncrement(&
dummy);
1062 SleepMilliseconds(random_.Generate(30));
1063 ::InterlockedIncrement(&
dummy);
1065 # error "Memory barrier not implemented on this platform."
1066 #endif // GTEST_HAS_PTHREAD
1078 void CountingThreadFunc(pair<AtomicCounterWithMutex*, int>
param) {
1079 for (
int i = 0;
i <
param.second; ++
i)
1080 param.first->Increment();
1084 TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1086 AtomicCounterWithMutex locked_counter(&mutex);
1088 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1089 const int kCycleCount = 20;
1090 const int kThreadCount = 7;
1091 std::unique_ptr<ThreadType> counting_threads[kThreadCount];
1092 Notification threads_can_start;
1095 for (
int i = 0;
i < kThreadCount; ++
i) {
1096 counting_threads[
i].reset(
new ThreadType(&CountingThreadFunc,
1097 make_pair(&locked_counter,
1099 &threads_can_start));
1101 threads_can_start.Notify();
1102 for (
int i = 0;
i < kThreadCount; ++
i)
1103 counting_threads[
i]->
Join();
1109 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1112 template <
typename T>
1114 ThreadWithParam<T> thread(
func,
param,
nullptr);
1118 void RetrieveThreadLocalValue(
1123 TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1124 ThreadLocal<std::string> thread_local_string(
"foo");
1125 EXPECT_STREQ(
"foo", thread_local_string.get().c_str());
1127 thread_local_string.set(
"bar");
1128 EXPECT_STREQ(
"bar", thread_local_string.get().c_str());
1131 RunFromThread(&RetrieveThreadLocalValue,
1132 make_pair(&thread_local_string, &result));
1138 class DestructorCall {
1142 #if GTEST_OS_WINDOWS
1143 wait_event_.Reset(::CreateEvent(
NULL, TRUE, FALSE,
NULL));
1148 bool CheckDestroyed()
const {
1149 #if GTEST_OS_WINDOWS
1150 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1156 void ReportDestroyed() {
1158 #if GTEST_OS_WINDOWS
1159 ::SetEvent(wait_event_.Get());
1163 static std::vector<DestructorCall*>& List() {
return *list_; }
1165 static void ResetList() {
1166 for (
size_t i = 0;
i < list_->size(); ++
i) {
1167 delete list_->at(
i);
1174 #if GTEST_OS_WINDOWS
1175 AutoHandle wait_event_;
1177 static std::vector<DestructorCall*>*
const list_;
1182 std::vector<DestructorCall*>*
const DestructorCall::list_ =
1183 new std::vector<DestructorCall*>;
1187 class DestructorTracker {
1189 DestructorTracker() :
index_(GetNewIndex()) {}
1190 DestructorTracker(
const DestructorTracker& )
1191 :
index_(GetNewIndex()) {}
1192 ~DestructorTracker() {
1195 DestructorCall::List()[
index_]->ReportDestroyed();
1199 static size_t GetNewIndex() {
1200 DestructorCall::List().push_back(
new DestructorCall);
1201 return DestructorCall::List().size() - 1;
1208 typedef ThreadLocal<DestructorTracker>* ThreadParam;
1210 void CallThreadLocalGet(ThreadParam thread_local_param) {
1211 thread_local_param->get();
1216 TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1217 DestructorCall::ResetList();
1220 ThreadLocal<DestructorTracker> thread_local_tracker;
1224 thread_local_tracker.get();
1226 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1231 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1233 DestructorCall::ResetList();
1238 TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1239 DestructorCall::ResetList();
1242 ThreadLocal<DestructorTracker> thread_local_tracker;
1246 ThreadWithParam<ThreadParam> thread(&CallThreadLocalGet,
1247 &thread_local_tracker,
nullptr);
1257 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1259 DestructorCall::ResetList();
1262 TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1263 ThreadLocal<std::string> thread_local_string;
1264 thread_local_string.set(
"Foo");
1265 EXPECT_STREQ(
"Foo", thread_local_string.get().c_str());
1268 RunFromThread(&RetrieveThreadLocalValue,
1269 make_pair(&thread_local_string, &result));
1273 #endif // GTEST_IS_THREADSAFE
1275 #if GTEST_OS_WINDOWS
1276 TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1277 StaticAssertTypeEq<HANDLE, void*>();
1280 #if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
1281 TEST(WindowsTypesTest, _CRITICAL_SECTIONIs_CRITICAL_SECTION) {
1282 StaticAssertTypeEq<CRITICAL_SECTION, _CRITICAL_SECTION>();
1285 TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1286 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
1290 #endif // GTEST_OS_WINDOWS