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);
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;
183 bool* const_converted_;
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>
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) {}
1070 pthread_mutex_t memory_barrier_mutex;
1072 pthread_mutex_init(&memory_barrier_mutex, NULL));
1075 SleepMilliseconds(random_.Generate(30));
1082 int value()
const {
return value_; }
1085 volatile int value_;
1086 Mutex*
const mutex_;
1090 void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1091 for (
int i = 0; i < param.second; ++i)
1092 param.first->Increment();
1096 TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1098 AtomicCounterWithMutex locked_counter(&mutex);
1100 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1101 const int kCycleCount = 20;
1102 const int kThreadCount = 7;
1104 Notification threads_can_start;
1107 for (
int i = 0; i < kThreadCount; ++i) {
1108 counting_threads[i].
reset(
new ThreadType(&CountingThreadFunc,
1109 make_pair(&locked_counter,
1111 &threads_can_start));
1113 threads_can_start.Notify();
1114 for (
int i = 0; i < kThreadCount; ++i)
1115 counting_threads[i]->Join();
1121 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1124 template <
typename T>
1125 void RunFromThread(
void (func)(T), T param) {
1126 ThreadWithParam<T> thread(func, param, NULL);
1130 void RetrieveThreadLocalValue(
1132 *param.second = param.first->get();
1135 TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1139 thread_local_string.
set(
"bar");
1143 RunFromThread(&RetrieveThreadLocalValue,
1144 make_pair(&thread_local_string, &result));
1150 static std::vector<bool> g_destroyed;
1152 class DestructorTracker {
1154 DestructorTracker() : index_(GetNewIndex()) {}
1155 DestructorTracker(
const DestructorTracker& )
1156 : index_(GetNewIndex()) {}
1157 ~DestructorTracker() {
1160 g_destroyed[index_] =
true;
1164 static int GetNewIndex() {
1165 g_destroyed.push_back(
false);
1166 return g_destroyed.size() - 1;
1173 void CallThreadLocalGet(ThreadParam thread_local_param) {
1174 thread_local_param->
get();
1179 TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1180 g_destroyed.clear();
1190 thread_local_tracker.
get();
1203 g_destroyed.clear();
1208 TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1209 g_destroyed.clear();
1219 ThreadWithParam<ThreadParam> thread(
1220 &CallThreadLocalGet, &thread_local_tracker, NULL);
1236 g_destroyed.clear();
1239 TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1241 thread_local_string.
set(
"Foo");
1245 RunFromThread(&RetrieveThreadLocalValue,
1246 make_pair(&thread_local_string, &result));
1250 #endif // GTEST_IS_THREADSAFE
std::string GetCapturedStdout()
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_NONFATAL_FAILURE(statement, substr)
Iterator::value_type value_type
#define GTEST_USES_POSIX_RE
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
#define EXPECT_TRUE(condition)
static bool PartialMatch(const ::std::string &str, const RE &re)
TYPED_TEST_CASE(TypedTest, MyTypes)
#define EXPECT_STREQ(expected, actual)
#define EXPECT_PRED1(pred, v1)
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
#define EXPECT_FALSE(condition)
NoDefaultContructor(const char *)
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)
#define ASSERT_TRUE(condition)
GTEST_API_::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
const char * pattern() const
#define ASSERT_EQ(val1, val2)
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
TYPED_TEST(TypedTest, TestA)
NoDefaultContructor(const NoDefaultContructor &)
bool StaticAssertTypeEq()
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)
#define GTEST_CHECK_(condition)
Castable(bool *converted)
#define EXPECT_EQ(expected, actual)
#define ASSERT_FALSE(condition)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
TEST(IsXDigitTest, WorksForNarrowAscii)