34 #include "gtest/internal/gtest-port.h" 40 #endif // GTEST_OS_MAC 46 #include "gtest/gtest.h" 47 #include "gtest/gtest-spi.h" 54 #define GTEST_IMPLEMENTATION_ 1 55 #include "src/gtest-internal-inl.h" 56 #undef GTEST_IMPLEMENTATION_ 64 TEST(IsXDigitTest, WorksForNarrowAscii) {
77 TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
82 TEST(IsXDigitTest, WorksForWideAscii) {
95 TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
119 TEST(ImplicitCastTest, ConvertsPointers) {
121 EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
124 TEST(ImplicitCastTest, CanUseInheritance) {
126 Base base = ::testing::internal::ImplicitCast_<Base>(derived);
127 EXPECT_EQ(derived.member(), base.member());
132 explicit Castable(
bool* converted) : converted_(converted) {}
142 TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
143 bool converted =
false;
145 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
161 TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
162 bool converted =
false;
164 Base base = ::testing::internal::ImplicitCast_<Base>(const_castable);
171 : converted_(converted), const_converted_(const_converted) {}
177 *const_converted_ =
true;
186 TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
187 bool converted =
false;
188 bool const_converted =
false;
190 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
195 const_converted =
false;
197 base = ::testing::internal::ImplicitCast_<Base>(const_castable);
204 To(
bool* converted) { *converted =
true; }
207 TEST(ImplicitCastTest, CanUseImplicitConstructor) {
208 bool converted =
false;
209 To to = ::testing::internal::ImplicitCast_<To>(&converted);
214 TEST(IteratorTraitsTest, WorksForSTLContainerIterators) {
221 TEST(IteratorTraitsTest, WorksForPointerToNonConst) {
222 StaticAssertTypeEq<char, IteratorTraits<char*>::value_type>();
223 StaticAssertTypeEq<const void*, IteratorTraits<const void**>::value_type>();
226 TEST(IteratorTraitsTest, WorksForPointerToConst) {
227 StaticAssertTypeEq<char, IteratorTraits<const char*>::value_type>();
234 TEST(ScopedPtrTest, DefinesElementType) {
235 StaticAssertTypeEq<int, ::testing::internal::scoped_ptr<int>::element_type>();
240 TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
242 GTEST_CHECK_(
false) <<
"This should never be executed; " 243 "It's a compilation test only.";
256 TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
270 TEST(FormatFileLocationTest, FormatsFileLocation) {
275 TEST(FormatFileLocationTest, FormatsUnknownFile) {
281 TEST(FormatFileLocationTest, FormatsUknownLine) {
285 TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
290 TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
294 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
299 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
303 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
307 #if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX 308 void* ThreadFunc(
void*
data) {
315 TEST(GetThreadCountTest, ReturnsCorrectValue) {
324 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
326 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
327 ASSERT_EQ(0, pthread_attr_destroy(&attr));
333 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
338 for (
int i = 0;
i < 5; ++
i) {
342 SleepMilliseconds(100);
348 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
351 #endif // GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX 353 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
354 const bool a_false_condition =
false;
357 "gtest-port_test\\.cc\\(\\d+\\):" 358 #elif GTEST_USES_POSIX_RE 359 "gtest-port_test\\.cc:[0-9]+" 361 "gtest-port_test\\.cc:\\d+" 363 ".*a_false_condition.*Extra info.*";
369 #if GTEST_HAS_DEATH_TEST 371 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
374 ::std::cerr <<
"Success\n";
376 ::testing::ExitedWithCode(0),
"Success");
379 #endif // GTEST_HAS_DEATH_TEST 384 TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
386 # if GTEST_HAS_POSIX_RE 395 #endif // !GTEST_USES_PCRE 398 #if GTEST_USES_POSIX_RE 400 # if GTEST_HAS_TYPED_TEST 402 template <
typename Str>
407 typedef testing::Types<
409 # if GTEST_HAS_GLOBAL_STRING 411 # endif // GTEST_HAS_GLOBAL_STRING 412 const char*> StringTypes;
417 TYPED_TEST(RETest, ImplicitConstructorWorks) {
421 const RE simple(TypeParam(
"hello"));
424 const RE normal(TypeParam(
".*(\\w+)"));
431 const RE invalid(TypeParam(
"?"));
432 },
"\"?\" is not a valid POSIX Extended regular expression.");
441 const RE re(TypeParam(
"a.*z"));
454 const RE re(TypeParam(
"a.*z"));
462 # endif // GTEST_HAS_TYPED_TEST 464 #elif GTEST_USES_SIMPLE_RE 466 TEST(IsInSetTest, NulCharIsNotInAnySet) {
472 TEST(IsInSetTest, WorksForNonNulChars) {
480 TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
489 TEST(IsAsciiDigitTest, IsTrueForDigit) {
496 TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
504 TEST(IsAsciiPunctTest, IsTrueForPunct) {
505 for (
const char* p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
510 TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
518 TEST(IsRepeatTest, IsTrueForRepeatChar) {
524 TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
532 TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
541 TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
549 TEST(IsAsciiWordCharTest, IsTrueForLetter) {
556 TEST(IsAsciiWordCharTest, IsTrueForDigit) {
563 TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
567 TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
572 TEST(IsValidEscapeTest, IsFalseForDigit) {
577 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
582 TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
587 TEST(IsValidEscapeTest, IsTrueForPunct) {
598 TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
607 TEST(AtomMatchesCharTest, EscapedPunct) {
619 TEST(AtomMatchesCharTest, Escaped_d) {
628 TEST(AtomMatchesCharTest, Escaped_D) {
637 TEST(AtomMatchesCharTest, Escaped_s) {
648 TEST(AtomMatchesCharTest, Escaped_S) {
657 TEST(AtomMatchesCharTest, Escaped_w) {
669 TEST(AtomMatchesCharTest, Escaped_W) {
680 TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
699 TEST(AtomMatchesCharTest, UnescapedDot) {
708 TEST(AtomMatchesCharTest, UnescapedChar) {
718 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
720 "NULL is not a valid simple regular expression");
723 "Syntax error at index 1 in simple regular expression \"a\\\": ");
725 "'\\' cannot appear at the end");
727 "'\\' cannot appear at the end");
729 "invalid escape sequence \"\\h\"");
731 "'^' can only appear at the beginning");
733 "'^' can only appear at the beginning");
735 "'$' can only appear at the end");
737 "'$' can only appear at the end");
739 "'(' is unsupported");
741 "')' is unsupported");
743 "'[' is unsupported");
745 "'{' is unsupported");
747 "'?' can only follow a repeatable token");
749 "'*' can only follow a repeatable token");
751 "'+' can only follow a repeatable token");
754 TEST(ValidateRegexTest, ReturnsTrueForValid) {
762 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
765 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
766 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
768 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
771 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
773 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
774 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
777 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
778 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
781 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
783 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
785 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
788 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
789 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
791 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
794 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
796 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
799 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
804 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
811 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
819 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
828 TEST(MatchRegexAtHeadTest,
829 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
839 TEST(MatchRegexAtHeadTest, MatchesSequentially) {
845 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
849 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
858 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
860 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
863 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
864 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
866 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
869 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
870 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
871 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
875 TEST(RETest, ImplicitConstructorWorks) {
884 TEST(RETest, RejectsInvalidRegex) {
886 const RE normal(NULL);
887 },
"NULL is not a valid simple regular expression");
890 const RE normal(
".*(\\w+");
891 },
"'(' is unsupported");
894 const RE invalid(
"^?");
895 },
"'?' can only follow a repeatable token");
899 TEST(RETest, FullMatchWorks) {
915 TEST(RETest, PartialMatchWorks) {
928 #endif // GTEST_USES_POSIX_RE 930 #if !GTEST_OS_WINDOWS_MOBILE 932 TEST(CaptureTest, CapturesStdout) {
934 fprintf(stdout,
"abc");
938 fprintf(stdout,
"def%cghi",
'\0');
942 TEST(CaptureTest, CapturesStderr) {
944 fprintf(stderr,
"jkl");
948 fprintf(stderr,
"jkl%cmno",
'\0');
953 TEST(CaptureTest, CapturesStdoutAndStderr) {
956 fprintf(stdout,
"pqr");
957 fprintf(stderr,
"stu");
962 TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
965 "Only one stdout capturer can exist at a time");
972 #endif // !GTEST_OS_WINDOWS_MOBILE 974 TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
982 TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
997 TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
1002 TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
1008 thread_local_string.
set(
"foo");
1012 TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
1015 thread_local_string;
1019 thread_local_string.
set(
"foo");
1023 #if GTEST_IS_THREADSAFE 1025 void AddTwo(
int* param) { *param += 2; }
1027 TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
1029 ThreadWithParam<int*> thread(&AddTwo, &i, NULL);
1034 TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1045 TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1051 class AtomicCounterWithMutex {
1053 explicit AtomicCounterWithMutex(
Mutex*
mutex) :
1054 value_(0),
mutex_(mutex), random_(42) {}
1063 #if GTEST_HAS_PTHREAD 1067 pthread_mutex_t memory_barrier_mutex;
1069 pthread_mutex_init(&memory_barrier_mutex, NULL));
1072 SleepMilliseconds(random_.Generate(30));
1076 #elif GTEST_OS_WINDOWS 1078 volatile LONG
dummy = 0;
1079 ::InterlockedIncrement(&dummy);
1080 SleepMilliseconds(random_.Generate(30));
1081 ::InterlockedIncrement(&dummy);
1083 # error "Memory barrier not implemented on this platform." 1084 #endif // GTEST_HAS_PTHREAD 1088 int value()
const {
return value_; }
1091 volatile int value_;
1097 for (
int i = 0;
i < param.second; ++
i)
1098 param.first->Increment();
1102 TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1104 AtomicCounterWithMutex locked_counter(&mutex);
1106 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1107 const int kCycleCount = 20;
1108 const int kThreadCount = 7;
1110 Notification threads_can_start;
1113 for (
int i = 0;
i < kThreadCount; ++
i) {
1114 counting_threads[
i].
reset(
new ThreadType(&CountingThreadFunc,
1115 make_pair(&locked_counter,
1117 &threads_can_start));
1119 threads_can_start.Notify();
1120 for (
int i = 0;
i < kThreadCount; ++
i)
1121 counting_threads[
i]->Join();
1127 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1130 template <
typename T>
1131 void RunFromThread(
void (func)(
T),
T param) {
1132 ThreadWithParam<T> thread(func, param, NULL);
1136 void RetrieveThreadLocalValue(
1138 *param.second = param.first->get();
1141 TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1145 thread_local_string.
set(
"bar");
1149 RunFromThread(&RetrieveThreadLocalValue,
1150 make_pair(&thread_local_string, &result));
1156 class DestructorCall {
1160 #if GTEST_OS_WINDOWS 1161 wait_event_.Reset(::CreateEvent(NULL,
TRUE,
FALSE, NULL));
1166 bool CheckDestroyed()
const {
1167 #if GTEST_OS_WINDOWS 1168 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1174 void ReportDestroyed() {
1176 #if GTEST_OS_WINDOWS 1177 ::SetEvent(wait_event_.Get());
1181 static std::vector<DestructorCall*>&
List() {
return *list_; }
1183 static void ResetList() {
1184 for (
size_t i = 0;
i < list_->size(); ++
i) {
1185 delete list_->at(
i);
1192 #if GTEST_OS_WINDOWS 1193 AutoHandle wait_event_;
1195 static std::vector<DestructorCall*>*
const list_;
1200 std::vector<DestructorCall*>*
const DestructorCall::list_ =
1201 new std::vector<DestructorCall*>;
1205 class DestructorTracker {
1207 DestructorTracker() : index_(GetNewIndex()) {}
1208 DestructorTracker(
const DestructorTracker& )
1209 : index_(GetNewIndex()) {}
1210 ~DestructorTracker() {
1213 DestructorCall::List()[index_]->ReportDestroyed();
1217 static size_t GetNewIndex() {
1218 DestructorCall::List().push_back(
new DestructorCall);
1219 return DestructorCall::List().size() - 1;
1221 const size_t index_;
1228 void CallThreadLocalGet(ThreadParam thread_local_param) {
1229 thread_local_param->
get();
1234 TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1235 DestructorCall::ResetList();
1242 thread_local_tracker.
get();
1244 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1249 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1251 DestructorCall::ResetList();
1256 TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1257 DestructorCall::ResetList();
1264 ThreadWithParam<ThreadParam> thread(
1265 &CallThreadLocalGet, &thread_local_tracker, NULL);
1275 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1277 DestructorCall::ResetList();
1280 TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1282 thread_local_string.
set(
"Foo");
1286 RunFromThread(&RetrieveThreadLocalValue,
1287 make_pair(&thread_local_string, &result));
1291 #endif // GTEST_IS_THREADSAFE 1293 #if GTEST_OS_WINDOWS 1294 TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1295 StaticAssertTypeEq<HANDLE, void*>();
1298 TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1299 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
1301 #endif // GTEST_OS_WINDOWS
TYPED_TEST(CodeLocationForTYPEDTEST, Verify)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
#define GTEST_USES_POSIX_RE
static bool FullMatch(const ::std::string &str, const RE &re)
TYPED_TEST_CASE(CodeLocationForTYPEDTEST, int)
#define ASSERT_EQ(val1, val2)
Iterator::value_type value_type
::std::string FormatFileLocation(const char *file, int line)
#define EXPECT_EQ(val1, val2)
#define EXPECT_PRED1(pred, v1)
static bool PartialMatch(const ::std::string &str, const RE &re)
#define GTEST_DISALLOW_ASSIGN_(type)
#define EXPECT_STREQ(s1, s2)
constexpr auto size(C const &c) -> decltype(c.size())
constexpr auto empty(C const &c) -> decltype(c.empty())
std::string GetCapturedStderr()
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
#define GTEST_CHECK_(condition)
GTEST_API_ bool AlwaysTrue()
static map< string, int > m
NoDefaultContructor(const char *)
AtomicCounter< T, DeterministicAtomic > Base
GTEST_API_ AssertionResult IsSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
ConstCastable(bool *converted)
ConstAndNonConstCastable(bool *converted, bool *const_converted)
static const char *const value
folly::Singleton< int > bar
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
const char * pattern() const
#define EXPECT_TRUE(condition)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
NoDefaultContructor(const NoDefaultContructor &)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
bool StaticAssertTypeEq()
std::string GetCapturedStdout()
#define ASSERT_FALSE(condition)
#define EXPECT_FALSE(condition)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Castable(bool *converted)
static constexpr uint64_t data[1]
TEST(IsXDigitTest, WorksForNarrowAscii)