• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 #include <gtest/internal/gtest-port.h>
35 
36 #if GTEST_OS_MAC
37 #include <pthread.h>
38 #include <time.h>
39 #endif  // GTEST_OS_MAC
40 
41 #include <gtest/gtest.h>
42 #include <gtest/gtest-spi.h>
43 
44 // Indicates that this translation unit is part of Google Test's
45 // implementation.  It must come before gtest-internal-inl.h is
46 // included, or there will be a compiler error.  This trick is to
47 // prevent a user from accidentally including gtest-internal-inl.h in
48 // his code.
49 #define GTEST_IMPLEMENTATION_ 1
50 #include "src/gtest-internal-inl.h"
51 #undef GTEST_IMPLEMENTATION_
52 
53 namespace testing {
54 namespace internal {
55 
TEST(GtestCheckSyntaxTest,BehavesLikeASingleStatement)56 TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) {
57   if (AlwaysFalse())
58     GTEST_CHECK_(false) << "This should never be executed; "
59                            "It's a compilation test only.";
60 
61   if (AlwaysTrue())
62     GTEST_CHECK_(true);
63   else
64     ;  // NOLINT
65 
66   if (AlwaysFalse())
67     ;  // NOLINT
68   else
69     GTEST_CHECK_(true) << "";
70 }
71 
TEST(GtestCheckSyntaxTest,WorksWithSwitch)72 TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
73   switch (0) {
74     case 1:
75       break;
76     default:
77       GTEST_CHECK_(true);
78   }
79 
80   switch(0)
81     case 0:
82       GTEST_CHECK_(true) << "Check failed in switch case";
83 }
84 
85 #if GTEST_OS_MAC
ThreadFunc(void * data)86 void* ThreadFunc(void* data) {
87   pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data);
88   pthread_mutex_lock(mutex);
89   pthread_mutex_unlock(mutex);
90   return NULL;
91 }
92 
TEST(GetThreadCountTest,ReturnsCorrectValue)93 TEST(GetThreadCountTest, ReturnsCorrectValue) {
94   EXPECT_EQ(1U, GetThreadCount());
95   pthread_mutex_t mutex;
96   pthread_attr_t  attr;
97   pthread_t       thread_id;
98 
99   // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
100   // destruction.
101   pthread_mutex_init(&mutex, NULL);
102   pthread_mutex_lock(&mutex);
103   ASSERT_EQ(0, pthread_attr_init(&attr));
104   ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
105 
106   const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
107   ASSERT_EQ(0, pthread_attr_destroy(&attr));
108   ASSERT_EQ(0, status);
109   EXPECT_EQ(2U, GetThreadCount());
110   pthread_mutex_unlock(&mutex);
111 
112   void* dummy;
113   ASSERT_EQ(0, pthread_join(thread_id, &dummy));
114 
115   // MacOS X may not immediately report the updated thread count after
116   // joining a thread, causing flakiness in this test. To counter that, we
117   // wait for up to .5 seconds for the OS to report the correct value.
118   for (int i = 0; i < 5; ++i) {
119     if (GetThreadCount() == 1)
120       break;
121 
122     timespec time;
123     time.tv_sec = 0;
124     time.tv_nsec = 100L * 1000 * 1000;  // .1 seconds.
125     nanosleep(&time, NULL);
126   }
127   EXPECT_EQ(1U, GetThreadCount());
128   pthread_mutex_destroy(&mutex);
129 }
130 #else
TEST(GetThreadCountTest,ReturnsZeroWhenUnableToCountThreads)131 TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
132   EXPECT_EQ(0U, GetThreadCount());
133 }
134 #endif  // GTEST_OS_MAC
135 
TEST(GtestCheckDeathTest,DiesWithCorrectOutputOnFailure)136 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
137   const bool a_false_condition = false;
138   const char regex[] =
139 #ifdef _MSC_VER
140      "gtest-port_test\\.cc\\(\\d+\\):"
141 #else
142      "gtest-port_test\\.cc:[0-9]+"
143 #endif  // _MSC_VER
144      ".*a_false_condition.*Extra info.*";
145 
146   EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info",
147                             regex);
148 }
149 
150 #if GTEST_HAS_DEATH_TEST
151 
TEST(GtestCheckDeathTest,LivesSilentlyOnSuccess)152 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
153   EXPECT_EXIT({
154       GTEST_CHECK_(true) << "Extra info";
155       ::std::cerr << "Success\n";
156       exit(0); },
157       ::testing::ExitedWithCode(0), "Success");
158 }
159 
160 #endif  // GTEST_HAS_DEATH_TEST
161 
162 #if GTEST_USES_POSIX_RE
163 
164 template <typename Str>
165 class RETest : public ::testing::Test {};
166 
167 // Defines StringTypes as the list of all string types that class RE
168 // supports.
169 typedef testing::Types<
170 #if GTEST_HAS_STD_STRING
171     ::std::string,
172 #endif  // GTEST_HAS_STD_STRING
173 #if GTEST_HAS_GLOBAL_STRING
174     ::string,
175 #endif  // GTEST_HAS_GLOBAL_STRING
176     const char*> StringTypes;
177 
178 TYPED_TEST_CASE(RETest, StringTypes);
179 
180 // Tests RE's implicit constructors.
TYPED_TEST(RETest,ImplicitConstructorWorks)181 TYPED_TEST(RETest, ImplicitConstructorWorks) {
182   const RE empty(TypeParam(""));
183   EXPECT_STREQ("", empty.pattern());
184 
185   const RE simple(TypeParam("hello"));
186   EXPECT_STREQ("hello", simple.pattern());
187 
188   const RE normal(TypeParam(".*(\\w+)"));
189   EXPECT_STREQ(".*(\\w+)", normal.pattern());
190 }
191 
192 // Tests that RE's constructors reject invalid regular expressions.
TYPED_TEST(RETest,RejectsInvalidRegex)193 TYPED_TEST(RETest, RejectsInvalidRegex) {
194   EXPECT_NONFATAL_FAILURE({
195     const RE invalid(TypeParam("?"));
196   }, "\"?\" is not a valid POSIX Extended regular expression.");
197 }
198 
199 // Tests RE::FullMatch().
TYPED_TEST(RETest,FullMatchWorks)200 TYPED_TEST(RETest, FullMatchWorks) {
201   const RE empty(TypeParam(""));
202   EXPECT_TRUE(RE::FullMatch(TypeParam(""), empty));
203   EXPECT_FALSE(RE::FullMatch(TypeParam("a"), empty));
204 
205   const RE re(TypeParam("a.*z"));
206   EXPECT_TRUE(RE::FullMatch(TypeParam("az"), re));
207   EXPECT_TRUE(RE::FullMatch(TypeParam("axyz"), re));
208   EXPECT_FALSE(RE::FullMatch(TypeParam("baz"), re));
209   EXPECT_FALSE(RE::FullMatch(TypeParam("azy"), re));
210 }
211 
212 // Tests RE::PartialMatch().
TYPED_TEST(RETest,PartialMatchWorks)213 TYPED_TEST(RETest, PartialMatchWorks) {
214   const RE empty(TypeParam(""));
215   EXPECT_TRUE(RE::PartialMatch(TypeParam(""), empty));
216   EXPECT_TRUE(RE::PartialMatch(TypeParam("a"), empty));
217 
218   const RE re(TypeParam("a.*z"));
219   EXPECT_TRUE(RE::PartialMatch(TypeParam("az"), re));
220   EXPECT_TRUE(RE::PartialMatch(TypeParam("axyz"), re));
221   EXPECT_TRUE(RE::PartialMatch(TypeParam("baz"), re));
222   EXPECT_TRUE(RE::PartialMatch(TypeParam("azy"), re));
223   EXPECT_FALSE(RE::PartialMatch(TypeParam("zza"), re));
224 }
225 
226 #elif GTEST_USES_SIMPLE_RE
227 
TEST(IsInSetTest,NulCharIsNotInAnySet)228 TEST(IsInSetTest, NulCharIsNotInAnySet) {
229   EXPECT_FALSE(IsInSet('\0', ""));
230   EXPECT_FALSE(IsInSet('\0', "\0"));
231   EXPECT_FALSE(IsInSet('\0', "a"));
232 }
233 
TEST(IsInSetTest,WorksForNonNulChars)234 TEST(IsInSetTest, WorksForNonNulChars) {
235   EXPECT_FALSE(IsInSet('a', "Ab"));
236   EXPECT_FALSE(IsInSet('c', ""));
237 
238   EXPECT_TRUE(IsInSet('b', "bcd"));
239   EXPECT_TRUE(IsInSet('b', "ab"));
240 }
241 
TEST(IsDigitTest,IsFalseForNonDigit)242 TEST(IsDigitTest, IsFalseForNonDigit) {
243   EXPECT_FALSE(IsDigit('\0'));
244   EXPECT_FALSE(IsDigit(' '));
245   EXPECT_FALSE(IsDigit('+'));
246   EXPECT_FALSE(IsDigit('-'));
247   EXPECT_FALSE(IsDigit('.'));
248   EXPECT_FALSE(IsDigit('a'));
249 }
250 
TEST(IsDigitTest,IsTrueForDigit)251 TEST(IsDigitTest, IsTrueForDigit) {
252   EXPECT_TRUE(IsDigit('0'));
253   EXPECT_TRUE(IsDigit('1'));
254   EXPECT_TRUE(IsDigit('5'));
255   EXPECT_TRUE(IsDigit('9'));
256 }
257 
TEST(IsPunctTest,IsFalseForNonPunct)258 TEST(IsPunctTest, IsFalseForNonPunct) {
259   EXPECT_FALSE(IsPunct('\0'));
260   EXPECT_FALSE(IsPunct(' '));
261   EXPECT_FALSE(IsPunct('\n'));
262   EXPECT_FALSE(IsPunct('a'));
263   EXPECT_FALSE(IsPunct('0'));
264 }
265 
TEST(IsPunctTest,IsTrueForPunct)266 TEST(IsPunctTest, IsTrueForPunct) {
267   for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) {
268     EXPECT_PRED1(IsPunct, *p);
269   }
270 }
271 
TEST(IsRepeatTest,IsFalseForNonRepeatChar)272 TEST(IsRepeatTest, IsFalseForNonRepeatChar) {
273   EXPECT_FALSE(IsRepeat('\0'));
274   EXPECT_FALSE(IsRepeat(' '));
275   EXPECT_FALSE(IsRepeat('a'));
276   EXPECT_FALSE(IsRepeat('1'));
277   EXPECT_FALSE(IsRepeat('-'));
278 }
279 
TEST(IsRepeatTest,IsTrueForRepeatChar)280 TEST(IsRepeatTest, IsTrueForRepeatChar) {
281   EXPECT_TRUE(IsRepeat('?'));
282   EXPECT_TRUE(IsRepeat('*'));
283   EXPECT_TRUE(IsRepeat('+'));
284 }
285 
TEST(IsWhiteSpaceTest,IsFalseForNonWhiteSpace)286 TEST(IsWhiteSpaceTest, IsFalseForNonWhiteSpace) {
287   EXPECT_FALSE(IsWhiteSpace('\0'));
288   EXPECT_FALSE(IsWhiteSpace('a'));
289   EXPECT_FALSE(IsWhiteSpace('1'));
290   EXPECT_FALSE(IsWhiteSpace('+'));
291   EXPECT_FALSE(IsWhiteSpace('_'));
292 }
293 
TEST(IsWhiteSpaceTest,IsTrueForWhiteSpace)294 TEST(IsWhiteSpaceTest, IsTrueForWhiteSpace) {
295   EXPECT_TRUE(IsWhiteSpace(' '));
296   EXPECT_TRUE(IsWhiteSpace('\n'));
297   EXPECT_TRUE(IsWhiteSpace('\r'));
298   EXPECT_TRUE(IsWhiteSpace('\t'));
299   EXPECT_TRUE(IsWhiteSpace('\v'));
300   EXPECT_TRUE(IsWhiteSpace('\f'));
301 }
302 
TEST(IsWordCharTest,IsFalseForNonWordChar)303 TEST(IsWordCharTest, IsFalseForNonWordChar) {
304   EXPECT_FALSE(IsWordChar('\0'));
305   EXPECT_FALSE(IsWordChar('+'));
306   EXPECT_FALSE(IsWordChar('.'));
307   EXPECT_FALSE(IsWordChar(' '));
308   EXPECT_FALSE(IsWordChar('\n'));
309 }
310 
TEST(IsWordCharTest,IsTrueForLetter)311 TEST(IsWordCharTest, IsTrueForLetter) {
312   EXPECT_TRUE(IsWordChar('a'));
313   EXPECT_TRUE(IsWordChar('b'));
314   EXPECT_TRUE(IsWordChar('A'));
315   EXPECT_TRUE(IsWordChar('Z'));
316 }
317 
TEST(IsWordCharTest,IsTrueForDigit)318 TEST(IsWordCharTest, IsTrueForDigit) {
319   EXPECT_TRUE(IsWordChar('0'));
320   EXPECT_TRUE(IsWordChar('1'));
321   EXPECT_TRUE(IsWordChar('7'));
322   EXPECT_TRUE(IsWordChar('9'));
323 }
324 
TEST(IsWordCharTest,IsTrueForUnderscore)325 TEST(IsWordCharTest, IsTrueForUnderscore) {
326   EXPECT_TRUE(IsWordChar('_'));
327 }
328 
TEST(IsValidEscapeTest,IsFalseForNonPrintable)329 TEST(IsValidEscapeTest, IsFalseForNonPrintable) {
330   EXPECT_FALSE(IsValidEscape('\0'));
331   EXPECT_FALSE(IsValidEscape('\007'));
332 }
333 
TEST(IsValidEscapeTest,IsFalseForDigit)334 TEST(IsValidEscapeTest, IsFalseForDigit) {
335   EXPECT_FALSE(IsValidEscape('0'));
336   EXPECT_FALSE(IsValidEscape('9'));
337 }
338 
TEST(IsValidEscapeTest,IsFalseForWhiteSpace)339 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) {
340   EXPECT_FALSE(IsValidEscape(' '));
341   EXPECT_FALSE(IsValidEscape('\n'));
342 }
343 
TEST(IsValidEscapeTest,IsFalseForSomeLetter)344 TEST(IsValidEscapeTest, IsFalseForSomeLetter) {
345   EXPECT_FALSE(IsValidEscape('a'));
346   EXPECT_FALSE(IsValidEscape('Z'));
347 }
348 
TEST(IsValidEscapeTest,IsTrueForPunct)349 TEST(IsValidEscapeTest, IsTrueForPunct) {
350   EXPECT_TRUE(IsValidEscape('.'));
351   EXPECT_TRUE(IsValidEscape('-'));
352   EXPECT_TRUE(IsValidEscape('^'));
353   EXPECT_TRUE(IsValidEscape('$'));
354   EXPECT_TRUE(IsValidEscape('('));
355   EXPECT_TRUE(IsValidEscape(']'));
356   EXPECT_TRUE(IsValidEscape('{'));
357   EXPECT_TRUE(IsValidEscape('|'));
358 }
359 
TEST(IsValidEscapeTest,IsTrueForSomeLetter)360 TEST(IsValidEscapeTest, IsTrueForSomeLetter) {
361   EXPECT_TRUE(IsValidEscape('d'));
362   EXPECT_TRUE(IsValidEscape('D'));
363   EXPECT_TRUE(IsValidEscape('s'));
364   EXPECT_TRUE(IsValidEscape('S'));
365   EXPECT_TRUE(IsValidEscape('w'));
366   EXPECT_TRUE(IsValidEscape('W'));
367 }
368 
TEST(AtomMatchesCharTest,EscapedPunct)369 TEST(AtomMatchesCharTest, EscapedPunct) {
370   EXPECT_FALSE(AtomMatchesChar(true, '\\', '\0'));
371   EXPECT_FALSE(AtomMatchesChar(true, '\\', ' '));
372   EXPECT_FALSE(AtomMatchesChar(true, '_', '.'));
373   EXPECT_FALSE(AtomMatchesChar(true, '.', 'a'));
374 
375   EXPECT_TRUE(AtomMatchesChar(true, '\\', '\\'));
376   EXPECT_TRUE(AtomMatchesChar(true, '_', '_'));
377   EXPECT_TRUE(AtomMatchesChar(true, '+', '+'));
378   EXPECT_TRUE(AtomMatchesChar(true, '.', '.'));
379 }
380 
TEST(AtomMatchesCharTest,Escaped_d)381 TEST(AtomMatchesCharTest, Escaped_d) {
382   EXPECT_FALSE(AtomMatchesChar(true, 'd', '\0'));
383   EXPECT_FALSE(AtomMatchesChar(true, 'd', 'a'));
384   EXPECT_FALSE(AtomMatchesChar(true, 'd', '.'));
385 
386   EXPECT_TRUE(AtomMatchesChar(true, 'd', '0'));
387   EXPECT_TRUE(AtomMatchesChar(true, 'd', '9'));
388 }
389 
TEST(AtomMatchesCharTest,Escaped_D)390 TEST(AtomMatchesCharTest, Escaped_D) {
391   EXPECT_FALSE(AtomMatchesChar(true, 'D', '0'));
392   EXPECT_FALSE(AtomMatchesChar(true, 'D', '9'));
393 
394   EXPECT_TRUE(AtomMatchesChar(true, 'D', '\0'));
395   EXPECT_TRUE(AtomMatchesChar(true, 'D', 'a'));
396   EXPECT_TRUE(AtomMatchesChar(true, 'D', '-'));
397 }
398 
TEST(AtomMatchesCharTest,Escaped_s)399 TEST(AtomMatchesCharTest, Escaped_s) {
400   EXPECT_FALSE(AtomMatchesChar(true, 's', '\0'));
401   EXPECT_FALSE(AtomMatchesChar(true, 's', 'a'));
402   EXPECT_FALSE(AtomMatchesChar(true, 's', '.'));
403   EXPECT_FALSE(AtomMatchesChar(true, 's', '9'));
404 
405   EXPECT_TRUE(AtomMatchesChar(true, 's', ' '));
406   EXPECT_TRUE(AtomMatchesChar(true, 's', '\n'));
407   EXPECT_TRUE(AtomMatchesChar(true, 's', '\t'));
408 }
409 
TEST(AtomMatchesCharTest,Escaped_S)410 TEST(AtomMatchesCharTest, Escaped_S) {
411   EXPECT_FALSE(AtomMatchesChar(true, 'S', ' '));
412   EXPECT_FALSE(AtomMatchesChar(true, 'S', '\r'));
413 
414   EXPECT_TRUE(AtomMatchesChar(true, 'S', '\0'));
415   EXPECT_TRUE(AtomMatchesChar(true, 'S', 'a'));
416   EXPECT_TRUE(AtomMatchesChar(true, 'S', '9'));
417 }
418 
TEST(AtomMatchesCharTest,Escaped_w)419 TEST(AtomMatchesCharTest, Escaped_w) {
420   EXPECT_FALSE(AtomMatchesChar(true, 'w', '\0'));
421   EXPECT_FALSE(AtomMatchesChar(true, 'w', '+'));
422   EXPECT_FALSE(AtomMatchesChar(true, 'w', ' '));
423   EXPECT_FALSE(AtomMatchesChar(true, 'w', '\n'));
424 
425   EXPECT_TRUE(AtomMatchesChar(true, 'w', '0'));
426   EXPECT_TRUE(AtomMatchesChar(true, 'w', 'b'));
427   EXPECT_TRUE(AtomMatchesChar(true, 'w', 'C'));
428   EXPECT_TRUE(AtomMatchesChar(true, 'w', '_'));
429 }
430 
TEST(AtomMatchesCharTest,Escaped_W)431 TEST(AtomMatchesCharTest, Escaped_W) {
432   EXPECT_FALSE(AtomMatchesChar(true, 'W', 'A'));
433   EXPECT_FALSE(AtomMatchesChar(true, 'W', 'b'));
434   EXPECT_FALSE(AtomMatchesChar(true, 'W', '9'));
435   EXPECT_FALSE(AtomMatchesChar(true, 'W', '_'));
436 
437   EXPECT_TRUE(AtomMatchesChar(true, 'W', '\0'));
438   EXPECT_TRUE(AtomMatchesChar(true, 'W', '*'));
439   EXPECT_TRUE(AtomMatchesChar(true, 'W', '\n'));
440 }
441 
TEST(AtomMatchesCharTest,EscapedWhiteSpace)442 TEST(AtomMatchesCharTest, EscapedWhiteSpace) {
443   EXPECT_FALSE(AtomMatchesChar(true, 'f', '\0'));
444   EXPECT_FALSE(AtomMatchesChar(true, 'f', '\n'));
445   EXPECT_FALSE(AtomMatchesChar(true, 'n', '\0'));
446   EXPECT_FALSE(AtomMatchesChar(true, 'n', '\r'));
447   EXPECT_FALSE(AtomMatchesChar(true, 'r', '\0'));
448   EXPECT_FALSE(AtomMatchesChar(true, 'r', 'a'));
449   EXPECT_FALSE(AtomMatchesChar(true, 't', '\0'));
450   EXPECT_FALSE(AtomMatchesChar(true, 't', 't'));
451   EXPECT_FALSE(AtomMatchesChar(true, 'v', '\0'));
452   EXPECT_FALSE(AtomMatchesChar(true, 'v', '\f'));
453 
454   EXPECT_TRUE(AtomMatchesChar(true, 'f', '\f'));
455   EXPECT_TRUE(AtomMatchesChar(true, 'n', '\n'));
456   EXPECT_TRUE(AtomMatchesChar(true, 'r', '\r'));
457   EXPECT_TRUE(AtomMatchesChar(true, 't', '\t'));
458   EXPECT_TRUE(AtomMatchesChar(true, 'v', '\v'));
459 }
460 
TEST(AtomMatchesCharTest,UnescapedDot)461 TEST(AtomMatchesCharTest, UnescapedDot) {
462   EXPECT_FALSE(AtomMatchesChar(false, '.', '\n'));
463 
464   EXPECT_TRUE(AtomMatchesChar(false, '.', '\0'));
465   EXPECT_TRUE(AtomMatchesChar(false, '.', '.'));
466   EXPECT_TRUE(AtomMatchesChar(false, '.', 'a'));
467   EXPECT_TRUE(AtomMatchesChar(false, '.', ' '));
468 }
469 
TEST(AtomMatchesCharTest,UnescapedChar)470 TEST(AtomMatchesCharTest, UnescapedChar) {
471   EXPECT_FALSE(AtomMatchesChar(false, 'a', '\0'));
472   EXPECT_FALSE(AtomMatchesChar(false, 'a', 'b'));
473   EXPECT_FALSE(AtomMatchesChar(false, '$', 'a'));
474 
475   EXPECT_TRUE(AtomMatchesChar(false, '$', '$'));
476   EXPECT_TRUE(AtomMatchesChar(false, '5', '5'));
477   EXPECT_TRUE(AtomMatchesChar(false, 'Z', 'Z'));
478 }
479 
TEST(ValidateRegexTest,GeneratesFailureAndReturnsFalseForInvalid)480 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) {
481   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)),
482                           "NULL is not a valid simple regular expression");
483   EXPECT_NONFATAL_FAILURE(
484       ASSERT_FALSE(ValidateRegex("a\\")),
485       "Syntax error at index 1 in simple regular expression \"a\\\": ");
486   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a\\")),
487                           "'\\' cannot appear at the end");
488   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\n\\")),
489                           "'\\' cannot appear at the end");
490   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\s\\hb")),
491                           "invalid escape sequence \"\\h\"");
492   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^^")),
493                           "'^' can only appear at the beginning");
494   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(".*^b")),
495                           "'^' can only appear at the beginning");
496   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("$$")),
497                           "'$' can only appear at the end");
498   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^$a")),
499                           "'$' can only appear at the end");
500   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a(b")),
501                           "'(' is unsupported");
502   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("ab)")),
503                           "')' is unsupported");
504   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("[ab")),
505                           "'[' is unsupported");
506   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a{2")),
507                           "'{' is unsupported");
508   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("?")),
509                           "'?' can only follow a repeatable token");
510   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^*")),
511                           "'*' can only follow a repeatable token");
512   EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("5*+")),
513                           "'+' can only follow a repeatable token");
514 }
515 
TEST(ValidateRegexTest,ReturnsTrueForValid)516 TEST(ValidateRegexTest, ReturnsTrueForValid) {
517   EXPECT_TRUE(ValidateRegex(""));
518   EXPECT_TRUE(ValidateRegex("a"));
519   EXPECT_TRUE(ValidateRegex(".*"));
520   EXPECT_TRUE(ValidateRegex("^a_+"));
521   EXPECT_TRUE(ValidateRegex("^a\\t\\&?"));
522   EXPECT_TRUE(ValidateRegex("09*$"));
523   EXPECT_TRUE(ValidateRegex("^Z$"));
524   EXPECT_TRUE(ValidateRegex("a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}"));
525 }
526 
TEST(MatchRepetitionAndRegexAtHeadTest,WorksForZeroOrOne)527 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) {
528   EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "a", "ba"));
529   // Repeating more than once.
530   EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "aab"));
531 
532   // Repeating zero times.
533   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ba"));
534   // Repeating once.
535   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ab"));
536   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '#', '?', ".", "##"));
537 }
538 
TEST(MatchRepetitionAndRegexAtHeadTest,WorksForZeroOrMany)539 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) {
540   EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '*', "a$", "baab"));
541 
542   // Repeating zero times.
543   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "bc"));
544   // Repeating once.
545   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "abc"));
546   // Repeating more than once.
547   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '*', "-", "ab_1-g"));
548 }
549 
TEST(MatchRepetitionAndRegexAtHeadTest,WorksForOneOrMany)550 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) {
551   EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "a$", "baab"));
552   // Repeating zero times.
553   EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "bc"));
554 
555   // Repeating once.
556   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "abc"));
557   // Repeating more than once.
558   EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '+', "-", "ab_1-g"));
559 }
560 
TEST(MatchRegexAtHeadTest,ReturnsTrueForEmptyRegex)561 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) {
562   EXPECT_TRUE(MatchRegexAtHead("", ""));
563   EXPECT_TRUE(MatchRegexAtHead("", "ab"));
564 }
565 
TEST(MatchRegexAtHeadTest,WorksWhenDollarIsInRegex)566 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) {
567   EXPECT_FALSE(MatchRegexAtHead("$", "a"));
568 
569   EXPECT_TRUE(MatchRegexAtHead("$", ""));
570   EXPECT_TRUE(MatchRegexAtHead("a$", "a"));
571 }
572 
TEST(MatchRegexAtHeadTest,WorksWhenRegexStartsWithEscapeSequence)573 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) {
574   EXPECT_FALSE(MatchRegexAtHead("\\w", "+"));
575   EXPECT_FALSE(MatchRegexAtHead("\\W", "ab"));
576 
577   EXPECT_TRUE(MatchRegexAtHead("\\sa", "\nab"));
578   EXPECT_TRUE(MatchRegexAtHead("\\d", "1a"));
579 }
580 
TEST(MatchRegexAtHeadTest,WorksWhenRegexStartsWithRepetition)581 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) {
582   EXPECT_FALSE(MatchRegexAtHead(".+a", "abc"));
583   EXPECT_FALSE(MatchRegexAtHead("a?b", "aab"));
584 
585   EXPECT_TRUE(MatchRegexAtHead(".*a", "bc12-ab"));
586   EXPECT_TRUE(MatchRegexAtHead("a?b", "b"));
587   EXPECT_TRUE(MatchRegexAtHead("a?b", "ab"));
588 }
589 
TEST(MatchRegexAtHeadTest,WorksWhenRegexStartsWithRepetionOfEscapeSequence)590 TEST(MatchRegexAtHeadTest,
591      WorksWhenRegexStartsWithRepetionOfEscapeSequence) {
592   EXPECT_FALSE(MatchRegexAtHead("\\.+a", "abc"));
593   EXPECT_FALSE(MatchRegexAtHead("\\s?b", "  b"));
594 
595   EXPECT_TRUE(MatchRegexAtHead("\\(*a", "((((ab"));
596   EXPECT_TRUE(MatchRegexAtHead("\\^?b", "^b"));
597   EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "b"));
598   EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "\\b"));
599 }
600 
TEST(MatchRegexAtHeadTest,MatchesSequentially)601 TEST(MatchRegexAtHeadTest, MatchesSequentially) {
602   EXPECT_FALSE(MatchRegexAtHead("ab.*c", "acabc"));
603 
604   EXPECT_TRUE(MatchRegexAtHead("ab.*c", "ab-fsc"));
605 }
606 
TEST(MatchRegexAnywhereTest,ReturnsFalseWhenStringIsNull)607 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) {
608   EXPECT_FALSE(MatchRegexAnywhere("", NULL));
609 }
610 
TEST(MatchRegexAnywhereTest,WorksWhenRegexStartsWithCaret)611 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) {
612   EXPECT_FALSE(MatchRegexAnywhere("^a", "ba"));
613   EXPECT_FALSE(MatchRegexAnywhere("^$", "a"));
614 
615   EXPECT_TRUE(MatchRegexAnywhere("^a", "ab"));
616   EXPECT_TRUE(MatchRegexAnywhere("^", "ab"));
617   EXPECT_TRUE(MatchRegexAnywhere("^$", ""));
618 }
619 
TEST(MatchRegexAnywhereTest,ReturnsFalseWhenNoMatch)620 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) {
621   EXPECT_FALSE(MatchRegexAnywhere("a", "bcde123"));
622   EXPECT_FALSE(MatchRegexAnywhere("a.+a", "--aa88888888"));
623 }
624 
TEST(MatchRegexAnywhereTest,ReturnsTrueWhenMatchingPrefix)625 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) {
626   EXPECT_TRUE(MatchRegexAnywhere("\\w+", "ab1_ - 5"));
627   EXPECT_TRUE(MatchRegexAnywhere(".*=", "="));
628   EXPECT_TRUE(MatchRegexAnywhere("x.*ab?.*bc", "xaaabc"));
629 }
630 
TEST(MatchRegexAnywhereTest,ReturnsTrueWhenMatchingNonPrefix)631 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) {
632   EXPECT_TRUE(MatchRegexAnywhere("\\w+", "$$$ ab1_ - 5"));
633   EXPECT_TRUE(MatchRegexAnywhere("\\.+=", "=  ...="));
634 }
635 
636 // Tests RE's implicit constructors.
TEST(RETest,ImplicitConstructorWorks)637 TEST(RETest, ImplicitConstructorWorks) {
638   const RE empty("");
639   EXPECT_STREQ("", empty.pattern());
640 
641   const RE simple("hello");
642   EXPECT_STREQ("hello", simple.pattern());
643 }
644 
645 // Tests that RE's constructors reject invalid regular expressions.
TEST(RETest,RejectsInvalidRegex)646 TEST(RETest, RejectsInvalidRegex) {
647   EXPECT_NONFATAL_FAILURE({
648     const RE normal(NULL);
649   }, "NULL is not a valid simple regular expression");
650 
651   EXPECT_NONFATAL_FAILURE({
652     const RE normal(".*(\\w+");
653   }, "'(' is unsupported");
654 
655   EXPECT_NONFATAL_FAILURE({
656     const RE invalid("^?");
657   }, "'?' can only follow a repeatable token");
658 }
659 
660 // Tests RE::FullMatch().
TEST(RETest,FullMatchWorks)661 TEST(RETest, FullMatchWorks) {
662   const RE empty("");
663   EXPECT_TRUE(RE::FullMatch("", empty));
664   EXPECT_FALSE(RE::FullMatch("a", empty));
665 
666   const RE re1("a");
667   EXPECT_TRUE(RE::FullMatch("a", re1));
668 
669   const RE re("a.*z");
670   EXPECT_TRUE(RE::FullMatch("az", re));
671   EXPECT_TRUE(RE::FullMatch("axyz", re));
672   EXPECT_FALSE(RE::FullMatch("baz", re));
673   EXPECT_FALSE(RE::FullMatch("azy", re));
674 }
675 
676 // Tests RE::PartialMatch().
TEST(RETest,PartialMatchWorks)677 TEST(RETest, PartialMatchWorks) {
678   const RE empty("");
679   EXPECT_TRUE(RE::PartialMatch("", empty));
680   EXPECT_TRUE(RE::PartialMatch("a", empty));
681 
682   const RE re("a.*z");
683   EXPECT_TRUE(RE::PartialMatch("az", re));
684   EXPECT_TRUE(RE::PartialMatch("axyz", re));
685   EXPECT_TRUE(RE::PartialMatch("baz", re));
686   EXPECT_TRUE(RE::PartialMatch("azy", re));
687   EXPECT_FALSE(RE::PartialMatch("zza", re));
688 }
689 
690 #endif  // GTEST_USES_POSIX_RE
691 
TEST(CaptureStderrTest,CapturesStdErr)692 TEST(CaptureStderrTest, CapturesStdErr) {
693   CaptureStderr();
694   fprintf(stderr, "abc");
695   ASSERT_STREQ("abc", GetCapturedStderr().c_str());
696 }
697 
698 }  // namespace internal
699 }  // namespace testing
700