56TEST(IsXDigitTest, WorksForNarrowAscii) {
69TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
74TEST(IsXDigitTest, WorksForWideAscii) {
87TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
98 explicit Base(
int n) : member_(n) {}
106class Derived :
public Base {
111TEST(ImplicitCastTest, ConvertsPointers) {
116TEST(ImplicitCastTest, CanUseInheritance) {
119 EXPECT_EQ(derived.member(), base.member());
124 explicit Castable(
bool* converted) : converted_(converted) {}
134TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
135 bool converted =
false;
144 operator Base()
const {
153TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
154 bool converted =
false;
163 : converted_(converted), const_converted_(const_converted) {}
168 operator Base()
const {
169 *const_converted_ =
true;
175 bool* const_converted_;
178TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
179 bool converted =
false;
180 bool const_converted =
false;
187 const_converted =
false;
196 To(
bool* converted) { *converted =
true; }
199TEST(ImplicitCastTest, CanUseImplicitConstructor) {
200 bool converted =
false;
206TEST(IteratorTraitsTest, WorksForSTLContainerIterators) {
207 StaticAssertTypeEq<int,
209 StaticAssertTypeEq<bool,
213TEST(IteratorTraitsTest, WorksForPointerToNonConst) {
214 StaticAssertTypeEq<char, IteratorTraits<char*>::value_type>();
215 StaticAssertTypeEq<const void*, IteratorTraits<const void**>::value_type>();
218TEST(IteratorTraitsTest, WorksForPointerToConst) {
219 StaticAssertTypeEq<char, IteratorTraits<const char*>::value_type>();
220 StaticAssertTypeEq<
const void*,
226TEST(ScopedPtrTest, DefinesElementType) {
227 StaticAssertTypeEq<int, ::testing::internal::scoped_ptr<int>::element_type>();
232TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
234 GTEST_CHECK_(
false) <<
"This should never be executed; "
235 "It's a compilation test only.";
248TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
262TEST(FormatFileLocationTest, FormatsFileLocation) {
267TEST(FormatFileLocationTest, FormatsUnknownFile) {
273TEST(FormatFileLocationTest, FormatsUknownLine) {
277TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
282TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
286TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
291TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
295TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
299#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA
300void* ThreadFunc(
void* data) {
307TEST(GetThreadCountTest, ReturnsCorrectValue) {
311 internal::Mutex mutex;
316 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
318 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
319 ASSERT_EQ(0, pthread_attr_destroy(&attr));
325 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
330 for (
int i = 0; i < 5; ++i) {
334 SleepMilliseconds(100);
340TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
345TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
346 const bool a_false_condition =
false;
349 "gtest-port_test\\.cc\\(\\d+\\):"
350#elif GTEST_USES_POSIX_RE
351 "gtest-port_test\\.cc:[0-9]+"
353 "gtest-port_test\\.cc:\\d+"
355 ".*a_false_condition.*Extra info.*";
361#if GTEST_HAS_DEATH_TEST
363TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
366 ::std::cerr <<
"Success\n";
368 ::testing::ExitedWithCode(0),
"Success");
376TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
378# if GTEST_HAS_POSIX_RE
390#if GTEST_USES_POSIX_RE
392# if GTEST_HAS_TYPED_TEST
394template <
typename Str>
399typedef testing::Types<
401# if GTEST_HAS_GLOBAL_STRING
404 const char*> StringTypes;
410 const RE empty(TypeParam(
""));
413 const RE simple(TypeParam(
"hello"));
416 const RE normal(TypeParam(
".*(\\w+)"));
423 const RE invalid(TypeParam(
"?"));
424 },
"\"?\" is not a valid POSIX Extended regular expression.");
429 const RE empty(TypeParam(
""));
433 const RE re(TypeParam(
"a.*z"));
442 const RE empty(TypeParam(
""));
446 const RE re(TypeParam(
"a.*z"));
456#elif GTEST_USES_SIMPLE_RE
458TEST(IsInSetTest, NulCharIsNotInAnySet) {
464TEST(IsInSetTest, WorksForNonNulChars) {
472TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
481TEST(IsAsciiDigitTest, IsTrueForDigit) {
488TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
496TEST(IsAsciiPunctTest, IsTrueForPunct) {
497 for (
const char*
p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *
p;
p++) {
502TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
510TEST(IsRepeatTest, IsTrueForRepeatChar) {
516TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
524TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
533TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
541TEST(IsAsciiWordCharTest, IsTrueForLetter) {
548TEST(IsAsciiWordCharTest, IsTrueForDigit) {
555TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
559TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
564TEST(IsValidEscapeTest, IsFalseForDigit) {
569TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
574TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
579TEST(IsValidEscapeTest, IsTrueForPunct) {
590TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
599TEST(AtomMatchesCharTest, EscapedPunct) {
611TEST(AtomMatchesCharTest, Escaped_d) {
620TEST(AtomMatchesCharTest, Escaped_D) {
629TEST(AtomMatchesCharTest, Escaped_s) {
640TEST(AtomMatchesCharTest, Escaped_S) {
649TEST(AtomMatchesCharTest, Escaped_w) {
661TEST(AtomMatchesCharTest, Escaped_W) {
672TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
691TEST(AtomMatchesCharTest, UnescapedDot) {
700TEST(AtomMatchesCharTest, UnescapedChar) {
710TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
712 "NULL is not a valid simple regular expression");
715 "Syntax error at index 1 in simple regular expression \"a\\\": ");
717 "'\\' cannot appear at the end");
719 "'\\' cannot appear at the end");
721 "invalid escape sequence \"\\h\"");
723 "'^' can only appear at the beginning");
725 "'^' can only appear at the beginning");
727 "'$' can only appear at the end");
729 "'$' can only appear at the end");
731 "'(' is unsupported");
733 "')' is unsupported");
735 "'[' is unsupported");
737 "'{' is unsupported");
739 "'?' can only follow a repeatable token");
741 "'*' can only follow a repeatable token");
743 "'+' can only follow a repeatable token");
746TEST(ValidateRegexTest, ReturnsTrueForValid) {
754 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
757TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
758 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
760 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
763 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
765 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
766 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
769TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
770 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
773 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
775 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
777 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
780TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
781 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
783 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
786 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
788 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
791TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
796TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
803TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
811TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
820TEST(MatchRegexAtHeadTest,
821 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
831TEST(MatchRegexAtHeadTest, MatchesSequentially) {
837TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
841TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
850TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
852 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
855TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
856 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
858 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
861TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
862 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
863 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
867TEST(RETest, ImplicitConstructorWorks) {
876TEST(RETest, RejectsInvalidRegex) {
879 },
"NULL is not a valid simple regular expression");
882 const RE
normal(
".*(\\w+");
883 },
"'(' is unsupported");
886 const RE invalid(
"^?");
887 },
"'?' can only follow a repeatable token");
891TEST(RETest, FullMatchWorks) {
907TEST(RETest, PartialMatchWorks) {
922#if !GTEST_OS_WINDOWS_MOBILE
924TEST(CaptureTest, CapturesStdout) {
926 fprintf(stdout,
"abc");
930 fprintf(stdout,
"def%cghi",
'\0');
934TEST(CaptureTest, CapturesStderr) {
936 fprintf(stderr,
"jkl");
940 fprintf(stderr,
"jkl%cmno",
'\0');
945TEST(CaptureTest, CapturesStdoutAndStderr) {
948 fprintf(stdout,
"pqr");
949 fprintf(stderr,
"stu");
954TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
957 "Only one stdout capturer can exist at a time");
966TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
974TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
989TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
994TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
1000 thread_local_string.
set(
"foo");
1004TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
1007 thread_local_string;
1011 thread_local_string.
set(
"foo");
1015#if GTEST_IS_THREADSAFE
1017void AddTwo(
int* param) { *param += 2; }
1019TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
1021 ThreadWithParam<int*> thread(&AddTwo, &i, NULL);
1026TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1037TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1043class AtomicCounterWithMutex {
1045 explicit AtomicCounterWithMutex(Mutex* mutex) :
1046 value_(0), mutex_(mutex), random_(42) {}
1055#if GTEST_HAS_PTHREAD
1059 pthread_mutex_t memory_barrier_mutex;
1061 pthread_mutex_init(&memory_barrier_mutex, NULL));
1064 SleepMilliseconds(random_.Generate(30));
1068#elif GTEST_OS_WINDOWS
1071 ::InterlockedIncrement(&dummy);
1072 SleepMilliseconds(random_.Generate(30));
1073 ::InterlockedIncrement(&dummy);
1075# error "Memory barrier not implemented on this platform."
1080 int value()
const {
return value_; }
1083 volatile int value_;
1084 Mutex*
const mutex_;
1088void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1089 for (
int i = 0; i < param.second; ++i)
1090 param.first->Increment();
1094TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1096 AtomicCounterWithMutex locked_counter(&mutex);
1098 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1099 const int kCycleCount = 20;
1100 const int kThreadCount = 7;
1101 scoped_ptr<ThreadType> counting_threads[kThreadCount];
1102 Notification threads_can_start;
1105 for (
int i = 0; i < kThreadCount; ++i) {
1106 counting_threads[i].reset(
new ThreadType(&CountingThreadFunc,
1107 make_pair(&locked_counter,
1109 &threads_can_start));
1111 threads_can_start.Notify();
1112 for (
int i = 0; i < kThreadCount; ++i)
1113 counting_threads[i]->
Join();
1119 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1122template <
typename T>
1123void RunFromThread(
void (func)(
T),
T param) {
1124 ThreadWithParam<T> thread(func, param, NULL);
1128void RetrieveThreadLocalValue(
1129 pair<ThreadLocal<std::string>*, std::string*> param) {
1130 *param.second = param.first->get();
1133TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1134 ThreadLocal<std::string> thread_local_string(
"foo");
1135 EXPECT_STREQ(
"foo", thread_local_string.get().c_str());
1137 thread_local_string.set(
"bar");
1138 EXPECT_STREQ(
"bar", thread_local_string.get().c_str());
1141 RunFromThread(&RetrieveThreadLocalValue,
1142 make_pair(&thread_local_string, &result));
1148class DestructorCall {
1153 wait_event_.Reset(::CreateEvent(NULL,
TRUE,
FALSE, NULL));
1158 bool CheckDestroyed()
const {
1160 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1166 void ReportDestroyed() {
1169 ::SetEvent(wait_event_.Get());
1173 static std::vector<DestructorCall*>&
List() {
return *list_; }
1175 static void ResetList() {
1176 for (
size_t i = 0; i < list_->size(); ++i) {
1177 delete list_->at(i);
1185 AutoHandle wait_event_;
1187 static std::vector<DestructorCall*>*
const list_;
1192std::vector<DestructorCall*>*
const DestructorCall::list_ =
1193 new std::vector<DestructorCall*>;
1197class DestructorTracker {
1199 DestructorTracker() : index_(GetNewIndex()) {}
1200 DestructorTracker(
const DestructorTracker& )
1201 : index_(GetNewIndex()) {}
1202 ~DestructorTracker() {
1205 DestructorCall::List()[index_]->ReportDestroyed();
1209 static size_t GetNewIndex() {
1210 DestructorCall::List().push_back(
new DestructorCall);
1211 return DestructorCall::List().size() - 1;
1213 const size_t index_;
1218typedef ThreadLocal<DestructorTracker>* ThreadParam;
1220void CallThreadLocalGet(ThreadParam thread_local_param) {
1221 thread_local_param->get();
1226TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1227 DestructorCall::ResetList();
1230 ThreadLocal<DestructorTracker> thread_local_tracker;
1231 ASSERT_EQ(0
U, DestructorCall::List().size());
1234 thread_local_tracker.get();
1235 ASSERT_EQ(1U, DestructorCall::List().size());
1236 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1240 ASSERT_EQ(1U, DestructorCall::List().size());
1241 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1243 DestructorCall::ResetList();
1248TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1249 DestructorCall::ResetList();
1252 ThreadLocal<DestructorTracker> thread_local_tracker;
1253 ASSERT_EQ(0
U, DestructorCall::List().size());
1256 ThreadWithParam<ThreadParam> thread(
1257 &CallThreadLocalGet, &thread_local_tracker, NULL);
1262 ASSERT_EQ(1U, DestructorCall::List().size());
1266 ASSERT_EQ(1U, DestructorCall::List().size());
1267 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1269 DestructorCall::ResetList();
1272TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1273 ThreadLocal<std::string> thread_local_string;
1274 thread_local_string.set(
"Foo");
1275 EXPECT_STREQ(
"Foo", thread_local_string.get().c_str());
1278 RunFromThread(&RetrieveThreadLocalValue,
1279 make_pair(&thread_local_string, &result));
1286TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1290#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
1291TEST(WindowsTypesTest, _CRITICAL_SECTIONIs_CRITICAL_SECTION) {
1295TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
Castable(bool *converted)
ConstAndNonConstCastable(bool *converted, bool *const_converted)
ConstCastable(bool *converted)
NoDefaultContructor(const char *)
NoDefaultContructor(const NoDefaultContructor &)
static bool PartialMatch(const ::std::string &str, const RE &re)
static bool FullMatch(const ::std::string &str, const RE &re)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
#define GTEST_CHECK_(condition)
#define GTEST_DISALLOW_ASSIGN_(type)
#define GTEST_USES_POSIX_RE
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define ASSERT_EQ(val1, val2)
#define EXPECT_EQ(val1, val2)
#define ASSERT_FALSE(condition)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define TEST(test_case_name, test_name)
#define EXPECT_FALSE(condition)
#define EXPECT_PRED1(pred, v1)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
std::string simple(const App *app, const Error &e)
Printout a clean, simple message on error (the default in CLI11 1.5+)
constexpr enabler dummy
An instance to use in EnableIf.
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
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()
GTEST_API_ void CaptureStdout()
GTEST_API_ std::string GetCapturedStdout()
TYPED_TEST(CodeLocationForTYPEDTEST, Verify)
bool StaticAssertTypeEq()
TYPED_TEST_CASE(CodeLocationForTYPEDTEST, int)
#define T(meth, val, expected)
Iterator::value_type value_type