33#include "gtest/internal/gtest-port.h"
44#include "gtest/gtest.h"
45#include "gtest/gtest-spi.h"
46#include "src/gtest-internal-inl.h"
54TEST(IsXDigitTest, WorksForNarrowAscii) {
55 EXPECT_TRUE(IsXDigit(
'0'));
56 EXPECT_TRUE(IsXDigit(
'9'));
57 EXPECT_TRUE(IsXDigit(
'A'));
58 EXPECT_TRUE(IsXDigit(
'F'));
59 EXPECT_TRUE(IsXDigit(
'a'));
60 EXPECT_TRUE(IsXDigit(
'f'));
62 EXPECT_FALSE(IsXDigit(
'-'));
63 EXPECT_FALSE(IsXDigit(
'g'));
64 EXPECT_FALSE(IsXDigit(
'G'));
67TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
68 EXPECT_FALSE(IsXDigit(
static_cast<char>(
'\x80')));
69 EXPECT_FALSE(IsXDigit(
static_cast<char>(
'0' |
'\x80')));
72TEST(IsXDigitTest, WorksForWideAscii) {
73 EXPECT_TRUE(IsXDigit(L
'0'));
74 EXPECT_TRUE(IsXDigit(L
'9'));
75 EXPECT_TRUE(IsXDigit(L
'A'));
76 EXPECT_TRUE(IsXDigit(L
'F'));
77 EXPECT_TRUE(IsXDigit(L
'a'));
78 EXPECT_TRUE(IsXDigit(L
'f'));
80 EXPECT_FALSE(IsXDigit(L
'-'));
81 EXPECT_FALSE(IsXDigit(L
'g'));
82 EXPECT_FALSE(IsXDigit(L
'G'));
85TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
86 EXPECT_FALSE(IsXDigit(
static_cast<wchar_t>(0x80)));
87 EXPECT_FALSE(IsXDigit(
static_cast<wchar_t>(L
'0' | 0x80)));
88 EXPECT_FALSE(IsXDigit(
static_cast<wchar_t>(L
'0' | 0x100)));
93 Base() : member_(0) {}
94 explicit Base(
int n) : member_(n) {}
95 Base(
const Base&) =
default;
96 Base& operator=(
const Base&) =
default;
98 int member() {
return member_; }
104class Derived :
public Base {
106 explicit Derived(
int n) : Base(n) {}
109TEST(ImplicitCastTest, ConvertsPointers) {
111 EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
114TEST(ImplicitCastTest, CanUseInheritance) {
116 Base base = ::testing::internal::ImplicitCast_<Base>(derived);
117 EXPECT_EQ(derived.member(), base.member());
122 explicit Castable(
bool* converted) : converted_(converted) {}
132TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
133 bool converted =
false;
135 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
136 EXPECT_TRUE(converted);
141 explicit ConstCastable(
bool* converted) : converted_(converted) {}
142 operator Base()
const {
151TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
152 bool converted =
false;
154 Base base = ::testing::internal::ImplicitCast_<Base>(const_castable);
155 EXPECT_TRUE(converted);
161 : converted_(converted), const_converted_(const_converted) {}
166 operator Base()
const {
167 *const_converted_ =
true;
173 bool* const_converted_;
176TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
177 bool converted =
false;
178 bool const_converted =
false;
180 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
181 EXPECT_TRUE(converted);
182 EXPECT_FALSE(const_converted);
185 const_converted =
false;
187 base = ::testing::internal::ImplicitCast_<Base>(const_castable);
188 EXPECT_FALSE(converted);
189 EXPECT_TRUE(const_converted);
194 To(
bool* converted) { *converted =
true; }
197TEST(ImplicitCastTest, CanUseImplicitConstructor) {
198 bool converted =
false;
199 To to = ::testing::internal::ImplicitCast_<To>(&converted);
201 EXPECT_TRUE(converted);
206#pragma GCC diagnostic push
207#pragma GCC diagnostic ignored "-Wdangling-else"
208#pragma GCC diagnostic ignored "-Wempty-body"
209#pragma GCC diagnostic ignored "-Wpragmas"
211TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
213 GTEST_CHECK_(
false) <<
"This should never be executed; "
214 "It's a compilation test only.";
224 GTEST_CHECK_(
true) <<
"";
227#pragma GCC diagnostic pop
230TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
240 GTEST_CHECK_(
true) <<
"Check failed in switch case";
244TEST(FormatFileLocationTest, FormatsFileLocation) {
245 EXPECT_PRED_FORMAT2(IsSubstring,
"foo.cc", FormatFileLocation(
"foo.cc", 42));
246 EXPECT_PRED_FORMAT2(IsSubstring,
"42", FormatFileLocation(
"foo.cc", 42));
249TEST(FormatFileLocationTest, FormatsUnknownFile) {
250 EXPECT_PRED_FORMAT2(IsSubstring,
"unknown file",
251 FormatFileLocation(
nullptr, 42));
252 EXPECT_PRED_FORMAT2(IsSubstring,
"42", FormatFileLocation(
nullptr, 42));
255TEST(FormatFileLocationTest, FormatsUknownLine) {
256 EXPECT_EQ(
"foo.cc:", FormatFileLocation(
"foo.cc", -1));
259TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
260 EXPECT_EQ(
"unknown file:", FormatFileLocation(
nullptr, -1));
264TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
265 EXPECT_EQ(
"foo.cc:42", FormatCompilerIndependentFileLocation(
"foo.cc", 42));
268TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
269 EXPECT_EQ(
"unknown file:42",
270 FormatCompilerIndependentFileLocation(
nullptr, 42));
273TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
274 EXPECT_EQ(
"foo.cc", FormatCompilerIndependentFileLocation(
"foo.cc", -1));
277TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
278 EXPECT_EQ(
"unknown file", FormatCompilerIndependentFileLocation(
nullptr, -1));
281#if GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_QNX || GTEST_OS_FUCHSIA || \
282 GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
283 GTEST_OS_NETBSD || GTEST_OS_OPENBSD
284void* ThreadFunc(
void* data) {
285 internal::Mutex* mutex =
static_cast<internal::Mutex*
>(data);
291TEST(GetThreadCountTest, ReturnsCorrectValue) {
292 const size_t starting_count = GetThreadCount();
295 internal::Mutex mutex;
297 internal::MutexLock lock(&mutex);
299 ASSERT_EQ(0, pthread_attr_init(&attr));
300 ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
302 const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
303 ASSERT_EQ(0, pthread_attr_destroy(&attr));
304 ASSERT_EQ(0, status);
305 EXPECT_EQ(starting_count + 1, GetThreadCount());
309 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
314 for (
int i = 0; i < 5; ++i) {
315 if (GetThreadCount() == starting_count)
318 SleepMilliseconds(100);
321 EXPECT_EQ(starting_count, GetThreadCount());
324TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
325 EXPECT_EQ(0U, GetThreadCount());
329TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
330 const bool a_false_condition =
false;
333 "googletest-port-test\\.cc\\(\\d+\\):"
334#elif GTEST_USES_POSIX_RE
335 "googletest-port-test\\.cc:[0-9]+"
337 "googletest-port-test\\.cc:\\d+"
339 ".*a_false_condition.*Extra info.*";
341 EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) <<
"Extra info",
345#if GTEST_HAS_DEATH_TEST
347TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
349 GTEST_CHECK_(
true) <<
"Extra info";
350 ::std::cerr <<
"Success\n";
352 ::testing::ExitedWithCode(0),
"Success");
360TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
362# if GTEST_HAS_POSIX_RE
364 EXPECT_TRUE(GTEST_USES_POSIX_RE);
368 EXPECT_TRUE(GTEST_USES_SIMPLE_RE);
374#if GTEST_USES_POSIX_RE
376template <
typename Str>
383TYPED_TEST_SUITE(RETest, StringTypes);
386TYPED_TEST(RETest, ImplicitConstructorWorks) {
387 const RE empty(TypeParam(
""));
388 EXPECT_STREQ(
"", empty.pattern());
390 const RE simple(TypeParam(
"hello"));
391 EXPECT_STREQ(
"hello", simple.pattern());
393 const RE normal(TypeParam(
".*(\\w+)"));
394 EXPECT_STREQ(
".*(\\w+)", normal.pattern());
398TYPED_TEST(RETest, RejectsInvalidRegex) {
399 EXPECT_NONFATAL_FAILURE({
400 const RE invalid(TypeParam(
"?"));
401 },
"\"?\" is not a valid POSIX Extended regular expression.");
405TYPED_TEST(RETest, FullMatchWorks) {
406 const RE empty(TypeParam(
""));
407 EXPECT_TRUE(RE::FullMatch(TypeParam(
""), empty));
408 EXPECT_FALSE(RE::FullMatch(TypeParam(
"a"), empty));
410 const RE re(TypeParam(
"a.*z"));
411 EXPECT_TRUE(RE::FullMatch(TypeParam(
"az"), re));
412 EXPECT_TRUE(RE::FullMatch(TypeParam(
"axyz"), re));
413 EXPECT_FALSE(RE::FullMatch(TypeParam(
"baz"), re));
414 EXPECT_FALSE(RE::FullMatch(TypeParam(
"azy"), re));
418TYPED_TEST(RETest, PartialMatchWorks) {
419 const RE empty(TypeParam(
""));
420 EXPECT_TRUE(RE::PartialMatch(TypeParam(
""), empty));
421 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"a"), empty));
423 const RE re(TypeParam(
"a.*z"));
424 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"az"), re));
425 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"axyz"), re));
426 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"baz"), re));
427 EXPECT_TRUE(RE::PartialMatch(TypeParam(
"azy"), re));
428 EXPECT_FALSE(RE::PartialMatch(TypeParam(
"zza"), re));
431#elif GTEST_USES_SIMPLE_RE
433TEST(IsInSetTest, NulCharIsNotInAnySet) {
434 EXPECT_FALSE(IsInSet(
'\0',
""));
435 EXPECT_FALSE(IsInSet(
'\0',
"\0"));
436 EXPECT_FALSE(IsInSet(
'\0',
"a"));
439TEST(IsInSetTest, WorksForNonNulChars) {
440 EXPECT_FALSE(IsInSet(
'a',
"Ab"));
441 EXPECT_FALSE(IsInSet(
'c',
""));
443 EXPECT_TRUE(IsInSet(
'b',
"bcd"));
444 EXPECT_TRUE(IsInSet(
'b',
"ab"));
447TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
448 EXPECT_FALSE(IsAsciiDigit(
'\0'));
449 EXPECT_FALSE(IsAsciiDigit(
' '));
450 EXPECT_FALSE(IsAsciiDigit(
'+'));
451 EXPECT_FALSE(IsAsciiDigit(
'-'));
452 EXPECT_FALSE(IsAsciiDigit(
'.'));
453 EXPECT_FALSE(IsAsciiDigit(
'a'));
456TEST(IsAsciiDigitTest, IsTrueForDigit) {
457 EXPECT_TRUE(IsAsciiDigit(
'0'));
458 EXPECT_TRUE(IsAsciiDigit(
'1'));
459 EXPECT_TRUE(IsAsciiDigit(
'5'));
460 EXPECT_TRUE(IsAsciiDigit(
'9'));
463TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
464 EXPECT_FALSE(IsAsciiPunct(
'\0'));
465 EXPECT_FALSE(IsAsciiPunct(
' '));
466 EXPECT_FALSE(IsAsciiPunct(
'\n'));
467 EXPECT_FALSE(IsAsciiPunct(
'a'));
468 EXPECT_FALSE(IsAsciiPunct(
'0'));
471TEST(IsAsciiPunctTest, IsTrueForPunct) {
472 for (
const char* p =
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
473 EXPECT_PRED1(IsAsciiPunct, *p);
477TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
478 EXPECT_FALSE(IsRepeat(
'\0'));
479 EXPECT_FALSE(IsRepeat(
' '));
480 EXPECT_FALSE(IsRepeat(
'a'));
481 EXPECT_FALSE(IsRepeat(
'1'));
482 EXPECT_FALSE(IsRepeat(
'-'));
485TEST(IsRepeatTest, IsTrueForRepeatChar) {
486 EXPECT_TRUE(IsRepeat(
'?'));
487 EXPECT_TRUE(IsRepeat(
'*'));
488 EXPECT_TRUE(IsRepeat(
'+'));
491TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
492 EXPECT_FALSE(IsAsciiWhiteSpace(
'\0'));
493 EXPECT_FALSE(IsAsciiWhiteSpace(
'a'));
494 EXPECT_FALSE(IsAsciiWhiteSpace(
'1'));
495 EXPECT_FALSE(IsAsciiWhiteSpace(
'+'));
496 EXPECT_FALSE(IsAsciiWhiteSpace(
'_'));
499TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
500 EXPECT_TRUE(IsAsciiWhiteSpace(
' '));
501 EXPECT_TRUE(IsAsciiWhiteSpace(
'\n'));
502 EXPECT_TRUE(IsAsciiWhiteSpace(
'\r'));
503 EXPECT_TRUE(IsAsciiWhiteSpace(
'\t'));
504 EXPECT_TRUE(IsAsciiWhiteSpace(
'\v'));
505 EXPECT_TRUE(IsAsciiWhiteSpace(
'\f'));
508TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
509 EXPECT_FALSE(IsAsciiWordChar(
'\0'));
510 EXPECT_FALSE(IsAsciiWordChar(
'+'));
511 EXPECT_FALSE(IsAsciiWordChar(
'.'));
512 EXPECT_FALSE(IsAsciiWordChar(
' '));
513 EXPECT_FALSE(IsAsciiWordChar(
'\n'));
516TEST(IsAsciiWordCharTest, IsTrueForLetter) {
517 EXPECT_TRUE(IsAsciiWordChar(
'a'));
518 EXPECT_TRUE(IsAsciiWordChar(
'b'));
519 EXPECT_TRUE(IsAsciiWordChar(
'A'));
520 EXPECT_TRUE(IsAsciiWordChar(
'Z'));
523TEST(IsAsciiWordCharTest, IsTrueForDigit) {
524 EXPECT_TRUE(IsAsciiWordChar(
'0'));
525 EXPECT_TRUE(IsAsciiWordChar(
'1'));
526 EXPECT_TRUE(IsAsciiWordChar(
'7'));
527 EXPECT_TRUE(IsAsciiWordChar(
'9'));
530TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
531 EXPECT_TRUE(IsAsciiWordChar(
'_'));
534TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
535 EXPECT_FALSE(IsValidEscape(
'\0'));
536 EXPECT_FALSE(IsValidEscape(
'\007'));
539TEST(IsValidEscapeTest, IsFalseForDigit) {
540 EXPECT_FALSE(IsValidEscape(
'0'));
541 EXPECT_FALSE(IsValidEscape(
'9'));
544TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
545 EXPECT_FALSE(IsValidEscape(
' '));
546 EXPECT_FALSE(IsValidEscape(
'\n'));
549TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
550 EXPECT_FALSE(IsValidEscape(
'a'));
551 EXPECT_FALSE(IsValidEscape(
'Z'));
554TEST(IsValidEscapeTest, IsTrueForPunct) {
555 EXPECT_TRUE(IsValidEscape(
'.'));
556 EXPECT_TRUE(IsValidEscape(
'-'));
557 EXPECT_TRUE(IsValidEscape(
'^'));
558 EXPECT_TRUE(IsValidEscape(
'$'));
559 EXPECT_TRUE(IsValidEscape(
'('));
560 EXPECT_TRUE(IsValidEscape(
']'));
561 EXPECT_TRUE(IsValidEscape(
'{'));
562 EXPECT_TRUE(IsValidEscape(
'|'));
565TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
566 EXPECT_TRUE(IsValidEscape(
'd'));
567 EXPECT_TRUE(IsValidEscape(
'D'));
568 EXPECT_TRUE(IsValidEscape(
's'));
569 EXPECT_TRUE(IsValidEscape(
'S'));
570 EXPECT_TRUE(IsValidEscape(
'w'));
571 EXPECT_TRUE(IsValidEscape(
'W'));
574TEST(AtomMatchesCharTest, EscapedPunct) {
575 EXPECT_FALSE(AtomMatchesChar(
true,
'\\',
'\0'));
576 EXPECT_FALSE(AtomMatchesChar(
true,
'\\',
' '));
577 EXPECT_FALSE(AtomMatchesChar(
true,
'_',
'.'));
578 EXPECT_FALSE(AtomMatchesChar(
true,
'.',
'a'));
580 EXPECT_TRUE(AtomMatchesChar(
true,
'\\',
'\\'));
581 EXPECT_TRUE(AtomMatchesChar(
true,
'_',
'_'));
582 EXPECT_TRUE(AtomMatchesChar(
true,
'+',
'+'));
583 EXPECT_TRUE(AtomMatchesChar(
true,
'.',
'.'));
586TEST(AtomMatchesCharTest, Escaped_d) {
587 EXPECT_FALSE(AtomMatchesChar(
true,
'd',
'\0'));
588 EXPECT_FALSE(AtomMatchesChar(
true,
'd',
'a'));
589 EXPECT_FALSE(AtomMatchesChar(
true,
'd',
'.'));
591 EXPECT_TRUE(AtomMatchesChar(
true,
'd',
'0'));
592 EXPECT_TRUE(AtomMatchesChar(
true,
'd',
'9'));
595TEST(AtomMatchesCharTest, Escaped_D) {
596 EXPECT_FALSE(AtomMatchesChar(
true,
'D',
'0'));
597 EXPECT_FALSE(AtomMatchesChar(
true,
'D',
'9'));
599 EXPECT_TRUE(AtomMatchesChar(
true,
'D',
'\0'));
600 EXPECT_TRUE(AtomMatchesChar(
true,
'D',
'a'));
601 EXPECT_TRUE(AtomMatchesChar(
true,
'D',
'-'));
604TEST(AtomMatchesCharTest, Escaped_s) {
605 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'\0'));
606 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'a'));
607 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'.'));
608 EXPECT_FALSE(AtomMatchesChar(
true,
's',
'9'));
610 EXPECT_TRUE(AtomMatchesChar(
true,
's',
' '));
611 EXPECT_TRUE(AtomMatchesChar(
true,
's',
'\n'));
612 EXPECT_TRUE(AtomMatchesChar(
true,
's',
'\t'));
615TEST(AtomMatchesCharTest, Escaped_S) {
616 EXPECT_FALSE(AtomMatchesChar(
true,
'S',
' '));
617 EXPECT_FALSE(AtomMatchesChar(
true,
'S',
'\r'));
619 EXPECT_TRUE(AtomMatchesChar(
true,
'S',
'\0'));
620 EXPECT_TRUE(AtomMatchesChar(
true,
'S',
'a'));
621 EXPECT_TRUE(AtomMatchesChar(
true,
'S',
'9'));
624TEST(AtomMatchesCharTest, Escaped_w) {
625 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
'\0'));
626 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
'+'));
627 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
' '));
628 EXPECT_FALSE(AtomMatchesChar(
true,
'w',
'\n'));
630 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'0'));
631 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'b'));
632 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'C'));
633 EXPECT_TRUE(AtomMatchesChar(
true,
'w',
'_'));
636TEST(AtomMatchesCharTest, Escaped_W) {
637 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'A'));
638 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'b'));
639 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'9'));
640 EXPECT_FALSE(AtomMatchesChar(
true,
'W',
'_'));
642 EXPECT_TRUE(AtomMatchesChar(
true,
'W',
'\0'));
643 EXPECT_TRUE(AtomMatchesChar(
true,
'W',
'*'));
644 EXPECT_TRUE(AtomMatchesChar(
true,
'W',
'\n'));
647TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
648 EXPECT_FALSE(AtomMatchesChar(
true,
'f',
'\0'));
649 EXPECT_FALSE(AtomMatchesChar(
true,
'f',
'\n'));
650 EXPECT_FALSE(AtomMatchesChar(
true,
'n',
'\0'));
651 EXPECT_FALSE(AtomMatchesChar(
true,
'n',
'\r'));
652 EXPECT_FALSE(AtomMatchesChar(
true,
'r',
'\0'));
653 EXPECT_FALSE(AtomMatchesChar(
true,
'r',
'a'));
654 EXPECT_FALSE(AtomMatchesChar(
true,
't',
'\0'));
655 EXPECT_FALSE(AtomMatchesChar(
true,
't',
't'));
656 EXPECT_FALSE(AtomMatchesChar(
true,
'v',
'\0'));
657 EXPECT_FALSE(AtomMatchesChar(
true,
'v',
'\f'));
659 EXPECT_TRUE(AtomMatchesChar(
true,
'f',
'\f'));
660 EXPECT_TRUE(AtomMatchesChar(
true,
'n',
'\n'));
661 EXPECT_TRUE(AtomMatchesChar(
true,
'r',
'\r'));
662 EXPECT_TRUE(AtomMatchesChar(
true,
't',
'\t'));
663 EXPECT_TRUE(AtomMatchesChar(
true,
'v',
'\v'));
666TEST(AtomMatchesCharTest, UnescapedDot) {
667 EXPECT_FALSE(AtomMatchesChar(
false,
'.',
'\n'));
669 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
'\0'));
670 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
'.'));
671 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
'a'));
672 EXPECT_TRUE(AtomMatchesChar(
false,
'.',
' '));
675TEST(AtomMatchesCharTest, UnescapedChar) {
676 EXPECT_FALSE(AtomMatchesChar(
false,
'a',
'\0'));
677 EXPECT_FALSE(AtomMatchesChar(
false,
'a',
'b'));
678 EXPECT_FALSE(AtomMatchesChar(
false,
'$',
'a'));
680 EXPECT_TRUE(AtomMatchesChar(
false,
'$',
'$'));
681 EXPECT_TRUE(AtomMatchesChar(
false,
'5',
'5'));
682 EXPECT_TRUE(AtomMatchesChar(
false,
'Z',
'Z'));
685TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
686 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)),
687 "NULL is not a valid simple regular expression");
688 EXPECT_NONFATAL_FAILURE(
689 ASSERT_FALSE(ValidateRegex(
"a\\")),
690 "Syntax error at index 1 in simple regular expression \"a\\\": ");
691 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"a\\")),
692 "'\\' cannot appear at the end");
693 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"\\n\\")),
694 "'\\' cannot appear at the end");
695 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"\\s\\hb")),
696 "invalid escape sequence \"\\h\"");
697 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"^^")),
698 "'^' can only appear at the beginning");
699 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
".*^b")),
700 "'^' can only appear at the beginning");
701 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"$$")),
702 "'$' can only appear at the end");
703 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"^$a")),
704 "'$' can only appear at the end");
705 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"a(b")),
706 "'(' is unsupported");
707 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"ab)")),
708 "')' is unsupported");
709 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"[ab")),
710 "'[' is unsupported");
711 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"a{2")),
712 "'{' is unsupported");
713 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"?")),
714 "'?' can only follow a repeatable token");
715 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"^*")),
716 "'*' can only follow a repeatable token");
717 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(
"5*+")),
718 "'+' can only follow a repeatable token");
721TEST(ValidateRegexTest, ReturnsTrueForValid) {
722 EXPECT_TRUE(ValidateRegex(
""));
723 EXPECT_TRUE(ValidateRegex(
"a"));
724 EXPECT_TRUE(ValidateRegex(
".*"));
725 EXPECT_TRUE(ValidateRegex(
"^a_+"));
726 EXPECT_TRUE(ValidateRegex(
"^a\\t\\&?"));
727 EXPECT_TRUE(ValidateRegex(
"09*$"));
728 EXPECT_TRUE(ValidateRegex(
"^Z$"));
729 EXPECT_TRUE(ValidateRegex(
"a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
732TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
733 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"a",
"ba"));
735 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"aab"));
738 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ba"));
740 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'a',
'?',
"b",
"ab"));
741 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'#',
'?',
".",
"##"));
744TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
745 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"a$",
"baab"));
748 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"bc"));
750 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'*',
"b",
"abc"));
752 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'*',
"-",
"ab_1-g"));
755TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
756 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"a$",
"baab"));
758 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"bc"));
761 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
false,
'.',
'+',
"b",
"abc"));
763 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(
true,
'w',
'+',
"-",
"ab_1-g"));
766TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
767 EXPECT_TRUE(MatchRegexAtHead(
"",
""));
768 EXPECT_TRUE(MatchRegexAtHead(
"",
"ab"));
771TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
772 EXPECT_FALSE(MatchRegexAtHead(
"$",
"a"));
774 EXPECT_TRUE(MatchRegexAtHead(
"$",
""));
775 EXPECT_TRUE(MatchRegexAtHead(
"a$",
"a"));
778TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
779 EXPECT_FALSE(MatchRegexAtHead(
"\\w",
"+"));
780 EXPECT_FALSE(MatchRegexAtHead(
"\\W",
"ab"));
782 EXPECT_TRUE(MatchRegexAtHead(
"\\sa",
"\nab"));
783 EXPECT_TRUE(MatchRegexAtHead(
"\\d",
"1a"));
786TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
787 EXPECT_FALSE(MatchRegexAtHead(
".+a",
"abc"));
788 EXPECT_FALSE(MatchRegexAtHead(
"a?b",
"aab"));
790 EXPECT_TRUE(MatchRegexAtHead(
".*a",
"bc12-ab"));
791 EXPECT_TRUE(MatchRegexAtHead(
"a?b",
"b"));
792 EXPECT_TRUE(MatchRegexAtHead(
"a?b",
"ab"));
795TEST(MatchRegexAtHeadTest,
796 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
797 EXPECT_FALSE(MatchRegexAtHead(
"\\.+a",
"abc"));
798 EXPECT_FALSE(MatchRegexAtHead(
"\\s?b",
" b"));
800 EXPECT_TRUE(MatchRegexAtHead(
"\\(*a",
"((((ab"));
801 EXPECT_TRUE(MatchRegexAtHead(
"\\^?b",
"^b"));
802 EXPECT_TRUE(MatchRegexAtHead(
"\\\\?b",
"b"));
803 EXPECT_TRUE(MatchRegexAtHead(
"\\\\?b",
"\\b"));
806TEST(MatchRegexAtHeadTest, MatchesSequentially) {
807 EXPECT_FALSE(MatchRegexAtHead(
"ab.*c",
"acabc"));
809 EXPECT_TRUE(MatchRegexAtHead(
"ab.*c",
"ab-fsc"));
812TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
813 EXPECT_FALSE(MatchRegexAnywhere(
"", NULL));
816TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
817 EXPECT_FALSE(MatchRegexAnywhere(
"^a",
"ba"));
818 EXPECT_FALSE(MatchRegexAnywhere(
"^$",
"a"));
820 EXPECT_TRUE(MatchRegexAnywhere(
"^a",
"ab"));
821 EXPECT_TRUE(MatchRegexAnywhere(
"^",
"ab"));
822 EXPECT_TRUE(MatchRegexAnywhere(
"^$",
""));
825TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
826 EXPECT_FALSE(MatchRegexAnywhere(
"a",
"bcde123"));
827 EXPECT_FALSE(MatchRegexAnywhere(
"a.+a",
"--aa88888888"));
830TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
831 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"ab1_ - 5"));
832 EXPECT_TRUE(MatchRegexAnywhere(
".*=",
"="));
833 EXPECT_TRUE(MatchRegexAnywhere(
"x.*ab?.*bc",
"xaaabc"));
836TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
837 EXPECT_TRUE(MatchRegexAnywhere(
"\\w+",
"$$$ ab1_ - 5"));
838 EXPECT_TRUE(MatchRegexAnywhere(
"\\.+=",
"= ...="));
842TEST(RETest, ImplicitConstructorWorks) {
844 EXPECT_STREQ(
"", empty.pattern());
846 const RE simple(
"hello");
847 EXPECT_STREQ(
"hello", simple.pattern());
851TEST(RETest, RejectsInvalidRegex) {
852 EXPECT_NONFATAL_FAILURE({
853 const RE normal(NULL);
854 },
"NULL is not a valid simple regular expression");
856 EXPECT_NONFATAL_FAILURE({
857 const RE normal(
".*(\\w+");
858 },
"'(' is unsupported");
860 EXPECT_NONFATAL_FAILURE({
861 const RE invalid(
"^?");
862 },
"'?' can only follow a repeatable token");
866TEST(RETest, FullMatchWorks) {
868 EXPECT_TRUE(RE::FullMatch(
"", empty));
869 EXPECT_FALSE(RE::FullMatch(
"a", empty));
872 EXPECT_TRUE(RE::FullMatch(
"a", re1));
875 EXPECT_TRUE(RE::FullMatch(
"az", re));
876 EXPECT_TRUE(RE::FullMatch(
"axyz", re));
877 EXPECT_FALSE(RE::FullMatch(
"baz", re));
878 EXPECT_FALSE(RE::FullMatch(
"azy", re));
882TEST(RETest, PartialMatchWorks) {
884 EXPECT_TRUE(RE::PartialMatch(
"", empty));
885 EXPECT_TRUE(RE::PartialMatch(
"a", empty));
888 EXPECT_TRUE(RE::PartialMatch(
"az", re));
889 EXPECT_TRUE(RE::PartialMatch(
"axyz", re));
890 EXPECT_TRUE(RE::PartialMatch(
"baz", re));
891 EXPECT_TRUE(RE::PartialMatch(
"azy", re));
892 EXPECT_FALSE(RE::PartialMatch(
"zza", re));
897#if !GTEST_OS_WINDOWS_MOBILE
899TEST(CaptureTest, CapturesStdout) {
901 fprintf(stdout,
"abc");
902 EXPECT_STREQ(
"abc", GetCapturedStdout().c_str());
905 fprintf(stdout,
"def%cghi",
'\0');
906 EXPECT_EQ(::std::string(
"def\0ghi", 7), ::std::string(GetCapturedStdout()));
909TEST(CaptureTest, CapturesStderr) {
911 fprintf(stderr,
"jkl");
912 EXPECT_STREQ(
"jkl", GetCapturedStderr().c_str());
915 fprintf(stderr,
"jkl%cmno",
'\0');
916 EXPECT_EQ(::std::string(
"jkl\0mno", 7), ::std::string(GetCapturedStderr()));
920TEST(CaptureTest, CapturesStdoutAndStderr) {
923 fprintf(stdout,
"pqr");
924 fprintf(stderr,
"stu");
925 EXPECT_STREQ(
"pqr", GetCapturedStdout().c_str());
926 EXPECT_STREQ(
"stu", GetCapturedStderr().c_str());
929TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
931 EXPECT_DEATH_IF_SUPPORTED(CaptureStdout(),
932 "Only one stdout capturer can exist at a time");
941TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
943 EXPECT_EQ(0, t1.get());
945 ThreadLocal<void*> t2;
946 EXPECT_TRUE(t2.get() ==
nullptr);
949TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
950 ThreadLocal<int> t1(123);
951 EXPECT_EQ(123, t1.get());
954 ThreadLocal<int*> t2(&i);
955 EXPECT_EQ(&i, t2.get());
964TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
969TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
970 ThreadLocal<std::string> thread_local_string;
972 EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
975 thread_local_string.set(
"foo");
976 EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
979TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
980 ThreadLocal<std::string> thread_local_string;
981 const ThreadLocal<std::string>& const_thread_local_string =
984 EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
986 thread_local_string.set(
"foo");
987 EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
990#if GTEST_IS_THREADSAFE
992void AddTwo(
int* param) { *param += 2; }
994TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
996 ThreadWithParam<int*> thread(&AddTwo, &i,
nullptr);
1001TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
1004 EXPECT_DEATH_IF_SUPPORTED({
1006 { MutexLock lock(&m); }
1012TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1018class AtomicCounterWithMutex {
1020 explicit AtomicCounterWithMutex(Mutex* mutex) :
1021 value_(0), mutex_(mutex), random_(42) {}
1024 MutexLock lock(mutex_);
1030#if GTEST_HAS_PTHREAD
1034 pthread_mutex_t memory_barrier_mutex;
1035 GTEST_CHECK_POSIX_SUCCESS_(
1036 pthread_mutex_init(&memory_barrier_mutex,
nullptr));
1037 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&memory_barrier_mutex));
1039 SleepMilliseconds(
static_cast<int>(random_.Generate(30)));
1041 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&memory_barrier_mutex));
1042 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&memory_barrier_mutex));
1043#elif GTEST_OS_WINDOWS
1045 volatile LONG dummy = 0;
1046 ::InterlockedIncrement(&dummy);
1047 SleepMilliseconds(
static_cast<int>(random_.Generate(30)));
1048 ::InterlockedIncrement(&dummy);
1050# error "Memory barrier not implemented on this platform."
1055 int value()
const {
return value_; }
1058 volatile int value_;
1059 Mutex*
const mutex_;
1063void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1064 for (
int i = 0; i < param.second; ++i)
1065 param.first->Increment();
1069TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
1071 AtomicCounterWithMutex locked_counter(&mutex);
1073 typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
1074 const int kCycleCount = 20;
1075 const int kThreadCount = 7;
1076 std::unique_ptr<ThreadType> counting_threads[kThreadCount];
1077 Notification threads_can_start;
1080 for (
int i = 0; i < kThreadCount; ++i) {
1081 counting_threads[i].reset(
new ThreadType(&CountingThreadFunc,
1082 make_pair(&locked_counter,
1084 &threads_can_start));
1086 threads_can_start.Notify();
1087 for (
int i = 0; i < kThreadCount; ++i)
1088 counting_threads[i]->Join();
1094 EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
1097template <
typename T>
1098void RunFromThread(
void (func)(T), T param) {
1099 ThreadWithParam<T> thread(func, param,
nullptr);
1103void RetrieveThreadLocalValue(
1104 pair<ThreadLocal<std::string>*, std::string*> param) {
1105 *param.second = param.first->get();
1108TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1109 ThreadLocal<std::string> thread_local_string(
"foo");
1110 EXPECT_STREQ(
"foo", thread_local_string.get().c_str());
1112 thread_local_string.set(
"bar");
1113 EXPECT_STREQ(
"bar", thread_local_string.get().c_str());
1116 RunFromThread(&RetrieveThreadLocalValue,
1117 make_pair(&thread_local_string, &result));
1118 EXPECT_STREQ(
"foo", result.c_str());
1123class DestructorCall {
1128 wait_event_.Reset(::CreateEvent(NULL, TRUE, FALSE, NULL));
1129 GTEST_CHECK_(wait_event_.Get() != NULL);
1133 bool CheckDestroyed()
const {
1135 if (::WaitForSingleObject(wait_event_.Get(), 1000) != WAIT_OBJECT_0)
1141 void ReportDestroyed() {
1144 ::SetEvent(wait_event_.Get());
1148 static std::vector<DestructorCall*>& List() {
return *list_; }
1150 static void ResetList() {
1151 for (
size_t i = 0; i < list_->size(); ++i) {
1152 delete list_->at(i);
1160 AutoHandle wait_event_;
1162 static std::vector<DestructorCall*>*
const list_;
1164 GTEST_DISALLOW_COPY_AND_ASSIGN_(DestructorCall);
1167std::vector<DestructorCall*>*
const DestructorCall::list_ =
1168 new std::vector<DestructorCall*>;
1172class DestructorTracker {
1174 DestructorTracker() : index_(GetNewIndex()) {}
1175 DestructorTracker(
const DestructorTracker& )
1176 : index_(GetNewIndex()) {}
1177 ~DestructorTracker() {
1180 DestructorCall::List()[index_]->ReportDestroyed();
1184 static size_t GetNewIndex() {
1185 DestructorCall::List().push_back(
new DestructorCall);
1186 return DestructorCall::List().size() - 1;
1188 const size_t index_;
1191typedef ThreadLocal<DestructorTracker>* ThreadParam;
1193void CallThreadLocalGet(ThreadParam thread_local_param) {
1194 thread_local_param->get();
1199TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1200 DestructorCall::ResetList();
1203 ThreadLocal<DestructorTracker> thread_local_tracker;
1204 ASSERT_EQ(0U, DestructorCall::List().size());
1207 thread_local_tracker.get();
1208 ASSERT_EQ(1U, DestructorCall::List().size());
1209 ASSERT_FALSE(DestructorCall::List()[0]->CheckDestroyed());
1213 ASSERT_EQ(1U, DestructorCall::List().size());
1214 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1216 DestructorCall::ResetList();
1221TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1222 DestructorCall::ResetList();
1225 ThreadLocal<DestructorTracker> thread_local_tracker;
1226 ASSERT_EQ(0U, DestructorCall::List().size());
1229 ThreadWithParam<ThreadParam> thread(&CallThreadLocalGet,
1230 &thread_local_tracker,
nullptr);
1235 ASSERT_EQ(1U, DestructorCall::List().size());
1239 ASSERT_EQ(1U, DestructorCall::List().size());
1240 EXPECT_TRUE(DestructorCall::List()[0]->CheckDestroyed());
1242 DestructorCall::ResetList();
1245TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1246 ThreadLocal<std::string> thread_local_string;
1247 thread_local_string.set(
"Foo");
1248 EXPECT_STREQ(
"Foo", thread_local_string.get().c_str());
1251 RunFromThread(&RetrieveThreadLocalValue,
1252 make_pair(&thread_local_string, &result));
1253 EXPECT_TRUE(result.empty());
1259TEST(WindowsTypesTest, HANDLEIsVoidStar) {
1260 StaticAssertTypeEq<HANDLE, void*>();
1263#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
1264TEST(WindowsTypesTest, _CRITICAL_SECTIONIs_CRITICAL_SECTION) {
1265 StaticAssertTypeEq<CRITICAL_SECTION, _CRITICAL_SECTION>();
1268TEST(WindowsTypesTest, CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION) {
1269 StaticAssertTypeEq<CRITICAL_SECTION, _RTL_CRITICAL_SECTION>();
Definition unittest.h:130
Definition googletest-port-test.cc:120
Definition googletest-port-test.cc:158
Definition googletest-port-test.cc:139
Definition googletest-port-test.cc:958
Definition gtest-port.h:1885
Definition googletest-port-test.cc:192
Definition gtest-type-util.h:153