Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright 2008, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Authors: vladl@google.com (Vlad Losev), wan@google.com (Zhanyong Wan) |
michael@0 | 31 | // |
michael@0 | 32 | // This file tests the internal cross-platform support utilities. |
michael@0 | 33 | |
michael@0 | 34 | #include "gtest/internal/gtest-port.h" |
michael@0 | 35 | |
michael@0 | 36 | #include <stdio.h> |
michael@0 | 37 | |
michael@0 | 38 | #if GTEST_OS_MAC |
michael@0 | 39 | # include <time.h> |
michael@0 | 40 | #endif // GTEST_OS_MAC |
michael@0 | 41 | |
michael@0 | 42 | #include <list> |
michael@0 | 43 | #include <utility> // For std::pair and std::make_pair. |
michael@0 | 44 | #include <vector> |
michael@0 | 45 | |
michael@0 | 46 | #include "gtest/gtest.h" |
michael@0 | 47 | #include "gtest/gtest-spi.h" |
michael@0 | 48 | |
michael@0 | 49 | // Indicates that this translation unit is part of Google Test's |
michael@0 | 50 | // implementation. It must come before gtest-internal-inl.h is |
michael@0 | 51 | // included, or there will be a compiler error. This trick is to |
michael@0 | 52 | // prevent a user from accidentally including gtest-internal-inl.h in |
michael@0 | 53 | // his code. |
michael@0 | 54 | #define GTEST_IMPLEMENTATION_ 1 |
michael@0 | 55 | #include "src/gtest-internal-inl.h" |
michael@0 | 56 | #undef GTEST_IMPLEMENTATION_ |
michael@0 | 57 | |
michael@0 | 58 | using std::make_pair; |
michael@0 | 59 | using std::pair; |
michael@0 | 60 | |
michael@0 | 61 | namespace testing { |
michael@0 | 62 | namespace internal { |
michael@0 | 63 | |
michael@0 | 64 | TEST(IsXDigitTest, WorksForNarrowAscii) { |
michael@0 | 65 | EXPECT_TRUE(IsXDigit('0')); |
michael@0 | 66 | EXPECT_TRUE(IsXDigit('9')); |
michael@0 | 67 | EXPECT_TRUE(IsXDigit('A')); |
michael@0 | 68 | EXPECT_TRUE(IsXDigit('F')); |
michael@0 | 69 | EXPECT_TRUE(IsXDigit('a')); |
michael@0 | 70 | EXPECT_TRUE(IsXDigit('f')); |
michael@0 | 71 | |
michael@0 | 72 | EXPECT_FALSE(IsXDigit('-')); |
michael@0 | 73 | EXPECT_FALSE(IsXDigit('g')); |
michael@0 | 74 | EXPECT_FALSE(IsXDigit('G')); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) { |
michael@0 | 78 | EXPECT_FALSE(IsXDigit(static_cast<char>(0x80))); |
michael@0 | 79 | EXPECT_FALSE(IsXDigit(static_cast<char>('0' | 0x80))); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | TEST(IsXDigitTest, WorksForWideAscii) { |
michael@0 | 83 | EXPECT_TRUE(IsXDigit(L'0')); |
michael@0 | 84 | EXPECT_TRUE(IsXDigit(L'9')); |
michael@0 | 85 | EXPECT_TRUE(IsXDigit(L'A')); |
michael@0 | 86 | EXPECT_TRUE(IsXDigit(L'F')); |
michael@0 | 87 | EXPECT_TRUE(IsXDigit(L'a')); |
michael@0 | 88 | EXPECT_TRUE(IsXDigit(L'f')); |
michael@0 | 89 | |
michael@0 | 90 | EXPECT_FALSE(IsXDigit(L'-')); |
michael@0 | 91 | EXPECT_FALSE(IsXDigit(L'g')); |
michael@0 | 92 | EXPECT_FALSE(IsXDigit(L'G')); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) { |
michael@0 | 96 | EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(0x80))); |
michael@0 | 97 | EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(L'0' | 0x80))); |
michael@0 | 98 | EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(L'0' | 0x100))); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | class Base { |
michael@0 | 102 | public: |
michael@0 | 103 | // Copy constructor and assignment operator do exactly what we need, so we |
michael@0 | 104 | // use them. |
michael@0 | 105 | Base() : member_(0) {} |
michael@0 | 106 | explicit Base(int n) : member_(n) {} |
michael@0 | 107 | virtual ~Base() {} |
michael@0 | 108 | int member() { return member_; } |
michael@0 | 109 | |
michael@0 | 110 | private: |
michael@0 | 111 | int member_; |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | class Derived : public Base { |
michael@0 | 115 | public: |
michael@0 | 116 | explicit Derived(int n) : Base(n) {} |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | TEST(ImplicitCastTest, ConvertsPointers) { |
michael@0 | 120 | Derived derived(0); |
michael@0 | 121 | EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived)); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | TEST(ImplicitCastTest, CanUseInheritance) { |
michael@0 | 125 | Derived derived(1); |
michael@0 | 126 | Base base = ::testing::internal::ImplicitCast_<Base>(derived); |
michael@0 | 127 | EXPECT_EQ(derived.member(), base.member()); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | class Castable { |
michael@0 | 131 | public: |
michael@0 | 132 | explicit Castable(bool* converted) : converted_(converted) {} |
michael@0 | 133 | operator Base() { |
michael@0 | 134 | *converted_ = true; |
michael@0 | 135 | return Base(); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | private: |
michael@0 | 139 | bool* converted_; |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | TEST(ImplicitCastTest, CanUseNonConstCastOperator) { |
michael@0 | 143 | bool converted = false; |
michael@0 | 144 | Castable castable(&converted); |
michael@0 | 145 | Base base = ::testing::internal::ImplicitCast_<Base>(castable); |
michael@0 | 146 | EXPECT_TRUE(converted); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | class ConstCastable { |
michael@0 | 150 | public: |
michael@0 | 151 | explicit ConstCastable(bool* converted) : converted_(converted) {} |
michael@0 | 152 | operator Base() const { |
michael@0 | 153 | *converted_ = true; |
michael@0 | 154 | return Base(); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | private: |
michael@0 | 158 | bool* converted_; |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) { |
michael@0 | 162 | bool converted = false; |
michael@0 | 163 | const ConstCastable const_castable(&converted); |
michael@0 | 164 | Base base = ::testing::internal::ImplicitCast_<Base>(const_castable); |
michael@0 | 165 | EXPECT_TRUE(converted); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | class ConstAndNonConstCastable { |
michael@0 | 169 | public: |
michael@0 | 170 | ConstAndNonConstCastable(bool* converted, bool* const_converted) |
michael@0 | 171 | : converted_(converted), const_converted_(const_converted) {} |
michael@0 | 172 | operator Base() { |
michael@0 | 173 | *converted_ = true; |
michael@0 | 174 | return Base(); |
michael@0 | 175 | } |
michael@0 | 176 | operator Base() const { |
michael@0 | 177 | *const_converted_ = true; |
michael@0 | 178 | return Base(); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | private: |
michael@0 | 182 | bool* converted_; |
michael@0 | 183 | bool* const_converted_; |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) { |
michael@0 | 187 | bool converted = false; |
michael@0 | 188 | bool const_converted = false; |
michael@0 | 189 | ConstAndNonConstCastable castable(&converted, &const_converted); |
michael@0 | 190 | Base base = ::testing::internal::ImplicitCast_<Base>(castable); |
michael@0 | 191 | EXPECT_TRUE(converted); |
michael@0 | 192 | EXPECT_FALSE(const_converted); |
michael@0 | 193 | |
michael@0 | 194 | converted = false; |
michael@0 | 195 | const_converted = false; |
michael@0 | 196 | const ConstAndNonConstCastable const_castable(&converted, &const_converted); |
michael@0 | 197 | base = ::testing::internal::ImplicitCast_<Base>(const_castable); |
michael@0 | 198 | EXPECT_FALSE(converted); |
michael@0 | 199 | EXPECT_TRUE(const_converted); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | class To { |
michael@0 | 203 | public: |
michael@0 | 204 | To(bool* converted) { *converted = true; } // NOLINT |
michael@0 | 205 | }; |
michael@0 | 206 | |
michael@0 | 207 | TEST(ImplicitCastTest, CanUseImplicitConstructor) { |
michael@0 | 208 | bool converted = false; |
michael@0 | 209 | To to = ::testing::internal::ImplicitCast_<To>(&converted); |
michael@0 | 210 | (void)to; |
michael@0 | 211 | EXPECT_TRUE(converted); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | TEST(IteratorTraitsTest, WorksForSTLContainerIterators) { |
michael@0 | 215 | StaticAssertTypeEq<int, |
michael@0 | 216 | IteratorTraits< ::std::vector<int>::const_iterator>::value_type>(); |
michael@0 | 217 | StaticAssertTypeEq<bool, |
michael@0 | 218 | IteratorTraits< ::std::list<bool>::iterator>::value_type>(); |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | TEST(IteratorTraitsTest, WorksForPointerToNonConst) { |
michael@0 | 222 | StaticAssertTypeEq<char, IteratorTraits<char*>::value_type>(); |
michael@0 | 223 | StaticAssertTypeEq<const void*, IteratorTraits<const void**>::value_type>(); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | TEST(IteratorTraitsTest, WorksForPointerToConst) { |
michael@0 | 227 | StaticAssertTypeEq<char, IteratorTraits<const char*>::value_type>(); |
michael@0 | 228 | StaticAssertTypeEq<const void*, |
michael@0 | 229 | IteratorTraits<const void* const*>::value_type>(); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | // Tests that the element_type typedef is available in scoped_ptr and refers |
michael@0 | 233 | // to the parameter type. |
michael@0 | 234 | TEST(ScopedPtrTest, DefinesElementType) { |
michael@0 | 235 | StaticAssertTypeEq<int, ::testing::internal::scoped_ptr<int>::element_type>(); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | // TODO(vladl@google.com): Implement THE REST of scoped_ptr tests. |
michael@0 | 239 | |
michael@0 | 240 | TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) { |
michael@0 | 241 | if (AlwaysFalse()) |
michael@0 | 242 | GTEST_CHECK_(false) << "This should never be executed; " |
michael@0 | 243 | "It's a compilation test only."; |
michael@0 | 244 | |
michael@0 | 245 | if (AlwaysTrue()) |
michael@0 | 246 | GTEST_CHECK_(true); |
michael@0 | 247 | else |
michael@0 | 248 | ; // NOLINT |
michael@0 | 249 | |
michael@0 | 250 | if (AlwaysFalse()) |
michael@0 | 251 | ; // NOLINT |
michael@0 | 252 | else |
michael@0 | 253 | GTEST_CHECK_(true) << ""; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | TEST(GtestCheckSyntaxTest, WorksWithSwitch) { |
michael@0 | 257 | switch (0) { |
michael@0 | 258 | case 1: |
michael@0 | 259 | break; |
michael@0 | 260 | default: |
michael@0 | 261 | GTEST_CHECK_(true); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | switch (0) |
michael@0 | 265 | case 0: |
michael@0 | 266 | GTEST_CHECK_(true) << "Check failed in switch case"; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | // Verifies behavior of FormatFileLocation. |
michael@0 | 270 | TEST(FormatFileLocationTest, FormatsFileLocation) { |
michael@0 | 271 | EXPECT_PRED_FORMAT2(IsSubstring, "foo.cc", FormatFileLocation("foo.cc", 42)); |
michael@0 | 272 | EXPECT_PRED_FORMAT2(IsSubstring, "42", FormatFileLocation("foo.cc", 42)); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | TEST(FormatFileLocationTest, FormatsUnknownFile) { |
michael@0 | 276 | EXPECT_PRED_FORMAT2( |
michael@0 | 277 | IsSubstring, "unknown file", FormatFileLocation(NULL, 42)); |
michael@0 | 278 | EXPECT_PRED_FORMAT2(IsSubstring, "42", FormatFileLocation(NULL, 42)); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | TEST(FormatFileLocationTest, FormatsUknownLine) { |
michael@0 | 282 | EXPECT_EQ("foo.cc:", FormatFileLocation("foo.cc", -1)); |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | TEST(FormatFileLocationTest, FormatsUknownFileAndLine) { |
michael@0 | 286 | EXPECT_EQ("unknown file:", FormatFileLocation(NULL, -1)); |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | // Verifies behavior of FormatCompilerIndependentFileLocation. |
michael@0 | 290 | TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) { |
michael@0 | 291 | EXPECT_EQ("foo.cc:42", FormatCompilerIndependentFileLocation("foo.cc", 42)); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) { |
michael@0 | 295 | EXPECT_EQ("unknown file:42", |
michael@0 | 296 | FormatCompilerIndependentFileLocation(NULL, 42)); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) { |
michael@0 | 300 | EXPECT_EQ("foo.cc", FormatCompilerIndependentFileLocation("foo.cc", -1)); |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) { |
michael@0 | 304 | EXPECT_EQ("unknown file", FormatCompilerIndependentFileLocation(NULL, -1)); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | #if GTEST_OS_MAC || GTEST_OS_QNX |
michael@0 | 308 | void* ThreadFunc(void* data) { |
michael@0 | 309 | pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data); |
michael@0 | 310 | pthread_mutex_lock(mutex); |
michael@0 | 311 | pthread_mutex_unlock(mutex); |
michael@0 | 312 | return NULL; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | TEST(GetThreadCountTest, ReturnsCorrectValue) { |
michael@0 | 316 | EXPECT_EQ(1U, GetThreadCount()); |
michael@0 | 317 | pthread_mutex_t mutex; |
michael@0 | 318 | pthread_attr_t attr; |
michael@0 | 319 | pthread_t thread_id; |
michael@0 | 320 | |
michael@0 | 321 | // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic |
michael@0 | 322 | // destruction. |
michael@0 | 323 | pthread_mutex_init(&mutex, NULL); |
michael@0 | 324 | pthread_mutex_lock(&mutex); |
michael@0 | 325 | ASSERT_EQ(0, pthread_attr_init(&attr)); |
michael@0 | 326 | ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); |
michael@0 | 327 | |
michael@0 | 328 | const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex); |
michael@0 | 329 | ASSERT_EQ(0, pthread_attr_destroy(&attr)); |
michael@0 | 330 | ASSERT_EQ(0, status); |
michael@0 | 331 | EXPECT_EQ(2U, GetThreadCount()); |
michael@0 | 332 | pthread_mutex_unlock(&mutex); |
michael@0 | 333 | |
michael@0 | 334 | void* dummy; |
michael@0 | 335 | ASSERT_EQ(0, pthread_join(thread_id, &dummy)); |
michael@0 | 336 | |
michael@0 | 337 | # if GTEST_OS_MAC |
michael@0 | 338 | |
michael@0 | 339 | // MacOS X may not immediately report the updated thread count after |
michael@0 | 340 | // joining a thread, causing flakiness in this test. To counter that, we |
michael@0 | 341 | // wait for up to .5 seconds for the OS to report the correct value. |
michael@0 | 342 | for (int i = 0; i < 5; ++i) { |
michael@0 | 343 | if (GetThreadCount() == 1) |
michael@0 | 344 | break; |
michael@0 | 345 | |
michael@0 | 346 | SleepMilliseconds(100); |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | # endif // GTEST_OS_MAC |
michael@0 | 350 | |
michael@0 | 351 | EXPECT_EQ(1U, GetThreadCount()); |
michael@0 | 352 | pthread_mutex_destroy(&mutex); |
michael@0 | 353 | } |
michael@0 | 354 | #else |
michael@0 | 355 | TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) { |
michael@0 | 356 | EXPECT_EQ(0U, GetThreadCount()); |
michael@0 | 357 | } |
michael@0 | 358 | #endif // GTEST_OS_MAC || GTEST_OS_QNX |
michael@0 | 359 | |
michael@0 | 360 | TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) { |
michael@0 | 361 | const bool a_false_condition = false; |
michael@0 | 362 | const char regex[] = |
michael@0 | 363 | #ifdef _MSC_VER |
michael@0 | 364 | "gtest-port_test\\.cc\\(\\d+\\):" |
michael@0 | 365 | #elif GTEST_USES_POSIX_RE |
michael@0 | 366 | "gtest-port_test\\.cc:[0-9]+" |
michael@0 | 367 | #else |
michael@0 | 368 | "gtest-port_test\\.cc:\\d+" |
michael@0 | 369 | #endif // _MSC_VER |
michael@0 | 370 | ".*a_false_condition.*Extra info.*"; |
michael@0 | 371 | |
michael@0 | 372 | EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info", |
michael@0 | 373 | regex); |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | #if GTEST_HAS_DEATH_TEST |
michael@0 | 377 | |
michael@0 | 378 | TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) { |
michael@0 | 379 | EXPECT_EXIT({ |
michael@0 | 380 | GTEST_CHECK_(true) << "Extra info"; |
michael@0 | 381 | ::std::cerr << "Success\n"; |
michael@0 | 382 | exit(0); }, |
michael@0 | 383 | ::testing::ExitedWithCode(0), "Success"); |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | #endif // GTEST_HAS_DEATH_TEST |
michael@0 | 387 | |
michael@0 | 388 | // Verifies that Google Test choose regular expression engine appropriate to |
michael@0 | 389 | // the platform. The test will produce compiler errors in case of failure. |
michael@0 | 390 | // For simplicity, we only cover the most important platforms here. |
michael@0 | 391 | TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) { |
michael@0 | 392 | #if GTEST_HAS_POSIX_RE |
michael@0 | 393 | |
michael@0 | 394 | EXPECT_TRUE(GTEST_USES_POSIX_RE); |
michael@0 | 395 | |
michael@0 | 396 | #else |
michael@0 | 397 | |
michael@0 | 398 | EXPECT_TRUE(GTEST_USES_SIMPLE_RE); |
michael@0 | 399 | |
michael@0 | 400 | #endif |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | #if GTEST_USES_POSIX_RE |
michael@0 | 404 | |
michael@0 | 405 | # if GTEST_HAS_TYPED_TEST |
michael@0 | 406 | |
michael@0 | 407 | template <typename Str> |
michael@0 | 408 | class RETest : public ::testing::Test {}; |
michael@0 | 409 | |
michael@0 | 410 | // Defines StringTypes as the list of all string types that class RE |
michael@0 | 411 | // supports. |
michael@0 | 412 | typedef testing::Types< |
michael@0 | 413 | ::std::string, |
michael@0 | 414 | # if GTEST_HAS_GLOBAL_STRING |
michael@0 | 415 | ::string, |
michael@0 | 416 | # endif // GTEST_HAS_GLOBAL_STRING |
michael@0 | 417 | const char*> StringTypes; |
michael@0 | 418 | |
michael@0 | 419 | TYPED_TEST_CASE(RETest, StringTypes); |
michael@0 | 420 | |
michael@0 | 421 | // Tests RE's implicit constructors. |
michael@0 | 422 | TYPED_TEST(RETest, ImplicitConstructorWorks) { |
michael@0 | 423 | const RE empty(TypeParam("")); |
michael@0 | 424 | EXPECT_STREQ("", empty.pattern()); |
michael@0 | 425 | |
michael@0 | 426 | const RE simple(TypeParam("hello")); |
michael@0 | 427 | EXPECT_STREQ("hello", simple.pattern()); |
michael@0 | 428 | |
michael@0 | 429 | const RE normal(TypeParam(".*(\\w+)")); |
michael@0 | 430 | EXPECT_STREQ(".*(\\w+)", normal.pattern()); |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | // Tests that RE's constructors reject invalid regular expressions. |
michael@0 | 434 | TYPED_TEST(RETest, RejectsInvalidRegex) { |
michael@0 | 435 | EXPECT_NONFATAL_FAILURE({ |
michael@0 | 436 | const RE invalid(TypeParam("?")); |
michael@0 | 437 | }, "\"?\" is not a valid POSIX Extended regular expression."); |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | // Tests RE::FullMatch(). |
michael@0 | 441 | TYPED_TEST(RETest, FullMatchWorks) { |
michael@0 | 442 | const RE empty(TypeParam("")); |
michael@0 | 443 | EXPECT_TRUE(RE::FullMatch(TypeParam(""), empty)); |
michael@0 | 444 | EXPECT_FALSE(RE::FullMatch(TypeParam("a"), empty)); |
michael@0 | 445 | |
michael@0 | 446 | const RE re(TypeParam("a.*z")); |
michael@0 | 447 | EXPECT_TRUE(RE::FullMatch(TypeParam("az"), re)); |
michael@0 | 448 | EXPECT_TRUE(RE::FullMatch(TypeParam("axyz"), re)); |
michael@0 | 449 | EXPECT_FALSE(RE::FullMatch(TypeParam("baz"), re)); |
michael@0 | 450 | EXPECT_FALSE(RE::FullMatch(TypeParam("azy"), re)); |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | // Tests RE::PartialMatch(). |
michael@0 | 454 | TYPED_TEST(RETest, PartialMatchWorks) { |
michael@0 | 455 | const RE empty(TypeParam("")); |
michael@0 | 456 | EXPECT_TRUE(RE::PartialMatch(TypeParam(""), empty)); |
michael@0 | 457 | EXPECT_TRUE(RE::PartialMatch(TypeParam("a"), empty)); |
michael@0 | 458 | |
michael@0 | 459 | const RE re(TypeParam("a.*z")); |
michael@0 | 460 | EXPECT_TRUE(RE::PartialMatch(TypeParam("az"), re)); |
michael@0 | 461 | EXPECT_TRUE(RE::PartialMatch(TypeParam("axyz"), re)); |
michael@0 | 462 | EXPECT_TRUE(RE::PartialMatch(TypeParam("baz"), re)); |
michael@0 | 463 | EXPECT_TRUE(RE::PartialMatch(TypeParam("azy"), re)); |
michael@0 | 464 | EXPECT_FALSE(RE::PartialMatch(TypeParam("zza"), re)); |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | # endif // GTEST_HAS_TYPED_TEST |
michael@0 | 468 | |
michael@0 | 469 | #elif GTEST_USES_SIMPLE_RE |
michael@0 | 470 | |
michael@0 | 471 | TEST(IsInSetTest, NulCharIsNotInAnySet) { |
michael@0 | 472 | EXPECT_FALSE(IsInSet('\0', "")); |
michael@0 | 473 | EXPECT_FALSE(IsInSet('\0', "\0")); |
michael@0 | 474 | EXPECT_FALSE(IsInSet('\0', "a")); |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | TEST(IsInSetTest, WorksForNonNulChars) { |
michael@0 | 478 | EXPECT_FALSE(IsInSet('a', "Ab")); |
michael@0 | 479 | EXPECT_FALSE(IsInSet('c', "")); |
michael@0 | 480 | |
michael@0 | 481 | EXPECT_TRUE(IsInSet('b', "bcd")); |
michael@0 | 482 | EXPECT_TRUE(IsInSet('b', "ab")); |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | TEST(IsAsciiDigitTest, IsFalseForNonDigit) { |
michael@0 | 486 | EXPECT_FALSE(IsAsciiDigit('\0')); |
michael@0 | 487 | EXPECT_FALSE(IsAsciiDigit(' ')); |
michael@0 | 488 | EXPECT_FALSE(IsAsciiDigit('+')); |
michael@0 | 489 | EXPECT_FALSE(IsAsciiDigit('-')); |
michael@0 | 490 | EXPECT_FALSE(IsAsciiDigit('.')); |
michael@0 | 491 | EXPECT_FALSE(IsAsciiDigit('a')); |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | TEST(IsAsciiDigitTest, IsTrueForDigit) { |
michael@0 | 495 | EXPECT_TRUE(IsAsciiDigit('0')); |
michael@0 | 496 | EXPECT_TRUE(IsAsciiDigit('1')); |
michael@0 | 497 | EXPECT_TRUE(IsAsciiDigit('5')); |
michael@0 | 498 | EXPECT_TRUE(IsAsciiDigit('9')); |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | TEST(IsAsciiPunctTest, IsFalseForNonPunct) { |
michael@0 | 502 | EXPECT_FALSE(IsAsciiPunct('\0')); |
michael@0 | 503 | EXPECT_FALSE(IsAsciiPunct(' ')); |
michael@0 | 504 | EXPECT_FALSE(IsAsciiPunct('\n')); |
michael@0 | 505 | EXPECT_FALSE(IsAsciiPunct('a')); |
michael@0 | 506 | EXPECT_FALSE(IsAsciiPunct('0')); |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | TEST(IsAsciiPunctTest, IsTrueForPunct) { |
michael@0 | 510 | for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) { |
michael@0 | 511 | EXPECT_PRED1(IsAsciiPunct, *p); |
michael@0 | 512 | } |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | TEST(IsRepeatTest, IsFalseForNonRepeatChar) { |
michael@0 | 516 | EXPECT_FALSE(IsRepeat('\0')); |
michael@0 | 517 | EXPECT_FALSE(IsRepeat(' ')); |
michael@0 | 518 | EXPECT_FALSE(IsRepeat('a')); |
michael@0 | 519 | EXPECT_FALSE(IsRepeat('1')); |
michael@0 | 520 | EXPECT_FALSE(IsRepeat('-')); |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | TEST(IsRepeatTest, IsTrueForRepeatChar) { |
michael@0 | 524 | EXPECT_TRUE(IsRepeat('?')); |
michael@0 | 525 | EXPECT_TRUE(IsRepeat('*')); |
michael@0 | 526 | EXPECT_TRUE(IsRepeat('+')); |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) { |
michael@0 | 530 | EXPECT_FALSE(IsAsciiWhiteSpace('\0')); |
michael@0 | 531 | EXPECT_FALSE(IsAsciiWhiteSpace('a')); |
michael@0 | 532 | EXPECT_FALSE(IsAsciiWhiteSpace('1')); |
michael@0 | 533 | EXPECT_FALSE(IsAsciiWhiteSpace('+')); |
michael@0 | 534 | EXPECT_FALSE(IsAsciiWhiteSpace('_')); |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) { |
michael@0 | 538 | EXPECT_TRUE(IsAsciiWhiteSpace(' ')); |
michael@0 | 539 | EXPECT_TRUE(IsAsciiWhiteSpace('\n')); |
michael@0 | 540 | EXPECT_TRUE(IsAsciiWhiteSpace('\r')); |
michael@0 | 541 | EXPECT_TRUE(IsAsciiWhiteSpace('\t')); |
michael@0 | 542 | EXPECT_TRUE(IsAsciiWhiteSpace('\v')); |
michael@0 | 543 | EXPECT_TRUE(IsAsciiWhiteSpace('\f')); |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) { |
michael@0 | 547 | EXPECT_FALSE(IsAsciiWordChar('\0')); |
michael@0 | 548 | EXPECT_FALSE(IsAsciiWordChar('+')); |
michael@0 | 549 | EXPECT_FALSE(IsAsciiWordChar('.')); |
michael@0 | 550 | EXPECT_FALSE(IsAsciiWordChar(' ')); |
michael@0 | 551 | EXPECT_FALSE(IsAsciiWordChar('\n')); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | TEST(IsAsciiWordCharTest, IsTrueForLetter) { |
michael@0 | 555 | EXPECT_TRUE(IsAsciiWordChar('a')); |
michael@0 | 556 | EXPECT_TRUE(IsAsciiWordChar('b')); |
michael@0 | 557 | EXPECT_TRUE(IsAsciiWordChar('A')); |
michael@0 | 558 | EXPECT_TRUE(IsAsciiWordChar('Z')); |
michael@0 | 559 | } |
michael@0 | 560 | |
michael@0 | 561 | TEST(IsAsciiWordCharTest, IsTrueForDigit) { |
michael@0 | 562 | EXPECT_TRUE(IsAsciiWordChar('0')); |
michael@0 | 563 | EXPECT_TRUE(IsAsciiWordChar('1')); |
michael@0 | 564 | EXPECT_TRUE(IsAsciiWordChar('7')); |
michael@0 | 565 | EXPECT_TRUE(IsAsciiWordChar('9')); |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | TEST(IsAsciiWordCharTest, IsTrueForUnderscore) { |
michael@0 | 569 | EXPECT_TRUE(IsAsciiWordChar('_')); |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | TEST(IsValidEscapeTest, IsFalseForNonPrintable) { |
michael@0 | 573 | EXPECT_FALSE(IsValidEscape('\0')); |
michael@0 | 574 | EXPECT_FALSE(IsValidEscape('\007')); |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | TEST(IsValidEscapeTest, IsFalseForDigit) { |
michael@0 | 578 | EXPECT_FALSE(IsValidEscape('0')); |
michael@0 | 579 | EXPECT_FALSE(IsValidEscape('9')); |
michael@0 | 580 | } |
michael@0 | 581 | |
michael@0 | 582 | TEST(IsValidEscapeTest, IsFalseForWhiteSpace) { |
michael@0 | 583 | EXPECT_FALSE(IsValidEscape(' ')); |
michael@0 | 584 | EXPECT_FALSE(IsValidEscape('\n')); |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | TEST(IsValidEscapeTest, IsFalseForSomeLetter) { |
michael@0 | 588 | EXPECT_FALSE(IsValidEscape('a')); |
michael@0 | 589 | EXPECT_FALSE(IsValidEscape('Z')); |
michael@0 | 590 | } |
michael@0 | 591 | |
michael@0 | 592 | TEST(IsValidEscapeTest, IsTrueForPunct) { |
michael@0 | 593 | EXPECT_TRUE(IsValidEscape('.')); |
michael@0 | 594 | EXPECT_TRUE(IsValidEscape('-')); |
michael@0 | 595 | EXPECT_TRUE(IsValidEscape('^')); |
michael@0 | 596 | EXPECT_TRUE(IsValidEscape('$')); |
michael@0 | 597 | EXPECT_TRUE(IsValidEscape('(')); |
michael@0 | 598 | EXPECT_TRUE(IsValidEscape(']')); |
michael@0 | 599 | EXPECT_TRUE(IsValidEscape('{')); |
michael@0 | 600 | EXPECT_TRUE(IsValidEscape('|')); |
michael@0 | 601 | } |
michael@0 | 602 | |
michael@0 | 603 | TEST(IsValidEscapeTest, IsTrueForSomeLetter) { |
michael@0 | 604 | EXPECT_TRUE(IsValidEscape('d')); |
michael@0 | 605 | EXPECT_TRUE(IsValidEscape('D')); |
michael@0 | 606 | EXPECT_TRUE(IsValidEscape('s')); |
michael@0 | 607 | EXPECT_TRUE(IsValidEscape('S')); |
michael@0 | 608 | EXPECT_TRUE(IsValidEscape('w')); |
michael@0 | 609 | EXPECT_TRUE(IsValidEscape('W')); |
michael@0 | 610 | } |
michael@0 | 611 | |
michael@0 | 612 | TEST(AtomMatchesCharTest, EscapedPunct) { |
michael@0 | 613 | EXPECT_FALSE(AtomMatchesChar(true, '\\', '\0')); |
michael@0 | 614 | EXPECT_FALSE(AtomMatchesChar(true, '\\', ' ')); |
michael@0 | 615 | EXPECT_FALSE(AtomMatchesChar(true, '_', '.')); |
michael@0 | 616 | EXPECT_FALSE(AtomMatchesChar(true, '.', 'a')); |
michael@0 | 617 | |
michael@0 | 618 | EXPECT_TRUE(AtomMatchesChar(true, '\\', '\\')); |
michael@0 | 619 | EXPECT_TRUE(AtomMatchesChar(true, '_', '_')); |
michael@0 | 620 | EXPECT_TRUE(AtomMatchesChar(true, '+', '+')); |
michael@0 | 621 | EXPECT_TRUE(AtomMatchesChar(true, '.', '.')); |
michael@0 | 622 | } |
michael@0 | 623 | |
michael@0 | 624 | TEST(AtomMatchesCharTest, Escaped_d) { |
michael@0 | 625 | EXPECT_FALSE(AtomMatchesChar(true, 'd', '\0')); |
michael@0 | 626 | EXPECT_FALSE(AtomMatchesChar(true, 'd', 'a')); |
michael@0 | 627 | EXPECT_FALSE(AtomMatchesChar(true, 'd', '.')); |
michael@0 | 628 | |
michael@0 | 629 | EXPECT_TRUE(AtomMatchesChar(true, 'd', '0')); |
michael@0 | 630 | EXPECT_TRUE(AtomMatchesChar(true, 'd', '9')); |
michael@0 | 631 | } |
michael@0 | 632 | |
michael@0 | 633 | TEST(AtomMatchesCharTest, Escaped_D) { |
michael@0 | 634 | EXPECT_FALSE(AtomMatchesChar(true, 'D', '0')); |
michael@0 | 635 | EXPECT_FALSE(AtomMatchesChar(true, 'D', '9')); |
michael@0 | 636 | |
michael@0 | 637 | EXPECT_TRUE(AtomMatchesChar(true, 'D', '\0')); |
michael@0 | 638 | EXPECT_TRUE(AtomMatchesChar(true, 'D', 'a')); |
michael@0 | 639 | EXPECT_TRUE(AtomMatchesChar(true, 'D', '-')); |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | TEST(AtomMatchesCharTest, Escaped_s) { |
michael@0 | 643 | EXPECT_FALSE(AtomMatchesChar(true, 's', '\0')); |
michael@0 | 644 | EXPECT_FALSE(AtomMatchesChar(true, 's', 'a')); |
michael@0 | 645 | EXPECT_FALSE(AtomMatchesChar(true, 's', '.')); |
michael@0 | 646 | EXPECT_FALSE(AtomMatchesChar(true, 's', '9')); |
michael@0 | 647 | |
michael@0 | 648 | EXPECT_TRUE(AtomMatchesChar(true, 's', ' ')); |
michael@0 | 649 | EXPECT_TRUE(AtomMatchesChar(true, 's', '\n')); |
michael@0 | 650 | EXPECT_TRUE(AtomMatchesChar(true, 's', '\t')); |
michael@0 | 651 | } |
michael@0 | 652 | |
michael@0 | 653 | TEST(AtomMatchesCharTest, Escaped_S) { |
michael@0 | 654 | EXPECT_FALSE(AtomMatchesChar(true, 'S', ' ')); |
michael@0 | 655 | EXPECT_FALSE(AtomMatchesChar(true, 'S', '\r')); |
michael@0 | 656 | |
michael@0 | 657 | EXPECT_TRUE(AtomMatchesChar(true, 'S', '\0')); |
michael@0 | 658 | EXPECT_TRUE(AtomMatchesChar(true, 'S', 'a')); |
michael@0 | 659 | EXPECT_TRUE(AtomMatchesChar(true, 'S', '9')); |
michael@0 | 660 | } |
michael@0 | 661 | |
michael@0 | 662 | TEST(AtomMatchesCharTest, Escaped_w) { |
michael@0 | 663 | EXPECT_FALSE(AtomMatchesChar(true, 'w', '\0')); |
michael@0 | 664 | EXPECT_FALSE(AtomMatchesChar(true, 'w', '+')); |
michael@0 | 665 | EXPECT_FALSE(AtomMatchesChar(true, 'w', ' ')); |
michael@0 | 666 | EXPECT_FALSE(AtomMatchesChar(true, 'w', '\n')); |
michael@0 | 667 | |
michael@0 | 668 | EXPECT_TRUE(AtomMatchesChar(true, 'w', '0')); |
michael@0 | 669 | EXPECT_TRUE(AtomMatchesChar(true, 'w', 'b')); |
michael@0 | 670 | EXPECT_TRUE(AtomMatchesChar(true, 'w', 'C')); |
michael@0 | 671 | EXPECT_TRUE(AtomMatchesChar(true, 'w', '_')); |
michael@0 | 672 | } |
michael@0 | 673 | |
michael@0 | 674 | TEST(AtomMatchesCharTest, Escaped_W) { |
michael@0 | 675 | EXPECT_FALSE(AtomMatchesChar(true, 'W', 'A')); |
michael@0 | 676 | EXPECT_FALSE(AtomMatchesChar(true, 'W', 'b')); |
michael@0 | 677 | EXPECT_FALSE(AtomMatchesChar(true, 'W', '9')); |
michael@0 | 678 | EXPECT_FALSE(AtomMatchesChar(true, 'W', '_')); |
michael@0 | 679 | |
michael@0 | 680 | EXPECT_TRUE(AtomMatchesChar(true, 'W', '\0')); |
michael@0 | 681 | EXPECT_TRUE(AtomMatchesChar(true, 'W', '*')); |
michael@0 | 682 | EXPECT_TRUE(AtomMatchesChar(true, 'W', '\n')); |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | TEST(AtomMatchesCharTest, EscapedWhiteSpace) { |
michael@0 | 686 | EXPECT_FALSE(AtomMatchesChar(true, 'f', '\0')); |
michael@0 | 687 | EXPECT_FALSE(AtomMatchesChar(true, 'f', '\n')); |
michael@0 | 688 | EXPECT_FALSE(AtomMatchesChar(true, 'n', '\0')); |
michael@0 | 689 | EXPECT_FALSE(AtomMatchesChar(true, 'n', '\r')); |
michael@0 | 690 | EXPECT_FALSE(AtomMatchesChar(true, 'r', '\0')); |
michael@0 | 691 | EXPECT_FALSE(AtomMatchesChar(true, 'r', 'a')); |
michael@0 | 692 | EXPECT_FALSE(AtomMatchesChar(true, 't', '\0')); |
michael@0 | 693 | EXPECT_FALSE(AtomMatchesChar(true, 't', 't')); |
michael@0 | 694 | EXPECT_FALSE(AtomMatchesChar(true, 'v', '\0')); |
michael@0 | 695 | EXPECT_FALSE(AtomMatchesChar(true, 'v', '\f')); |
michael@0 | 696 | |
michael@0 | 697 | EXPECT_TRUE(AtomMatchesChar(true, 'f', '\f')); |
michael@0 | 698 | EXPECT_TRUE(AtomMatchesChar(true, 'n', '\n')); |
michael@0 | 699 | EXPECT_TRUE(AtomMatchesChar(true, 'r', '\r')); |
michael@0 | 700 | EXPECT_TRUE(AtomMatchesChar(true, 't', '\t')); |
michael@0 | 701 | EXPECT_TRUE(AtomMatchesChar(true, 'v', '\v')); |
michael@0 | 702 | } |
michael@0 | 703 | |
michael@0 | 704 | TEST(AtomMatchesCharTest, UnescapedDot) { |
michael@0 | 705 | EXPECT_FALSE(AtomMatchesChar(false, '.', '\n')); |
michael@0 | 706 | |
michael@0 | 707 | EXPECT_TRUE(AtomMatchesChar(false, '.', '\0')); |
michael@0 | 708 | EXPECT_TRUE(AtomMatchesChar(false, '.', '.')); |
michael@0 | 709 | EXPECT_TRUE(AtomMatchesChar(false, '.', 'a')); |
michael@0 | 710 | EXPECT_TRUE(AtomMatchesChar(false, '.', ' ')); |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | TEST(AtomMatchesCharTest, UnescapedChar) { |
michael@0 | 714 | EXPECT_FALSE(AtomMatchesChar(false, 'a', '\0')); |
michael@0 | 715 | EXPECT_FALSE(AtomMatchesChar(false, 'a', 'b')); |
michael@0 | 716 | EXPECT_FALSE(AtomMatchesChar(false, '$', 'a')); |
michael@0 | 717 | |
michael@0 | 718 | EXPECT_TRUE(AtomMatchesChar(false, '$', '$')); |
michael@0 | 719 | EXPECT_TRUE(AtomMatchesChar(false, '5', '5')); |
michael@0 | 720 | EXPECT_TRUE(AtomMatchesChar(false, 'Z', 'Z')); |
michael@0 | 721 | } |
michael@0 | 722 | |
michael@0 | 723 | TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) { |
michael@0 | 724 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)), |
michael@0 | 725 | "NULL is not a valid simple regular expression"); |
michael@0 | 726 | EXPECT_NONFATAL_FAILURE( |
michael@0 | 727 | ASSERT_FALSE(ValidateRegex("a\\")), |
michael@0 | 728 | "Syntax error at index 1 in simple regular expression \"a\\\": "); |
michael@0 | 729 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a\\")), |
michael@0 | 730 | "'\\' cannot appear at the end"); |
michael@0 | 731 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\n\\")), |
michael@0 | 732 | "'\\' cannot appear at the end"); |
michael@0 | 733 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\s\\hb")), |
michael@0 | 734 | "invalid escape sequence \"\\h\""); |
michael@0 | 735 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^^")), |
michael@0 | 736 | "'^' can only appear at the beginning"); |
michael@0 | 737 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(".*^b")), |
michael@0 | 738 | "'^' can only appear at the beginning"); |
michael@0 | 739 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("$$")), |
michael@0 | 740 | "'$' can only appear at the end"); |
michael@0 | 741 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^$a")), |
michael@0 | 742 | "'$' can only appear at the end"); |
michael@0 | 743 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a(b")), |
michael@0 | 744 | "'(' is unsupported"); |
michael@0 | 745 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("ab)")), |
michael@0 | 746 | "')' is unsupported"); |
michael@0 | 747 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("[ab")), |
michael@0 | 748 | "'[' is unsupported"); |
michael@0 | 749 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a{2")), |
michael@0 | 750 | "'{' is unsupported"); |
michael@0 | 751 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("?")), |
michael@0 | 752 | "'?' can only follow a repeatable token"); |
michael@0 | 753 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^*")), |
michael@0 | 754 | "'*' can only follow a repeatable token"); |
michael@0 | 755 | EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("5*+")), |
michael@0 | 756 | "'+' can only follow a repeatable token"); |
michael@0 | 757 | } |
michael@0 | 758 | |
michael@0 | 759 | TEST(ValidateRegexTest, ReturnsTrueForValid) { |
michael@0 | 760 | EXPECT_TRUE(ValidateRegex("")); |
michael@0 | 761 | EXPECT_TRUE(ValidateRegex("a")); |
michael@0 | 762 | EXPECT_TRUE(ValidateRegex(".*")); |
michael@0 | 763 | EXPECT_TRUE(ValidateRegex("^a_+")); |
michael@0 | 764 | EXPECT_TRUE(ValidateRegex("^a\\t\\&?")); |
michael@0 | 765 | EXPECT_TRUE(ValidateRegex("09*$")); |
michael@0 | 766 | EXPECT_TRUE(ValidateRegex("^Z$")); |
michael@0 | 767 | EXPECT_TRUE(ValidateRegex("a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}")); |
michael@0 | 768 | } |
michael@0 | 769 | |
michael@0 | 770 | TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) { |
michael@0 | 771 | EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "a", "ba")); |
michael@0 | 772 | // Repeating more than once. |
michael@0 | 773 | EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "aab")); |
michael@0 | 774 | |
michael@0 | 775 | // Repeating zero times. |
michael@0 | 776 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ba")); |
michael@0 | 777 | // Repeating once. |
michael@0 | 778 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ab")); |
michael@0 | 779 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '#', '?', ".", "##")); |
michael@0 | 780 | } |
michael@0 | 781 | |
michael@0 | 782 | TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) { |
michael@0 | 783 | EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '*', "a$", "baab")); |
michael@0 | 784 | |
michael@0 | 785 | // Repeating zero times. |
michael@0 | 786 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "bc")); |
michael@0 | 787 | // Repeating once. |
michael@0 | 788 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "abc")); |
michael@0 | 789 | // Repeating more than once. |
michael@0 | 790 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '*', "-", "ab_1-g")); |
michael@0 | 791 | } |
michael@0 | 792 | |
michael@0 | 793 | TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) { |
michael@0 | 794 | EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "a$", "baab")); |
michael@0 | 795 | // Repeating zero times. |
michael@0 | 796 | EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "bc")); |
michael@0 | 797 | |
michael@0 | 798 | // Repeating once. |
michael@0 | 799 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "abc")); |
michael@0 | 800 | // Repeating more than once. |
michael@0 | 801 | EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '+', "-", "ab_1-g")); |
michael@0 | 802 | } |
michael@0 | 803 | |
michael@0 | 804 | TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) { |
michael@0 | 805 | EXPECT_TRUE(MatchRegexAtHead("", "")); |
michael@0 | 806 | EXPECT_TRUE(MatchRegexAtHead("", "ab")); |
michael@0 | 807 | } |
michael@0 | 808 | |
michael@0 | 809 | TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) { |
michael@0 | 810 | EXPECT_FALSE(MatchRegexAtHead("$", "a")); |
michael@0 | 811 | |
michael@0 | 812 | EXPECT_TRUE(MatchRegexAtHead("$", "")); |
michael@0 | 813 | EXPECT_TRUE(MatchRegexAtHead("a$", "a")); |
michael@0 | 814 | } |
michael@0 | 815 | |
michael@0 | 816 | TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) { |
michael@0 | 817 | EXPECT_FALSE(MatchRegexAtHead("\\w", "+")); |
michael@0 | 818 | EXPECT_FALSE(MatchRegexAtHead("\\W", "ab")); |
michael@0 | 819 | |
michael@0 | 820 | EXPECT_TRUE(MatchRegexAtHead("\\sa", "\nab")); |
michael@0 | 821 | EXPECT_TRUE(MatchRegexAtHead("\\d", "1a")); |
michael@0 | 822 | } |
michael@0 | 823 | |
michael@0 | 824 | TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) { |
michael@0 | 825 | EXPECT_FALSE(MatchRegexAtHead(".+a", "abc")); |
michael@0 | 826 | EXPECT_FALSE(MatchRegexAtHead("a?b", "aab")); |
michael@0 | 827 | |
michael@0 | 828 | EXPECT_TRUE(MatchRegexAtHead(".*a", "bc12-ab")); |
michael@0 | 829 | EXPECT_TRUE(MatchRegexAtHead("a?b", "b")); |
michael@0 | 830 | EXPECT_TRUE(MatchRegexAtHead("a?b", "ab")); |
michael@0 | 831 | } |
michael@0 | 832 | |
michael@0 | 833 | TEST(MatchRegexAtHeadTest, |
michael@0 | 834 | WorksWhenRegexStartsWithRepetionOfEscapeSequence) { |
michael@0 | 835 | EXPECT_FALSE(MatchRegexAtHead("\\.+a", "abc")); |
michael@0 | 836 | EXPECT_FALSE(MatchRegexAtHead("\\s?b", " b")); |
michael@0 | 837 | |
michael@0 | 838 | EXPECT_TRUE(MatchRegexAtHead("\\(*a", "((((ab")); |
michael@0 | 839 | EXPECT_TRUE(MatchRegexAtHead("\\^?b", "^b")); |
michael@0 | 840 | EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "b")); |
michael@0 | 841 | EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "\\b")); |
michael@0 | 842 | } |
michael@0 | 843 | |
michael@0 | 844 | TEST(MatchRegexAtHeadTest, MatchesSequentially) { |
michael@0 | 845 | EXPECT_FALSE(MatchRegexAtHead("ab.*c", "acabc")); |
michael@0 | 846 | |
michael@0 | 847 | EXPECT_TRUE(MatchRegexAtHead("ab.*c", "ab-fsc")); |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) { |
michael@0 | 851 | EXPECT_FALSE(MatchRegexAnywhere("", NULL)); |
michael@0 | 852 | } |
michael@0 | 853 | |
michael@0 | 854 | TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) { |
michael@0 | 855 | EXPECT_FALSE(MatchRegexAnywhere("^a", "ba")); |
michael@0 | 856 | EXPECT_FALSE(MatchRegexAnywhere("^$", "a")); |
michael@0 | 857 | |
michael@0 | 858 | EXPECT_TRUE(MatchRegexAnywhere("^a", "ab")); |
michael@0 | 859 | EXPECT_TRUE(MatchRegexAnywhere("^", "ab")); |
michael@0 | 860 | EXPECT_TRUE(MatchRegexAnywhere("^$", "")); |
michael@0 | 861 | } |
michael@0 | 862 | |
michael@0 | 863 | TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) { |
michael@0 | 864 | EXPECT_FALSE(MatchRegexAnywhere("a", "bcde123")); |
michael@0 | 865 | EXPECT_FALSE(MatchRegexAnywhere("a.+a", "--aa88888888")); |
michael@0 | 866 | } |
michael@0 | 867 | |
michael@0 | 868 | TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) { |
michael@0 | 869 | EXPECT_TRUE(MatchRegexAnywhere("\\w+", "ab1_ - 5")); |
michael@0 | 870 | EXPECT_TRUE(MatchRegexAnywhere(".*=", "=")); |
michael@0 | 871 | EXPECT_TRUE(MatchRegexAnywhere("x.*ab?.*bc", "xaaabc")); |
michael@0 | 872 | } |
michael@0 | 873 | |
michael@0 | 874 | TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) { |
michael@0 | 875 | EXPECT_TRUE(MatchRegexAnywhere("\\w+", "$$$ ab1_ - 5")); |
michael@0 | 876 | EXPECT_TRUE(MatchRegexAnywhere("\\.+=", "= ...=")); |
michael@0 | 877 | } |
michael@0 | 878 | |
michael@0 | 879 | // Tests RE's implicit constructors. |
michael@0 | 880 | TEST(RETest, ImplicitConstructorWorks) { |
michael@0 | 881 | const RE empty(""); |
michael@0 | 882 | EXPECT_STREQ("", empty.pattern()); |
michael@0 | 883 | |
michael@0 | 884 | const RE simple("hello"); |
michael@0 | 885 | EXPECT_STREQ("hello", simple.pattern()); |
michael@0 | 886 | } |
michael@0 | 887 | |
michael@0 | 888 | // Tests that RE's constructors reject invalid regular expressions. |
michael@0 | 889 | TEST(RETest, RejectsInvalidRegex) { |
michael@0 | 890 | EXPECT_NONFATAL_FAILURE({ |
michael@0 | 891 | const RE normal(NULL); |
michael@0 | 892 | }, "NULL is not a valid simple regular expression"); |
michael@0 | 893 | |
michael@0 | 894 | EXPECT_NONFATAL_FAILURE({ |
michael@0 | 895 | const RE normal(".*(\\w+"); |
michael@0 | 896 | }, "'(' is unsupported"); |
michael@0 | 897 | |
michael@0 | 898 | EXPECT_NONFATAL_FAILURE({ |
michael@0 | 899 | const RE invalid("^?"); |
michael@0 | 900 | }, "'?' can only follow a repeatable token"); |
michael@0 | 901 | } |
michael@0 | 902 | |
michael@0 | 903 | // Tests RE::FullMatch(). |
michael@0 | 904 | TEST(RETest, FullMatchWorks) { |
michael@0 | 905 | const RE empty(""); |
michael@0 | 906 | EXPECT_TRUE(RE::FullMatch("", empty)); |
michael@0 | 907 | EXPECT_FALSE(RE::FullMatch("a", empty)); |
michael@0 | 908 | |
michael@0 | 909 | const RE re1("a"); |
michael@0 | 910 | EXPECT_TRUE(RE::FullMatch("a", re1)); |
michael@0 | 911 | |
michael@0 | 912 | const RE re("a.*z"); |
michael@0 | 913 | EXPECT_TRUE(RE::FullMatch("az", re)); |
michael@0 | 914 | EXPECT_TRUE(RE::FullMatch("axyz", re)); |
michael@0 | 915 | EXPECT_FALSE(RE::FullMatch("baz", re)); |
michael@0 | 916 | EXPECT_FALSE(RE::FullMatch("azy", re)); |
michael@0 | 917 | } |
michael@0 | 918 | |
michael@0 | 919 | // Tests RE::PartialMatch(). |
michael@0 | 920 | TEST(RETest, PartialMatchWorks) { |
michael@0 | 921 | const RE empty(""); |
michael@0 | 922 | EXPECT_TRUE(RE::PartialMatch("", empty)); |
michael@0 | 923 | EXPECT_TRUE(RE::PartialMatch("a", empty)); |
michael@0 | 924 | |
michael@0 | 925 | const RE re("a.*z"); |
michael@0 | 926 | EXPECT_TRUE(RE::PartialMatch("az", re)); |
michael@0 | 927 | EXPECT_TRUE(RE::PartialMatch("axyz", re)); |
michael@0 | 928 | EXPECT_TRUE(RE::PartialMatch("baz", re)); |
michael@0 | 929 | EXPECT_TRUE(RE::PartialMatch("azy", re)); |
michael@0 | 930 | EXPECT_FALSE(RE::PartialMatch("zza", re)); |
michael@0 | 931 | } |
michael@0 | 932 | |
michael@0 | 933 | #endif // GTEST_USES_POSIX_RE |
michael@0 | 934 | |
michael@0 | 935 | #if !GTEST_OS_WINDOWS_MOBILE |
michael@0 | 936 | |
michael@0 | 937 | TEST(CaptureTest, CapturesStdout) { |
michael@0 | 938 | CaptureStdout(); |
michael@0 | 939 | fprintf(stdout, "abc"); |
michael@0 | 940 | EXPECT_STREQ("abc", GetCapturedStdout().c_str()); |
michael@0 | 941 | |
michael@0 | 942 | CaptureStdout(); |
michael@0 | 943 | fprintf(stdout, "def%cghi", '\0'); |
michael@0 | 944 | EXPECT_EQ(::std::string("def\0ghi", 7), ::std::string(GetCapturedStdout())); |
michael@0 | 945 | } |
michael@0 | 946 | |
michael@0 | 947 | TEST(CaptureTest, CapturesStderr) { |
michael@0 | 948 | CaptureStderr(); |
michael@0 | 949 | fprintf(stderr, "jkl"); |
michael@0 | 950 | EXPECT_STREQ("jkl", GetCapturedStderr().c_str()); |
michael@0 | 951 | |
michael@0 | 952 | CaptureStderr(); |
michael@0 | 953 | fprintf(stderr, "jkl%cmno", '\0'); |
michael@0 | 954 | EXPECT_EQ(::std::string("jkl\0mno", 7), ::std::string(GetCapturedStderr())); |
michael@0 | 955 | } |
michael@0 | 956 | |
michael@0 | 957 | // Tests that stdout and stderr capture don't interfere with each other. |
michael@0 | 958 | TEST(CaptureTest, CapturesStdoutAndStderr) { |
michael@0 | 959 | CaptureStdout(); |
michael@0 | 960 | CaptureStderr(); |
michael@0 | 961 | fprintf(stdout, "pqr"); |
michael@0 | 962 | fprintf(stderr, "stu"); |
michael@0 | 963 | EXPECT_STREQ("pqr", GetCapturedStdout().c_str()); |
michael@0 | 964 | EXPECT_STREQ("stu", GetCapturedStderr().c_str()); |
michael@0 | 965 | } |
michael@0 | 966 | |
michael@0 | 967 | TEST(CaptureDeathTest, CannotReenterStdoutCapture) { |
michael@0 | 968 | CaptureStdout(); |
michael@0 | 969 | EXPECT_DEATH_IF_SUPPORTED(CaptureStdout(), |
michael@0 | 970 | "Only one stdout capturer can exist at a time"); |
michael@0 | 971 | GetCapturedStdout(); |
michael@0 | 972 | |
michael@0 | 973 | // We cannot test stderr capturing using death tests as they use it |
michael@0 | 974 | // themselves. |
michael@0 | 975 | } |
michael@0 | 976 | |
michael@0 | 977 | #endif // !GTEST_OS_WINDOWS_MOBILE |
michael@0 | 978 | |
michael@0 | 979 | TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) { |
michael@0 | 980 | ThreadLocal<int> t1; |
michael@0 | 981 | EXPECT_EQ(0, t1.get()); |
michael@0 | 982 | |
michael@0 | 983 | ThreadLocal<void*> t2; |
michael@0 | 984 | EXPECT_TRUE(t2.get() == NULL); |
michael@0 | 985 | } |
michael@0 | 986 | |
michael@0 | 987 | TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) { |
michael@0 | 988 | ThreadLocal<int> t1(123); |
michael@0 | 989 | EXPECT_EQ(123, t1.get()); |
michael@0 | 990 | |
michael@0 | 991 | int i = 0; |
michael@0 | 992 | ThreadLocal<int*> t2(&i); |
michael@0 | 993 | EXPECT_EQ(&i, t2.get()); |
michael@0 | 994 | } |
michael@0 | 995 | |
michael@0 | 996 | class NoDefaultContructor { |
michael@0 | 997 | public: |
michael@0 | 998 | explicit NoDefaultContructor(const char*) {} |
michael@0 | 999 | NoDefaultContructor(const NoDefaultContructor&) {} |
michael@0 | 1000 | }; |
michael@0 | 1001 | |
michael@0 | 1002 | TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) { |
michael@0 | 1003 | ThreadLocal<NoDefaultContructor> bar(NoDefaultContructor("foo")); |
michael@0 | 1004 | bar.pointer(); |
michael@0 | 1005 | } |
michael@0 | 1006 | |
michael@0 | 1007 | TEST(ThreadLocalTest, GetAndPointerReturnSameValue) { |
michael@0 | 1008 | ThreadLocal<String> thread_local_string; |
michael@0 | 1009 | |
michael@0 | 1010 | EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get())); |
michael@0 | 1011 | |
michael@0 | 1012 | // Verifies the condition still holds after calling set. |
michael@0 | 1013 | thread_local_string.set("foo"); |
michael@0 | 1014 | EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get())); |
michael@0 | 1015 | } |
michael@0 | 1016 | |
michael@0 | 1017 | TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) { |
michael@0 | 1018 | ThreadLocal<String> thread_local_string; |
michael@0 | 1019 | const ThreadLocal<String>& const_thread_local_string = thread_local_string; |
michael@0 | 1020 | |
michael@0 | 1021 | EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer()); |
michael@0 | 1022 | |
michael@0 | 1023 | thread_local_string.set("foo"); |
michael@0 | 1024 | EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer()); |
michael@0 | 1025 | } |
michael@0 | 1026 | |
michael@0 | 1027 | #if GTEST_IS_THREADSAFE |
michael@0 | 1028 | |
michael@0 | 1029 | void AddTwo(int* param) { *param += 2; } |
michael@0 | 1030 | |
michael@0 | 1031 | TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) { |
michael@0 | 1032 | int i = 40; |
michael@0 | 1033 | ThreadWithParam<int*> thread(&AddTwo, &i, NULL); |
michael@0 | 1034 | thread.Join(); |
michael@0 | 1035 | EXPECT_EQ(42, i); |
michael@0 | 1036 | } |
michael@0 | 1037 | |
michael@0 | 1038 | TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) { |
michael@0 | 1039 | // AssertHeld() is flaky only in the presence of multiple threads accessing |
michael@0 | 1040 | // the lock. In this case, the test is robust. |
michael@0 | 1041 | EXPECT_DEATH_IF_SUPPORTED({ |
michael@0 | 1042 | Mutex m; |
michael@0 | 1043 | { MutexLock lock(&m); } |
michael@0 | 1044 | m.AssertHeld(); |
michael@0 | 1045 | }, |
michael@0 | 1046 | "thread .*hold"); |
michael@0 | 1047 | } |
michael@0 | 1048 | |
michael@0 | 1049 | TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) { |
michael@0 | 1050 | Mutex m; |
michael@0 | 1051 | MutexLock lock(&m); |
michael@0 | 1052 | m.AssertHeld(); |
michael@0 | 1053 | } |
michael@0 | 1054 | |
michael@0 | 1055 | class AtomicCounterWithMutex { |
michael@0 | 1056 | public: |
michael@0 | 1057 | explicit AtomicCounterWithMutex(Mutex* mutex) : |
michael@0 | 1058 | value_(0), mutex_(mutex), random_(42) {} |
michael@0 | 1059 | |
michael@0 | 1060 | void Increment() { |
michael@0 | 1061 | MutexLock lock(mutex_); |
michael@0 | 1062 | int temp = value_; |
michael@0 | 1063 | { |
michael@0 | 1064 | // Locking a mutex puts up a memory barrier, preventing reads and |
michael@0 | 1065 | // writes to value_ rearranged when observed from other threads. |
michael@0 | 1066 | // |
michael@0 | 1067 | // We cannot use Mutex and MutexLock here or rely on their memory |
michael@0 | 1068 | // barrier functionality as we are testing them here. |
michael@0 | 1069 | pthread_mutex_t memory_barrier_mutex; |
michael@0 | 1070 | GTEST_CHECK_POSIX_SUCCESS_( |
michael@0 | 1071 | pthread_mutex_init(&memory_barrier_mutex, NULL)); |
michael@0 | 1072 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&memory_barrier_mutex)); |
michael@0 | 1073 | |
michael@0 | 1074 | SleepMilliseconds(random_.Generate(30)); |
michael@0 | 1075 | |
michael@0 | 1076 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&memory_barrier_mutex)); |
michael@0 | 1077 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&memory_barrier_mutex)); |
michael@0 | 1078 | } |
michael@0 | 1079 | value_ = temp + 1; |
michael@0 | 1080 | } |
michael@0 | 1081 | int value() const { return value_; } |
michael@0 | 1082 | |
michael@0 | 1083 | private: |
michael@0 | 1084 | volatile int value_; |
michael@0 | 1085 | Mutex* const mutex_; // Protects value_. |
michael@0 | 1086 | Random random_; |
michael@0 | 1087 | }; |
michael@0 | 1088 | |
michael@0 | 1089 | void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) { |
michael@0 | 1090 | for (int i = 0; i < param.second; ++i) |
michael@0 | 1091 | param.first->Increment(); |
michael@0 | 1092 | } |
michael@0 | 1093 | |
michael@0 | 1094 | // Tests that the mutex only lets one thread at a time to lock it. |
michael@0 | 1095 | TEST(MutexTest, OnlyOneThreadCanLockAtATime) { |
michael@0 | 1096 | Mutex mutex; |
michael@0 | 1097 | AtomicCounterWithMutex locked_counter(&mutex); |
michael@0 | 1098 | |
michael@0 | 1099 | typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType; |
michael@0 | 1100 | const int kCycleCount = 20; |
michael@0 | 1101 | const int kThreadCount = 7; |
michael@0 | 1102 | scoped_ptr<ThreadType> counting_threads[kThreadCount]; |
michael@0 | 1103 | Notification threads_can_start; |
michael@0 | 1104 | // Creates and runs kThreadCount threads that increment locked_counter |
michael@0 | 1105 | // kCycleCount times each. |
michael@0 | 1106 | for (int i = 0; i < kThreadCount; ++i) { |
michael@0 | 1107 | counting_threads[i].reset(new ThreadType(&CountingThreadFunc, |
michael@0 | 1108 | make_pair(&locked_counter, |
michael@0 | 1109 | kCycleCount), |
michael@0 | 1110 | &threads_can_start)); |
michael@0 | 1111 | } |
michael@0 | 1112 | threads_can_start.Notify(); |
michael@0 | 1113 | for (int i = 0; i < kThreadCount; ++i) |
michael@0 | 1114 | counting_threads[i]->Join(); |
michael@0 | 1115 | |
michael@0 | 1116 | // If the mutex lets more than one thread to increment the counter at a |
michael@0 | 1117 | // time, they are likely to encounter a race condition and have some |
michael@0 | 1118 | // increments overwritten, resulting in the lower then expected counter |
michael@0 | 1119 | // value. |
michael@0 | 1120 | EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value()); |
michael@0 | 1121 | } |
michael@0 | 1122 | |
michael@0 | 1123 | template <typename T> |
michael@0 | 1124 | void RunFromThread(void (func)(T), T param) { |
michael@0 | 1125 | ThreadWithParam<T> thread(func, param, NULL); |
michael@0 | 1126 | thread.Join(); |
michael@0 | 1127 | } |
michael@0 | 1128 | |
michael@0 | 1129 | void RetrieveThreadLocalValue(pair<ThreadLocal<String>*, String*> param) { |
michael@0 | 1130 | *param.second = param.first->get(); |
michael@0 | 1131 | } |
michael@0 | 1132 | |
michael@0 | 1133 | TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) { |
michael@0 | 1134 | ThreadLocal<String> thread_local_string("foo"); |
michael@0 | 1135 | EXPECT_STREQ("foo", thread_local_string.get().c_str()); |
michael@0 | 1136 | |
michael@0 | 1137 | thread_local_string.set("bar"); |
michael@0 | 1138 | EXPECT_STREQ("bar", thread_local_string.get().c_str()); |
michael@0 | 1139 | |
michael@0 | 1140 | String result; |
michael@0 | 1141 | RunFromThread(&RetrieveThreadLocalValue, |
michael@0 | 1142 | make_pair(&thread_local_string, &result)); |
michael@0 | 1143 | EXPECT_STREQ("foo", result.c_str()); |
michael@0 | 1144 | } |
michael@0 | 1145 | |
michael@0 | 1146 | // DestructorTracker keeps track of whether its instances have been |
michael@0 | 1147 | // destroyed. |
michael@0 | 1148 | static std::vector<bool> g_destroyed; |
michael@0 | 1149 | |
michael@0 | 1150 | class DestructorTracker { |
michael@0 | 1151 | public: |
michael@0 | 1152 | DestructorTracker() : index_(GetNewIndex()) {} |
michael@0 | 1153 | DestructorTracker(const DestructorTracker& /* rhs */) |
michael@0 | 1154 | : index_(GetNewIndex()) {} |
michael@0 | 1155 | ~DestructorTracker() { |
michael@0 | 1156 | // We never access g_destroyed concurrently, so we don't need to |
michael@0 | 1157 | // protect the write operation under a mutex. |
michael@0 | 1158 | g_destroyed[index_] = true; |
michael@0 | 1159 | } |
michael@0 | 1160 | |
michael@0 | 1161 | private: |
michael@0 | 1162 | static int GetNewIndex() { |
michael@0 | 1163 | g_destroyed.push_back(false); |
michael@0 | 1164 | return g_destroyed.size() - 1; |
michael@0 | 1165 | } |
michael@0 | 1166 | const int index_; |
michael@0 | 1167 | }; |
michael@0 | 1168 | |
michael@0 | 1169 | typedef ThreadLocal<DestructorTracker>* ThreadParam; |
michael@0 | 1170 | |
michael@0 | 1171 | void CallThreadLocalGet(ThreadParam thread_local_param) { |
michael@0 | 1172 | thread_local_param->get(); |
michael@0 | 1173 | } |
michael@0 | 1174 | |
michael@0 | 1175 | // Tests that when a ThreadLocal object dies in a thread, it destroys |
michael@0 | 1176 | // the managed object for that thread. |
michael@0 | 1177 | TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) { |
michael@0 | 1178 | g_destroyed.clear(); |
michael@0 | 1179 | |
michael@0 | 1180 | { |
michael@0 | 1181 | // The next line default constructs a DestructorTracker object as |
michael@0 | 1182 | // the default value of objects managed by thread_local_tracker. |
michael@0 | 1183 | ThreadLocal<DestructorTracker> thread_local_tracker; |
michael@0 | 1184 | ASSERT_EQ(1U, g_destroyed.size()); |
michael@0 | 1185 | ASSERT_FALSE(g_destroyed[0]); |
michael@0 | 1186 | |
michael@0 | 1187 | // This creates another DestructorTracker object for the main thread. |
michael@0 | 1188 | thread_local_tracker.get(); |
michael@0 | 1189 | ASSERT_EQ(2U, g_destroyed.size()); |
michael@0 | 1190 | ASSERT_FALSE(g_destroyed[0]); |
michael@0 | 1191 | ASSERT_FALSE(g_destroyed[1]); |
michael@0 | 1192 | } |
michael@0 | 1193 | |
michael@0 | 1194 | // Now thread_local_tracker has died. It should have destroyed both the |
michael@0 | 1195 | // default value shared by all threads and the value for the main |
michael@0 | 1196 | // thread. |
michael@0 | 1197 | ASSERT_EQ(2U, g_destroyed.size()); |
michael@0 | 1198 | EXPECT_TRUE(g_destroyed[0]); |
michael@0 | 1199 | EXPECT_TRUE(g_destroyed[1]); |
michael@0 | 1200 | |
michael@0 | 1201 | g_destroyed.clear(); |
michael@0 | 1202 | } |
michael@0 | 1203 | |
michael@0 | 1204 | // Tests that when a thread exits, the thread-local object for that |
michael@0 | 1205 | // thread is destroyed. |
michael@0 | 1206 | TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) { |
michael@0 | 1207 | g_destroyed.clear(); |
michael@0 | 1208 | |
michael@0 | 1209 | { |
michael@0 | 1210 | // The next line default constructs a DestructorTracker object as |
michael@0 | 1211 | // the default value of objects managed by thread_local_tracker. |
michael@0 | 1212 | ThreadLocal<DestructorTracker> thread_local_tracker; |
michael@0 | 1213 | ASSERT_EQ(1U, g_destroyed.size()); |
michael@0 | 1214 | ASSERT_FALSE(g_destroyed[0]); |
michael@0 | 1215 | |
michael@0 | 1216 | // This creates another DestructorTracker object in the new thread. |
michael@0 | 1217 | ThreadWithParam<ThreadParam> thread( |
michael@0 | 1218 | &CallThreadLocalGet, &thread_local_tracker, NULL); |
michael@0 | 1219 | thread.Join(); |
michael@0 | 1220 | |
michael@0 | 1221 | // Now the new thread has exited. The per-thread object for it |
michael@0 | 1222 | // should have been destroyed. |
michael@0 | 1223 | ASSERT_EQ(2U, g_destroyed.size()); |
michael@0 | 1224 | ASSERT_FALSE(g_destroyed[0]); |
michael@0 | 1225 | ASSERT_TRUE(g_destroyed[1]); |
michael@0 | 1226 | } |
michael@0 | 1227 | |
michael@0 | 1228 | // Now thread_local_tracker has died. The default value should have been |
michael@0 | 1229 | // destroyed too. |
michael@0 | 1230 | ASSERT_EQ(2U, g_destroyed.size()); |
michael@0 | 1231 | EXPECT_TRUE(g_destroyed[0]); |
michael@0 | 1232 | EXPECT_TRUE(g_destroyed[1]); |
michael@0 | 1233 | |
michael@0 | 1234 | g_destroyed.clear(); |
michael@0 | 1235 | } |
michael@0 | 1236 | |
michael@0 | 1237 | TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) { |
michael@0 | 1238 | ThreadLocal<String> thread_local_string; |
michael@0 | 1239 | thread_local_string.set("Foo"); |
michael@0 | 1240 | EXPECT_STREQ("Foo", thread_local_string.get().c_str()); |
michael@0 | 1241 | |
michael@0 | 1242 | String result; |
michael@0 | 1243 | RunFromThread(&RetrieveThreadLocalValue, |
michael@0 | 1244 | make_pair(&thread_local_string, &result)); |
michael@0 | 1245 | EXPECT_TRUE(result.c_str() == NULL); |
michael@0 | 1246 | } |
michael@0 | 1247 | |
michael@0 | 1248 | #endif // GTEST_IS_THREADSAFE |
michael@0 | 1249 | |
michael@0 | 1250 | } // namespace internal |
michael@0 | 1251 | } // namespace testing |