/Unittests/googletest/test/gtest-port_test.cc

http://unladen-swallow.googlecode.com/ · C++ · 699 lines · 524 code · 114 blank · 61 comment · 9 complexity · ace832568da81d4b480471bb087d64b5 MD5 · raw file

  1. // Copyright 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Authors: vladl@google.com (Vlad Losev), wan@google.com (Zhanyong Wan)
  31. //
  32. // This file tests the internal cross-platform support utilities.
  33. #include <gtest/internal/gtest-port.h>
  34. #if GTEST_OS_MAC
  35. #include <pthread.h>
  36. #include <time.h>
  37. #endif // GTEST_OS_MAC
  38. #include <gtest/gtest.h>
  39. #include <gtest/gtest-spi.h>
  40. // Indicates that this translation unit is part of Google Test's
  41. // implementation. It must come before gtest-internal-inl.h is
  42. // included, or there will be a compiler error. This trick is to
  43. // prevent a user from accidentally including gtest-internal-inl.h in
  44. // his code.
  45. #define GTEST_IMPLEMENTATION_ 1
  46. #include "src/gtest-internal-inl.h"
  47. #undef GTEST_IMPLEMENTATION_
  48. namespace testing {
  49. namespace internal {
  50. TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
  51. if (AlwaysFalse())
  52. GTEST_CHECK_(false) << "This should never be executed; "
  53. "It's a compilation test only.";
  54. if (AlwaysTrue())
  55. GTEST_CHECK_(true);
  56. else
  57. ; // NOLINT
  58. if (AlwaysFalse())
  59. ; // NOLINT
  60. else
  61. GTEST_CHECK_(true) << "";
  62. }
  63. TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
  64. switch (0) {
  65. case 1:
  66. break;
  67. default:
  68. GTEST_CHECK_(true);
  69. }
  70. switch(0)
  71. case 0:
  72. GTEST_CHECK_(true) << "Check failed in switch case";
  73. }
  74. #if GTEST_OS_MAC
  75. void* ThreadFunc(void* data) {
  76. pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data);
  77. pthread_mutex_lock(mutex);
  78. pthread_mutex_unlock(mutex);
  79. return NULL;
  80. }
  81. TEST(GetThreadCountTest, ReturnsCorrectValue) {
  82. EXPECT_EQ(1U, GetThreadCount());
  83. pthread_mutex_t mutex;
  84. pthread_attr_t attr;
  85. pthread_t thread_id;
  86. // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
  87. // destruction.
  88. pthread_mutex_init(&mutex, NULL);
  89. pthread_mutex_lock(&mutex);
  90. ASSERT_EQ(0, pthread_attr_init(&attr));
  91. ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
  92. const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
  93. ASSERT_EQ(0, pthread_attr_destroy(&attr));
  94. ASSERT_EQ(0, status);
  95. EXPECT_EQ(2U, GetThreadCount());
  96. pthread_mutex_unlock(&mutex);
  97. void* dummy;
  98. ASSERT_EQ(0, pthread_join(thread_id, &dummy));
  99. // MacOS X may not immediately report the updated thread count after
  100. // joining a thread, causing flakiness in this test. To counter that, we
  101. // wait for up to .5 seconds for the OS to report the correct value.
  102. for (int i = 0; i < 5; ++i) {
  103. if (GetThreadCount() == 1)
  104. break;
  105. timespec time;
  106. time.tv_sec = 0;
  107. time.tv_nsec = 100L * 1000 * 1000; // .1 seconds.
  108. nanosleep(&time, NULL);
  109. }
  110. EXPECT_EQ(1U, GetThreadCount());
  111. pthread_mutex_destroy(&mutex);
  112. }
  113. #else
  114. TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
  115. EXPECT_EQ(0U, GetThreadCount());
  116. }
  117. #endif // GTEST_OS_MAC
  118. TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
  119. const bool a_false_condition = false;
  120. const char regex[] =
  121. #ifdef _MSC_VER
  122. "gtest-port_test\\.cc\\(\\d+\\):"
  123. #else
  124. "gtest-port_test\\.cc:[0-9]+"
  125. #endif // _MSC_VER
  126. ".*a_false_condition.*Extra info.*";
  127. EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info",
  128. regex);
  129. }
  130. #if GTEST_HAS_DEATH_TEST
  131. TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
  132. EXPECT_EXIT({
  133. GTEST_CHECK_(true) << "Extra info";
  134. ::std::cerr << "Success\n";
  135. exit(0); },
  136. ::testing::ExitedWithCode(0), "Success");
  137. }
  138. #endif // GTEST_HAS_DEATH_TEST
  139. #if GTEST_USES_POSIX_RE
  140. template <typename Str>
  141. class RETest : public ::testing::Test {};
  142. // Defines StringTypes as the list of all string types that class RE
  143. // supports.
  144. typedef testing::Types<
  145. #if GTEST_HAS_STD_STRING
  146. ::std::string,
  147. #endif // GTEST_HAS_STD_STRING
  148. #if GTEST_HAS_GLOBAL_STRING
  149. ::string,
  150. #endif // GTEST_HAS_GLOBAL_STRING
  151. const char*> StringTypes;
  152. TYPED_TEST_CASE(RETest, StringTypes);
  153. // Tests RE's implicit constructors.
  154. TYPED_TEST(RETest, ImplicitConstructorWorks) {
  155. const RE empty(TypeParam(""));
  156. EXPECT_STREQ("", empty.pattern());
  157. const RE simple(TypeParam("hello"));
  158. EXPECT_STREQ("hello", simple.pattern());
  159. const RE normal(TypeParam(".*(\\w+)"));
  160. EXPECT_STREQ(".*(\\w+)", normal.pattern());
  161. }
  162. // Tests that RE's constructors reject invalid regular expressions.
  163. TYPED_TEST(RETest, RejectsInvalidRegex) {
  164. EXPECT_NONFATAL_FAILURE({
  165. const RE invalid(TypeParam("?"));
  166. }, "\"?\" is not a valid POSIX Extended regular expression.");
  167. }
  168. // Tests RE::FullMatch().
  169. TYPED_TEST(RETest, FullMatchWorks) {
  170. const RE empty(TypeParam(""));
  171. EXPECT_TRUE(RE::FullMatch(TypeParam(""), empty));
  172. EXPECT_FALSE(RE::FullMatch(TypeParam("a"), empty));
  173. const RE re(TypeParam("a.*z"));
  174. EXPECT_TRUE(RE::FullMatch(TypeParam("az"), re));
  175. EXPECT_TRUE(RE::FullMatch(TypeParam("axyz"), re));
  176. EXPECT_FALSE(RE::FullMatch(TypeParam("baz"), re));
  177. EXPECT_FALSE(RE::FullMatch(TypeParam("azy"), re));
  178. }
  179. // Tests RE::PartialMatch().
  180. TYPED_TEST(RETest, PartialMatchWorks) {
  181. const RE empty(TypeParam(""));
  182. EXPECT_TRUE(RE::PartialMatch(TypeParam(""), empty));
  183. EXPECT_TRUE(RE::PartialMatch(TypeParam("a"), empty));
  184. const RE re(TypeParam("a.*z"));
  185. EXPECT_TRUE(RE::PartialMatch(TypeParam("az"), re));
  186. EXPECT_TRUE(RE::PartialMatch(TypeParam("axyz"), re));
  187. EXPECT_TRUE(RE::PartialMatch(TypeParam("baz"), re));
  188. EXPECT_TRUE(RE::PartialMatch(TypeParam("azy"), re));
  189. EXPECT_FALSE(RE::PartialMatch(TypeParam("zza"), re));
  190. }
  191. #elif GTEST_USES_SIMPLE_RE
  192. TEST(IsInSetTest, NulCharIsNotInAnySet) {
  193. EXPECT_FALSE(IsInSet('\0', ""));
  194. EXPECT_FALSE(IsInSet('\0', "\0"));
  195. EXPECT_FALSE(IsInSet('\0', "a"));
  196. }
  197. TEST(IsInSetTest, WorksForNonNulChars) {
  198. EXPECT_FALSE(IsInSet('a', "Ab"));
  199. EXPECT_FALSE(IsInSet('c', ""));
  200. EXPECT_TRUE(IsInSet('b', "bcd"));
  201. EXPECT_TRUE(IsInSet('b', "ab"));
  202. }
  203. TEST(IsDigitTest, IsFalseForNonDigit) {
  204. EXPECT_FALSE(IsDigit('\0'));
  205. EXPECT_FALSE(IsDigit(' '));
  206. EXPECT_FALSE(IsDigit('+'));
  207. EXPECT_FALSE(IsDigit('-'));
  208. EXPECT_FALSE(IsDigit('.'));
  209. EXPECT_FALSE(IsDigit('a'));
  210. }
  211. TEST(IsDigitTest, IsTrueForDigit) {
  212. EXPECT_TRUE(IsDigit('0'));
  213. EXPECT_TRUE(IsDigit('1'));
  214. EXPECT_TRUE(IsDigit('5'));
  215. EXPECT_TRUE(IsDigit('9'));
  216. }
  217. TEST(IsPunctTest, IsFalseForNonPunct) {
  218. EXPECT_FALSE(IsPunct('\0'));
  219. EXPECT_FALSE(IsPunct(' '));
  220. EXPECT_FALSE(IsPunct('\n'));
  221. EXPECT_FALSE(IsPunct('a'));
  222. EXPECT_FALSE(IsPunct('0'));
  223. }
  224. TEST(IsPunctTest, IsTrueForPunct) {
  225. for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
  226. EXPECT_PRED1(IsPunct, *p);
  227. }
  228. }
  229. TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
  230. EXPECT_FALSE(IsRepeat('\0'));
  231. EXPECT_FALSE(IsRepeat(' '));
  232. EXPECT_FALSE(IsRepeat('a'));
  233. EXPECT_FALSE(IsRepeat('1'));
  234. EXPECT_FALSE(IsRepeat('-'));
  235. }
  236. TEST(IsRepeatTest, IsTrueForRepeatChar) {
  237. EXPECT_TRUE(IsRepeat('?'));
  238. EXPECT_TRUE(IsRepeat('*'));
  239. EXPECT_TRUE(IsRepeat('+'));
  240. }
  241. TEST(IsWhiteSpaceTest, IsFalseForNonWhiteSpace) {
  242. EXPECT_FALSE(IsWhiteSpace('\0'));
  243. EXPECT_FALSE(IsWhiteSpace('a'));
  244. EXPECT_FALSE(IsWhiteSpace('1'));
  245. EXPECT_FALSE(IsWhiteSpace('+'));
  246. EXPECT_FALSE(IsWhiteSpace('_'));
  247. }
  248. TEST(IsWhiteSpaceTest, IsTrueForWhiteSpace) {
  249. EXPECT_TRUE(IsWhiteSpace(' '));
  250. EXPECT_TRUE(IsWhiteSpace('\n'));
  251. EXPECT_TRUE(IsWhiteSpace('\r'));
  252. EXPECT_TRUE(IsWhiteSpace('\t'));
  253. EXPECT_TRUE(IsWhiteSpace('\v'));
  254. EXPECT_TRUE(IsWhiteSpace('\f'));
  255. }
  256. TEST(IsWordCharTest, IsFalseForNonWordChar) {
  257. EXPECT_FALSE(IsWordChar('\0'));
  258. EXPECT_FALSE(IsWordChar('+'));
  259. EXPECT_FALSE(IsWordChar('.'));
  260. EXPECT_FALSE(IsWordChar(' '));
  261. EXPECT_FALSE(IsWordChar('\n'));
  262. }
  263. TEST(IsWordCharTest, IsTrueForLetter) {
  264. EXPECT_TRUE(IsWordChar('a'));
  265. EXPECT_TRUE(IsWordChar('b'));
  266. EXPECT_TRUE(IsWordChar('A'));
  267. EXPECT_TRUE(IsWordChar('Z'));
  268. }
  269. TEST(IsWordCharTest, IsTrueForDigit) {
  270. EXPECT_TRUE(IsWordChar('0'));
  271. EXPECT_TRUE(IsWordChar('1'));
  272. EXPECT_TRUE(IsWordChar('7'));
  273. EXPECT_TRUE(IsWordChar('9'));
  274. }
  275. TEST(IsWordCharTest, IsTrueForUnderscore) {
  276. EXPECT_TRUE(IsWordChar('_'));
  277. }
  278. TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
  279. EXPECT_FALSE(IsValidEscape('\0'));
  280. EXPECT_FALSE(IsValidEscape('\007'));
  281. }
  282. TEST(IsValidEscapeTest, IsFalseForDigit) {
  283. EXPECT_FALSE(IsValidEscape('0'));
  284. EXPECT_FALSE(IsValidEscape('9'));
  285. }
  286. TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
  287. EXPECT_FALSE(IsValidEscape(' '));
  288. EXPECT_FALSE(IsValidEscape('\n'));
  289. }
  290. TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
  291. EXPECT_FALSE(IsValidEscape('a'));
  292. EXPECT_FALSE(IsValidEscape('Z'));
  293. }
  294. TEST(IsValidEscapeTest, IsTrueForPunct) {
  295. EXPECT_TRUE(IsValidEscape('.'));
  296. EXPECT_TRUE(IsValidEscape('-'));
  297. EXPECT_TRUE(IsValidEscape('^'));
  298. EXPECT_TRUE(IsValidEscape('$'));
  299. EXPECT_TRUE(IsValidEscape('('));
  300. EXPECT_TRUE(IsValidEscape(']'));
  301. EXPECT_TRUE(IsValidEscape('{'));
  302. EXPECT_TRUE(IsValidEscape('|'));
  303. }
  304. TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
  305. EXPECT_TRUE(IsValidEscape('d'));
  306. EXPECT_TRUE(IsValidEscape('D'));
  307. EXPECT_TRUE(IsValidEscape('s'));
  308. EXPECT_TRUE(IsValidEscape('S'));
  309. EXPECT_TRUE(IsValidEscape('w'));
  310. EXPECT_TRUE(IsValidEscape('W'));
  311. }
  312. TEST(AtomMatchesCharTest, EscapedPunct) {
  313. EXPECT_FALSE(AtomMatchesChar(true, '\\', '\0'));
  314. EXPECT_FALSE(AtomMatchesChar(true, '\\', ' '));
  315. EXPECT_FALSE(AtomMatchesChar(true, '_', '.'));
  316. EXPECT_FALSE(AtomMatchesChar(true, '.', 'a'));
  317. EXPECT_TRUE(AtomMatchesChar(true, '\\', '\\'));
  318. EXPECT_TRUE(AtomMatchesChar(true, '_', '_'));
  319. EXPECT_TRUE(AtomMatchesChar(true, '+', '+'));
  320. EXPECT_TRUE(AtomMatchesChar(true, '.', '.'));
  321. }
  322. TEST(AtomMatchesCharTest, Escaped_d) {
  323. EXPECT_FALSE(AtomMatchesChar(true, 'd', '\0'));
  324. EXPECT_FALSE(AtomMatchesChar(true, 'd', 'a'));
  325. EXPECT_FALSE(AtomMatchesChar(true, 'd', '.'));
  326. EXPECT_TRUE(AtomMatchesChar(true, 'd', '0'));
  327. EXPECT_TRUE(AtomMatchesChar(true, 'd', '9'));
  328. }
  329. TEST(AtomMatchesCharTest, Escaped_D) {
  330. EXPECT_FALSE(AtomMatchesChar(true, 'D', '0'));
  331. EXPECT_FALSE(AtomMatchesChar(true, 'D', '9'));
  332. EXPECT_TRUE(AtomMatchesChar(true, 'D', '\0'));
  333. EXPECT_TRUE(AtomMatchesChar(true, 'D', 'a'));
  334. EXPECT_TRUE(AtomMatchesChar(true, 'D', '-'));
  335. }
  336. TEST(AtomMatchesCharTest, Escaped_s) {
  337. EXPECT_FALSE(AtomMatchesChar(true, 's', '\0'));
  338. EXPECT_FALSE(AtomMatchesChar(true, 's', 'a'));
  339. EXPECT_FALSE(AtomMatchesChar(true, 's', '.'));
  340. EXPECT_FALSE(AtomMatchesChar(true, 's', '9'));
  341. EXPECT_TRUE(AtomMatchesChar(true, 's', ' '));
  342. EXPECT_TRUE(AtomMatchesChar(true, 's', '\n'));
  343. EXPECT_TRUE(AtomMatchesChar(true, 's', '\t'));
  344. }
  345. TEST(AtomMatchesCharTest, Escaped_S) {
  346. EXPECT_FALSE(AtomMatchesChar(true, 'S', ' '));
  347. EXPECT_FALSE(AtomMatchesChar(true, 'S', '\r'));
  348. EXPECT_TRUE(AtomMatchesChar(true, 'S', '\0'));
  349. EXPECT_TRUE(AtomMatchesChar(true, 'S', 'a'));
  350. EXPECT_TRUE(AtomMatchesChar(true, 'S', '9'));
  351. }
  352. TEST(AtomMatchesCharTest, Escaped_w) {
  353. EXPECT_FALSE(AtomMatchesChar(true, 'w', '\0'));
  354. EXPECT_FALSE(AtomMatchesChar(true, 'w', '+'));
  355. EXPECT_FALSE(AtomMatchesChar(true, 'w', ' '));
  356. EXPECT_FALSE(AtomMatchesChar(true, 'w', '\n'));
  357. EXPECT_TRUE(AtomMatchesChar(true, 'w', '0'));
  358. EXPECT_TRUE(AtomMatchesChar(true, 'w', 'b'));
  359. EXPECT_TRUE(AtomMatchesChar(true, 'w', 'C'));
  360. EXPECT_TRUE(AtomMatchesChar(true, 'w', '_'));
  361. }
  362. TEST(AtomMatchesCharTest, Escaped_W) {
  363. EXPECT_FALSE(AtomMatchesChar(true, 'W', 'A'));
  364. EXPECT_FALSE(AtomMatchesChar(true, 'W', 'b'));
  365. EXPECT_FALSE(AtomMatchesChar(true, 'W', '9'));
  366. EXPECT_FALSE(AtomMatchesChar(true, 'W', '_'));
  367. EXPECT_TRUE(AtomMatchesChar(true, 'W', '\0'));
  368. EXPECT_TRUE(AtomMatchesChar(true, 'W', '*'));
  369. EXPECT_TRUE(AtomMatchesChar(true, 'W', '\n'));
  370. }
  371. TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
  372. EXPECT_FALSE(AtomMatchesChar(true, 'f', '\0'));
  373. EXPECT_FALSE(AtomMatchesChar(true, 'f', '\n'));
  374. EXPECT_FALSE(AtomMatchesChar(true, 'n', '\0'));
  375. EXPECT_FALSE(AtomMatchesChar(true, 'n', '\r'));
  376. EXPECT_FALSE(AtomMatchesChar(true, 'r', '\0'));
  377. EXPECT_FALSE(AtomMatchesChar(true, 'r', 'a'));
  378. EXPECT_FALSE(AtomMatchesChar(true, 't', '\0'));
  379. EXPECT_FALSE(AtomMatchesChar(true, 't', 't'));
  380. EXPECT_FALSE(AtomMatchesChar(true, 'v', '\0'));
  381. EXPECT_FALSE(AtomMatchesChar(true, 'v', '\f'));
  382. EXPECT_TRUE(AtomMatchesChar(true, 'f', '\f'));
  383. EXPECT_TRUE(AtomMatchesChar(true, 'n', '\n'));
  384. EXPECT_TRUE(AtomMatchesChar(true, 'r', '\r'));
  385. EXPECT_TRUE(AtomMatchesChar(true, 't', '\t'));
  386. EXPECT_TRUE(AtomMatchesChar(true, 'v', '\v'));
  387. }
  388. TEST(AtomMatchesCharTest, UnescapedDot) {
  389. EXPECT_FALSE(AtomMatchesChar(false, '.', '\n'));
  390. EXPECT_TRUE(AtomMatchesChar(false, '.', '\0'));
  391. EXPECT_TRUE(AtomMatchesChar(false, '.', '.'));
  392. EXPECT_TRUE(AtomMatchesChar(false, '.', 'a'));
  393. EXPECT_TRUE(AtomMatchesChar(false, '.', ' '));
  394. }
  395. TEST(AtomMatchesCharTest, UnescapedChar) {
  396. EXPECT_FALSE(AtomMatchesChar(false, 'a', '\0'));
  397. EXPECT_FALSE(AtomMatchesChar(false, 'a', 'b'));
  398. EXPECT_FALSE(AtomMatchesChar(false, '$', 'a'));
  399. EXPECT_TRUE(AtomMatchesChar(false, '$', '$'));
  400. EXPECT_TRUE(AtomMatchesChar(false, '5', '5'));
  401. EXPECT_TRUE(AtomMatchesChar(false, 'Z', 'Z'));
  402. }
  403. TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
  404. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)),
  405. "NULL is not a valid simple regular expression");
  406. EXPECT_NONFATAL_FAILURE(
  407. ASSERT_FALSE(ValidateRegex("a\\")),
  408. "Syntax error at index 1 in simple regular expression \"a\\\": ");
  409. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a\\")),
  410. "'\\' cannot appear at the end");
  411. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\n\\")),
  412. "'\\' cannot appear at the end");
  413. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\s\\hb")),
  414. "invalid escape sequence \"\\h\"");
  415. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^^")),
  416. "'^' can only appear at the beginning");
  417. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(".*^b")),
  418. "'^' can only appear at the beginning");
  419. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("$$")),
  420. "'$' can only appear at the end");
  421. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^$a")),
  422. "'$' can only appear at the end");
  423. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a(b")),
  424. "'(' is unsupported");
  425. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("ab)")),
  426. "')' is unsupported");
  427. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("[ab")),
  428. "'[' is unsupported");
  429. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a{2")),
  430. "'{' is unsupported");
  431. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("?")),
  432. "'?' can only follow a repeatable token");
  433. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^*")),
  434. "'*' can only follow a repeatable token");
  435. EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("5*+")),
  436. "'+' can only follow a repeatable token");
  437. }
  438. TEST(ValidateRegexTest, ReturnsTrueForValid) {
  439. EXPECT_TRUE(ValidateRegex(""));
  440. EXPECT_TRUE(ValidateRegex("a"));
  441. EXPECT_TRUE(ValidateRegex(".*"));
  442. EXPECT_TRUE(ValidateRegex("^a_+"));
  443. EXPECT_TRUE(ValidateRegex("^a\\t\\&?"));
  444. EXPECT_TRUE(ValidateRegex("09*$"));
  445. EXPECT_TRUE(ValidateRegex("^Z$"));
  446. EXPECT_TRUE(ValidateRegex("a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
  447. }
  448. TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
  449. EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "a", "ba"));
  450. // Repeating more than once.
  451. EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "aab"));
  452. // Repeating zero times.
  453. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ba"));
  454. // Repeating once.
  455. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ab"));
  456. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '#', '?', ".", "##"));
  457. }
  458. TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
  459. EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '*', "a$", "baab"));
  460. // Repeating zero times.
  461. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "bc"));
  462. // Repeating once.
  463. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "abc"));
  464. // Repeating more than once.
  465. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '*', "-", "ab_1-g"));
  466. }
  467. TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
  468. EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "a$", "baab"));
  469. // Repeating zero times.
  470. EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "bc"));
  471. // Repeating once.
  472. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "abc"));
  473. // Repeating more than once.
  474. EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '+', "-", "ab_1-g"));
  475. }
  476. TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
  477. EXPECT_TRUE(MatchRegexAtHead("", ""));
  478. EXPECT_TRUE(MatchRegexAtHead("", "ab"));
  479. }
  480. TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
  481. EXPECT_FALSE(MatchRegexAtHead("$", "a"));
  482. EXPECT_TRUE(MatchRegexAtHead("$", ""));
  483. EXPECT_TRUE(MatchRegexAtHead("a$", "a"));
  484. }
  485. TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
  486. EXPECT_FALSE(MatchRegexAtHead("\\w", "+"));
  487. EXPECT_FALSE(MatchRegexAtHead("\\W", "ab"));
  488. EXPECT_TRUE(MatchRegexAtHead("\\sa", "\nab"));
  489. EXPECT_TRUE(MatchRegexAtHead("\\d", "1a"));
  490. }
  491. TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
  492. EXPECT_FALSE(MatchRegexAtHead(".+a", "abc"));
  493. EXPECT_FALSE(MatchRegexAtHead("a?b", "aab"));
  494. EXPECT_TRUE(MatchRegexAtHead(".*a", "bc12-ab"));
  495. EXPECT_TRUE(MatchRegexAtHead("a?b", "b"));
  496. EXPECT_TRUE(MatchRegexAtHead("a?b", "ab"));
  497. }
  498. TEST(MatchRegexAtHeadTest,
  499. WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
  500. EXPECT_FALSE(MatchRegexAtHead("\\.+a", "abc"));
  501. EXPECT_FALSE(MatchRegexAtHead("\\s?b", " b"));
  502. EXPECT_TRUE(MatchRegexAtHead("\\(*a", "((((ab"));
  503. EXPECT_TRUE(MatchRegexAtHead("\\^?b", "^b"));
  504. EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "b"));
  505. EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "\\b"));
  506. }
  507. TEST(MatchRegexAtHeadTest, MatchesSequentially) {
  508. EXPECT_FALSE(MatchRegexAtHead("ab.*c", "acabc"));
  509. EXPECT_TRUE(MatchRegexAtHead("ab.*c", "ab-fsc"));
  510. }
  511. TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
  512. EXPECT_FALSE(MatchRegexAnywhere("", NULL));
  513. }
  514. TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
  515. EXPECT_FALSE(MatchRegexAnywhere("^a", "ba"));
  516. EXPECT_FALSE(MatchRegexAnywhere("^$", "a"));
  517. EXPECT_TRUE(MatchRegexAnywhere("^a", "ab"));
  518. EXPECT_TRUE(MatchRegexAnywhere("^", "ab"));
  519. EXPECT_TRUE(MatchRegexAnywhere("^$", ""));
  520. }
  521. TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
  522. EXPECT_FALSE(MatchRegexAnywhere("a", "bcde123"));
  523. EXPECT_FALSE(MatchRegexAnywhere("a.+a", "--aa88888888"));
  524. }
  525. TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
  526. EXPECT_TRUE(MatchRegexAnywhere("\\w+", "ab1_ - 5"));
  527. EXPECT_TRUE(MatchRegexAnywhere(".*=", "="));
  528. EXPECT_TRUE(MatchRegexAnywhere("x.*ab?.*bc", "xaaabc"));
  529. }
  530. TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
  531. EXPECT_TRUE(MatchRegexAnywhere("\\w+", "$$$ ab1_ - 5"));
  532. EXPECT_TRUE(MatchRegexAnywhere("\\.+=", "= ...="));
  533. }
  534. // Tests RE's implicit constructors.
  535. TEST(RETest, ImplicitConstructorWorks) {
  536. const RE empty("");
  537. EXPECT_STREQ("", empty.pattern());
  538. const RE simple("hello");
  539. EXPECT_STREQ("hello", simple.pattern());
  540. }
  541. // Tests that RE's constructors reject invalid regular expressions.
  542. TEST(RETest, RejectsInvalidRegex) {
  543. EXPECT_NONFATAL_FAILURE({
  544. const RE normal(NULL);
  545. }, "NULL is not a valid simple regular expression");
  546. EXPECT_NONFATAL_FAILURE({
  547. const RE normal(".*(\\w+");
  548. }, "'(' is unsupported");
  549. EXPECT_NONFATAL_FAILURE({
  550. const RE invalid("^?");
  551. }, "'?' can only follow a repeatable token");
  552. }
  553. // Tests RE::FullMatch().
  554. TEST(RETest, FullMatchWorks) {
  555. const RE empty("");
  556. EXPECT_TRUE(RE::FullMatch("", empty));
  557. EXPECT_FALSE(RE::FullMatch("a", empty));
  558. const RE re1("a");
  559. EXPECT_TRUE(RE::FullMatch("a", re1));
  560. const RE re("a.*z");
  561. EXPECT_TRUE(RE::FullMatch("az", re));
  562. EXPECT_TRUE(RE::FullMatch("axyz", re));
  563. EXPECT_FALSE(RE::FullMatch("baz", re));
  564. EXPECT_FALSE(RE::FullMatch("azy", re));
  565. }
  566. // Tests RE::PartialMatch().
  567. TEST(RETest, PartialMatchWorks) {
  568. const RE empty("");
  569. EXPECT_TRUE(RE::PartialMatch("", empty));
  570. EXPECT_TRUE(RE::PartialMatch("a", empty));
  571. const RE re("a.*z");
  572. EXPECT_TRUE(RE::PartialMatch("az", re));
  573. EXPECT_TRUE(RE::PartialMatch("axyz", re));
  574. EXPECT_TRUE(RE::PartialMatch("baz", re));
  575. EXPECT_TRUE(RE::PartialMatch("azy", re));
  576. EXPECT_FALSE(RE::PartialMatch("zza", re));
  577. }
  578. #endif // GTEST_USES_POSIX_RE
  579. TEST(CaptureStderrTest, CapturesStdErr) {
  580. CaptureStderr();
  581. fprintf(stderr, "abc");
  582. ASSERT_STREQ("abc", GetCapturedStderr().c_str());
  583. }
  584. } // namespace internal
  585. } // namespace testing