33 #include "gtest/internal/gtest-port.h"
37 #endif // GTEST_OS_MAC
44 #include "gtest/gtest.h"
45 #include "gtest/gtest-spi.h"
46 #include "src/gtest-internal-inl.h"
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(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
206 GTEST_CHECK_(
false) <<
"This should never be executed; "
207 "It's a compilation test only.";
220 TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
234 TEST(FormatFileLocationTest, FormatsFileLocation) {
239 TEST(FormatFileLocationTest, FormatsUnknownFile) {
245 TEST(FormatFileLocationTest, FormatsUknownLine) {
249 TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
254 TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
258 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
263 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
267 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
271 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA || \
272 GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
273 GTEST_OS_NETBSD || GTEST_OS_OPENBSD
274 void* ThreadFunc(
void*
data) {
281 TEST(GetThreadCountTest, ReturnsCorrectValue) {
290 ASSERT_EQ(0, pthread_attr_setdetachstate(&
attr, PTHREAD_CREATE_JOINABLE));
304 for (
int i = 0;
i < 5; ++
i) {
308 SleepMilliseconds(100);
314 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
317 #endif // GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA
319 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
320 const bool a_false_condition =
false;
323 "googletest-port-test\\.cc\\(\\d+\\):"
324 #elif GTEST_USES_POSIX_RE
325 "googletest-port-test\\.cc:[0-9]+"
327 "googletest-port-test\\.cc:\\d+"
329 ".*a_false_condition.*Extra info.*";
335 #if GTEST_HAS_DEATH_TEST
337 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
340 ::std::cerr <<
"Success\n";
342 ::testing::ExitedWithCode(0),
"Success");
345 #endif // GTEST_HAS_DEATH_TEST
350 TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
352 # if GTEST_HAS_POSIX_RE
361 #endif // !GTEST_USES_PCRE
364 #if GTEST_USES_POSIX_RE
366 # if GTEST_HAS_TYPED_TEST
368 template <
typename Str>
378 TYPED_TEST(RETest, ImplicitConstructorWorks) {
379 const RE
empty(TypeParam(
""));
382 const RE simple(TypeParam(
"hello"));
385 const RE normal(TypeParam(
".*(\\w+)"));
392 const RE
invalid(TypeParam(
"?"));
393 },
"\"?\" is not a valid POSIX Extended regular expression.");
398 const RE
empty(TypeParam(
""));
402 const RE re(TypeParam(
"a.*z"));
411 const RE
empty(TypeParam(
""));
415 const RE re(TypeParam(
"a.*z"));
423 # endif // GTEST_HAS_TYPED_TEST
425 #elif GTEST_USES_SIMPLE_RE
427 TEST(IsInSetTest, NulCharIsNotInAnySet) {
433 TEST(IsInSetTest, WorksForNonNulChars) {
441 TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
450 TEST(IsAsciiDigitTest, IsTrueForDigit) {
457 TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
465 TEST(IsAsciiPunctTest, IsTrueForPunct) {
466 for (
const char* p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *
p;
p++) {
471 TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
479 TEST(IsRepeatTest, IsTrueForRepeatChar) {
485 TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
493 TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
502 TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
510 TEST(IsAsciiWordCharTest, IsTrueForLetter) {
517 TEST(IsAsciiWordCharTest, IsTrueForDigit) {
524 TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
528 TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
533 TEST(IsValidEscapeTest, IsFalseForDigit) {
538 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
543 TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
548 TEST(IsValidEscapeTest, IsTrueForPunct) {
559 TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
568 TEST(AtomMatchesCharTest, EscapedPunct) {
580 TEST(AtomMatchesCharTest, Escaped_d) {
589 TEST(AtomMatchesCharTest, Escaped_D) {
598 TEST(AtomMatchesCharTest, Escaped_s) {
609 TEST(AtomMatchesCharTest, Escaped_S) {
618 TEST(AtomMatchesCharTest, Escaped_w) {
630 TEST(AtomMatchesCharTest, Escaped_W) {
641 TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
660 TEST(AtomMatchesCharTest, UnescapedDot) {
669 TEST(AtomMatchesCharTest, UnescapedChar) {
679 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
681 "NULL is not a valid simple regular expression");
684 "Syntax error at index 1 in simple regular expression \"a\\\": ");
686 "'\\' cannot appear at the end");
688 "'\\' cannot appear at the end");
690 "invalid escape sequence \"\\h\"");
692 "'^' can only appear at the beginning");
694 "'^' can only appear at the beginning");
696 "'$' can only appear at the end");
698 "'$' can only appear at the end");
700 "'(' is unsupported");
702 "')' is unsupported");
704 "'[' is unsupported");
706 "'{' is unsupported");
708 "'?' can only follow a repeatable token");
710 "'*' can only follow a repeatable token");
712 "'+' can only follow a repeatable token");
715 TEST(ValidateRegexTest, ReturnsTrueForValid) {
723 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
726 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
727 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
729 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
732 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
734 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
735 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
738 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
739 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
742 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
744 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
746 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
749 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
750 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
752 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
755 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
757 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
760 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
765 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
772 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
780 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
789 TEST(MatchRegexAtHeadTest,
790 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
800 TEST(MatchRegexAtHeadTest, MatchesSequentially) {
806 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
810 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
819 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
821 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
824 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
825 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
827 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
830 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
831 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
832 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
836 TEST(RETest, ImplicitConstructorWorks) {
840 const RE simple(
"hello");
845 TEST(RETest, RejectsInvalidRegex) {
847 const RE normal(NULL);
848 },
"NULL is not a valid simple regular expression");
851 const RE normal(
".*(\\w+");
852 },
"'(' is unsupported");
856 },
"'?' can only follow a repeatable token");
860 TEST(RETest, FullMatchWorks) {
876 TEST(RETest, PartialMatchWorks) {
889 #endif // GTEST_USES_POSIX_RE
891 #if !GTEST_OS_WINDOWS_MOBILE
893 TEST(CaptureTest, CapturesStdout) {
899 fprintf(
stdout,
"def%cghi",
'\0');
903 TEST(CaptureTest, CapturesStderr) {
909 fprintf(
stderr,
"jkl%cmno",
'\0');
914 TEST(CaptureTest, CapturesStdoutAndStderr) {
923 TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
926 "Only one stdout capturer can exist at a time");
933 #endif // !GTEST_OS_WINDOWS_MOBILE
935 TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
943 TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
958 TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
963 TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
969 thread_local_string.
set(
"foo");
973 TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
980 thread_local_string.
set(
"foo");
984 #if GTEST_IS_THREADSAFE
986 void AddTwo(
int* param) { *param += 2; }
988 TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
990 ThreadWithParam<int*>
thread(&AddTwo, &i,
nullptr);
995 TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1006 TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1012 class AtomicCounterWithMutex {
1014 explicit AtomicCounterWithMutex(
Mutex*
mutex) :
1024 #if GTEST_HAS_PTHREAD
1028 pthread_mutex_t memory_barrier_mutex;
1030 pthread_mutex_init(&memory_barrier_mutex,
nullptr));
1033 SleepMilliseconds(
static_cast<int>(random_.Generate(30)));
1037 #elif GTEST_OS_WINDOWS
1040 ::InterlockedIncrement(&
dummy);
1041 SleepMilliseconds(
static_cast<int>(random_.Generate(30)));
1042 ::InterlockedIncrement(&
dummy);
1044 # error "Memory barrier not implemented on this platform."
1045 #endif // GTEST_HAS_PTHREAD
1057 void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1058 for (
int i = 0;
i < param.second; ++
i)
1059 param.first->Increment();
1063 TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1065 AtomicCounterWithMutex locked_counter(&
mutex);
1067 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1068 const int kCycleCount = 20;
1069 const int kThreadCount = 7;
1070 std::unique_ptr<ThreadType> counting_threads[kThreadCount];
1071 Notification threads_can_start;
1074 for (
int i = 0;
i < kThreadCount; ++
i) {
1075 counting_threads[
i].reset(
new ThreadType(&CountingThreadFunc,
1076 make_pair(&locked_counter,
1078 &threads_can_start));
1080 threads_can_start.Notify();
1081 for (
int i = 0;
i < kThreadCount; ++
i)
1082 counting_threads[i]->
Join();
1088 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1091 template <
typename T>
1092 void RunFromThread(
void (
func)(
T),
T param) {
1093 ThreadWithParam<T>
thread(
func, param,
nullptr);
1097 void RetrieveThreadLocalValue(
1099 *param.second = param.first->get();
1102 TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1103 ThreadLocal<std::string> thread_local_string(
"foo");
1104 EXPECT_STREQ(
"foo", thread_local_string.get().c_str());
1106 thread_local_string.set(
"bar");
1107 EXPECT_STREQ(
"bar", thread_local_string.get().c_str());
1110 RunFromThread(&RetrieveThreadLocalValue,
1111 make_pair(&thread_local_string, &
result));
1117 class DestructorCall {
1121 #if GTEST_OS_WINDOWS
1122 wait_event_.Reset(::CreateEvent(NULL, TRUE, FALSE, NULL));
1127 bool CheckDestroyed()
const {
1128 #if GTEST_OS_WINDOWS
1129 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1135 void ReportDestroyed() {
1137 #if GTEST_OS_WINDOWS
1138 ::SetEvent(wait_event_.Get());
1142 static std::vector<DestructorCall*>& List() {
return *list_; }
1144 static void ResetList() {
1145 for (
size_t i = 0;
i < list_->size(); ++
i) {
1146 delete list_->at(i);
1153 #if GTEST_OS_WINDOWS
1154 AutoHandle wait_event_;
1156 static std::vector<DestructorCall*>*
const list_;
1161 std::vector<DestructorCall*>*
const DestructorCall::list_ =
1162 new std::vector<DestructorCall*>;
1166 class DestructorTracker {
1168 DestructorTracker() :
index_(GetNewIndex()) {}
1169 DestructorTracker(
const DestructorTracker& )
1170 :
index_(GetNewIndex()) {}
1171 ~DestructorTracker() {
1174 DestructorCall::List()[
index_]->ReportDestroyed();
1178 static size_t GetNewIndex() {
1179 DestructorCall::List().push_back(
new DestructorCall);
1180 return DestructorCall::List().size() - 1;
1187 typedef ThreadLocal<DestructorTracker>* ThreadParam;
1189 void CallThreadLocalGet(ThreadParam thread_local_param) {
1190 thread_local_param->get();
1195 TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1196 DestructorCall::ResetList();
1199 ThreadLocal<DestructorTracker> thread_local_tracker;
1203 thread_local_tracker.get();
1205 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1210 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1212 DestructorCall::ResetList();
1217 TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1218 DestructorCall::ResetList();
1221 ThreadLocal<DestructorTracker> thread_local_tracker;
1225 ThreadWithParam<ThreadParam>
thread(&CallThreadLocalGet,
1226 &thread_local_tracker,
nullptr);
1236 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1238 DestructorCall::ResetList();
1241 TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1242 ThreadLocal<std::string> thread_local_string;
1243 thread_local_string.set(
"Foo");
1244 EXPECT_STREQ(
"Foo", thread_local_string.get().c_str());
1247 RunFromThread(&RetrieveThreadLocalValue,
1248 make_pair(&thread_local_string, &
result));
1252 #endif // GTEST_IS_THREADSAFE
1254 #if GTEST_OS_WINDOWS
1255 TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1256 StaticAssertTypeEq<HANDLE, void*>();
1259 #if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
1260 TEST(WindowsTypesTest, _CRITICAL_SECTIONIs_CRITICAL_SECTION) {
1261 StaticAssertTypeEq<CRITICAL_SECTION, _CRITICAL_SECTION>();
1264 TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1265 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
1269 #endif // GTEST_OS_WINDOWS