media/webrtc/trunk/testing/gtest/test/gtest-port_test.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-port_test.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1251 @@
     1.4 +// Copyright 2008, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +//
    1.33 +// Authors: vladl@google.com (Vlad Losev), wan@google.com (Zhanyong Wan)
    1.34 +//
    1.35 +// This file tests the internal cross-platform support utilities.
    1.36 +
    1.37 +#include "gtest/internal/gtest-port.h"
    1.38 +
    1.39 +#include <stdio.h>
    1.40 +
    1.41 +#if GTEST_OS_MAC
    1.42 +# include <time.h>
    1.43 +#endif  // GTEST_OS_MAC
    1.44 +
    1.45 +#include <list>
    1.46 +#include <utility>  // For std::pair and std::make_pair.
    1.47 +#include <vector>
    1.48 +
    1.49 +#include "gtest/gtest.h"
    1.50 +#include "gtest/gtest-spi.h"
    1.51 +
    1.52 +// Indicates that this translation unit is part of Google Test's
    1.53 +// implementation.  It must come before gtest-internal-inl.h is
    1.54 +// included, or there will be a compiler error.  This trick is to
    1.55 +// prevent a user from accidentally including gtest-internal-inl.h in
    1.56 +// his code.
    1.57 +#define GTEST_IMPLEMENTATION_ 1
    1.58 +#include "src/gtest-internal-inl.h"
    1.59 +#undef GTEST_IMPLEMENTATION_
    1.60 +
    1.61 +using std::make_pair;
    1.62 +using std::pair;
    1.63 +
    1.64 +namespace testing {
    1.65 +namespace internal {
    1.66 +
    1.67 +TEST(IsXDigitTest, WorksForNarrowAscii) {
    1.68 +  EXPECT_TRUE(IsXDigit('0'));
    1.69 +  EXPECT_TRUE(IsXDigit('9'));
    1.70 +  EXPECT_TRUE(IsXDigit('A'));
    1.71 +  EXPECT_TRUE(IsXDigit('F'));
    1.72 +  EXPECT_TRUE(IsXDigit('a'));
    1.73 +  EXPECT_TRUE(IsXDigit('f'));
    1.74 +
    1.75 +  EXPECT_FALSE(IsXDigit('-'));
    1.76 +  EXPECT_FALSE(IsXDigit('g'));
    1.77 +  EXPECT_FALSE(IsXDigit('G'));
    1.78 +}
    1.79 +
    1.80 +TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) {
    1.81 +  EXPECT_FALSE(IsXDigit(static_cast<char>(0x80)));
    1.82 +  EXPECT_FALSE(IsXDigit(static_cast<char>('0' | 0x80)));
    1.83 +}
    1.84 +
    1.85 +TEST(IsXDigitTest, WorksForWideAscii) {
    1.86 +  EXPECT_TRUE(IsXDigit(L'0'));
    1.87 +  EXPECT_TRUE(IsXDigit(L'9'));
    1.88 +  EXPECT_TRUE(IsXDigit(L'A'));
    1.89 +  EXPECT_TRUE(IsXDigit(L'F'));
    1.90 +  EXPECT_TRUE(IsXDigit(L'a'));
    1.91 +  EXPECT_TRUE(IsXDigit(L'f'));
    1.92 +
    1.93 +  EXPECT_FALSE(IsXDigit(L'-'));
    1.94 +  EXPECT_FALSE(IsXDigit(L'g'));
    1.95 +  EXPECT_FALSE(IsXDigit(L'G'));
    1.96 +}
    1.97 +
    1.98 +TEST(IsXDigitTest, ReturnsFalseForWideNonAscii) {
    1.99 +  EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(0x80)));
   1.100 +  EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(L'0' | 0x80)));
   1.101 +  EXPECT_FALSE(IsXDigit(static_cast<wchar_t>(L'0' | 0x100)));
   1.102 +}
   1.103 +
   1.104 +class Base {
   1.105 + public:
   1.106 +  // Copy constructor and assignment operator do exactly what we need, so we
   1.107 +  // use them.
   1.108 +  Base() : member_(0) {}
   1.109 +  explicit Base(int n) : member_(n) {}
   1.110 +  virtual ~Base() {}
   1.111 +  int member() { return member_; }
   1.112 +
   1.113 + private:
   1.114 +  int member_;
   1.115 +};
   1.116 +
   1.117 +class Derived : public Base {
   1.118 + public:
   1.119 +  explicit Derived(int n) : Base(n) {}
   1.120 +};
   1.121 +
   1.122 +TEST(ImplicitCastTest, ConvertsPointers) {
   1.123 +  Derived derived(0);
   1.124 +  EXPECT_TRUE(&derived == ::testing::internal::ImplicitCast_<Base*>(&derived));
   1.125 +}
   1.126 +
   1.127 +TEST(ImplicitCastTest, CanUseInheritance) {
   1.128 +  Derived derived(1);
   1.129 +  Base base = ::testing::internal::ImplicitCast_<Base>(derived);
   1.130 +  EXPECT_EQ(derived.member(), base.member());
   1.131 +}
   1.132 +
   1.133 +class Castable {
   1.134 + public:
   1.135 +  explicit Castable(bool* converted) : converted_(converted) {}
   1.136 +  operator Base() {
   1.137 +    *converted_ = true;
   1.138 +    return Base();
   1.139 +  }
   1.140 +
   1.141 + private:
   1.142 +  bool* converted_;
   1.143 +};
   1.144 +
   1.145 +TEST(ImplicitCastTest, CanUseNonConstCastOperator) {
   1.146 +  bool converted = false;
   1.147 +  Castable castable(&converted);
   1.148 +  Base base = ::testing::internal::ImplicitCast_<Base>(castable);
   1.149 +  EXPECT_TRUE(converted);
   1.150 +}
   1.151 +
   1.152 +class ConstCastable {
   1.153 + public:
   1.154 +  explicit ConstCastable(bool* converted) : converted_(converted) {}
   1.155 +  operator Base() const {
   1.156 +    *converted_ = true;
   1.157 +    return Base();
   1.158 +  }
   1.159 +
   1.160 + private:
   1.161 +  bool* converted_;
   1.162 +};
   1.163 +
   1.164 +TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) {
   1.165 +  bool converted = false;
   1.166 +  const ConstCastable const_castable(&converted);
   1.167 +  Base base = ::testing::internal::ImplicitCast_<Base>(const_castable);
   1.168 +  EXPECT_TRUE(converted);
   1.169 +}
   1.170 +
   1.171 +class ConstAndNonConstCastable {
   1.172 + public:
   1.173 +  ConstAndNonConstCastable(bool* converted, bool* const_converted)
   1.174 +      : converted_(converted), const_converted_(const_converted) {}
   1.175 +  operator Base() {
   1.176 +    *converted_ = true;
   1.177 +    return Base();
   1.178 +  }
   1.179 +  operator Base() const {
   1.180 +    *const_converted_ = true;
   1.181 +    return Base();
   1.182 +  }
   1.183 +
   1.184 + private:
   1.185 +  bool* converted_;
   1.186 +  bool* const_converted_;
   1.187 +};
   1.188 +
   1.189 +TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) {
   1.190 +  bool converted = false;
   1.191 +  bool const_converted = false;
   1.192 +  ConstAndNonConstCastable castable(&converted, &const_converted);
   1.193 +  Base base = ::testing::internal::ImplicitCast_<Base>(castable);
   1.194 +  EXPECT_TRUE(converted);
   1.195 +  EXPECT_FALSE(const_converted);
   1.196 +
   1.197 +  converted = false;
   1.198 +  const_converted = false;
   1.199 +  const ConstAndNonConstCastable const_castable(&converted, &const_converted);
   1.200 +  base = ::testing::internal::ImplicitCast_<Base>(const_castable);
   1.201 +  EXPECT_FALSE(converted);
   1.202 +  EXPECT_TRUE(const_converted);
   1.203 +}
   1.204 +
   1.205 +class To {
   1.206 + public:
   1.207 +  To(bool* converted) { *converted = true; }  // NOLINT
   1.208 +};
   1.209 +
   1.210 +TEST(ImplicitCastTest, CanUseImplicitConstructor) {
   1.211 +  bool converted = false;
   1.212 +  To to = ::testing::internal::ImplicitCast_<To>(&converted);
   1.213 +  (void)to;
   1.214 +  EXPECT_TRUE(converted);
   1.215 +}
   1.216 +
   1.217 +TEST(IteratorTraitsTest, WorksForSTLContainerIterators) {
   1.218 +  StaticAssertTypeEq<int,
   1.219 +      IteratorTraits< ::std::vector<int>::const_iterator>::value_type>();
   1.220 +  StaticAssertTypeEq<bool,
   1.221 +      IteratorTraits< ::std::list<bool>::iterator>::value_type>();
   1.222 +}
   1.223 +
   1.224 +TEST(IteratorTraitsTest, WorksForPointerToNonConst) {
   1.225 +  StaticAssertTypeEq<char, IteratorTraits<char*>::value_type>();
   1.226 +  StaticAssertTypeEq<const void*, IteratorTraits<const void**>::value_type>();
   1.227 +}
   1.228 +
   1.229 +TEST(IteratorTraitsTest, WorksForPointerToConst) {
   1.230 +  StaticAssertTypeEq<char, IteratorTraits<const char*>::value_type>();
   1.231 +  StaticAssertTypeEq<const void*,
   1.232 +      IteratorTraits<const void* const*>::value_type>();
   1.233 +}
   1.234 +
   1.235 +// Tests that the element_type typedef is available in scoped_ptr and refers
   1.236 +// to the parameter type.
   1.237 +TEST(ScopedPtrTest, DefinesElementType) {
   1.238 +  StaticAssertTypeEq<int, ::testing::internal::scoped_ptr<int>::element_type>();
   1.239 +}
   1.240 +
   1.241 +// TODO(vladl@google.com): Implement THE REST of scoped_ptr tests.
   1.242 +
   1.243 +TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
   1.244 +  if (AlwaysFalse())
   1.245 +    GTEST_CHECK_(false) << "This should never be executed; "
   1.246 +                           "It's a compilation test only.";
   1.247 +
   1.248 +  if (AlwaysTrue())
   1.249 +    GTEST_CHECK_(true);
   1.250 +  else
   1.251 +    ;  // NOLINT
   1.252 +
   1.253 +  if (AlwaysFalse())
   1.254 +    ;  // NOLINT
   1.255 +  else
   1.256 +    GTEST_CHECK_(true) << "";
   1.257 +}
   1.258 +
   1.259 +TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
   1.260 +  switch (0) {
   1.261 +    case 1:
   1.262 +      break;
   1.263 +    default:
   1.264 +      GTEST_CHECK_(true);
   1.265 +  }
   1.266 +
   1.267 +  switch (0)
   1.268 +    case 0:
   1.269 +      GTEST_CHECK_(true) << "Check failed in switch case";
   1.270 +}
   1.271 +
   1.272 +// Verifies behavior of FormatFileLocation.
   1.273 +TEST(FormatFileLocationTest, FormatsFileLocation) {
   1.274 +  EXPECT_PRED_FORMAT2(IsSubstring, "foo.cc", FormatFileLocation("foo.cc", 42));
   1.275 +  EXPECT_PRED_FORMAT2(IsSubstring, "42", FormatFileLocation("foo.cc", 42));
   1.276 +}
   1.277 +
   1.278 +TEST(FormatFileLocationTest, FormatsUnknownFile) {
   1.279 +  EXPECT_PRED_FORMAT2(
   1.280 +      IsSubstring, "unknown file", FormatFileLocation(NULL, 42));
   1.281 +  EXPECT_PRED_FORMAT2(IsSubstring, "42", FormatFileLocation(NULL, 42));
   1.282 +}
   1.283 +
   1.284 +TEST(FormatFileLocationTest, FormatsUknownLine) {
   1.285 +  EXPECT_EQ("foo.cc:", FormatFileLocation("foo.cc", -1));
   1.286 +}
   1.287 +
   1.288 +TEST(FormatFileLocationTest, FormatsUknownFileAndLine) {
   1.289 +  EXPECT_EQ("unknown file:", FormatFileLocation(NULL, -1));
   1.290 +}
   1.291 +
   1.292 +// Verifies behavior of FormatCompilerIndependentFileLocation.
   1.293 +TEST(FormatCompilerIndependentFileLocationTest, FormatsFileLocation) {
   1.294 +  EXPECT_EQ("foo.cc:42", FormatCompilerIndependentFileLocation("foo.cc", 42));
   1.295 +}
   1.296 +
   1.297 +TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFile) {
   1.298 +  EXPECT_EQ("unknown file:42",
   1.299 +            FormatCompilerIndependentFileLocation(NULL, 42));
   1.300 +}
   1.301 +
   1.302 +TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownLine) {
   1.303 +  EXPECT_EQ("foo.cc", FormatCompilerIndependentFileLocation("foo.cc", -1));
   1.304 +}
   1.305 +
   1.306 +TEST(FormatCompilerIndependentFileLocationTest, FormatsUknownFileAndLine) {
   1.307 +  EXPECT_EQ("unknown file", FormatCompilerIndependentFileLocation(NULL, -1));
   1.308 +}
   1.309 +
   1.310 +#if GTEST_OS_MAC || GTEST_OS_QNX
   1.311 +void* ThreadFunc(void* data) {
   1.312 +  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data);
   1.313 +  pthread_mutex_lock(mutex);
   1.314 +  pthread_mutex_unlock(mutex);
   1.315 +  return NULL;
   1.316 +}
   1.317 +
   1.318 +TEST(GetThreadCountTest, ReturnsCorrectValue) {
   1.319 +  EXPECT_EQ(1U, GetThreadCount());
   1.320 +  pthread_mutex_t mutex;
   1.321 +  pthread_attr_t  attr;
   1.322 +  pthread_t       thread_id;
   1.323 +
   1.324 +  // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
   1.325 +  // destruction.
   1.326 +  pthread_mutex_init(&mutex, NULL);
   1.327 +  pthread_mutex_lock(&mutex);
   1.328 +  ASSERT_EQ(0, pthread_attr_init(&attr));
   1.329 +  ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
   1.330 +
   1.331 +  const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
   1.332 +  ASSERT_EQ(0, pthread_attr_destroy(&attr));
   1.333 +  ASSERT_EQ(0, status);
   1.334 +  EXPECT_EQ(2U, GetThreadCount());
   1.335 +  pthread_mutex_unlock(&mutex);
   1.336 +
   1.337 +  void* dummy;
   1.338 +  ASSERT_EQ(0, pthread_join(thread_id, &dummy));
   1.339 +
   1.340 +# if GTEST_OS_MAC
   1.341 +
   1.342 +  // MacOS X may not immediately report the updated thread count after
   1.343 +  // joining a thread, causing flakiness in this test. To counter that, we
   1.344 +  // wait for up to .5 seconds for the OS to report the correct value.
   1.345 +  for (int i = 0; i < 5; ++i) {
   1.346 +    if (GetThreadCount() == 1)
   1.347 +      break;
   1.348 +
   1.349 +    SleepMilliseconds(100);
   1.350 +  }
   1.351 +
   1.352 +# endif  // GTEST_OS_MAC
   1.353 +
   1.354 +  EXPECT_EQ(1U, GetThreadCount());
   1.355 +  pthread_mutex_destroy(&mutex);
   1.356 +}
   1.357 +#else
   1.358 +TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
   1.359 +  EXPECT_EQ(0U, GetThreadCount());
   1.360 +}
   1.361 +#endif  // GTEST_OS_MAC || GTEST_OS_QNX
   1.362 +
   1.363 +TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
   1.364 +  const bool a_false_condition = false;
   1.365 +  const char regex[] =
   1.366 +#ifdef _MSC_VER
   1.367 +     "gtest-port_test\\.cc\\(\\d+\\):"
   1.368 +#elif GTEST_USES_POSIX_RE
   1.369 +     "gtest-port_test\\.cc:[0-9]+"
   1.370 +#else
   1.371 +     "gtest-port_test\\.cc:\\d+"
   1.372 +#endif  // _MSC_VER
   1.373 +     ".*a_false_condition.*Extra info.*";
   1.374 +
   1.375 +  EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info",
   1.376 +                            regex);
   1.377 +}
   1.378 +
   1.379 +#if GTEST_HAS_DEATH_TEST
   1.380 +
   1.381 +TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
   1.382 +  EXPECT_EXIT({
   1.383 +      GTEST_CHECK_(true) << "Extra info";
   1.384 +      ::std::cerr << "Success\n";
   1.385 +      exit(0); },
   1.386 +      ::testing::ExitedWithCode(0), "Success");
   1.387 +}
   1.388 +
   1.389 +#endif  // GTEST_HAS_DEATH_TEST
   1.390 +
   1.391 +// Verifies that Google Test choose regular expression engine appropriate to
   1.392 +// the platform. The test will produce compiler errors in case of failure.
   1.393 +// For simplicity, we only cover the most important platforms here.
   1.394 +TEST(RegexEngineSelectionTest, SelectsCorrectRegexEngine) {
   1.395 +#if GTEST_HAS_POSIX_RE
   1.396 +
   1.397 +  EXPECT_TRUE(GTEST_USES_POSIX_RE);
   1.398 +
   1.399 +#else
   1.400 +
   1.401 +  EXPECT_TRUE(GTEST_USES_SIMPLE_RE);
   1.402 +
   1.403 +#endif
   1.404 +}
   1.405 +
   1.406 +#if GTEST_USES_POSIX_RE
   1.407 +
   1.408 +# if GTEST_HAS_TYPED_TEST
   1.409 +
   1.410 +template <typename Str>
   1.411 +class RETest : public ::testing::Test {};
   1.412 +
   1.413 +// Defines StringTypes as the list of all string types that class RE
   1.414 +// supports.
   1.415 +typedef testing::Types<
   1.416 +    ::std::string,
   1.417 +#  if GTEST_HAS_GLOBAL_STRING
   1.418 +    ::string,
   1.419 +#  endif  // GTEST_HAS_GLOBAL_STRING
   1.420 +    const char*> StringTypes;
   1.421 +
   1.422 +TYPED_TEST_CASE(RETest, StringTypes);
   1.423 +
   1.424 +// Tests RE's implicit constructors.
   1.425 +TYPED_TEST(RETest, ImplicitConstructorWorks) {
   1.426 +  const RE empty(TypeParam(""));
   1.427 +  EXPECT_STREQ("", empty.pattern());
   1.428 +
   1.429 +  const RE simple(TypeParam("hello"));
   1.430 +  EXPECT_STREQ("hello", simple.pattern());
   1.431 +
   1.432 +  const RE normal(TypeParam(".*(\\w+)"));
   1.433 +  EXPECT_STREQ(".*(\\w+)", normal.pattern());
   1.434 +}
   1.435 +
   1.436 +// Tests that RE's constructors reject invalid regular expressions.
   1.437 +TYPED_TEST(RETest, RejectsInvalidRegex) {
   1.438 +  EXPECT_NONFATAL_FAILURE({
   1.439 +    const RE invalid(TypeParam("?"));
   1.440 +  }, "\"?\" is not a valid POSIX Extended regular expression.");
   1.441 +}
   1.442 +
   1.443 +// Tests RE::FullMatch().
   1.444 +TYPED_TEST(RETest, FullMatchWorks) {
   1.445 +  const RE empty(TypeParam(""));
   1.446 +  EXPECT_TRUE(RE::FullMatch(TypeParam(""), empty));
   1.447 +  EXPECT_FALSE(RE::FullMatch(TypeParam("a"), empty));
   1.448 +
   1.449 +  const RE re(TypeParam("a.*z"));
   1.450 +  EXPECT_TRUE(RE::FullMatch(TypeParam("az"), re));
   1.451 +  EXPECT_TRUE(RE::FullMatch(TypeParam("axyz"), re));
   1.452 +  EXPECT_FALSE(RE::FullMatch(TypeParam("baz"), re));
   1.453 +  EXPECT_FALSE(RE::FullMatch(TypeParam("azy"), re));
   1.454 +}
   1.455 +
   1.456 +// Tests RE::PartialMatch().
   1.457 +TYPED_TEST(RETest, PartialMatchWorks) {
   1.458 +  const RE empty(TypeParam(""));
   1.459 +  EXPECT_TRUE(RE::PartialMatch(TypeParam(""), empty));
   1.460 +  EXPECT_TRUE(RE::PartialMatch(TypeParam("a"), empty));
   1.461 +
   1.462 +  const RE re(TypeParam("a.*z"));
   1.463 +  EXPECT_TRUE(RE::PartialMatch(TypeParam("az"), re));
   1.464 +  EXPECT_TRUE(RE::PartialMatch(TypeParam("axyz"), re));
   1.465 +  EXPECT_TRUE(RE::PartialMatch(TypeParam("baz"), re));
   1.466 +  EXPECT_TRUE(RE::PartialMatch(TypeParam("azy"), re));
   1.467 +  EXPECT_FALSE(RE::PartialMatch(TypeParam("zza"), re));
   1.468 +}
   1.469 +
   1.470 +# endif  // GTEST_HAS_TYPED_TEST
   1.471 +
   1.472 +#elif GTEST_USES_SIMPLE_RE
   1.473 +
   1.474 +TEST(IsInSetTest, NulCharIsNotInAnySet) {
   1.475 +  EXPECT_FALSE(IsInSet('\0', ""));
   1.476 +  EXPECT_FALSE(IsInSet('\0', "\0"));
   1.477 +  EXPECT_FALSE(IsInSet('\0', "a"));
   1.478 +}
   1.479 +
   1.480 +TEST(IsInSetTest, WorksForNonNulChars) {
   1.481 +  EXPECT_FALSE(IsInSet('a', "Ab"));
   1.482 +  EXPECT_FALSE(IsInSet('c', ""));
   1.483 +
   1.484 +  EXPECT_TRUE(IsInSet('b', "bcd"));
   1.485 +  EXPECT_TRUE(IsInSet('b', "ab"));
   1.486 +}
   1.487 +
   1.488 +TEST(IsAsciiDigitTest, IsFalseForNonDigit) {
   1.489 +  EXPECT_FALSE(IsAsciiDigit('\0'));
   1.490 +  EXPECT_FALSE(IsAsciiDigit(' '));
   1.491 +  EXPECT_FALSE(IsAsciiDigit('+'));
   1.492 +  EXPECT_FALSE(IsAsciiDigit('-'));
   1.493 +  EXPECT_FALSE(IsAsciiDigit('.'));
   1.494 +  EXPECT_FALSE(IsAsciiDigit('a'));
   1.495 +}
   1.496 +
   1.497 +TEST(IsAsciiDigitTest, IsTrueForDigit) {
   1.498 +  EXPECT_TRUE(IsAsciiDigit('0'));
   1.499 +  EXPECT_TRUE(IsAsciiDigit('1'));
   1.500 +  EXPECT_TRUE(IsAsciiDigit('5'));
   1.501 +  EXPECT_TRUE(IsAsciiDigit('9'));
   1.502 +}
   1.503 +
   1.504 +TEST(IsAsciiPunctTest, IsFalseForNonPunct) {
   1.505 +  EXPECT_FALSE(IsAsciiPunct('\0'));
   1.506 +  EXPECT_FALSE(IsAsciiPunct(' '));
   1.507 +  EXPECT_FALSE(IsAsciiPunct('\n'));
   1.508 +  EXPECT_FALSE(IsAsciiPunct('a'));
   1.509 +  EXPECT_FALSE(IsAsciiPunct('0'));
   1.510 +}
   1.511 +
   1.512 +TEST(IsAsciiPunctTest, IsTrueForPunct) {
   1.513 +  for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
   1.514 +    EXPECT_PRED1(IsAsciiPunct, *p);
   1.515 +  }
   1.516 +}
   1.517 +
   1.518 +TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
   1.519 +  EXPECT_FALSE(IsRepeat('\0'));
   1.520 +  EXPECT_FALSE(IsRepeat(' '));
   1.521 +  EXPECT_FALSE(IsRepeat('a'));
   1.522 +  EXPECT_FALSE(IsRepeat('1'));
   1.523 +  EXPECT_FALSE(IsRepeat('-'));
   1.524 +}
   1.525 +
   1.526 +TEST(IsRepeatTest, IsTrueForRepeatChar) {
   1.527 +  EXPECT_TRUE(IsRepeat('?'));
   1.528 +  EXPECT_TRUE(IsRepeat('*'));
   1.529 +  EXPECT_TRUE(IsRepeat('+'));
   1.530 +}
   1.531 +
   1.532 +TEST(IsAsciiWhiteSpaceTest, IsFalseForNonWhiteSpace) {
   1.533 +  EXPECT_FALSE(IsAsciiWhiteSpace('\0'));
   1.534 +  EXPECT_FALSE(IsAsciiWhiteSpace('a'));
   1.535 +  EXPECT_FALSE(IsAsciiWhiteSpace('1'));
   1.536 +  EXPECT_FALSE(IsAsciiWhiteSpace('+'));
   1.537 +  EXPECT_FALSE(IsAsciiWhiteSpace('_'));
   1.538 +}
   1.539 +
   1.540 +TEST(IsAsciiWhiteSpaceTest, IsTrueForWhiteSpace) {
   1.541 +  EXPECT_TRUE(IsAsciiWhiteSpace(' '));
   1.542 +  EXPECT_TRUE(IsAsciiWhiteSpace('\n'));
   1.543 +  EXPECT_TRUE(IsAsciiWhiteSpace('\r'));
   1.544 +  EXPECT_TRUE(IsAsciiWhiteSpace('\t'));
   1.545 +  EXPECT_TRUE(IsAsciiWhiteSpace('\v'));
   1.546 +  EXPECT_TRUE(IsAsciiWhiteSpace('\f'));
   1.547 +}
   1.548 +
   1.549 +TEST(IsAsciiWordCharTest, IsFalseForNonWordChar) {
   1.550 +  EXPECT_FALSE(IsAsciiWordChar('\0'));
   1.551 +  EXPECT_FALSE(IsAsciiWordChar('+'));
   1.552 +  EXPECT_FALSE(IsAsciiWordChar('.'));
   1.553 +  EXPECT_FALSE(IsAsciiWordChar(' '));
   1.554 +  EXPECT_FALSE(IsAsciiWordChar('\n'));
   1.555 +}
   1.556 +
   1.557 +TEST(IsAsciiWordCharTest, IsTrueForLetter) {
   1.558 +  EXPECT_TRUE(IsAsciiWordChar('a'));
   1.559 +  EXPECT_TRUE(IsAsciiWordChar('b'));
   1.560 +  EXPECT_TRUE(IsAsciiWordChar('A'));
   1.561 +  EXPECT_TRUE(IsAsciiWordChar('Z'));
   1.562 +}
   1.563 +
   1.564 +TEST(IsAsciiWordCharTest, IsTrueForDigit) {
   1.565 +  EXPECT_TRUE(IsAsciiWordChar('0'));
   1.566 +  EXPECT_TRUE(IsAsciiWordChar('1'));
   1.567 +  EXPECT_TRUE(IsAsciiWordChar('7'));
   1.568 +  EXPECT_TRUE(IsAsciiWordChar('9'));
   1.569 +}
   1.570 +
   1.571 +TEST(IsAsciiWordCharTest, IsTrueForUnderscore) {
   1.572 +  EXPECT_TRUE(IsAsciiWordChar('_'));
   1.573 +}
   1.574 +
   1.575 +TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
   1.576 +  EXPECT_FALSE(IsValidEscape('\0'));
   1.577 +  EXPECT_FALSE(IsValidEscape('\007'));
   1.578 +}
   1.579 +
   1.580 +TEST(IsValidEscapeTest, IsFalseForDigit) {
   1.581 +  EXPECT_FALSE(IsValidEscape('0'));
   1.582 +  EXPECT_FALSE(IsValidEscape('9'));
   1.583 +}
   1.584 +
   1.585 +TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
   1.586 +  EXPECT_FALSE(IsValidEscape(' '));
   1.587 +  EXPECT_FALSE(IsValidEscape('\n'));
   1.588 +}
   1.589 +
   1.590 +TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
   1.591 +  EXPECT_FALSE(IsValidEscape('a'));
   1.592 +  EXPECT_FALSE(IsValidEscape('Z'));
   1.593 +}
   1.594 +
   1.595 +TEST(IsValidEscapeTest, IsTrueForPunct) {
   1.596 +  EXPECT_TRUE(IsValidEscape('.'));
   1.597 +  EXPECT_TRUE(IsValidEscape('-'));
   1.598 +  EXPECT_TRUE(IsValidEscape('^'));
   1.599 +  EXPECT_TRUE(IsValidEscape('$'));
   1.600 +  EXPECT_TRUE(IsValidEscape('('));
   1.601 +  EXPECT_TRUE(IsValidEscape(']'));
   1.602 +  EXPECT_TRUE(IsValidEscape('{'));
   1.603 +  EXPECT_TRUE(IsValidEscape('|'));
   1.604 +}
   1.605 +
   1.606 +TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
   1.607 +  EXPECT_TRUE(IsValidEscape('d'));
   1.608 +  EXPECT_TRUE(IsValidEscape('D'));
   1.609 +  EXPECT_TRUE(IsValidEscape('s'));
   1.610 +  EXPECT_TRUE(IsValidEscape('S'));
   1.611 +  EXPECT_TRUE(IsValidEscape('w'));
   1.612 +  EXPECT_TRUE(IsValidEscape('W'));
   1.613 +}
   1.614 +
   1.615 +TEST(AtomMatchesCharTest, EscapedPunct) {
   1.616 +  EXPECT_FALSE(AtomMatchesChar(true, '\\', '\0'));
   1.617 +  EXPECT_FALSE(AtomMatchesChar(true, '\\', ' '));
   1.618 +  EXPECT_FALSE(AtomMatchesChar(true, '_', '.'));
   1.619 +  EXPECT_FALSE(AtomMatchesChar(true, '.', 'a'));
   1.620 +
   1.621 +  EXPECT_TRUE(AtomMatchesChar(true, '\\', '\\'));
   1.622 +  EXPECT_TRUE(AtomMatchesChar(true, '_', '_'));
   1.623 +  EXPECT_TRUE(AtomMatchesChar(true, '+', '+'));
   1.624 +  EXPECT_TRUE(AtomMatchesChar(true, '.', '.'));
   1.625 +}
   1.626 +
   1.627 +TEST(AtomMatchesCharTest, Escaped_d) {
   1.628 +  EXPECT_FALSE(AtomMatchesChar(true, 'd', '\0'));
   1.629 +  EXPECT_FALSE(AtomMatchesChar(true, 'd', 'a'));
   1.630 +  EXPECT_FALSE(AtomMatchesChar(true, 'd', '.'));
   1.631 +
   1.632 +  EXPECT_TRUE(AtomMatchesChar(true, 'd', '0'));
   1.633 +  EXPECT_TRUE(AtomMatchesChar(true, 'd', '9'));
   1.634 +}
   1.635 +
   1.636 +TEST(AtomMatchesCharTest, Escaped_D) {
   1.637 +  EXPECT_FALSE(AtomMatchesChar(true, 'D', '0'));
   1.638 +  EXPECT_FALSE(AtomMatchesChar(true, 'D', '9'));
   1.639 +
   1.640 +  EXPECT_TRUE(AtomMatchesChar(true, 'D', '\0'));
   1.641 +  EXPECT_TRUE(AtomMatchesChar(true, 'D', 'a'));
   1.642 +  EXPECT_TRUE(AtomMatchesChar(true, 'D', '-'));
   1.643 +}
   1.644 +
   1.645 +TEST(AtomMatchesCharTest, Escaped_s) {
   1.646 +  EXPECT_FALSE(AtomMatchesChar(true, 's', '\0'));
   1.647 +  EXPECT_FALSE(AtomMatchesChar(true, 's', 'a'));
   1.648 +  EXPECT_FALSE(AtomMatchesChar(true, 's', '.'));
   1.649 +  EXPECT_FALSE(AtomMatchesChar(true, 's', '9'));
   1.650 +
   1.651 +  EXPECT_TRUE(AtomMatchesChar(true, 's', ' '));
   1.652 +  EXPECT_TRUE(AtomMatchesChar(true, 's', '\n'));
   1.653 +  EXPECT_TRUE(AtomMatchesChar(true, 's', '\t'));
   1.654 +}
   1.655 +
   1.656 +TEST(AtomMatchesCharTest, Escaped_S) {
   1.657 +  EXPECT_FALSE(AtomMatchesChar(true, 'S', ' '));
   1.658 +  EXPECT_FALSE(AtomMatchesChar(true, 'S', '\r'));
   1.659 +
   1.660 +  EXPECT_TRUE(AtomMatchesChar(true, 'S', '\0'));
   1.661 +  EXPECT_TRUE(AtomMatchesChar(true, 'S', 'a'));
   1.662 +  EXPECT_TRUE(AtomMatchesChar(true, 'S', '9'));
   1.663 +}
   1.664 +
   1.665 +TEST(AtomMatchesCharTest, Escaped_w) {
   1.666 +  EXPECT_FALSE(AtomMatchesChar(true, 'w', '\0'));
   1.667 +  EXPECT_FALSE(AtomMatchesChar(true, 'w', '+'));
   1.668 +  EXPECT_FALSE(AtomMatchesChar(true, 'w', ' '));
   1.669 +  EXPECT_FALSE(AtomMatchesChar(true, 'w', '\n'));
   1.670 +
   1.671 +  EXPECT_TRUE(AtomMatchesChar(true, 'w', '0'));
   1.672 +  EXPECT_TRUE(AtomMatchesChar(true, 'w', 'b'));
   1.673 +  EXPECT_TRUE(AtomMatchesChar(true, 'w', 'C'));
   1.674 +  EXPECT_TRUE(AtomMatchesChar(true, 'w', '_'));
   1.675 +}
   1.676 +
   1.677 +TEST(AtomMatchesCharTest, Escaped_W) {
   1.678 +  EXPECT_FALSE(AtomMatchesChar(true, 'W', 'A'));
   1.679 +  EXPECT_FALSE(AtomMatchesChar(true, 'W', 'b'));
   1.680 +  EXPECT_FALSE(AtomMatchesChar(true, 'W', '9'));
   1.681 +  EXPECT_FALSE(AtomMatchesChar(true, 'W', '_'));
   1.682 +
   1.683 +  EXPECT_TRUE(AtomMatchesChar(true, 'W', '\0'));
   1.684 +  EXPECT_TRUE(AtomMatchesChar(true, 'W', '*'));
   1.685 +  EXPECT_TRUE(AtomMatchesChar(true, 'W', '\n'));
   1.686 +}
   1.687 +
   1.688 +TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
   1.689 +  EXPECT_FALSE(AtomMatchesChar(true, 'f', '\0'));
   1.690 +  EXPECT_FALSE(AtomMatchesChar(true, 'f', '\n'));
   1.691 +  EXPECT_FALSE(AtomMatchesChar(true, 'n', '\0'));
   1.692 +  EXPECT_FALSE(AtomMatchesChar(true, 'n', '\r'));
   1.693 +  EXPECT_FALSE(AtomMatchesChar(true, 'r', '\0'));
   1.694 +  EXPECT_FALSE(AtomMatchesChar(true, 'r', 'a'));
   1.695 +  EXPECT_FALSE(AtomMatchesChar(true, 't', '\0'));
   1.696 +  EXPECT_FALSE(AtomMatchesChar(true, 't', 't'));
   1.697 +  EXPECT_FALSE(AtomMatchesChar(true, 'v', '\0'));
   1.698 +  EXPECT_FALSE(AtomMatchesChar(true, 'v', '\f'));
   1.699 +
   1.700 +  EXPECT_TRUE(AtomMatchesChar(true, 'f', '\f'));
   1.701 +  EXPECT_TRUE(AtomMatchesChar(true, 'n', '\n'));
   1.702 +  EXPECT_TRUE(AtomMatchesChar(true, 'r', '\r'));
   1.703 +  EXPECT_TRUE(AtomMatchesChar(true, 't', '\t'));
   1.704 +  EXPECT_TRUE(AtomMatchesChar(true, 'v', '\v'));
   1.705 +}
   1.706 +
   1.707 +TEST(AtomMatchesCharTest, UnescapedDot) {
   1.708 +  EXPECT_FALSE(AtomMatchesChar(false, '.', '\n'));
   1.709 +
   1.710 +  EXPECT_TRUE(AtomMatchesChar(false, '.', '\0'));
   1.711 +  EXPECT_TRUE(AtomMatchesChar(false, '.', '.'));
   1.712 +  EXPECT_TRUE(AtomMatchesChar(false, '.', 'a'));
   1.713 +  EXPECT_TRUE(AtomMatchesChar(false, '.', ' '));
   1.714 +}
   1.715 +
   1.716 +TEST(AtomMatchesCharTest, UnescapedChar) {
   1.717 +  EXPECT_FALSE(AtomMatchesChar(false, 'a', '\0'));
   1.718 +  EXPECT_FALSE(AtomMatchesChar(false, 'a', 'b'));
   1.719 +  EXPECT_FALSE(AtomMatchesChar(false, '$', 'a'));
   1.720 +
   1.721 +  EXPECT_TRUE(AtomMatchesChar(false, '$', '$'));
   1.722 +  EXPECT_TRUE(AtomMatchesChar(false, '5', '5'));
   1.723 +  EXPECT_TRUE(AtomMatchesChar(false, 'Z', 'Z'));
   1.724 +}
   1.725 +
   1.726 +TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
   1.727 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)),
   1.728 +                          "NULL is not a valid simple regular expression");
   1.729 +  EXPECT_NONFATAL_FAILURE(
   1.730 +      ASSERT_FALSE(ValidateRegex("a\\")),
   1.731 +      "Syntax error at index 1 in simple regular expression \"a\\\": ");
   1.732 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a\\")),
   1.733 +                          "'\\' cannot appear at the end");
   1.734 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\n\\")),
   1.735 +                          "'\\' cannot appear at the end");
   1.736 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\s\\hb")),
   1.737 +                          "invalid escape sequence \"\\h\"");
   1.738 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^^")),
   1.739 +                          "'^' can only appear at the beginning");
   1.740 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(".*^b")),
   1.741 +                          "'^' can only appear at the beginning");
   1.742 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("$$")),
   1.743 +                          "'$' can only appear at the end");
   1.744 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^$a")),
   1.745 +                          "'$' can only appear at the end");
   1.746 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a(b")),
   1.747 +                          "'(' is unsupported");
   1.748 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("ab)")),
   1.749 +                          "')' is unsupported");
   1.750 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("[ab")),
   1.751 +                          "'[' is unsupported");
   1.752 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a{2")),
   1.753 +                          "'{' is unsupported");
   1.754 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("?")),
   1.755 +                          "'?' can only follow a repeatable token");
   1.756 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^*")),
   1.757 +                          "'*' can only follow a repeatable token");
   1.758 +  EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("5*+")),
   1.759 +                          "'+' can only follow a repeatable token");
   1.760 +}
   1.761 +
   1.762 +TEST(ValidateRegexTest, ReturnsTrueForValid) {
   1.763 +  EXPECT_TRUE(ValidateRegex(""));
   1.764 +  EXPECT_TRUE(ValidateRegex("a"));
   1.765 +  EXPECT_TRUE(ValidateRegex(".*"));
   1.766 +  EXPECT_TRUE(ValidateRegex("^a_+"));
   1.767 +  EXPECT_TRUE(ValidateRegex("^a\\t\\&?"));
   1.768 +  EXPECT_TRUE(ValidateRegex("09*$"));
   1.769 +  EXPECT_TRUE(ValidateRegex("^Z$"));
   1.770 +  EXPECT_TRUE(ValidateRegex("a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
   1.771 +}
   1.772 +
   1.773 +TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
   1.774 +  EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "a", "ba"));
   1.775 +  // Repeating more than once.
   1.776 +  EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "aab"));
   1.777 +
   1.778 +  // Repeating zero times.
   1.779 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ba"));
   1.780 +  // Repeating once.
   1.781 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ab"));
   1.782 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '#', '?', ".", "##"));
   1.783 +}
   1.784 +
   1.785 +TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
   1.786 +  EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '*', "a$", "baab"));
   1.787 +
   1.788 +  // Repeating zero times.
   1.789 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "bc"));
   1.790 +  // Repeating once.
   1.791 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "abc"));
   1.792 +  // Repeating more than once.
   1.793 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '*', "-", "ab_1-g"));
   1.794 +}
   1.795 +
   1.796 +TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
   1.797 +  EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "a$", "baab"));
   1.798 +  // Repeating zero times.
   1.799 +  EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "bc"));
   1.800 +
   1.801 +  // Repeating once.
   1.802 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "abc"));
   1.803 +  // Repeating more than once.
   1.804 +  EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '+', "-", "ab_1-g"));
   1.805 +}
   1.806 +
   1.807 +TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
   1.808 +  EXPECT_TRUE(MatchRegexAtHead("", ""));
   1.809 +  EXPECT_TRUE(MatchRegexAtHead("", "ab"));
   1.810 +}
   1.811 +
   1.812 +TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
   1.813 +  EXPECT_FALSE(MatchRegexAtHead("$", "a"));
   1.814 +
   1.815 +  EXPECT_TRUE(MatchRegexAtHead("$", ""));
   1.816 +  EXPECT_TRUE(MatchRegexAtHead("a$", "a"));
   1.817 +}
   1.818 +
   1.819 +TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
   1.820 +  EXPECT_FALSE(MatchRegexAtHead("\\w", "+"));
   1.821 +  EXPECT_FALSE(MatchRegexAtHead("\\W", "ab"));
   1.822 +
   1.823 +  EXPECT_TRUE(MatchRegexAtHead("\\sa", "\nab"));
   1.824 +  EXPECT_TRUE(MatchRegexAtHead("\\d", "1a"));
   1.825 +}
   1.826 +
   1.827 +TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
   1.828 +  EXPECT_FALSE(MatchRegexAtHead(".+a", "abc"));
   1.829 +  EXPECT_FALSE(MatchRegexAtHead("a?b", "aab"));
   1.830 +
   1.831 +  EXPECT_TRUE(MatchRegexAtHead(".*a", "bc12-ab"));
   1.832 +  EXPECT_TRUE(MatchRegexAtHead("a?b", "b"));
   1.833 +  EXPECT_TRUE(MatchRegexAtHead("a?b", "ab"));
   1.834 +}
   1.835 +
   1.836 +TEST(MatchRegexAtHeadTest,
   1.837 +     WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
   1.838 +  EXPECT_FALSE(MatchRegexAtHead("\\.+a", "abc"));
   1.839 +  EXPECT_FALSE(MatchRegexAtHead("\\s?b", "  b"));
   1.840 +
   1.841 +  EXPECT_TRUE(MatchRegexAtHead("\\(*a", "((((ab"));
   1.842 +  EXPECT_TRUE(MatchRegexAtHead("\\^?b", "^b"));
   1.843 +  EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "b"));
   1.844 +  EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "\\b"));
   1.845 +}
   1.846 +
   1.847 +TEST(MatchRegexAtHeadTest, MatchesSequentially) {
   1.848 +  EXPECT_FALSE(MatchRegexAtHead("ab.*c", "acabc"));
   1.849 +
   1.850 +  EXPECT_TRUE(MatchRegexAtHead("ab.*c", "ab-fsc"));
   1.851 +}
   1.852 +
   1.853 +TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
   1.854 +  EXPECT_FALSE(MatchRegexAnywhere("", NULL));
   1.855 +}
   1.856 +
   1.857 +TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
   1.858 +  EXPECT_FALSE(MatchRegexAnywhere("^a", "ba"));
   1.859 +  EXPECT_FALSE(MatchRegexAnywhere("^$", "a"));
   1.860 +
   1.861 +  EXPECT_TRUE(MatchRegexAnywhere("^a", "ab"));
   1.862 +  EXPECT_TRUE(MatchRegexAnywhere("^", "ab"));
   1.863 +  EXPECT_TRUE(MatchRegexAnywhere("^$", ""));
   1.864 +}
   1.865 +
   1.866 +TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
   1.867 +  EXPECT_FALSE(MatchRegexAnywhere("a", "bcde123"));
   1.868 +  EXPECT_FALSE(MatchRegexAnywhere("a.+a", "--aa88888888"));
   1.869 +}
   1.870 +
   1.871 +TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
   1.872 +  EXPECT_TRUE(MatchRegexAnywhere("\\w+", "ab1_ - 5"));
   1.873 +  EXPECT_TRUE(MatchRegexAnywhere(".*=", "="));
   1.874 +  EXPECT_TRUE(MatchRegexAnywhere("x.*ab?.*bc", "xaaabc"));
   1.875 +}
   1.876 +
   1.877 +TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
   1.878 +  EXPECT_TRUE(MatchRegexAnywhere("\\w+", "$$$ ab1_ - 5"));
   1.879 +  EXPECT_TRUE(MatchRegexAnywhere("\\.+=", "=  ...="));
   1.880 +}
   1.881 +
   1.882 +// Tests RE's implicit constructors.
   1.883 +TEST(RETest, ImplicitConstructorWorks) {
   1.884 +  const RE empty("");
   1.885 +  EXPECT_STREQ("", empty.pattern());
   1.886 +
   1.887 +  const RE simple("hello");
   1.888 +  EXPECT_STREQ("hello", simple.pattern());
   1.889 +}
   1.890 +
   1.891 +// Tests that RE's constructors reject invalid regular expressions.
   1.892 +TEST(RETest, RejectsInvalidRegex) {
   1.893 +  EXPECT_NONFATAL_FAILURE({
   1.894 +    const RE normal(NULL);
   1.895 +  }, "NULL is not a valid simple regular expression");
   1.896 +
   1.897 +  EXPECT_NONFATAL_FAILURE({
   1.898 +    const RE normal(".*(\\w+");
   1.899 +  }, "'(' is unsupported");
   1.900 +
   1.901 +  EXPECT_NONFATAL_FAILURE({
   1.902 +    const RE invalid("^?");
   1.903 +  }, "'?' can only follow a repeatable token");
   1.904 +}
   1.905 +
   1.906 +// Tests RE::FullMatch().
   1.907 +TEST(RETest, FullMatchWorks) {
   1.908 +  const RE empty("");
   1.909 +  EXPECT_TRUE(RE::FullMatch("", empty));
   1.910 +  EXPECT_FALSE(RE::FullMatch("a", empty));
   1.911 +
   1.912 +  const RE re1("a");
   1.913 +  EXPECT_TRUE(RE::FullMatch("a", re1));
   1.914 +
   1.915 +  const RE re("a.*z");
   1.916 +  EXPECT_TRUE(RE::FullMatch("az", re));
   1.917 +  EXPECT_TRUE(RE::FullMatch("axyz", re));
   1.918 +  EXPECT_FALSE(RE::FullMatch("baz", re));
   1.919 +  EXPECT_FALSE(RE::FullMatch("azy", re));
   1.920 +}
   1.921 +
   1.922 +// Tests RE::PartialMatch().
   1.923 +TEST(RETest, PartialMatchWorks) {
   1.924 +  const RE empty("");
   1.925 +  EXPECT_TRUE(RE::PartialMatch("", empty));
   1.926 +  EXPECT_TRUE(RE::PartialMatch("a", empty));
   1.927 +
   1.928 +  const RE re("a.*z");
   1.929 +  EXPECT_TRUE(RE::PartialMatch("az", re));
   1.930 +  EXPECT_TRUE(RE::PartialMatch("axyz", re));
   1.931 +  EXPECT_TRUE(RE::PartialMatch("baz", re));
   1.932 +  EXPECT_TRUE(RE::PartialMatch("azy", re));
   1.933 +  EXPECT_FALSE(RE::PartialMatch("zza", re));
   1.934 +}
   1.935 +
   1.936 +#endif  // GTEST_USES_POSIX_RE
   1.937 +
   1.938 +#if !GTEST_OS_WINDOWS_MOBILE
   1.939 +
   1.940 +TEST(CaptureTest, CapturesStdout) {
   1.941 +  CaptureStdout();
   1.942 +  fprintf(stdout, "abc");
   1.943 +  EXPECT_STREQ("abc", GetCapturedStdout().c_str());
   1.944 +
   1.945 +  CaptureStdout();
   1.946 +  fprintf(stdout, "def%cghi", '\0');
   1.947 +  EXPECT_EQ(::std::string("def\0ghi", 7), ::std::string(GetCapturedStdout()));
   1.948 +}
   1.949 +
   1.950 +TEST(CaptureTest, CapturesStderr) {
   1.951 +  CaptureStderr();
   1.952 +  fprintf(stderr, "jkl");
   1.953 +  EXPECT_STREQ("jkl", GetCapturedStderr().c_str());
   1.954 +
   1.955 +  CaptureStderr();
   1.956 +  fprintf(stderr, "jkl%cmno", '\0');
   1.957 +  EXPECT_EQ(::std::string("jkl\0mno", 7), ::std::string(GetCapturedStderr()));
   1.958 +}
   1.959 +
   1.960 +// Tests that stdout and stderr capture don't interfere with each other.
   1.961 +TEST(CaptureTest, CapturesStdoutAndStderr) {
   1.962 +  CaptureStdout();
   1.963 +  CaptureStderr();
   1.964 +  fprintf(stdout, "pqr");
   1.965 +  fprintf(stderr, "stu");
   1.966 +  EXPECT_STREQ("pqr", GetCapturedStdout().c_str());
   1.967 +  EXPECT_STREQ("stu", GetCapturedStderr().c_str());
   1.968 +}
   1.969 +
   1.970 +TEST(CaptureDeathTest, CannotReenterStdoutCapture) {
   1.971 +  CaptureStdout();
   1.972 +  EXPECT_DEATH_IF_SUPPORTED(CaptureStdout(),
   1.973 +                            "Only one stdout capturer can exist at a time");
   1.974 +  GetCapturedStdout();
   1.975 +
   1.976 +  // We cannot test stderr capturing using death tests as they use it
   1.977 +  // themselves.
   1.978 +}
   1.979 +
   1.980 +#endif  // !GTEST_OS_WINDOWS_MOBILE
   1.981 +
   1.982 +TEST(ThreadLocalTest, DefaultConstructorInitializesToDefaultValues) {
   1.983 +  ThreadLocal<int> t1;
   1.984 +  EXPECT_EQ(0, t1.get());
   1.985 +
   1.986 +  ThreadLocal<void*> t2;
   1.987 +  EXPECT_TRUE(t2.get() == NULL);
   1.988 +}
   1.989 +
   1.990 +TEST(ThreadLocalTest, SingleParamConstructorInitializesToParam) {
   1.991 +  ThreadLocal<int> t1(123);
   1.992 +  EXPECT_EQ(123, t1.get());
   1.993 +
   1.994 +  int i = 0;
   1.995 +  ThreadLocal<int*> t2(&i);
   1.996 +  EXPECT_EQ(&i, t2.get());
   1.997 +}
   1.998 +
   1.999 +class NoDefaultContructor {
  1.1000 + public:
  1.1001 +  explicit NoDefaultContructor(const char*) {}
  1.1002 +  NoDefaultContructor(const NoDefaultContructor&) {}
  1.1003 +};
  1.1004 +
  1.1005 +TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
  1.1006 +  ThreadLocal<NoDefaultContructor> bar(NoDefaultContructor("foo"));
  1.1007 +  bar.pointer();
  1.1008 +}
  1.1009 +
  1.1010 +TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
  1.1011 +  ThreadLocal<String> thread_local_string;
  1.1012 +
  1.1013 +  EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
  1.1014 +
  1.1015 +  // Verifies the condition still holds after calling set.
  1.1016 +  thread_local_string.set("foo");
  1.1017 +  EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
  1.1018 +}
  1.1019 +
  1.1020 +TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
  1.1021 +  ThreadLocal<String> thread_local_string;
  1.1022 +  const ThreadLocal<String>& const_thread_local_string = thread_local_string;
  1.1023 +
  1.1024 +  EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
  1.1025 +
  1.1026 +  thread_local_string.set("foo");
  1.1027 +  EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
  1.1028 +}
  1.1029 +
  1.1030 +#if GTEST_IS_THREADSAFE
  1.1031 +
  1.1032 +void AddTwo(int* param) { *param += 2; }
  1.1033 +
  1.1034 +TEST(ThreadWithParamTest, ConstructorExecutesThreadFunc) {
  1.1035 +  int i = 40;
  1.1036 +  ThreadWithParam<int*> thread(&AddTwo, &i, NULL);
  1.1037 +  thread.Join();
  1.1038 +  EXPECT_EQ(42, i);
  1.1039 +}
  1.1040 +
  1.1041 +TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
  1.1042 +  // AssertHeld() is flaky only in the presence of multiple threads accessing
  1.1043 +  // the lock. In this case, the test is robust.
  1.1044 +  EXPECT_DEATH_IF_SUPPORTED({
  1.1045 +    Mutex m;
  1.1046 +    { MutexLock lock(&m); }
  1.1047 +    m.AssertHeld();
  1.1048 +  },
  1.1049 +  "thread .*hold");
  1.1050 +}
  1.1051 +
  1.1052 +TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
  1.1053 +  Mutex m;
  1.1054 +  MutexLock lock(&m);
  1.1055 +  m.AssertHeld();
  1.1056 +}
  1.1057 +
  1.1058 +class AtomicCounterWithMutex {
  1.1059 + public:
  1.1060 +  explicit AtomicCounterWithMutex(Mutex* mutex) :
  1.1061 +    value_(0), mutex_(mutex), random_(42) {}
  1.1062 +
  1.1063 +  void Increment() {
  1.1064 +    MutexLock lock(mutex_);
  1.1065 +    int temp = value_;
  1.1066 +    {
  1.1067 +      // Locking a mutex puts up a memory barrier, preventing reads and
  1.1068 +      // writes to value_ rearranged when observed from other threads.
  1.1069 +      //
  1.1070 +      // We cannot use Mutex and MutexLock here or rely on their memory
  1.1071 +      // barrier functionality as we are testing them here.
  1.1072 +      pthread_mutex_t memory_barrier_mutex;
  1.1073 +      GTEST_CHECK_POSIX_SUCCESS_(
  1.1074 +          pthread_mutex_init(&memory_barrier_mutex, NULL));
  1.1075 +      GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&memory_barrier_mutex));
  1.1076 +
  1.1077 +      SleepMilliseconds(random_.Generate(30));
  1.1078 +
  1.1079 +      GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&memory_barrier_mutex));
  1.1080 +      GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&memory_barrier_mutex));
  1.1081 +    }
  1.1082 +    value_ = temp + 1;
  1.1083 +  }
  1.1084 +  int value() const { return value_; }
  1.1085 +
  1.1086 + private:
  1.1087 +  volatile int value_;
  1.1088 +  Mutex* const mutex_;  // Protects value_.
  1.1089 +  Random       random_;
  1.1090 +};
  1.1091 +
  1.1092 +void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
  1.1093 +  for (int i = 0; i < param.second; ++i)
  1.1094 +      param.first->Increment();
  1.1095 +}
  1.1096 +
  1.1097 +// Tests that the mutex only lets one thread at a time to lock it.
  1.1098 +TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
  1.1099 +  Mutex mutex;
  1.1100 +  AtomicCounterWithMutex locked_counter(&mutex);
  1.1101 +
  1.1102 +  typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
  1.1103 +  const int kCycleCount = 20;
  1.1104 +  const int kThreadCount = 7;
  1.1105 +  scoped_ptr<ThreadType> counting_threads[kThreadCount];
  1.1106 +  Notification threads_can_start;
  1.1107 +  // Creates and runs kThreadCount threads that increment locked_counter
  1.1108 +  // kCycleCount times each.
  1.1109 +  for (int i = 0; i < kThreadCount; ++i) {
  1.1110 +    counting_threads[i].reset(new ThreadType(&CountingThreadFunc,
  1.1111 +                                             make_pair(&locked_counter,
  1.1112 +                                                       kCycleCount),
  1.1113 +                                             &threads_can_start));
  1.1114 +  }
  1.1115 +  threads_can_start.Notify();
  1.1116 +  for (int i = 0; i < kThreadCount; ++i)
  1.1117 +    counting_threads[i]->Join();
  1.1118 +
  1.1119 +  // If the mutex lets more than one thread to increment the counter at a
  1.1120 +  // time, they are likely to encounter a race condition and have some
  1.1121 +  // increments overwritten, resulting in the lower then expected counter
  1.1122 +  // value.
  1.1123 +  EXPECT_EQ(kCycleCount * kThreadCount, locked_counter.value());
  1.1124 +}
  1.1125 +
  1.1126 +template <typename T>
  1.1127 +void RunFromThread(void (func)(T), T param) {
  1.1128 +  ThreadWithParam<T> thread(func, param, NULL);
  1.1129 +  thread.Join();
  1.1130 +}
  1.1131 +
  1.1132 +void RetrieveThreadLocalValue(pair<ThreadLocal<String>*, String*> param) {
  1.1133 +  *param.second = param.first->get();
  1.1134 +}
  1.1135 +
  1.1136 +TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
  1.1137 +  ThreadLocal<String> thread_local_string("foo");
  1.1138 +  EXPECT_STREQ("foo", thread_local_string.get().c_str());
  1.1139 +
  1.1140 +  thread_local_string.set("bar");
  1.1141 +  EXPECT_STREQ("bar", thread_local_string.get().c_str());
  1.1142 +
  1.1143 +  String result;
  1.1144 +  RunFromThread(&RetrieveThreadLocalValue,
  1.1145 +                make_pair(&thread_local_string, &result));
  1.1146 +  EXPECT_STREQ("foo", result.c_str());
  1.1147 +}
  1.1148 +
  1.1149 +// DestructorTracker keeps track of whether its instances have been
  1.1150 +// destroyed.
  1.1151 +static std::vector<bool> g_destroyed;
  1.1152 +
  1.1153 +class DestructorTracker {
  1.1154 + public:
  1.1155 +  DestructorTracker() : index_(GetNewIndex()) {}
  1.1156 +  DestructorTracker(const DestructorTracker& /* rhs */)
  1.1157 +      : index_(GetNewIndex()) {}
  1.1158 +  ~DestructorTracker() {
  1.1159 +    // We never access g_destroyed concurrently, so we don't need to
  1.1160 +    // protect the write operation under a mutex.
  1.1161 +    g_destroyed[index_] = true;
  1.1162 +  }
  1.1163 +
  1.1164 + private:
  1.1165 +  static int GetNewIndex() {
  1.1166 +    g_destroyed.push_back(false);
  1.1167 +    return g_destroyed.size() - 1;
  1.1168 +  }
  1.1169 +  const int index_;
  1.1170 +};
  1.1171 +
  1.1172 +typedef ThreadLocal<DestructorTracker>* ThreadParam;
  1.1173 +
  1.1174 +void CallThreadLocalGet(ThreadParam thread_local_param) {
  1.1175 +  thread_local_param->get();
  1.1176 +}
  1.1177 +
  1.1178 +// Tests that when a ThreadLocal object dies in a thread, it destroys
  1.1179 +// the managed object for that thread.
  1.1180 +TEST(ThreadLocalTest, DestroysManagedObjectForOwnThreadWhenDying) {
  1.1181 +  g_destroyed.clear();
  1.1182 +
  1.1183 +  {
  1.1184 +    // The next line default constructs a DestructorTracker object as
  1.1185 +    // the default value of objects managed by thread_local_tracker.
  1.1186 +    ThreadLocal<DestructorTracker> thread_local_tracker;
  1.1187 +    ASSERT_EQ(1U, g_destroyed.size());
  1.1188 +    ASSERT_FALSE(g_destroyed[0]);
  1.1189 +
  1.1190 +    // This creates another DestructorTracker object for the main thread.
  1.1191 +    thread_local_tracker.get();
  1.1192 +    ASSERT_EQ(2U, g_destroyed.size());
  1.1193 +    ASSERT_FALSE(g_destroyed[0]);
  1.1194 +    ASSERT_FALSE(g_destroyed[1]);
  1.1195 +  }
  1.1196 +
  1.1197 +  // Now thread_local_tracker has died.  It should have destroyed both the
  1.1198 +  // default value shared by all threads and the value for the main
  1.1199 +  // thread.
  1.1200 +  ASSERT_EQ(2U, g_destroyed.size());
  1.1201 +  EXPECT_TRUE(g_destroyed[0]);
  1.1202 +  EXPECT_TRUE(g_destroyed[1]);
  1.1203 +
  1.1204 +  g_destroyed.clear();
  1.1205 +}
  1.1206 +
  1.1207 +// Tests that when a thread exits, the thread-local object for that
  1.1208 +// thread is destroyed.
  1.1209 +TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
  1.1210 +  g_destroyed.clear();
  1.1211 +
  1.1212 +  {
  1.1213 +    // The next line default constructs a DestructorTracker object as
  1.1214 +    // the default value of objects managed by thread_local_tracker.
  1.1215 +    ThreadLocal<DestructorTracker> thread_local_tracker;
  1.1216 +    ASSERT_EQ(1U, g_destroyed.size());
  1.1217 +    ASSERT_FALSE(g_destroyed[0]);
  1.1218 +
  1.1219 +    // This creates another DestructorTracker object in the new thread.
  1.1220 +    ThreadWithParam<ThreadParam> thread(
  1.1221 +        &CallThreadLocalGet, &thread_local_tracker, NULL);
  1.1222 +    thread.Join();
  1.1223 +
  1.1224 +    // Now the new thread has exited.  The per-thread object for it
  1.1225 +    // should have been destroyed.
  1.1226 +    ASSERT_EQ(2U, g_destroyed.size());
  1.1227 +    ASSERT_FALSE(g_destroyed[0]);
  1.1228 +    ASSERT_TRUE(g_destroyed[1]);
  1.1229 +  }
  1.1230 +
  1.1231 +  // Now thread_local_tracker has died.  The default value should have been
  1.1232 +  // destroyed too.
  1.1233 +  ASSERT_EQ(2U, g_destroyed.size());
  1.1234 +  EXPECT_TRUE(g_destroyed[0]);
  1.1235 +  EXPECT_TRUE(g_destroyed[1]);
  1.1236 +
  1.1237 +  g_destroyed.clear();
  1.1238 +}
  1.1239 +
  1.1240 +TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
  1.1241 +  ThreadLocal<String> thread_local_string;
  1.1242 +  thread_local_string.set("Foo");
  1.1243 +  EXPECT_STREQ("Foo", thread_local_string.get().c_str());
  1.1244 +
  1.1245 +  String result;
  1.1246 +  RunFromThread(&RetrieveThreadLocalValue,
  1.1247 +                make_pair(&thread_local_string, &result));
  1.1248 +  EXPECT_TRUE(result.c_str() == NULL);
  1.1249 +}
  1.1250 +
  1.1251 +#endif  // GTEST_IS_THREADSAFE
  1.1252 +
  1.1253 +}  // namespace internal
  1.1254 +}  // namespace testing

mercurial