54TEST(IsXDigitTest, WorksForNarrowAscii) {
67TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
72TEST(IsXDigitTest, WorksForWideAscii) {
85TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
94 explicit Base(
int n) : member_(n) {}
95 Base(
const Base&) =
default;
104class Derived :
public Base {
109TEST(ImplicitCastTest, ConvertsPointers) {
111 EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
114TEST(ImplicitCastTest, CanUseInheritance) {
116 Base base = ::testing::internal::ImplicitCast_<Base>(derived);
117 EXPECT_EQ(derived.member(), base.member());
122 explicit Castable(
bool* converted) : converted_(converted) {}
132TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
133 bool converted =
false;
135 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
142 operator Base()
const {
151TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
152 bool converted =
false;
154 Base base = ::testing::internal::ImplicitCast_<Base>(const_castable);
161 : converted_(converted), const_converted_(const_converted) {}
166 operator Base()
const {
167 *const_converted_ =
true;
173 bool* const_converted_;
176TEST(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; }
197TEST(ImplicitCastTest, CanUseImplicitConstructor) {
198 bool converted =
false;
199 To to = ::testing::internal::ImplicitCast_<To>(&converted);
206#pragma GCC diagnostic push
207#pragma GCC diagnostic ignored "-Wdangling-else"
208#pragma GCC diagnostic ignored "-Wempty-body"
209#pragma GCC diagnostic ignored "-Wpragmas"
211TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
213 GTEST_CHECK_(
false) <<
"This should never be executed; "
214 "It's a compilation test only.";
227#pragma GCC diagnostic pop
230TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
244TEST(FormatFileLocationTest, FormatsFileLocation) {
249TEST(FormatFileLocationTest, FormatsUnknownFile) {
255TEST(FormatFileLocationTest, FormatsUknownLine) {
259TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
264TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
268TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
273TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
277TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
281#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA || \
282 GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
283 GTEST_OS_NETBSD || GTEST_OS_OPENBSD
284void* ThreadFunc(
void* data) {
291TEST(GetThreadCountTest, ReturnsCorrectValue) {
295 internal::Mutex mutex;
300 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
302 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
303 ASSERT_EQ(0, pthread_attr_destroy(&attr));
309 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
314 for (
int i = 0;
i < 5; ++
i) {
318 SleepMilliseconds(100);
324TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
329TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
330 const bool a_false_condition =
false;
333 "googletest-port-test\\.cc\\(\\d+\\):"
334#elif GTEST_USES_POSIX_RE
335 "googletest-port-test\\.cc:[0-9]+"
337 "googletest-port-test\\.cc:\\d+"
339 ".*a_false_condition.*Extra info.*";
345#if GTEST_HAS_DEATH_TEST
347TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
350 ::std::cerr <<
"Success\n";
352 ::testing::ExitedWithCode(0),
"Success");
360TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
362# if GTEST_HAS_POSIX_RE
374#if GTEST_USES_POSIX_RE
376template <
typename Str>
387 const RE empty(TypeParam(
""));
390 const RE simple(TypeParam(
"hello"));
393 const RE
normal(TypeParam(
".*(\\w+)"));
400 const RE invalid(TypeParam(
"?"));
401 },
"\"?\" is not a valid POSIX Extended regular expression.");
406 const RE empty(TypeParam(
""));
410 const RE re(TypeParam(
"a.*z"));
419 const RE empty(TypeParam(
""));
423 const RE re(TypeParam(
"a.*z"));
431#elif GTEST_USES_SIMPLE_RE
433TEST(IsInSetTest, NulCharIsNotInAnySet) {
439TEST(IsInSetTest, WorksForNonNulChars) {
447TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
456TEST(IsAsciiDigitTest, IsTrueForDigit) {
463TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
471TEST(IsAsciiPunctTest, IsTrueForPunct) {
472 for (
const char*
p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *
p;
p++) {
477TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
485TEST(IsRepeatTest, IsTrueForRepeatChar) {
491TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
499TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
508TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
516TEST(IsAsciiWordCharTest, IsTrueForLetter) {
523TEST(IsAsciiWordCharTest, IsTrueForDigit) {
530TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
534TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
539TEST(IsValidEscapeTest, IsFalseForDigit) {
544TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
549TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
554TEST(IsValidEscapeTest, IsTrueForPunct) {
565TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
574TEST(AtomMatchesCharTest, EscapedPunct) {
586TEST(AtomMatchesCharTest, Escaped_d) {
595TEST(AtomMatchesCharTest, Escaped_D) {
604TEST(AtomMatchesCharTest, Escaped_s) {
615TEST(AtomMatchesCharTest, Escaped_S) {
624TEST(AtomMatchesCharTest, Escaped_w) {
636TEST(AtomMatchesCharTest, Escaped_W) {
647TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
666TEST(AtomMatchesCharTest, UnescapedDot) {
675TEST(AtomMatchesCharTest, UnescapedChar) {
685TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
687 "NULL is not a valid simple regular expression");
690 "Syntax error at index 1 in simple regular expression \"a\\\": ");
692 "'\\' cannot appear at the end");
694 "'\\' cannot appear at the end");
696 "invalid escape sequence \"\\h\"");
698 "'^' can only appear at the beginning");
700 "'^' can only appear at the beginning");
702 "'$' can only appear at the end");
704 "'$' can only appear at the end");
706 "'(' is unsupported");
708 "')' is unsupported");
710 "'[' is unsupported");
712 "'{' is unsupported");
714 "'?' can only follow a repeatable token");
716 "'*' can only follow a repeatable token");
718 "'+' can only follow a repeatable token");
721TEST(ValidateRegexTest, ReturnsTrueForValid) {
729 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
732TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
733 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
735 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
738 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
740 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
741 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
744TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
745 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
748 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
750 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
752 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
755TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
756 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
758 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
761 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
763 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
766TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
771TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
778TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
786TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
795TEST(MatchRegexAtHeadTest,
796 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
806TEST(MatchRegexAtHeadTest, MatchesSequentially) {
812TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
816TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
825TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
827 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
830TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
831 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
833 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
836TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
837 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
838 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
842TEST(RETest, ImplicitConstructorWorks) {
846 const RE simple(
"hello");
851TEST(RETest, RejectsInvalidRegex) {
854 },
"NULL is not a valid simple regular expression");
857 const RE
normal(
".*(\\w+");
858 },
"'(' is unsupported");
861 const RE invalid(
"^?");
862 },
"'?' can only follow a repeatable token");
866TEST(RETest, FullMatchWorks) {
882TEST(RETest, PartialMatchWorks) {
897#if !GTEST_OS_WINDOWS_MOBILE
899TEST(CaptureTest, CapturesStdout) {
901 fprintf(stdout,
"abc");
905 fprintf(stdout,
"def%cghi",
'\0');
909TEST(CaptureTest, CapturesStderr) {
911 fprintf(stderr,
"jkl");
915 fprintf(stderr,
"jkl%cmno",
'\0');
920TEST(CaptureTest, CapturesStdoutAndStderr) {
923 fprintf(stdout,
"pqr");
924 fprintf(stderr,
"stu");
929TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
932 "Only one stdout capturer can exist at a time");
941TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
949TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
964TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
969TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
975 thread_local_string.
set(
"foo");
979TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
986 thread_local_string.
set(
"foo");
990#if GTEST_IS_THREADSAFE
992void AddTwo(
int* param) { *param += 2; }
994TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
996 ThreadWithParam<int*> thread(&AddTwo, &
i,
nullptr);
1001TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1012TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1018class AtomicCounterWithMutex {
1020 explicit AtomicCounterWithMutex(Mutex* mutex) :
1021 value_(0), mutex_(mutex),
random_(42) {}
1030#if GTEST_HAS_PTHREAD
1034 pthread_mutex_t memory_barrier_mutex;
1036 pthread_mutex_init(&memory_barrier_mutex,
nullptr));
1039 SleepMilliseconds(
static_cast<int>(
random_.Generate(30)));
1043#elif GTEST_OS_WINDOWS
1045 volatile LONG dummy = 0;
1046 ::InterlockedIncrement(&dummy);
1047 SleepMilliseconds(
static_cast<int>(
random_.Generate(30)));
1048 ::InterlockedIncrement(&dummy);
1050# error "Memory barrier not implemented on this platform."
1055 int value()
const {
return value_; }
1058 volatile int value_;
1059 Mutex*
const mutex_;
1063void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1064 for (
int i = 0;
i < param.second; ++
i)
1065 param.first->Increment();
1069TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1071 AtomicCounterWithMutex locked_counter(&mutex);
1073 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1074 const int kCycleCount = 20;
1075 const int kThreadCount = 7;
1076 std::unique_ptr<ThreadType> counting_threads[kThreadCount];
1077 Notification threads_can_start;
1080 for (
int i = 0;
i < kThreadCount; ++
i) {
1081 counting_threads[
i].reset(
new ThreadType(&CountingThreadFunc,
1082 make_pair(&locked_counter,
1084 &threads_can_start));
1086 threads_can_start.Notify();
1087 for (
int i = 0;
i < kThreadCount; ++
i)
1088 counting_threads[
i]->Join();
1094 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1097template <
typename T>
1098void RunFromThread(
void (func)(T), T param) {
1099 ThreadWithParam<T> thread(func, param,
nullptr);
1103void RetrieveThreadLocalValue(
1104 pair<ThreadLocal<std::string>*, std::string*> param) {
1105 *param.second = param.first->get();
1108TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1109 ThreadLocal<std::string> thread_local_string(
"foo");
1110 EXPECT_STREQ(
"foo", thread_local_string.get().c_str());
1112 thread_local_string.set(
"bar");
1113 EXPECT_STREQ(
"bar", thread_local_string.get().c_str());
1116 RunFromThread(&RetrieveThreadLocalValue,
1117 make_pair(&thread_local_string, &result));
1123class DestructorCall {
1128 wait_event_.Reset(::CreateEvent(NULL,
TRUE,
FALSE, NULL));
1133 bool CheckDestroyed()
const {
1135 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1141 void ReportDestroyed() {
1144 ::SetEvent(wait_event_.Get());
1148 static std::vector<DestructorCall*>& List() {
return *list_; }
1150 static void ResetList() {
1151 for (
size_t i = 0;
i < list_->size(); ++
i) {
1152 delete list_->at(
i);
1160 AutoHandle wait_event_;
1162 static std::vector<DestructorCall*>*
const list_;
1167std::vector<DestructorCall*>*
const DestructorCall::list_ =
1168 new std::vector<DestructorCall*>;
1172class DestructorTracker {
1174 DestructorTracker() : index_(GetNewIndex()) {}
1175 DestructorTracker(
const DestructorTracker& )
1176 : index_(GetNewIndex()) {}
1177 ~DestructorTracker() {
1180 DestructorCall::List()[index_]->ReportDestroyed();
1184 static size_t GetNewIndex() {
1185 DestructorCall::List().push_back(
new DestructorCall);
1186 return DestructorCall::List().size() - 1;
1188 const size_t index_;
1191typedef ThreadLocal<DestructorTracker>* ThreadParam;
1193void CallThreadLocalGet(ThreadParam thread_local_param) {
1194 thread_local_param->get();
1199TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1200 DestructorCall::ResetList();
1203 ThreadLocal<DestructorTracker> thread_local_tracker;
1204 ASSERT_EQ(0U, DestructorCall::List().size());
1207 thread_local_tracker.get();
1208 ASSERT_EQ(1U, DestructorCall::List().size());
1209 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1213 ASSERT_EQ(1U, DestructorCall::List().size());
1214 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1216 DestructorCall::ResetList();
1221TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1222 DestructorCall::ResetList();
1225 ThreadLocal<DestructorTracker> thread_local_tracker;
1226 ASSERT_EQ(0U, DestructorCall::List().size());
1229 ThreadWithParam<ThreadParam> thread(&CallThreadLocalGet,
1230 &thread_local_tracker,
nullptr);
1235 ASSERT_EQ(1U, DestructorCall::List().size());
1239 ASSERT_EQ(1U, DestructorCall::List().size());
1240 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1242 DestructorCall::ResetList();
1245TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1246 ThreadLocal<std::string> thread_local_string;
1247 thread_local_string.set(
"Foo");
1248 EXPECT_STREQ(
"Foo", thread_local_string.get().c_str());
1251 RunFromThread(&RetrieveThreadLocalValue,
1252 make_pair(&thread_local_string, &result));
1259TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1260 StaticAssertTypeEq<HANDLE, void*>();
1263#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
1264TEST(WindowsTypesTest, _CRITICAL_SECTIONIs_CRITICAL_SECTION) {
1265 StaticAssertTypeEq<CRITICAL_SECTION, _CRITICAL_SECTION>();
1268TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1269 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
#define ASSERT_EQ(val1, val2)
#define EXPECT_EQ(val1, val2)
#define ASSERT_FALSE(condition)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define EXPECT_FALSE(condition)
#define EXPECT_PRED1(pred, v1)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
#define GTEST_CHECK_(condition)
#define GTEST_USES_POSIX_RE
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
GTEST_API_ AssertionResult IsSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
TEST(IsXDigitTest, WorksForNarrowAscii)
GTEST_API_::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
GTEST_API_ std::string GetCapturedStderr()
GTEST_API_ size_t GetThreadCount()
GTEST_API_ void CaptureStderr()
GTEST_API_ bool AlwaysTrue()
TYPED_TEST(TestSuiteWithCommentTest, Dummy)
TYPED_TEST_SUITE(TestSuiteWithCommentTest, Types< int >)
GTEST_API_ void CaptureStdout()
GTEST_API_ std::string GetCapturedStdout()
static bool PartialMatch(const ::std::string &str, const RE &re)
static bool FullMatch(const ::std::string &str, const RE &re)
Base(const Base &)=default
Base & operator=(const Base &)=default
Castable(bool *converted)
ConstCastable(bool *converted)
ConstAndNonConstCastable(bool *converted, bool *const_converted)
NoDefaultContructor(const char *)
NoDefaultContructor(const NoDefaultContructor &)