40 #endif // GTEST_OS_MAC 54 #define GTEST_IMPLEMENTATION_ 1 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);
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_MAC || GTEST_OS_QNX 308 void* ThreadFunc(
void*
data) {
309 pthread_mutex_t*
mutex =
static_cast<pthread_mutex_t*
>(
data);
310 pthread_mutex_lock(mutex);
311 pthread_mutex_unlock(mutex);
315 TEST(GetThreadCountTest, ReturnsCorrectValue) {
317 pthread_mutex_t
mutex;
323 pthread_mutex_init(&mutex, NULL);
324 pthread_mutex_lock(&mutex);
326 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
328 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
329 ASSERT_EQ(0, pthread_attr_destroy(&attr));
332 pthread_mutex_unlock(&mutex);
335 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
342 for (
int i = 0;
i < 5; ++
i) {
346 SleepMilliseconds(100);
349 # endif // GTEST_OS_MAC 352 pthread_mutex_destroy(&mutex);
355 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
358 #endif // GTEST_OS_MAC || GTEST_OS_QNX 360 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
361 const bool a_false_condition =
false;
364 "gtest-port_test\\.cc\\(\\d+\\):" 365 #elif GTEST_USES_POSIX_RE 366 "gtest-port_test\\.cc:[0-9]+" 368 "gtest-port_test\\.cc:\\d+" 370 ".*a_false_condition.*Extra info.*";
376 #if GTEST_HAS_DEATH_TEST 378 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
381 ::std::cerr <<
"Success\n";
383 ::testing::ExitedWithCode(0),
"Success");
386 #endif // GTEST_HAS_DEATH_TEST 391 TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
392 #if GTEST_HAS_POSIX_RE 403 #if GTEST_USES_POSIX_RE 405 # if GTEST_HAS_TYPED_TEST 407 template <
typename Str>
412 typedef testing::Types<
414 # if GTEST_HAS_GLOBAL_STRING 416 # endif // GTEST_HAS_GLOBAL_STRING 417 const char*> StringTypes;
422 TYPED_TEST(RETest, ImplicitConstructorWorks) {
423 const RE empty(TypeParam(
""));
426 const RE simple(TypeParam(
"hello"));
429 const RE normal(TypeParam(
".*(\\w+)"));
436 const RE invalid(TypeParam(
"?"));
437 },
"\"?\" is not a valid POSIX Extended regular expression.");
442 const RE empty(TypeParam(
""));
446 const RE re(TypeParam(
"a.*z"));
455 const RE empty(TypeParam(
""));
459 const RE re(TypeParam(
"a.*z"));
467 # endif // GTEST_HAS_TYPED_TEST 469 #elif GTEST_USES_SIMPLE_RE 471 TEST(IsInSetTest, NulCharIsNotInAnySet) {
477 TEST(IsInSetTest, WorksForNonNulChars) {
485 TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
494 TEST(IsAsciiDigitTest, IsTrueForDigit) {
501 TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
509 TEST(IsAsciiPunctTest, IsTrueForPunct) {
510 for (
const char*
p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *
p;
p++) {
515 TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
523 TEST(IsRepeatTest, IsTrueForRepeatChar) {
529 TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
537 TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
546 TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
554 TEST(IsAsciiWordCharTest, IsTrueForLetter) {
561 TEST(IsAsciiWordCharTest, IsTrueForDigit) {
568 TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
572 TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
577 TEST(IsValidEscapeTest, IsFalseForDigit) {
582 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
587 TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
592 TEST(IsValidEscapeTest, IsTrueForPunct) {
603 TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
612 TEST(AtomMatchesCharTest, EscapedPunct) {
624 TEST(AtomMatchesCharTest, Escaped_d) {
633 TEST(AtomMatchesCharTest, Escaped_D) {
642 TEST(AtomMatchesCharTest, Escaped_s) {
653 TEST(AtomMatchesCharTest, Escaped_S) {
662 TEST(AtomMatchesCharTest, Escaped_w) {
674 TEST(AtomMatchesCharTest, Escaped_W) {
685 TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
704 TEST(AtomMatchesCharTest, UnescapedDot) {
713 TEST(AtomMatchesCharTest, UnescapedChar) {
723 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
725 "NULL is not a valid simple regular expression");
728 "Syntax error at index 1 in simple regular expression \"a\\\": ");
730 "'\\' cannot appear at the end");
732 "'\\' cannot appear at the end");
734 "invalid escape sequence \"\\h\"");
736 "'^' can only appear at the beginning");
738 "'^' can only appear at the beginning");
740 "'$' can only appear at the end");
742 "'$' can only appear at the end");
744 "'(' is unsupported");
746 "')' is unsupported");
748 "'[' is unsupported");
750 "'{' is unsupported");
752 "'?' can only follow a repeatable token");
754 "'*' can only follow a repeatable token");
756 "'+' can only follow a repeatable token");
759 TEST(ValidateRegexTest, ReturnsTrueForValid) {
767 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
770 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
771 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
773 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
776 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
778 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
779 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
782 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
783 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
786 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
788 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
790 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
793 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
794 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
796 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
799 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
801 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
804 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
809 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
816 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
824 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
833 TEST(MatchRegexAtHeadTest,
834 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
844 TEST(MatchRegexAtHeadTest, MatchesSequentially) {
850 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
854 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
863 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
865 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
868 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
869 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
871 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
874 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
875 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
876 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
880 TEST(RETest, ImplicitConstructorWorks) {
884 const RE simple(
"hello");
889 TEST(RETest, RejectsInvalidRegex) {
891 const RE normal(NULL);
892 },
"NULL is not a valid simple regular expression");
895 const RE normal(
".*(\\w+");
896 },
"'(' is unsupported");
899 const RE invalid(
"^?");
900 },
"'?' can only follow a repeatable token");
904 TEST(RETest, FullMatchWorks) {
920 TEST(RETest, PartialMatchWorks) {
933 #endif // GTEST_USES_POSIX_RE 935 #if !GTEST_OS_WINDOWS_MOBILE 937 TEST(CaptureTest, CapturesStdout) {
943 fprintf(stdout,
"def%cghi",
'\0');
947 TEST(CaptureTest, CapturesStderr) {
953 fprintf(stderr,
"jkl%cmno",
'\0');
958 TEST(CaptureTest, CapturesStdoutAndStderr) {
967 TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
970 "Only one stdout capturer can exist at a time");
977 #endif // !GTEST_OS_WINDOWS_MOBILE 979 TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
987 TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
1002 TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
1007 TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
1013 thread_local_string.
set(
"foo");
1017 TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
1020 thread_local_string;
1024 thread_local_string.
set(
"foo");
1028 #if GTEST_IS_THREADSAFE 1030 void AddTwo(
int*
param) { *param += 2; }
1032 TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
1034 ThreadWithParam<int*> thread(&AddTwo, &i, NULL);
1039 TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1050 TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1056 class AtomicCounterWithMutex {
1058 explicit AtomicCounterWithMutex(
Mutex*
mutex) :
1059 value_(0), mutex_(mutex), random_(42) {}
1068 #if GTEST_HAS_PTHREAD 1072 pthread_mutex_t memory_barrier_mutex;
1074 pthread_mutex_init(&memory_barrier_mutex, NULL));
1077 SleepMilliseconds(random_.Generate(30));
1081 #elif GTEST_OS_WINDOWS 1083 volatile LONG dummy = 0;
1084 ::InterlockedIncrement(&dummy);
1085 SleepMilliseconds(random_.Generate(30));
1086 ::InterlockedIncrement(&dummy);
1088 # error "Memory barrier not implemented on this platform." 1089 #endif // GTEST_HAS_PTHREAD 1093 int value()
const {
return value_; }
1096 volatile int value_;
1097 Mutex*
const mutex_;
1101 void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1102 for (
int i = 0;
i < param.second; ++
i)
1103 param.first->Increment();
1107 TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1109 AtomicCounterWithMutex locked_counter(&mutex);
1111 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1112 const int kCycleCount = 20;
1113 const int kThreadCount = 7;
1118 for (
int i = 0;
i < kThreadCount; ++
i) {
1119 counting_threads[
i].
reset(
new ThreadType(&CountingThreadFunc,
1120 make_pair(&locked_counter,
1122 &threads_can_start));
1124 threads_can_start.Notify();
1125 for (
int i = 0;
i < kThreadCount; ++
i)
1126 counting_threads[
i]->Join();
1132 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1135 template <
typename T>
1136 void RunFromThread(
void (func)(T), T param) {
1137 ThreadWithParam<T> thread(func, param, NULL);
1141 void RetrieveThreadLocalValue(
1143 *param.second = param.first->get();
1146 TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1150 thread_local_string.
set(
"bar");
1154 RunFromThread(&RetrieveThreadLocalValue,
1155 make_pair(&thread_local_string, &result));
1161 class DestructorCall {
1165 #if GTEST_OS_WINDOWS 1166 wait_event_.Reset(::CreateEvent(NULL,
TRUE,
FALSE, NULL));
1171 bool CheckDestroyed()
const {
1172 #if GTEST_OS_WINDOWS 1173 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1179 void ReportDestroyed() {
1181 #if GTEST_OS_WINDOWS 1182 ::SetEvent(wait_event_.Get());
1186 static std::vector<DestructorCall*>& List() {
return *list_; }
1188 static void ResetList() {
1189 for (
size_t i = 0;
i < list_->size(); ++
i) {
1190 delete list_->at(
i);
1197 #if GTEST_OS_WINDOWS 1198 AutoHandle wait_event_;
1200 static std::vector<DestructorCall*>*
const list_;
1205 std::vector<DestructorCall*>*
const DestructorCall::list_ =
1206 new std::vector<DestructorCall*>;
1210 class DestructorTracker {
1212 DestructorTracker() : index_(GetNewIndex()) {}
1213 DestructorTracker(
const DestructorTracker& )
1214 : index_(GetNewIndex()) {}
1215 ~DestructorTracker() {
1218 DestructorCall::List()[index_]->ReportDestroyed();
1222 static int GetNewIndex() {
1223 DestructorCall::List().push_back(
new DestructorCall);
1224 return DestructorCall::List().size() - 1;
1233 void CallThreadLocalGet(ThreadParam thread_local_param) {
1234 thread_local_param->
get();
1239 TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1240 DestructorCall::ResetList();
1247 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1250 thread_local_tracker.
get();
1252 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1253 ASSERT_FALSE(DestructorCall::List()[1]->CheckDestroyed());
1260 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1261 EXPECT_TRUE(DestructorCall::List()[1]->CheckDestroyed());
1263 DestructorCall::ResetList();
1268 TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1269 DestructorCall::ResetList();
1276 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1279 ThreadWithParam<ThreadParam> thread(
1280 &CallThreadLocalGet, &thread_local_tracker, NULL);
1287 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1293 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1294 EXPECT_TRUE(DestructorCall::List()[1]->CheckDestroyed());
1296 DestructorCall::ResetList();
1299 TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1301 thread_local_string.
set(
"Foo");
1305 RunFromThread(&RetrieveThreadLocalValue,
1306 make_pair(&thread_local_string, &result));
1310 #endif // GTEST_IS_THREADSAFE 1312 #if GTEST_OS_WINDOWS 1313 TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1314 StaticAssertTypeEq<HANDLE, void*>();
1317 TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1318 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
1320 #endif // GTEST_OS_WINDOWS
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
GTEST_API_ std::string GetCapturedStdout()
GTEST_API_ std::string GetCapturedStderr()
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
static bool FullMatch(const ::std::string &str, const RE &re)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Iterator::value_type value_type
static pthread_mutex_t mutex
#define GTEST_USES_POSIX_RE
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
GTEST_API_ size_t GetThreadCount()
#define ASSERT_EQ(val1, val2)
GTEST_API_ void CaptureStdout()
static bool PartialMatch(const ::std::string &str, const RE &re)
UNITTEST_START int result
#define GTEST_DISALLOW_ASSIGN_(type)
#define EXPECT_PRED1(pred, v1)
TYPED_TEST(TypedTest, TestA)
TYPED_TEST_CASE(TypedTest, MyTypes)
#define GTEST_CHECK_(condition)
UNITTEST_START struct Curl_easy data
GTEST_API_ bool AlwaysTrue()
#define EXPECT_STREQ(expected, actual)
NoDefaultContructor(const char *)
GTEST_API_ AssertionResult IsSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
ConstCastable(bool *converted)
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
ConstAndNonConstCastable(bool *converted, bool *const_converted)
GTEST_API_::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
const char * pattern() const
#define EXPECT_EQ(expected, actual)
NoDefaultContructor(const NoDefaultContructor &)
GTEST_API_ void CaptureStderr()
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
bool StaticAssertTypeEq()
UNITTEST_START int * value
#define EXPECT_TRUE(condition)
#define EXPECT_FALSE(condition)
Castable(bool *converted)
static unsigned long thread_id(void)
#define ASSERT_FALSE(condition)
TEST(IsXDigitTest, WorksForNarrowAscii)