33#include "gtest/internal/gtest-port.h"
46#include "gtest/gtest-spi.h"
47#include "gtest/gtest.h"
48#include "src/gtest-internal-inl.h"
56TEST(IsXDigitTest, WorksForNarrowAscii) {
57 EXPECT_TRUE(IsXDigit(
'0'));
58 EXPECT_TRUE(IsXDigit(
'9'));
59 EXPECT_TRUE(IsXDigit(
'A'));
60 EXPECT_TRUE(IsXDigit(
'F'));
61 EXPECT_TRUE(IsXDigit(
'a'));
62 EXPECT_TRUE(IsXDigit(
'f'));
64 EXPECT_FALSE(IsXDigit(
'-'));
65 EXPECT_FALSE(IsXDigit(
'g'));
66 EXPECT_FALSE(IsXDigit(
'G'));
69TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
70 EXPECT_FALSE(IsXDigit(
static_cast<char>(
'\x80')));
71 EXPECT_FALSE(IsXDigit(
static_cast<char>(
'0' |
'\x80')));
74TEST(IsXDigitTest, WorksForWideAscii) {
75 EXPECT_TRUE(IsXDigit(L
'0'));
76 EXPECT_TRUE(IsXDigit(L
'9'));
77 EXPECT_TRUE(IsXDigit(L
'A'));
78 EXPECT_TRUE(IsXDigit(L
'F'));
79 EXPECT_TRUE(IsXDigit(L
'a'));
80 EXPECT_TRUE(IsXDigit(L
'f'));
82 EXPECT_FALSE(IsXDigit(L
'-'));
83 EXPECT_FALSE(IsXDigit(L
'g'));
84 EXPECT_FALSE(IsXDigit(L
'G'));
87TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
88 EXPECT_FALSE(IsXDigit(
static_cast<wchar_t>(0x80)));
89 EXPECT_FALSE(IsXDigit(
static_cast<wchar_t>(L
'0' | 0x80)));
90 EXPECT_FALSE(IsXDigit(
static_cast<wchar_t>(L
'0' | 0x100)));
95 Base() : member_(0) {}
96 explicit Base(
int n) : member_(n) {}
97 Base(
const Base&) =
default;
98 Base& operator=(
const Base&) =
default;
100 int member() {
return member_; }
106class Derived :
public Base {
108 explicit Derived(
int n) : Base(n) {}
111TEST(ImplicitCastTest, ConvertsPointers) {
113 EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
116TEST(ImplicitCastTest, CanUseInheritance) {
118 Base base = ::testing::internal::ImplicitCast_<Base>(derived);
119 EXPECT_EQ(derived.member(), base.member());
124 explicit Castable(
bool* converted) : converted_(converted) {}
134TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
135 bool converted =
false;
137 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
138 EXPECT_TRUE(converted);
143 explicit ConstCastable(
bool* converted) : converted_(converted) {}
144 operator Base()
const {
153TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
154 bool converted =
false;
156 Base base = ::testing::internal::ImplicitCast_<Base>(const_castable);
157 EXPECT_TRUE(converted);
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;
182 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
183 EXPECT_TRUE(converted);
184 EXPECT_FALSE(const_converted);
187 const_converted =
false;
189 base = ::testing::internal::ImplicitCast_<Base>(const_castable);
190 EXPECT_FALSE(converted);
191 EXPECT_TRUE(const_converted);
196 To(
bool* converted) { *converted =
true; }
199TEST(ImplicitCastTest, CanUseImplicitConstructor) {
200 bool converted =
false;
201 To to = ::testing::internal::ImplicitCast_<To>(&converted);
203 EXPECT_TRUE(converted);
208#pragma GCC diagnostic push
209#pragma GCC diagnostic ignored "-Wdangling-else"
210#pragma GCC diagnostic ignored "-Wempty-body"
211#pragma GCC diagnostic ignored "-Wpragmas"
213TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
215 GTEST_CHECK_(
false) <<
"This should never be executed; "
216 "It's a compilation test only.";
226 GTEST_CHECK_(
true) <<
"";
229#pragma GCC diagnostic pop
232TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
242 GTEST_CHECK_(
true) <<
"Check failed in switch case";
246TEST(FormatFileLocationTest, FormatsFileLocation) {
247 EXPECT_PRED_FORMAT2(IsSubstring,
"foo.cc", FormatFileLocation(
"foo.cc", 42));
248 EXPECT_PRED_FORMAT2(IsSubstring,
"42", FormatFileLocation(
"foo.cc", 42));
251TEST(FormatFileLocationTest, FormatsUnknownFile) {
252 EXPECT_PRED_FORMAT2(IsSubstring,
"unknown file",
253 FormatFileLocation(
nullptr, 42));
254 EXPECT_PRED_FORMAT2(IsSubstring,
"42", FormatFileLocation(
nullptr, 42));
257TEST(FormatFileLocationTest, FormatsUknownLine) {
258 EXPECT_EQ(
"foo.cc:", FormatFileLocation(
"foo.cc", -1));
261TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
262 EXPECT_EQ(
"unknown file:", FormatFileLocation(
nullptr, -1));
266TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
267 EXPECT_EQ(
"foo.cc:42", FormatCompilerIndependentFileLocation(
"foo.cc", 42));
270TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
271 EXPECT_EQ(
"unknown file:42",
272 FormatCompilerIndependentFileLocation(
nullptr, 42));
275TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
276 EXPECT_EQ(
"foo.cc", FormatCompilerIndependentFileLocation(
"foo.cc", -1));
279TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
280 EXPECT_EQ(
"unknown file", FormatCompilerIndependentFileLocation(
nullptr, -1));
283#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA || \
284 GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
285 GTEST_OS_NETBSD || GTEST_OS_OPENBSD || GTEST_OS_GNU_HURD
286void* ThreadFunc(
void* data) {
287 internal::Mutex* mutex =
static_cast<internal::Mutex*
>(data);
293TEST(GetThreadCountTest, ReturnsCorrectValue) {
294 size_t starting_count;
295 size_t thread_count_after_create;
296 size_t thread_count_after_join;
302 for (
int attempt = 0; attempt < 20; ++attempt) {
303 starting_count = GetThreadCount();
306 internal::Mutex mutex;
308 internal::MutexLock lock(&mutex);
310 ASSERT_EQ(0, pthread_attr_init(&attr));
311 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
313 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
314 ASSERT_EQ(0, pthread_attr_destroy(&attr));
315 ASSERT_EQ(0, status);
318 thread_count_after_create = GetThreadCount();
321 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
325 if (thread_count_after_create != starting_count + 1)
continue;
330 bool thread_count_matches =
false;
331 for (
int i = 0; i < 5; ++i) {
332 thread_count_after_join = GetThreadCount();
333 if (thread_count_after_join == starting_count) {
334 thread_count_matches =
true;
338 std::this_thread::sleep_for(std::chrono::milliseconds(100));
342 if (!thread_count_matches)
continue;
347 EXPECT_EQ(thread_count_after_create, starting_count + 1);
348 EXPECT_EQ(thread_count_after_join, starting_count);
351TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
352 EXPECT_EQ(0U, GetThreadCount());
356TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
357 const bool a_false_condition =
false;
360 "googletest-port-test\\.cc\\(\\d+\\):"
361#elif GTEST_USES_POSIX_RE
362 "googletest-port-test\\.cc:[0-9]+"
364 "googletest-port-test\\.cc:\\d+"
366 ".*a_false_condition.*Extra info.*";
368 EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) <<
"Extra info",
372#if GTEST_HAS_DEATH_TEST
374TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
377 GTEST_CHECK_(
true) <<
"Extra info";
378 ::std::cerr <<
"Success\n";
381 ::testing::ExitedWithCode(0),
"Success");
389TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
391 EXPECT_TRUE(GTEST_USES_RE2);
392#elif GTEST_HAS_POSIX_RE
393 EXPECT_TRUE(GTEST_USES_POSIX_RE);
395 EXPECT_TRUE(GTEST_USES_SIMPLE_RE);
399#if GTEST_USES_POSIX_RE
401template <
typename Str>
408TYPED_TEST_SUITE(RETest, StringTypes);
411TYPED_TEST(RETest, ImplicitConstructorWorks) {
412 const RE empty(TypeParam(
""));
413 EXPECT_STREQ(
"", empty.pattern());
415 const RE simple(TypeParam(
"hello"));
416 EXPECT_STREQ(
"hello", simple.pattern());
418 const RE normal(TypeParam(
".*(\\w+)"));
419 EXPECT_STREQ(
".*(\\w+)", normal.pattern());
423TYPED_TEST(RETest, RejectsInvalidRegex) {
424 EXPECT_NONFATAL_FAILURE(
425 {
const RE invalid(TypeParam(
"?")); },
426 "\"?\" is not a valid POSIX Extended regular expression.");
430TYPED_TEST(RETest, FullMatchWorks) {
431 const RE empty(TypeParam(
""));
432 EXPECT_TRUE(RE::FullMatch(TypeParam(
""), empty));
433 EXPECT_FALSE(RE::FullMatch(TypeParam(
"a"), empty));
435 const RE re(TypeParam(
"a.*z"));
436 EXPECT_TRUE(RE::FullMatch(TypeParam(
"az"), re));
437 EXPECT_TRUE(RE::FullMatch(TypeParam(
"axyz"), re));
438 EXPECT_FALSE(RE::FullMatch(TypeParam(
"baz"), re));
439 EXPECT_FALSE(RE::FullMatch(TypeParam(
"azy"), re));
443TYPED_TEST(RETest, PartialMatchWorks) {
444 const RE empty(TypeParam(
""));
445 EXPECT_TRUE(RE::PartialMatch(TypeParam(
""), empty));
446 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"a"), empty));
448 const RE re(TypeParam(
"a.*z"));
449 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"az"), re));
450 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"axyz"), re));
451 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"baz"), re));
452 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"azy"), re));
453 EXPECT_FALSE(RE::PartialMatch(TypeParam(
"zza"), re));
456#elif GTEST_USES_SIMPLE_RE
458TEST(IsInSetTest, NulCharIsNotInAnySet) {
459 EXPECT_FALSE(IsInSet(
'\0',
""));
460 EXPECT_FALSE(IsInSet(
'\0',
"\0"));
461 EXPECT_FALSE(IsInSet(
'\0',
"a"));
464TEST(IsInSetTest, WorksForNonNulChars) {
465 EXPECT_FALSE(IsInSet(
'a',
"Ab"));
466 EXPECT_FALSE(IsInSet(
'c',
""));
468 EXPECT_TRUE(IsInSet(
'b',
"bcd"));
469 EXPECT_TRUE(IsInSet(
'b',
"ab"));
472TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
473 EXPECT_FALSE(IsAsciiDigit(
'\0'));
474 EXPECT_FALSE(IsAsciiDigit(
' '));
475 EXPECT_FALSE(IsAsciiDigit(
'+'));
476 EXPECT_FALSE(IsAsciiDigit(
'-'));
477 EXPECT_FALSE(IsAsciiDigit(
'.'));
478 EXPECT_FALSE(IsAsciiDigit(
'a'));
481TEST(IsAsciiDigitTest, IsTrueForDigit) {
482 EXPECT_TRUE(IsAsciiDigit(
'0'));
483 EXPECT_TRUE(IsAsciiDigit(
'1'));
484 EXPECT_TRUE(IsAsciiDigit(
'5'));
485 EXPECT_TRUE(IsAsciiDigit(
'9'));
488TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
489 EXPECT_FALSE(IsAsciiPunct(
'\0'));
490 EXPECT_FALSE(IsAsciiPunct(
' '));
491 EXPECT_FALSE(IsAsciiPunct(
'\n'));
492 EXPECT_FALSE(IsAsciiPunct(
'a'));
493 EXPECT_FALSE(IsAsciiPunct(
'0'));
496TEST(IsAsciiPunctTest, IsTrueForPunct) {
497 for (
const char* p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
498 EXPECT_PRED1(IsAsciiPunct, *p);
502TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
503 EXPECT_FALSE(IsRepeat(
'\0'));
504 EXPECT_FALSE(IsRepeat(
' '));
505 EXPECT_FALSE(IsRepeat(
'a'));
506 EXPECT_FALSE(IsRepeat(
'1'));
507 EXPECT_FALSE(IsRepeat(
'-'));
510TEST(IsRepeatTest, IsTrueForRepeatChar) {
511 EXPECT_TRUE(IsRepeat(
'?'));
512 EXPECT_TRUE(IsRepeat(
'*'));
513 EXPECT_TRUE(IsRepeat(
'+'));
516TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
517 EXPECT_FALSE(IsAsciiWhiteSpace(
'\0'));
518 EXPECT_FALSE(IsAsciiWhiteSpace(
'a'));
519 EXPECT_FALSE(IsAsciiWhiteSpace(
'1'));
520 EXPECT_FALSE(IsAsciiWhiteSpace(
'+'));
521 EXPECT_FALSE(IsAsciiWhiteSpace(
'_'));
524TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
525 EXPECT_TRUE(IsAsciiWhiteSpace(
' '));
526 EXPECT_TRUE(IsAsciiWhiteSpace(
'\n'));
527 EXPECT_TRUE(IsAsciiWhiteSpace(
'\r'));
528 EXPECT_TRUE(IsAsciiWhiteSpace(
'\t'));
529 EXPECT_TRUE(IsAsciiWhiteSpace(
'\v'));
530 EXPECT_TRUE(IsAsciiWhiteSpace(
'\f'));
533TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
534 EXPECT_FALSE(IsAsciiWordChar(
'\0'));
535 EXPECT_FALSE(IsAsciiWordChar(
'+'));
536 EXPECT_FALSE(IsAsciiWordChar(
'.'));
537 EXPECT_FALSE(IsAsciiWordChar(
' '));
538 EXPECT_FALSE(IsAsciiWordChar(
'\n'));
541TEST(IsAsciiWordCharTest, IsTrueForLetter) {
542 EXPECT_TRUE(IsAsciiWordChar(
'a'));
543 EXPECT_TRUE(IsAsciiWordChar(
'b'));
544 EXPECT_TRUE(IsAsciiWordChar(
'A'));
545 EXPECT_TRUE(IsAsciiWordChar(
'Z'));
548TEST(IsAsciiWordCharTest, IsTrueForDigit) {
549 EXPECT_TRUE(IsAsciiWordChar(
'0'));
550 EXPECT_TRUE(IsAsciiWordChar(
'1'));
551 EXPECT_TRUE(IsAsciiWordChar(
'7'));
552 EXPECT_TRUE(IsAsciiWordChar(
'9'));
555TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
556 EXPECT_TRUE(IsAsciiWordChar(
'_'));
559TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
560 EXPECT_FALSE(IsValidEscape(
'\0'));
561 EXPECT_FALSE(IsValidEscape(
'\007'));
564TEST(IsValidEscapeTest, IsFalseForDigit) {
565 EXPECT_FALSE(IsValidEscape(
'0'));
566 EXPECT_FALSE(IsValidEscape(
'9'));
569TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
570 EXPECT_FALSE(IsValidEscape(
' '));
571 EXPECT_FALSE(IsValidEscape(
'\n'));
574TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
575 EXPECT_FALSE(IsValidEscape(
'a'));
576 EXPECT_FALSE(IsValidEscape(
'Z'));
579TEST(IsValidEscapeTest, IsTrueForPunct) {
580 EXPECT_TRUE(IsValidEscape(
'.'));
581 EXPECT_TRUE(IsValidEscape(
'-'));
582 EXPECT_TRUE(IsValidEscape(
'^'));
583 EXPECT_TRUE(IsValidEscape(
'$'));
584 EXPECT_TRUE(IsValidEscape(
'('));
585 EXPECT_TRUE(IsValidEscape(
']'));
586 EXPECT_TRUE(IsValidEscape(
'{'));
587 EXPECT_TRUE(IsValidEscape(
'|'));
590TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
591 EXPECT_TRUE(IsValidEscape(
'd'));
592 EXPECT_TRUE(IsValidEscape(
'D'));
593 EXPECT_TRUE(IsValidEscape(
's'));
594 EXPECT_TRUE(IsValidEscape(
'S'));
595 EXPECT_TRUE(IsValidEscape(
'w'));
596 EXPECT_TRUE(IsValidEscape(
'W'));
599TEST(AtomMatchesCharTest, EscapedPunct) {
600 EXPECT_FALSE(AtomMatchesChar(
true,
'\\',
'\0'));
601 EXPECT_FALSE(AtomMatchesChar(
true,
'\\',
' '));
602 EXPECT_FALSE(AtomMatchesChar(
true,
'_',
'.'));
603 EXPECT_FALSE(AtomMatchesChar(
true,
'.',
'a'));
605 EXPECT_TRUE(AtomMatchesChar(
true,
'\\',
'\\'));
606 EXPECT_TRUE(AtomMatchesChar(
true,
'_',
'_'));
607 EXPECT_TRUE(AtomMatchesChar(
true,
'+',
'+'));
608 EXPECT_TRUE(AtomMatchesChar(
true,
'.',
'.'));
611TEST(AtomMatchesCharTest, Escaped_d) {
612 EXPECT_FALSE(AtomMatchesChar(
true,
'd',
'\0'));
613 EXPECT_FALSE(AtomMatchesChar(
true,
'd',
'a'));
614 EXPECT_FALSE(AtomMatchesChar(
true,
'd',
'.'));
616 EXPECT_TRUE(AtomMatchesChar(
true,
'd',
'0'));
617 EXPECT_TRUE(AtomMatchesChar(
true,
'd',
'9'));
620TEST(AtomMatchesCharTest, Escaped_D) {
621 EXPECT_FALSE(AtomMatchesChar(
true,
'D',
'0'));
622 EXPECT_FALSE(AtomMatchesChar(
true,
'D',
'9'));
624 EXPECT_TRUE(AtomMatchesChar(
true,
'D',
'\0'));
625 EXPECT_TRUE(AtomMatchesChar(
true,
'D',
'a'));
626 EXPECT_TRUE(AtomMatchesChar(
true,
'D',
'-'));
629TEST(AtomMatchesCharTest, Escaped_s) {
630 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'\0'));
631 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'a'));
632 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'.'));
633 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'9'));
635 EXPECT_TRUE(AtomMatchesChar(
true,
's',
' '));
636 EXPECT_TRUE(AtomMatchesChar(
true,
's',
'\n'));
637 EXPECT_TRUE(AtomMatchesChar(
true,
's',
'\t'));
640TEST(AtomMatchesCharTest, Escaped_S) {
641 EXPECT_FALSE(AtomMatchesChar(
true,
'S',
' '));
642 EXPECT_FALSE(AtomMatchesChar(
true,
'S',
'\r'));
644 EXPECT_TRUE(AtomMatchesChar(
true,
'S',
'\0'));
645 EXPECT_TRUE(AtomMatchesChar(
true,
'S',
'a'));
646 EXPECT_TRUE(AtomMatchesChar(
true,
'S',
'9'));
649TEST(AtomMatchesCharTest, Escaped_w) {
650 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
'\0'));
651 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
'+'));
652 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
' '));
653 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
'\n'));
655 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'0'));
656 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'b'));
657 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'C'));
658 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'_'));
661TEST(AtomMatchesCharTest, Escaped_W) {
662 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'A'));
663 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'b'));
664 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'9'));
665 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'_'));
667 EXPECT_TRUE(AtomMatchesChar(
true,
'W',
'\0'));
668 EXPECT_TRUE(AtomMatchesChar(
true,
'W',
'*'));
669 EXPECT_TRUE(AtomMatchesChar(
true,
'W',
'\n'));
672TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
673 EXPECT_FALSE(AtomMatchesChar(
true,
'f',
'\0'));
674 EXPECT_FALSE(AtomMatchesChar(
true,
'f',
'\n'));
675 EXPECT_FALSE(AtomMatchesChar(
true,
'n',
'\0'));
676 EXPECT_FALSE(AtomMatchesChar(
true,
'n',
'\r'));
677 EXPECT_FALSE(AtomMatchesChar(
true,
'r',
'\0'));
678 EXPECT_FALSE(AtomMatchesChar(
true,
'r',
'a'));
679 EXPECT_FALSE(AtomMatchesChar(
true,
't',
'\0'));
680 EXPECT_FALSE(AtomMatchesChar(
true,
't',
't'));
681 EXPECT_FALSE(AtomMatchesChar(
true,
'v',
'\0'));
682 EXPECT_FALSE(AtomMatchesChar(
true,
'v',
'\f'));
684 EXPECT_TRUE(AtomMatchesChar(
true,
'f',
'\f'));
685 EXPECT_TRUE(AtomMatchesChar(
true,
'n',
'\n'));
686 EXPECT_TRUE(AtomMatchesChar(
true,
'r',
'\r'));
687 EXPECT_TRUE(AtomMatchesChar(
true,
't',
'\t'));
688 EXPECT_TRUE(AtomMatchesChar(
true,
'v',
'\v'));
691TEST(AtomMatchesCharTest, UnescapedDot) {
692 EXPECT_FALSE(AtomMatchesChar(
false,
'.',
'\n'));
694 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
'\0'));
695 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
'.'));
696 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
'a'));
697 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
' '));
700TEST(AtomMatchesCharTest, UnescapedChar) {
701 EXPECT_FALSE(AtomMatchesChar(
false,
'a',
'\0'));
702 EXPECT_FALSE(AtomMatchesChar(
false,
'a',
'b'));
703 EXPECT_FALSE(AtomMatchesChar(
false,
'$',
'a'));
705 EXPECT_TRUE(AtomMatchesChar(
false,
'$',
'$'));
706 EXPECT_TRUE(AtomMatchesChar(
false,
'5',
'5'));
707 EXPECT_TRUE(AtomMatchesChar(
false,
'Z',
'Z'));
710TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
711 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)),
712 "NULL is not a valid simple regular expression");
713 EXPECT_NONFATAL_FAILURE(
714 ASSERT_FALSE(ValidateRegex(
"a\\")),
715 "Syntax error at index 1 in simple regular expression \"a\\\": ");
716 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"a\\")),
717 "'\\' cannot appear at the end");
718 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"\\n\\")),
719 "'\\' cannot appear at the end");
720 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"\\s\\hb")),
721 "invalid escape sequence \"\\h\"");
722 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"^^")),
723 "'^' can only appear at the beginning");
724 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
".*^b")),
725 "'^' can only appear at the beginning");
726 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"$$")),
727 "'$' can only appear at the end");
728 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"^$a")),
729 "'$' can only appear at the end");
730 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"a(b")),
731 "'(' is unsupported");
732 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"ab)")),
733 "')' is unsupported");
734 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"[ab")),
735 "'[' is unsupported");
736 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"a{2")),
737 "'{' is unsupported");
738 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"?")),
739 "'?' can only follow a repeatable token");
740 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"^*")),
741 "'*' can only follow a repeatable token");
742 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"5*+")),
743 "'+' can only follow a repeatable token");
746TEST(ValidateRegexTest, ReturnsTrueForValid) {
747 EXPECT_TRUE(ValidateRegex(
""));
748 EXPECT_TRUE(ValidateRegex(
"a"));
749 EXPECT_TRUE(ValidateRegex(
".*"));
750 EXPECT_TRUE(ValidateRegex(
"^a_+"));
751 EXPECT_TRUE(ValidateRegex(
"^a\\t\\&?"));
752 EXPECT_TRUE(ValidateRegex(
"09*$"));
753 EXPECT_TRUE(ValidateRegex(
"^Z$"));
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) {
792 EXPECT_TRUE(MatchRegexAtHead(
"",
""));
793 EXPECT_TRUE(MatchRegexAtHead(
"",
"ab"));
796TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
797 EXPECT_FALSE(MatchRegexAtHead(
"$",
"a"));
799 EXPECT_TRUE(MatchRegexAtHead(
"$",
""));
800 EXPECT_TRUE(MatchRegexAtHead(
"a$",
"a"));
803TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
804 EXPECT_FALSE(MatchRegexAtHead(
"\\w",
"+"));
805 EXPECT_FALSE(MatchRegexAtHead(
"\\W",
"ab"));
807 EXPECT_TRUE(MatchRegexAtHead(
"\\sa",
"\nab"));
808 EXPECT_TRUE(MatchRegexAtHead(
"\\d",
"1a"));
811TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
812 EXPECT_FALSE(MatchRegexAtHead(
".+a",
"abc"));
813 EXPECT_FALSE(MatchRegexAtHead(
"a?b",
"aab"));
815 EXPECT_TRUE(MatchRegexAtHead(
".*a",
"bc12-ab"));
816 EXPECT_TRUE(MatchRegexAtHead(
"a?b",
"b"));
817 EXPECT_TRUE(MatchRegexAtHead(
"a?b",
"ab"));
820TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
821 EXPECT_FALSE(MatchRegexAtHead(
"\\.+a",
"abc"));
822 EXPECT_FALSE(MatchRegexAtHead(
"\\s?b",
" b"));
824 EXPECT_TRUE(MatchRegexAtHead(
"\\(*a",
"((((ab"));
825 EXPECT_TRUE(MatchRegexAtHead(
"\\^?b",
"^b"));
826 EXPECT_TRUE(MatchRegexAtHead(
"\\\\?b",
"b"));
827 EXPECT_TRUE(MatchRegexAtHead(
"\\\\?b",
"\\b"));
830TEST(MatchRegexAtHeadTest, MatchesSequentially) {
831 EXPECT_FALSE(MatchRegexAtHead(
"ab.*c",
"acabc"));
833 EXPECT_TRUE(MatchRegexAtHead(
"ab.*c",
"ab-fsc"));
836TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
837 EXPECT_FALSE(MatchRegexAnywhere(
"", NULL));
840TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
841 EXPECT_FALSE(MatchRegexAnywhere(
"^a",
"ba"));
842 EXPECT_FALSE(MatchRegexAnywhere(
"^$",
"a"));
844 EXPECT_TRUE(MatchRegexAnywhere(
"^a",
"ab"));
845 EXPECT_TRUE(MatchRegexAnywhere(
"^",
"ab"));
846 EXPECT_TRUE(MatchRegexAnywhere(
"^$",
""));
849TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
850 EXPECT_FALSE(MatchRegexAnywhere(
"a",
"bcde123"));
851 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
854TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
855 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
856 EXPECT_TRUE(MatchRegexAnywhere(
".*=",
"="));
857 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
860TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
861 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
862 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
866TEST(RETest, ImplicitConstructorWorks) {
868 EXPECT_STREQ(
"", empty.pattern());
870 const RE simple(
"hello");
871 EXPECT_STREQ(
"hello", simple.pattern());
875TEST(RETest, RejectsInvalidRegex) {
876 EXPECT_NONFATAL_FAILURE({
const RE normal(NULL); },
877 "NULL is not a valid simple regular expression");
879 EXPECT_NONFATAL_FAILURE({
const RE normal(
".*(\\w+"); },
880 "'(' is unsupported");
882 EXPECT_NONFATAL_FAILURE({
const RE invalid(
"^?"); },
883 "'?' can only follow a repeatable token");
887TEST(RETest, FullMatchWorks) {
889 EXPECT_TRUE(RE::FullMatch(
"", empty));
890 EXPECT_FALSE(RE::FullMatch(
"a", empty));
893 EXPECT_TRUE(RE::FullMatch(
"a", re1));
896 EXPECT_TRUE(RE::FullMatch(
"az", re));
897 EXPECT_TRUE(RE::FullMatch(
"axyz", re));
898 EXPECT_FALSE(RE::FullMatch(
"baz", re));
899 EXPECT_FALSE(RE::FullMatch(
"azy", re));
903TEST(RETest, PartialMatchWorks) {
905 EXPECT_TRUE(RE::PartialMatch(
"", empty));
906 EXPECT_TRUE(RE::PartialMatch(
"a", empty));
909 EXPECT_TRUE(RE::PartialMatch(
"az", re));
910 EXPECT_TRUE(RE::PartialMatch(
"axyz", re));
911 EXPECT_TRUE(RE::PartialMatch(
"baz", re));
912 EXPECT_TRUE(RE::PartialMatch(
"azy", re));
913 EXPECT_FALSE(RE::PartialMatch(
"zza", re));
918#if !GTEST_OS_WINDOWS_MOBILE
920TEST(CaptureTest, CapturesStdout) {
922 fprintf(stdout,
"abc");
923 EXPECT_STREQ(
"abc", GetCapturedStdout().c_str());
926 fprintf(stdout,
"def%cghi",
'\0');
927 EXPECT_EQ(::std::string(
"def\0ghi", 7), ::std::string(GetCapturedStdout()));
930TEST(CaptureTest, CapturesStderr) {
932 fprintf(stderr,
"jkl");
933 EXPECT_STREQ(
"jkl", GetCapturedStderr().c_str());
936 fprintf(stderr,
"jkl%cmno",
'\0');
937 EXPECT_EQ(::std::string(
"jkl\0mno", 7), ::std::string(GetCapturedStderr()));
941TEST(CaptureTest, CapturesStdoutAndStderr) {
944 fprintf(stdout,
"pqr");
945 fprintf(stderr,
"stu");
946 EXPECT_STREQ(
"pqr", GetCapturedStdout().c_str());
947 EXPECT_STREQ(
"stu", GetCapturedStderr().c_str());
950TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
952 EXPECT_DEATH_IF_SUPPORTED(CaptureStdout(),
953 "Only one stdout capturer can exist at a time");
962TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
964 EXPECT_EQ(0, t1.get());
966 ThreadLocal<void*> t2;
967 EXPECT_TRUE(t2.get() ==
nullptr);
970TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
971 ThreadLocal<int> t1(123);
972 EXPECT_EQ(123, t1.get());
975 ThreadLocal<int*> t2(&i);
976 EXPECT_EQ(&i, t2.get());
985TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
990TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
991 ThreadLocal<std::string> thread_local_string;
993 EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
996 thread_local_string.set(
"foo");
997 EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
1000TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
1001 ThreadLocal<std::string> thread_local_string;
1002 const ThreadLocal<std::string>& const_thread_local_string =
1003 thread_local_string;
1005 EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
1007 thread_local_string.set(
"foo");
1008 EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
1011#if GTEST_IS_THREADSAFE
1013void AddTwo(
int* param) { *param += 2; }
1015TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
1017 ThreadWithParam<int*> thread(&AddTwo, &i,
nullptr);
1022TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1025 EXPECT_DEATH_IF_SUPPORTED(
1028 { MutexLock lock(&m); }
1034TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1040class AtomicCounterWithMutex {
1042 explicit AtomicCounterWithMutex(Mutex* mutex)
1043 : value_(0), mutex_(mutex), random_(42) {}
1046 MutexLock lock(mutex_);
1052#if GTEST_HAS_PTHREAD
1056 pthread_mutex_t memory_barrier_mutex;
1057 GTEST_CHECK_POSIX_SUCCESS_(
1058 pthread_mutex_init(&memory_barrier_mutex,
nullptr));
1059 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&memory_barrier_mutex));
1061 std::this_thread::sleep_for(
1062 std::chrono::milliseconds(random_.Generate(30)));
1064 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&memory_barrier_mutex));
1065 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&memory_barrier_mutex));
1066#elif GTEST_OS_WINDOWS
1068 volatile LONG dummy = 0;
1069 ::InterlockedIncrement(&dummy);
1070 std::this_thread::sleep_for(
1071 std::chrono::milliseconds(random_.Generate(30)));
1072 ::InterlockedIncrement(&dummy);
1074#error "Memory barrier not implemented on this platform."
1079 int value()
const {
return value_; }
1082 volatile int value_;
1083 Mutex*
const mutex_;
1087void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1088 for (
int i = 0; i < param.second; ++i) param.first->Increment();
1092TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1094 AtomicCounterWithMutex locked_counter(&mutex);
1096 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1097 const int kCycleCount = 20;
1098 const int kThreadCount = 7;
1099 std::unique_ptr<ThreadType> counting_threads[kThreadCount];
1100 Notification threads_can_start;
1103 for (
int i = 0; i < kThreadCount; ++i) {
1104 counting_threads[i].reset(
new ThreadType(
1105 &CountingThreadFunc, make_pair(&locked_counter, kCycleCount),
1106 &threads_can_start));
1108 threads_can_start.Notify();
1109 for (
int i = 0; i < kThreadCount; ++i) counting_threads[i]->Join();
1115 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1118template <
typename T>
1119void RunFromThread(
void(func)(T), T param) {
1120 ThreadWithParam<T> thread(func, param,
nullptr);
1124void RetrieveThreadLocalValue(
1125 pair<ThreadLocal<std::string>*, std::string*> param) {
1126 *param.second = param.first->get();
1129TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1130 ThreadLocal<std::string> thread_local_string(
"foo");
1131 EXPECT_STREQ(
"foo", thread_local_string.get().c_str());
1133 thread_local_string.set(
"bar");
1134 EXPECT_STREQ(
"bar", thread_local_string.get().c_str());
1137 RunFromThread(&RetrieveThreadLocalValue,
1138 make_pair(&thread_local_string, &result));
1139 EXPECT_STREQ(
"foo", result.c_str());
1144class DestructorCall {
1149 wait_event_.Reset(::CreateEvent(NULL, TRUE, FALSE, NULL));
1150 GTEST_CHECK_(wait_event_.Get() != NULL);
1154 bool CheckDestroyed()
const {
1156 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1162 void ReportDestroyed() {
1165 ::SetEvent(wait_event_.Get());
1169 static std::vector<DestructorCall*>& List() {
return *list_; }
1171 static void ResetList() {
1172 for (
size_t i = 0; i < list_->size(); ++i) {
1173 delete list_->at(i);
1181 AutoHandle wait_event_;
1183 static std::vector<DestructorCall*>*
const list_;
1185 DestructorCall(
const DestructorCall&) =
delete;
1186 DestructorCall& operator=(
const DestructorCall&) =
delete;
1189std::vector<DestructorCall*>*
const DestructorCall::list_ =
1190 new std::vector<DestructorCall*>;
1194class DestructorTracker {
1196 DestructorTracker() : index_(GetNewIndex()) {}
1197 DestructorTracker(
const DestructorTracker& )
1198 : index_(GetNewIndex()) {}
1199 ~DestructorTracker() {
1202 DestructorCall::List()[index_]->ReportDestroyed();
1206 static size_t GetNewIndex() {
1207 DestructorCall::List().push_back(
new DestructorCall);
1208 return DestructorCall::List().size() - 1;
1210 const size_t index_;
1213typedef ThreadLocal<DestructorTracker>* ThreadParam;
1215void CallThreadLocalGet(ThreadParam thread_local_param) {
1216 thread_local_param->get();
1221TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1222 DestructorCall::ResetList();
1225 ThreadLocal<DestructorTracker> thread_local_tracker;
1226 ASSERT_EQ(0U, DestructorCall::List().size());
1229 thread_local_tracker.get();
1230 ASSERT_EQ(1U, DestructorCall::List().size());
1231 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1235 ASSERT_EQ(1U, DestructorCall::List().size());
1236 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1238 DestructorCall::ResetList();
1243TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1244 DestructorCall::ResetList();
1247 ThreadLocal<DestructorTracker> thread_local_tracker;
1248 ASSERT_EQ(0U, DestructorCall::List().size());
1251 ThreadWithParam<ThreadParam> thread(&CallThreadLocalGet,
1252 &thread_local_tracker,
nullptr);
1257 ASSERT_EQ(1U, DestructorCall::List().size());
1261 ASSERT_EQ(1U, DestructorCall::List().size());
1262 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1264 DestructorCall::ResetList();
1267TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1268 ThreadLocal<std::string> thread_local_string;
1269 thread_local_string.set(
"Foo");
1270 EXPECT_STREQ(
"Foo", thread_local_string.get().c_str());
1273 RunFromThread(&RetrieveThreadLocalValue,
1274 make_pair(&thread_local_string, &result));
1275 EXPECT_TRUE(result.empty());
1281TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1282 StaticAssertTypeEq<HANDLE, void*>();
1285#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
1286TEST(WindowsTypesTest, _CRITICAL_SECTIONIs_CRITICAL_SECTION) {
1287 StaticAssertTypeEq<CRITICAL_SECTION, _CRITICAL_SECTION>();
1290TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1291 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
Definition googletest-port-test.cc:122
Definition googletest-port-test.cc:160
Definition googletest-port-test.cc:141
Definition googletest-port-test.cc:979
Definition gtest-port.h:1868
Definition googletest-port-test.cc:194
Definition gtest-type-util.h:156