1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Authors: vladl@google.com (Vlad Losev), wan@google.com (Zhanyong Wan)
32 // This file tests the internal cross-platform support utilities.
34 #include "gtest/internal/gtest-port.h"
40 #endif // GTEST_OS_MAC
43 #include <utility> // For std::pair and std::make_pair.
46 #include "gtest/gtest.h"
47 #include "gtest/gtest-spi.h"
49 // Indicates that this translation unit is part of Google Test's
50 // implementation. It must come before gtest-internal-inl.h is
51 // included, or there will be a compiler error. This trick is to
52 // prevent a user from accidentally including gtest-internal-inl.h in
54 #define GTEST_IMPLEMENTATION_ 1
55 #include "src/gtest-internal-inl.h"
56 #undef GTEST_IMPLEMENTATION_
64 TEST(IsXDigitTest, WorksForNarrowAscii) {
65 EXPECT_TRUE(IsXDigit('0'));
66 EXPECT_TRUE(IsXDigit('9'));
67 EXPECT_TRUE(IsXDigit('A'));
68 EXPECT_TRUE(IsXDigit('F'));
69 EXPECT_TRUE(IsXDigit('a'));
70 EXPECT_TRUE(IsXDigit('f'));
72 EXPECT_FALSE(IsXDigit('-'));
73 EXPECT_FALSE(IsXDigit('g'));
74 EXPECT_FALSE(IsXDigit('G'));
77 TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
78 EXPECT_FALSE(IsXDigit(static_cast<char>(0x80)));
79 EXPECT_FALSE(IsXDigit(static_cast<char>('0' | 0x80)));
82 TEST(IsXDigitTest, WorksForWideAscii) {
83 EXPECT_TRUE(IsXDigit(L'0'));
84 EXPECT_TRUE(IsXDigit(L'9'));
85 EXPECT_TRUE(IsXDigit(L'A'));
86 EXPECT_TRUE(IsXDigit(L'F'));
87 EXPECT_TRUE(IsXDigit(L'a'));
88 EXPECT_TRUE(IsXDigit(L'f'));
90 EXPECT_FALSE(IsXDigit(L'-'));
91 EXPECT_FALSE(IsXDigit(L'g'));
92 EXPECT_FALSE(IsXDigit(L'G'));
95 TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
96 EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(0x80)));
97 EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(L'0' | 0x80)));
98 EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(L'0' | 0x100)));
103 // Copy constructor and assignment operator do exactly what we need, so we
105 Base() : member_(0) {}
106 explicit Base(int n) : member_(n) {}
108 int member() { return member_; }
114 class Derived : public Base {
116 explicit Derived(int n) : Base(n) {}
119 TEST(ImplicitCastTest, ConvertsPointers) {
121 EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
124 TEST(ImplicitCastTest, CanUseInheritance) {
126 Base base = ::testing::internal::ImplicitCast_<Base>(derived);
127 EXPECT_EQ(derived.member(), base.member());
132 explicit Castable(bool* converted) : converted_(converted) {}
142 TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
143 bool converted = false;
144 Castable castable(&converted);
145 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
146 EXPECT_TRUE(converted);
149 class ConstCastable {
151 explicit ConstCastable(bool* converted) : converted_(converted) {}
152 operator Base() const {
161 TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
162 bool converted = false;
163 const ConstCastable const_castable(&converted);
164 Base base = ::testing::internal::ImplicitCast_<Base>(const_castable);
165 EXPECT_TRUE(converted);
168 class ConstAndNonConstCastable {
170 ConstAndNonConstCastable(bool* converted, bool* const_converted)
171 : converted_(converted), const_converted_(const_converted) {}
176 operator Base() const {
177 *const_converted_ = true;
183 bool* const_converted_;
186 TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
187 bool converted = false;
188 bool const_converted = false;
189 ConstAndNonConstCastable castable(&converted, &const_converted);
190 Base base = ::testing::internal::ImplicitCast_<Base>(castable);
191 EXPECT_TRUE(converted);
192 EXPECT_FALSE(const_converted);
195 const_converted = false;
196 const ConstAndNonConstCastable const_castable(&converted, &const_converted);
197 base = ::testing::internal::ImplicitCast_<Base>(const_castable);
198 EXPECT_FALSE(converted);
199 EXPECT_TRUE(const_converted);
204 To(bool* converted) { *converted = true; } // NOLINT
207 TEST(ImplicitCastTest, CanUseImplicitConstructor) {
208 bool converted = false;
209 To to = ::testing::internal::ImplicitCast_<To>(&converted);
211 EXPECT_TRUE(converted);
214 TEST(IteratorTraitsTest, WorksForSTLContainerIterators) {
215 StaticAssertTypeEq<int,
216 IteratorTraits< ::std::vector<int>::const_iterator>::value_type>();
217 StaticAssertTypeEq<bool,
218 IteratorTraits< ::std::list<bool>::iterator>::value_type>();
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>();
228 StaticAssertTypeEq<const void*,
229 IteratorTraits<const void* const*>::value_type>();
232 // Tests that the element_type typedef is available in scoped_ptr and refers
233 // to the parameter type.
234 TEST(ScopedPtrTest, DefinesElementType) {
235 StaticAssertTypeEq<int, ::testing::internal::scoped_ptr<int>::element_type>();
238 // TODO(vladl@google.com): Implement THE REST of scoped_ptr tests.
240 TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
242 GTEST_CHECK_(false) << "This should never be executed; "
243 "It's a compilation test only.";
253 GTEST_CHECK_(true) << "";
256 TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
266 GTEST_CHECK_(true) << "Check failed in switch case";
269 // Verifies behavior of FormatFileLocation.
270 TEST(FormatFileLocationTest, FormatsFileLocation) {
271 EXPECT_PRED_FORMAT2(IsSubstring, "foo.cc", FormatFileLocation("foo.cc", 42));
272 EXPECT_PRED_FORMAT2(IsSubstring, "42", FormatFileLocation("foo.cc", 42));
275 TEST(FormatFileLocationTest, FormatsUnknownFile) {
277 IsSubstring, "unknown file", FormatFileLocation(NULL, 42));
278 EXPECT_PRED_FORMAT2(IsSubstring, "42", FormatFileLocation(NULL, 42));
281 TEST(FormatFileLocationTest, FormatsUknownLine) {
282 EXPECT_EQ("foo.cc:", FormatFileLocation("foo.cc", -1));
285 TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
286 EXPECT_EQ("unknown file:", FormatFileLocation(NULL, -1));
289 // Verifies behavior of FormatCompilerIndependentFileLocation.
290 TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
291 EXPECT_EQ("foo.cc:42", FormatCompilerIndependentFileLocation("foo.cc", 42));
294 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
295 EXPECT_EQ("unknown file:42",
296 FormatCompilerIndependentFileLocation(NULL, 42));
299 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
300 EXPECT_EQ("foo.cc", FormatCompilerIndependentFileLocation("foo.cc", -1));
303 TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
304 EXPECT_EQ("unknown file", FormatCompilerIndependentFileLocation(NULL, -1));
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) {
316 EXPECT_EQ(1U, GetThreadCount());
317 pthread_mutex_t mutex;
321 // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
323 pthread_mutex_init(&mutex, NULL);
324 pthread_mutex_lock(&mutex);
325 ASSERT_EQ(0, pthread_attr_init(&attr));
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));
330 ASSERT_EQ(0, status);
331 EXPECT_EQ(2U, GetThreadCount());
332 pthread_mutex_unlock(&mutex);
335 ASSERT_EQ(0, pthread_join(thread_id, &dummy));
339 // MacOS X may not immediately report the updated thread count after
340 // joining a thread, causing flakiness in this test. To counter that, we
341 // wait for up to .5 seconds for the OS to report the correct value.
342 for (int i = 0; i < 5; ++i) {
343 if (GetThreadCount() == 1)
346 SleepMilliseconds(100);
349 # endif // GTEST_OS_MAC
351 EXPECT_EQ(1U, GetThreadCount());
352 pthread_mutex_destroy(&mutex);
355 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
356 EXPECT_EQ(0U, GetThreadCount());
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.*";
372 EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info",
376 #if GTEST_HAS_DEATH_TEST
378 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
380 GTEST_CHECK_(true) << "Extra info";
381 ::std::cerr << "Success\n";
383 ::testing::ExitedWithCode(0), "Success");
386 #endif // GTEST_HAS_DEATH_TEST
388 // Verifies that Google Test choose regular expression engine appropriate to
389 // the platform. The test will produce compiler errors in case of failure.
390 // For simplicity, we only cover the most important platforms here.
391 TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
392 #if GTEST_HAS_POSIX_RE
394 EXPECT_TRUE(GTEST_USES_POSIX_RE);
398 EXPECT_TRUE(GTEST_USES_SIMPLE_RE);
403 #if GTEST_USES_POSIX_RE
405 # if GTEST_HAS_TYPED_TEST
407 template <typename Str>
408 class RETest : public ::testing::Test {};
410 // Defines StringTypes as the list of all string types that class RE
412 typedef testing::Types<
414 # if GTEST_HAS_GLOBAL_STRING
416 # endif // GTEST_HAS_GLOBAL_STRING
417 const char*> StringTypes;
419 TYPED_TEST_CASE(RETest, StringTypes);
421 // Tests RE's implicit constructors.
422 TYPED_TEST(RETest, ImplicitConstructorWorks) {
423 const RE empty(TypeParam(""));
424 EXPECT_STREQ("", empty.pattern());
426 const RE simple(TypeParam("hello"));
427 EXPECT_STREQ("hello", simple.pattern());
429 const RE normal(TypeParam(".*(\\w+)"));
430 EXPECT_STREQ(".*(\\w+)", normal.pattern());
433 // Tests that RE's constructors reject invalid regular expressions.
434 TYPED_TEST(RETest, RejectsInvalidRegex) {
435 EXPECT_NONFATAL_FAILURE({
436 const RE invalid(TypeParam("?"));
437 }, "\"?\" is not a valid POSIX Extended regular expression.");
440 // Tests RE::FullMatch().
441 TYPED_TEST(RETest, FullMatchWorks) {
442 const RE empty(TypeParam(""));
443 EXPECT_TRUE(RE::FullMatch(TypeParam(""), empty));
444 EXPECT_FALSE(RE::FullMatch(TypeParam("a"), empty));
446 const RE re(TypeParam("a.*z"));
447 EXPECT_TRUE(RE::FullMatch(TypeParam("az"), re));
448 EXPECT_TRUE(RE::FullMatch(TypeParam("axyz"), re));
449 EXPECT_FALSE(RE::FullMatch(TypeParam("baz"), re));
450 EXPECT_FALSE(RE::FullMatch(TypeParam("azy"), re));
453 // Tests RE::PartialMatch().
454 TYPED_TEST(RETest, PartialMatchWorks) {
455 const RE empty(TypeParam(""));
456 EXPECT_TRUE(RE::PartialMatch(TypeParam(""), empty));
457 EXPECT_TRUE(RE::PartialMatch(TypeParam("a"), empty));
459 const RE re(TypeParam("a.*z"));
460 EXPECT_TRUE(RE::PartialMatch(TypeParam("az"), re));
461 EXPECT_TRUE(RE::PartialMatch(TypeParam("axyz"), re));
462 EXPECT_TRUE(RE::PartialMatch(TypeParam("baz"), re));
463 EXPECT_TRUE(RE::PartialMatch(TypeParam("azy"), re));
464 EXPECT_FALSE(RE::PartialMatch(TypeParam("zza"), re));
467 # endif // GTEST_HAS_TYPED_TEST
469 #elif GTEST_USES_SIMPLE_RE
471 TEST(IsInSetTest, NulCharIsNotInAnySet) {
472 EXPECT_FALSE(IsInSet('\0', ""));
473 EXPECT_FALSE(IsInSet('\0', "\0"));
474 EXPECT_FALSE(IsInSet('\0', "a"));
477 TEST(IsInSetTest, WorksForNonNulChars) {
478 EXPECT_FALSE(IsInSet('a', "Ab"));
479 EXPECT_FALSE(IsInSet('c', ""));
481 EXPECT_TRUE(IsInSet('b', "bcd"));
482 EXPECT_TRUE(IsInSet('b', "ab"));
485 TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
486 EXPECT_FALSE(IsAsciiDigit('\0'));
487 EXPECT_FALSE(IsAsciiDigit(' '));
488 EXPECT_FALSE(IsAsciiDigit('+'));
489 EXPECT_FALSE(IsAsciiDigit('-'));
490 EXPECT_FALSE(IsAsciiDigit('.'));
491 EXPECT_FALSE(IsAsciiDigit('a'));
494 TEST(IsAsciiDigitTest, IsTrueForDigit) {
495 EXPECT_TRUE(IsAsciiDigit('0'));
496 EXPECT_TRUE(IsAsciiDigit('1'));
497 EXPECT_TRUE(IsAsciiDigit('5'));
498 EXPECT_TRUE(IsAsciiDigit('9'));
501 TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
502 EXPECT_FALSE(IsAsciiPunct('\0'));
503 EXPECT_FALSE(IsAsciiPunct(' '));
504 EXPECT_FALSE(IsAsciiPunct('\n'));
505 EXPECT_FALSE(IsAsciiPunct('a'));
506 EXPECT_FALSE(IsAsciiPunct('0'));
509 TEST(IsAsciiPunctTest, IsTrueForPunct) {
510 for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
511 EXPECT_PRED1(IsAsciiPunct, *p);
515 TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
516 EXPECT_FALSE(IsRepeat('\0'));
517 EXPECT_FALSE(IsRepeat(' '));
518 EXPECT_FALSE(IsRepeat('a'));
519 EXPECT_FALSE(IsRepeat('1'));
520 EXPECT_FALSE(IsRepeat('-'));
523 TEST(IsRepeatTest, IsTrueForRepeatChar) {
524 EXPECT_TRUE(IsRepeat('?'));
525 EXPECT_TRUE(IsRepeat('*'));
526 EXPECT_TRUE(IsRepeat('+'));
529 TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
530 EXPECT_FALSE(IsAsciiWhiteSpace('\0'));
531 EXPECT_FALSE(IsAsciiWhiteSpace('a'));
532 EXPECT_FALSE(IsAsciiWhiteSpace('1'));
533 EXPECT_FALSE(IsAsciiWhiteSpace('+'));
534 EXPECT_FALSE(IsAsciiWhiteSpace('_'));
537 TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
538 EXPECT_TRUE(IsAsciiWhiteSpace(' '));
539 EXPECT_TRUE(IsAsciiWhiteSpace('\n'));
540 EXPECT_TRUE(IsAsciiWhiteSpace('\r'));
541 EXPECT_TRUE(IsAsciiWhiteSpace('\t'));
542 EXPECT_TRUE(IsAsciiWhiteSpace('\v'));
543 EXPECT_TRUE(IsAsciiWhiteSpace('\f'));
546 TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
547 EXPECT_FALSE(IsAsciiWordChar('\0'));
548 EXPECT_FALSE(IsAsciiWordChar('+'));
549 EXPECT_FALSE(IsAsciiWordChar('.'));
550 EXPECT_FALSE(IsAsciiWordChar(' '));
551 EXPECT_FALSE(IsAsciiWordChar('\n'));
554 TEST(IsAsciiWordCharTest, IsTrueForLetter) {
555 EXPECT_TRUE(IsAsciiWordChar('a'));
556 EXPECT_TRUE(IsAsciiWordChar('b'));
557 EXPECT_TRUE(IsAsciiWordChar('A'));
558 EXPECT_TRUE(IsAsciiWordChar('Z'));
561 TEST(IsAsciiWordCharTest, IsTrueForDigit) {
562 EXPECT_TRUE(IsAsciiWordChar('0'));
563 EXPECT_TRUE(IsAsciiWordChar('1'));
564 EXPECT_TRUE(IsAsciiWordChar('7'));
565 EXPECT_TRUE(IsAsciiWordChar('9'));
568 TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
569 EXPECT_TRUE(IsAsciiWordChar('_'));
572 TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
573 EXPECT_FALSE(IsValidEscape('\0'));
574 EXPECT_FALSE(IsValidEscape('\007'));
577 TEST(IsValidEscapeTest, IsFalseForDigit) {
578 EXPECT_FALSE(IsValidEscape('0'));
579 EXPECT_FALSE(IsValidEscape('9'));
582 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
583 EXPECT_FALSE(IsValidEscape(' '));
584 EXPECT_FALSE(IsValidEscape('\n'));
587 TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
588 EXPECT_FALSE(IsValidEscape('a'));
589 EXPECT_FALSE(IsValidEscape('Z'));
592 TEST(IsValidEscapeTest, IsTrueForPunct) {
593 EXPECT_TRUE(IsValidEscape('.'));
594 EXPECT_TRUE(IsValidEscape('-'));
595 EXPECT_TRUE(IsValidEscape('^'));
596 EXPECT_TRUE(IsValidEscape('$'));
597 EXPECT_TRUE(IsValidEscape('('));
598 EXPECT_TRUE(IsValidEscape(']'));
599 EXPECT_TRUE(IsValidEscape('{'));
600 EXPECT_TRUE(IsValidEscape('|'));
603 TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
604 EXPECT_TRUE(IsValidEscape('d'));
605 EXPECT_TRUE(IsValidEscape('D'));
606 EXPECT_TRUE(IsValidEscape('s'));
607 EXPECT_TRUE(IsValidEscape('S'));
608 EXPECT_TRUE(IsValidEscape('w'));
609 EXPECT_TRUE(IsValidEscape('W'));
612 TEST(AtomMatchesCharTest, EscapedPunct) {
613 EXPECT_FALSE(AtomMatchesChar(true, '\\', '\0'));
614 EXPECT_FALSE(AtomMatchesChar(true, '\\', ' '));
615 EXPECT_FALSE(AtomMatchesChar(true, '_', '.'));
616 EXPECT_FALSE(AtomMatchesChar(true, '.', 'a'));
618 EXPECT_TRUE(AtomMatchesChar(true, '\\', '\\'));
619 EXPECT_TRUE(AtomMatchesChar(true, '_', '_'));
620 EXPECT_TRUE(AtomMatchesChar(true, '+', '+'));
621 EXPECT_TRUE(AtomMatchesChar(true, '.', '.'));
624 TEST(AtomMatchesCharTest, Escaped_d) {
625 EXPECT_FALSE(AtomMatchesChar(true, 'd', '\0'));
626 EXPECT_FALSE(AtomMatchesChar(true, 'd', 'a'));
627 EXPECT_FALSE(AtomMatchesChar(true, 'd', '.'));
629 EXPECT_TRUE(AtomMatchesChar(true, 'd', '0'));
630 EXPECT_TRUE(AtomMatchesChar(true, 'd', '9'));
633 TEST(AtomMatchesCharTest, Escaped_D) {
634 EXPECT_FALSE(AtomMatchesChar(true, 'D', '0'));
635 EXPECT_FALSE(AtomMatchesChar(true, 'D', '9'));
637 EXPECT_TRUE(AtomMatchesChar(true, 'D', '\0'));
638 EXPECT_TRUE(AtomMatchesChar(true, 'D', 'a'));
639 EXPECT_TRUE(AtomMatchesChar(true, 'D', '-'));
642 TEST(AtomMatchesCharTest, Escaped_s) {
643 EXPECT_FALSE(AtomMatchesChar(true, 's', '\0'));
644 EXPECT_FALSE(AtomMatchesChar(true, 's', 'a'));
645 EXPECT_FALSE(AtomMatchesChar(true, 's', '.'));
646 EXPECT_FALSE(AtomMatchesChar(true, 's', '9'));
648 EXPECT_TRUE(AtomMatchesChar(true, 's', ' '));
649 EXPECT_TRUE(AtomMatchesChar(true, 's', '\n'));
650 EXPECT_TRUE(AtomMatchesChar(true, 's', '\t'));
653 TEST(AtomMatchesCharTest, Escaped_S) {
654 EXPECT_FALSE(AtomMatchesChar(true, 'S', ' '));
655 EXPECT_FALSE(AtomMatchesChar(true, 'S', '\r'));
657 EXPECT_TRUE(AtomMatchesChar(true, 'S', '\0'));
658 EXPECT_TRUE(AtomMatchesChar(true, 'S', 'a'));
659 EXPECT_TRUE(AtomMatchesChar(true, 'S', '9'));
662 TEST(AtomMatchesCharTest, Escaped_w) {
663 EXPECT_FALSE(AtomMatchesChar(true, 'w', '\0'));
664 EXPECT_FALSE(AtomMatchesChar(true, 'w', '+'));
665 EXPECT_FALSE(AtomMatchesChar(true, 'w', ' '));
666 EXPECT_FALSE(AtomMatchesChar(true, 'w', '\n'));
668 EXPECT_TRUE(AtomMatchesChar(true, 'w', '0'));
669 EXPECT_TRUE(AtomMatchesChar(true, 'w', 'b'));
670 EXPECT_TRUE(AtomMatchesChar(true, 'w', 'C'));
671 EXPECT_TRUE(AtomMatchesChar(true, 'w', '_'));
674 TEST(AtomMatchesCharTest, Escaped_W) {
675 EXPECT_FALSE(AtomMatchesChar(true, 'W', 'A'));
676 EXPECT_FALSE(AtomMatchesChar(true, 'W', 'b'));
677 EXPECT_FALSE(AtomMatchesChar(true, 'W', '9'));
678 EXPECT_FALSE(AtomMatchesChar(true, 'W', '_'));
680 EXPECT_TRUE(AtomMatchesChar(true, 'W', '\0'));
681 EXPECT_TRUE(AtomMatchesChar(true, 'W', '*'));
682 EXPECT_TRUE(AtomMatchesChar(true, 'W', '\n'));
685 TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
686 EXPECT_FALSE(AtomMatchesChar(true, 'f', '\0'));
687 EXPECT_FALSE(AtomMatchesChar(true, 'f', '\n'));
688 EXPECT_FALSE(AtomMatchesChar(true, 'n', '\0'));
689 EXPECT_FALSE(AtomMatchesChar(true, 'n', '\r'));
690 EXPECT_FALSE(AtomMatchesChar(true, 'r', '\0'));
691 EXPECT_FALSE(AtomMatchesChar(true, 'r', 'a'));
692 EXPECT_FALSE(AtomMatchesChar(true, 't', '\0'));
693 EXPECT_FALSE(AtomMatchesChar(true, 't', 't'));
694 EXPECT_FALSE(AtomMatchesChar(true, 'v', '\0'));
695 EXPECT_FALSE(AtomMatchesChar(true, 'v', '\f'));
697 EXPECT_TRUE(AtomMatchesChar(true, 'f', '\f'));
698 EXPECT_TRUE(AtomMatchesChar(true, 'n', '\n'));
699 EXPECT_TRUE(AtomMatchesChar(true, 'r', '\r'));
700 EXPECT_TRUE(AtomMatchesChar(true, 't', '\t'));
701 EXPECT_TRUE(AtomMatchesChar(true, 'v', '\v'));
704 TEST(AtomMatchesCharTest, UnescapedDot) {
705 EXPECT_FALSE(AtomMatchesChar(false, '.', '\n'));
707 EXPECT_TRUE(AtomMatchesChar(false, '.', '\0'));
708 EXPECT_TRUE(AtomMatchesChar(false, '.', '.'));
709 EXPECT_TRUE(AtomMatchesChar(false, '.', 'a'));
710 EXPECT_TRUE(AtomMatchesChar(false, '.', ' '));
713 TEST(AtomMatchesCharTest, UnescapedChar) {
714 EXPECT_FALSE(AtomMatchesChar(false, 'a', '\0'));
715 EXPECT_FALSE(AtomMatchesChar(false, 'a', 'b'));
716 EXPECT_FALSE(AtomMatchesChar(false, '$', 'a'));
718 EXPECT_TRUE(AtomMatchesChar(false, '$', '$'));
719 EXPECT_TRUE(AtomMatchesChar(false, '5', '5'));
720 EXPECT_TRUE(AtomMatchesChar(false, 'Z', 'Z'));
723 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
724 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)),
725 "NULL is not a valid simple regular expression");
726 EXPECT_NONFATAL_FAILURE(
727 ASSERT_FALSE(ValidateRegex("a\\")),
728 "Syntax error at index 1 in simple regular expression \"a\\\": ");
729 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a\\")),
730 "'\\' cannot appear at the end");
731 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\n\\")),
732 "'\\' cannot appear at the end");
733 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\s\\hb")),
734 "invalid escape sequence \"\\h\"");
735 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^^")),
736 "'^' can only appear at the beginning");
737 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(".*^b")),
738 "'^' can only appear at the beginning");
739 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("$$")),
740 "'$' can only appear at the end");
741 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^$a")),
742 "'$' can only appear at the end");
743 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a(b")),
744 "'(' is unsupported");
745 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("ab)")),
746 "')' is unsupported");
747 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("[ab")),
748 "'[' is unsupported");
749 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a{2")),
750 "'{' is unsupported");
751 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("?")),
752 "'?' can only follow a repeatable token");
753 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^*")),
754 "'*' can only follow a repeatable token");
755 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("5*+")),
756 "'+' can only follow a repeatable token");
759 TEST(ValidateRegexTest, ReturnsTrueForValid) {
760 EXPECT_TRUE(ValidateRegex(""));
761 EXPECT_TRUE(ValidateRegex("a"));
762 EXPECT_TRUE(ValidateRegex(".*"));
763 EXPECT_TRUE(ValidateRegex("^a_+"));
764 EXPECT_TRUE(ValidateRegex("^a\\t\\&?"));
765 EXPECT_TRUE(ValidateRegex("09*$"));
766 EXPECT_TRUE(ValidateRegex("^Z$"));
767 EXPECT_TRUE(ValidateRegex("a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
770 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
771 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "a", "ba"));
772 // Repeating more than once.
773 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "aab"));
775 // Repeating zero times.
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"));
785 // Repeating zero times.
786 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "bc"));
788 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "abc"));
789 // Repeating more than once.
790 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '*', "-", "ab_1-g"));
793 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
794 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "a$", "baab"));
795 // Repeating zero times.
796 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "bc"));
799 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "abc"));
800 // Repeating more than once.
801 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '+', "-", "ab_1-g"));
804 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
805 EXPECT_TRUE(MatchRegexAtHead("", ""));
806 EXPECT_TRUE(MatchRegexAtHead("", "ab"));
809 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
810 EXPECT_FALSE(MatchRegexAtHead("$", "a"));
812 EXPECT_TRUE(MatchRegexAtHead("$", ""));
813 EXPECT_TRUE(MatchRegexAtHead("a$", "a"));
816 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
817 EXPECT_FALSE(MatchRegexAtHead("\\w", "+"));
818 EXPECT_FALSE(MatchRegexAtHead("\\W", "ab"));
820 EXPECT_TRUE(MatchRegexAtHead("\\sa", "\nab"));
821 EXPECT_TRUE(MatchRegexAtHead("\\d", "1a"));
824 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
825 EXPECT_FALSE(MatchRegexAtHead(".+a", "abc"));
826 EXPECT_FALSE(MatchRegexAtHead("a?b", "aab"));
828 EXPECT_TRUE(MatchRegexAtHead(".*a", "bc12-ab"));
829 EXPECT_TRUE(MatchRegexAtHead("a?b", "b"));
830 EXPECT_TRUE(MatchRegexAtHead("a?b", "ab"));
833 TEST(MatchRegexAtHeadTest,
834 WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
835 EXPECT_FALSE(MatchRegexAtHead("\\.+a", "abc"));
836 EXPECT_FALSE(MatchRegexAtHead("\\s?b", " b"));
838 EXPECT_TRUE(MatchRegexAtHead("\\(*a", "((((ab"));
839 EXPECT_TRUE(MatchRegexAtHead("\\^?b", "^b"));
840 EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "b"));
841 EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "\\b"));
844 TEST(MatchRegexAtHeadTest, MatchesSequentially) {
845 EXPECT_FALSE(MatchRegexAtHead("ab.*c", "acabc"));
847 EXPECT_TRUE(MatchRegexAtHead("ab.*c", "ab-fsc"));
850 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
851 EXPECT_FALSE(MatchRegexAnywhere("", NULL));
854 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
855 EXPECT_FALSE(MatchRegexAnywhere("^a", "ba"));
856 EXPECT_FALSE(MatchRegexAnywhere("^$", "a"));
858 EXPECT_TRUE(MatchRegexAnywhere("^a", "ab"));
859 EXPECT_TRUE(MatchRegexAnywhere("^", "ab"));
860 EXPECT_TRUE(MatchRegexAnywhere("^$", ""));
863 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
864 EXPECT_FALSE(MatchRegexAnywhere("a", "bcde123"));
865 EXPECT_FALSE(MatchRegexAnywhere("a.+a", "--aa88888888"));
868 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
869 EXPECT_TRUE(MatchRegexAnywhere("\\w+", "ab1_ - 5"));
870 EXPECT_TRUE(MatchRegexAnywhere(".*=", "="));
871 EXPECT_TRUE(MatchRegexAnywhere("x.*ab?.*bc", "xaaabc"));
874 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
875 EXPECT_TRUE(MatchRegexAnywhere("\\w+", "$$$ ab1_ - 5"));
876 EXPECT_TRUE(MatchRegexAnywhere("\\.+=", "= ...="));
879 // Tests RE's implicit constructors.
880 TEST(RETest, ImplicitConstructorWorks) {
882 EXPECT_STREQ("", empty.pattern());
884 const RE simple("hello");
885 EXPECT_STREQ("hello", simple.pattern());
888 // Tests that RE's constructors reject invalid regular expressions.
889 TEST(RETest, RejectsInvalidRegex) {
890 EXPECT_NONFATAL_FAILURE({
891 const RE normal(NULL);
892 }, "NULL is not a valid simple regular expression");
894 EXPECT_NONFATAL_FAILURE({
895 const RE normal(".*(\\w+");
896 }, "'(' is unsupported");
898 EXPECT_NONFATAL_FAILURE({
899 const RE invalid("^?");
900 }, "'?' can only follow a repeatable token");
903 // Tests RE::FullMatch().
904 TEST(RETest, FullMatchWorks) {
906 EXPECT_TRUE(RE::FullMatch("", empty));
907 EXPECT_FALSE(RE::FullMatch("a", empty));
910 EXPECT_TRUE(RE::FullMatch("a", re1));
913 EXPECT_TRUE(RE::FullMatch("az", re));
914 EXPECT_TRUE(RE::FullMatch("axyz", re));
915 EXPECT_FALSE(RE::FullMatch("baz", re));
916 EXPECT_FALSE(RE::FullMatch("azy", re));
919 // Tests RE::PartialMatch().
920 TEST(RETest, PartialMatchWorks) {
922 EXPECT_TRUE(RE::PartialMatch("", empty));
923 EXPECT_TRUE(RE::PartialMatch("a", empty));
926 EXPECT_TRUE(RE::PartialMatch("az", re));
927 EXPECT_TRUE(RE::PartialMatch("axyz", re));
928 EXPECT_TRUE(RE::PartialMatch("baz", re));
929 EXPECT_TRUE(RE::PartialMatch("azy", re));
930 EXPECT_FALSE(RE::PartialMatch("zza", re));
933 #endif // GTEST_USES_POSIX_RE
935 #if !GTEST_OS_WINDOWS_MOBILE
937 TEST(CaptureTest, CapturesStdout) {
939 fprintf(stdout, "abc");
940 EXPECT_STREQ("abc", GetCapturedStdout().c_str());
943 fprintf(stdout, "def%cghi", '\0');
944 EXPECT_EQ(::std::string("def\0ghi", 7), ::std::string(GetCapturedStdout()));
947 TEST(CaptureTest, CapturesStderr) {
949 fprintf(stderr, "jkl");
950 EXPECT_STREQ("jkl", GetCapturedStderr().c_str());
953 fprintf(stderr, "jkl%cmno", '\0');
954 EXPECT_EQ(::std::string("jkl\0mno", 7), ::std::string(GetCapturedStderr()));
957 // Tests that stdout and stderr capture don't interfere with each other.
958 TEST(CaptureTest, CapturesStdoutAndStderr) {
961 fprintf(stdout, "pqr");
962 fprintf(stderr, "stu");
963 EXPECT_STREQ("pqr", GetCapturedStdout().c_str());
964 EXPECT_STREQ("stu", GetCapturedStderr().c_str());
967 TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
969 EXPECT_DEATH_IF_SUPPORTED(CaptureStdout(),
970 "Only one stdout capturer can exist at a time");
973 // We cannot test stderr capturing using death tests as they use it
977 #endif // !GTEST_OS_WINDOWS_MOBILE
979 TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
981 EXPECT_EQ(0, t1.get());
983 ThreadLocal<void*> t2;
984 EXPECT_TRUE(t2.get() == NULL);
987 TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
988 ThreadLocal<int> t1(123);
989 EXPECT_EQ(123, t1.get());
992 ThreadLocal<int*> t2(&i);
993 EXPECT_EQ(&i, t2.get());
996 class NoDefaultContructor {
998 explicit NoDefaultContructor(const char*) {}
999 NoDefaultContructor(const NoDefaultContructor&) {}
1002 TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
1003 ThreadLocal<NoDefaultContructor> bar(NoDefaultContructor("foo"));
1007 TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
1008 ThreadLocal<std::string> thread_local_string;
1010 EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
1012 // Verifies the condition still holds after calling set.
1013 thread_local_string.set("foo");
1014 EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
1017 TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
1018 ThreadLocal<std::string> thread_local_string;
1019 const ThreadLocal<std::string>& const_thread_local_string =
1020 thread_local_string;
1022 EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
1024 thread_local_string.set("foo");
1025 EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
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) {
1040 // AssertHeld() is flaky only in the presence of multiple threads accessing
1041 // the lock. In this case, the test is robust.
1042 EXPECT_DEATH_IF_SUPPORTED({
1044 { MutexLock lock(&m); }
1050 TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
1056 class AtomicCounterWithMutex {
1058 explicit AtomicCounterWithMutex(Mutex* mutex) :
1059 value_(0), mutex_(mutex), random_(42) {}
1062 MutexLock lock(mutex_);
1065 // Locking a mutex puts up a memory barrier, preventing reads and
1066 // writes to value_ rearranged when observed from other threads.
1068 // We cannot use Mutex and MutexLock here or rely on their memory
1069 // barrier functionality as we are testing them here.
1070 pthread_mutex_t memory_barrier_mutex;
1071 GTEST_CHECK_POSIX_SUCCESS_(
1072 pthread_mutex_init(&memory_barrier_mutex, NULL));
1073 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&memory_barrier_mutex));
1075 SleepMilliseconds(random_.Generate(30));
1077 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&memory_barrier_mutex));
1078 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&memory_barrier_mutex));
1082 int value() const { return value_; }
1085 volatile int value_;
1086 Mutex* const mutex_; // Protects value_.
1090 void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
1091 for (int i = 0; i < param.second; ++i)
1092 param.first->Increment();
1095 // Tests that the mutex only lets one thread at a time to lock it.
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;
1103 scoped_ptr<ThreadType> counting_threads[kThreadCount];
1104 Notification threads_can_start;
1105 // Creates and runs kThreadCount threads that increment locked_counter
1106 // kCycleCount times each.
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();
1117 // If the mutex lets more than one thread to increment the counter at a
1118 // time, they are likely to encounter a race condition and have some
1119 // increments overwritten, resulting in the lower then expected counter
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(
1131 pair<ThreadLocal<std::string>*, std::string*> param) {
1132 *param.second = param.first->get();
1135 TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
1136 ThreadLocal<std::string> thread_local_string("foo");
1137 EXPECT_STREQ("foo", thread_local_string.get().c_str());
1139 thread_local_string.set("bar");
1140 EXPECT_STREQ("bar", thread_local_string.get().c_str());
1143 RunFromThread(&RetrieveThreadLocalValue,
1144 make_pair(&thread_local_string, &result));
1145 EXPECT_STREQ("foo", result.c_str());
1148 // DestructorTracker keeps track of whether its instances have been
1150 static std::vector<bool> g_destroyed;
1152 class DestructorTracker {
1154 DestructorTracker() : index_(GetNewIndex()) {}
1155 DestructorTracker(const DestructorTracker& /* rhs */)
1156 : index_(GetNewIndex()) {}
1157 ~DestructorTracker() {
1158 // We never access g_destroyed concurrently, so we don't need to
1159 // protect the write operation under a mutex.
1160 g_destroyed[index_] = true;
1164 static int GetNewIndex() {
1165 g_destroyed.push_back(false);
1166 return g_destroyed.size() - 1;
1171 typedef ThreadLocal<DestructorTracker>* ThreadParam;
1173 void CallThreadLocalGet(ThreadParam thread_local_param) {
1174 thread_local_param->get();
1177 // Tests that when a ThreadLocal object dies in a thread, it destroys
1178 // the managed object for that thread.
1179 TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
1180 g_destroyed.clear();
1183 // The next line default constructs a DestructorTracker object as
1184 // the default value of objects managed by thread_local_tracker.
1185 ThreadLocal<DestructorTracker> thread_local_tracker;
1186 ASSERT_EQ(1U, g_destroyed.size());
1187 ASSERT_FALSE(g_destroyed[0]);
1189 // This creates another DestructorTracker object for the main thread.
1190 thread_local_tracker.get();
1191 ASSERT_EQ(2U, g_destroyed.size());
1192 ASSERT_FALSE(g_destroyed[0]);
1193 ASSERT_FALSE(g_destroyed[1]);
1196 // Now thread_local_tracker has died. It should have destroyed both the
1197 // default value shared by all threads and the value for the main
1199 ASSERT_EQ(2U, g_destroyed.size());
1200 EXPECT_TRUE(g_destroyed[0]);
1201 EXPECT_TRUE(g_destroyed[1]);
1203 g_destroyed.clear();
1206 // Tests that when a thread exits, the thread-local object for that
1207 // thread is destroyed.
1208 TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
1209 g_destroyed.clear();
1212 // The next line default constructs a DestructorTracker object as
1213 // the default value of objects managed by thread_local_tracker.
1214 ThreadLocal<DestructorTracker> thread_local_tracker;
1215 ASSERT_EQ(1U, g_destroyed.size());
1216 ASSERT_FALSE(g_destroyed[0]);
1218 // This creates another DestructorTracker object in the new thread.
1219 ThreadWithParam<ThreadParam> thread(
1220 &CallThreadLocalGet, &thread_local_tracker, NULL);
1223 // Now the new thread has exited. The per-thread object for it
1224 // should have been destroyed.
1225 ASSERT_EQ(2U, g_destroyed.size());
1226 ASSERT_FALSE(g_destroyed[0]);
1227 ASSERT_TRUE(g_destroyed[1]);
1230 // Now thread_local_tracker has died. The default value should have been
1232 ASSERT_EQ(2U, g_destroyed.size());
1233 EXPECT_TRUE(g_destroyed[0]);
1234 EXPECT_TRUE(g_destroyed[1]);
1236 g_destroyed.clear();
1239 TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
1240 ThreadLocal<std::string> thread_local_string;
1241 thread_local_string.set("Foo");
1242 EXPECT_STREQ("Foo", thread_local_string.get().c_str());
1245 RunFromThread(&RetrieveThreadLocalValue,
1246 make_pair(&thread_local_string, &result));
1247 EXPECT_TRUE(result.empty());
1250 #endif // GTEST_IS_THREADSAFE
1252 } // namespace internal
1253 } // namespace testing