39 #if GTEST_OS_WINDOWS_MOBILE
41 #elif GTEST_OS_WINDOWS
43 # include <sys/stat.h>
46 #endif // GTEST_OS_WINDOWS_MOBILE
49 # include <mach/mach_init.h>
50 # include <mach/task.h>
51 # include <mach/vm_map.h>
52 #endif // GTEST_OS_MAC
56 # include <sys/procfs.h>
57 #endif // GTEST_OS_QNX
69 #define GTEST_IMPLEMENTATION_ 1
71 #undef GTEST_IMPLEMENTATION_
76 #if defined(_MSC_VER) || defined(__BORLANDC__)
90 const task_t task = mach_task_self();
91 mach_msg_type_number_t thread_count;
92 thread_act_array_t thread_list;
93 const kern_return_t status = task_threads(task, &thread_list, &thread_count);
94 if (status == KERN_SUCCESS) {
98 reinterpret_cast<vm_address_t
>(thread_list),
99 sizeof(thread_t) * thread_count);
100 return static_cast<size_t>(thread_count);
111 const int fd = open(
"/proc/self/as", O_RDONLY);
115 procfs_info process_info;
117 devctl(fd, DCMD_PROC_INFO, &process_info,
sizeof(process_info), NULL);
120 return static_cast<size_t>(process_info.num_threads);
134 #endif // GTEST_OS_MAC
136 #if GTEST_USES_POSIX_RE
154 if (!re.is_valid_)
return false;
157 return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
163 if (!re.is_valid_)
return false;
166 return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;
175 const size_t full_regex_len = strlen(regex) + 10;
176 char*
const full_pattern =
new char[full_regex_len];
178 snprintf(full_pattern, full_regex_len,
"^(%s)$", regex);
189 const char*
const partial_regex = (*regex ==
'\0') ?
"()" : regex;
193 <<
"Regular expression \"" << regex
194 <<
"\" is not a valid POSIX Extended regular expression.";
196 delete[] full_pattern;
199 #elif GTEST_USES_SIMPLE_RE
203 bool IsInSet(
char ch,
const char* str) {
204 return ch !=
'\0' && strchr(str, ch) != NULL;
210 bool IsAsciiDigit(
char ch) {
return '0' <= ch && ch <=
'9'; }
211 bool IsAsciiPunct(
char ch) {
212 return IsInSet(ch,
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~");
214 bool IsRepeat(
char ch) {
return IsInSet(ch,
"?*+"); }
215 bool IsAsciiWhiteSpace(
char ch) {
return IsInSet(ch,
" \f\n\r\t\v"); }
216 bool IsAsciiWordChar(
char ch) {
217 return (
'a' <= ch && ch <=
'z') || (
'A' <= ch && ch <=
'Z') ||
218 (
'0' <= ch && ch <=
'9') || ch ==
'_';
222 bool IsValidEscape(
char c) {
223 return (IsAsciiPunct(c) || IsInSet(c,
"dDfnrsStvwW"));
228 bool AtomMatchesChar(
bool escaped,
char pattern_char,
char ch) {
230 switch (pattern_char) {
231 case 'd':
return IsAsciiDigit(ch);
232 case 'D':
return !IsAsciiDigit(ch);
233 case 'f':
return ch ==
'\f';
234 case 'n':
return ch ==
'\n';
235 case 'r':
return ch ==
'\r';
236 case 's':
return IsAsciiWhiteSpace(ch);
237 case 'S':
return !IsAsciiWhiteSpace(ch);
238 case 't':
return ch ==
'\t';
239 case 'v':
return ch ==
'\v';
240 case 'w':
return IsAsciiWordChar(ch);
241 case 'W':
return !IsAsciiWordChar(ch);
243 return IsAsciiPunct(pattern_char) && pattern_char == ch;
246 return (pattern_char ==
'.' && ch !=
'\n') || pattern_char == ch;
250 std::string FormatRegexSyntaxError(
const char* regex,
int index) {
251 return (Message() <<
"Syntax error at index " << index
252 <<
" in simple regular expression \"" << regex <<
"\": ").GetString();
257 bool ValidateRegex(
const char* regex) {
262 ADD_FAILURE() <<
"NULL is not a valid simple regular expression.";
266 bool is_valid =
true;
269 bool prev_repeatable =
false;
270 for (
int i = 0; regex[i]; i++) {
271 if (regex[i] ==
'\\') {
273 if (regex[i] ==
'\0') {
274 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
275 <<
"'\\' cannot appear at the end.";
279 if (!IsValidEscape(regex[i])) {
280 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
281 <<
"invalid escape sequence \"\\" << regex[i] <<
"\".";
284 prev_repeatable =
true;
286 const char ch = regex[i];
288 if (ch ==
'^' && i > 0) {
290 <<
"'^' can only appear at the beginning.";
292 }
else if (ch ==
'$' && regex[i + 1] !=
'\0') {
294 <<
"'$' can only appear at the end.";
296 }
else if (IsInSet(ch,
"()[]{}|")) {
298 <<
"'" << ch <<
"' is unsupported.";
300 }
else if (IsRepeat(ch) && !prev_repeatable) {
302 <<
"'" << ch <<
"' can only follow a repeatable token.";
306 prev_repeatable = !IsInSet(ch,
"^$?*+");
320 bool MatchRepetitionAndRegexAtHead(
321 bool escaped,
char c,
char repeat,
const char* regex,
323 const size_t min_count = (repeat ==
'+') ? 1 : 0;
324 const size_t max_count = (repeat ==
'?') ? 1 :
325 static_cast<size_t>(-1) - 1;
329 for (
size_t i = 0; i <= max_count; ++i) {
331 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
338 if (str[i] ==
'\0' || !AtomMatchesChar(escaped, c, str[i]))
347 bool MatchRegexAtHead(
const char* regex,
const char* str) {
357 const bool escaped = *regex ==
'\\';
360 if (IsRepeat(regex[1])) {
364 return MatchRepetitionAndRegexAtHead(
365 escaped, regex[0], regex[1], regex + 2, str);
370 return (*str !=
'\0') && AtomMatchesChar(escaped, *regex, *str) &&
371 MatchRegexAtHead(regex + 1, str + 1);
383 bool MatchRegexAnywhere(
const char* regex,
const char* str) {
384 if (regex == NULL || str == NULL)
388 return MatchRegexAtHead(regex + 1, str);
392 if (MatchRegexAtHead(regex, str))
394 }
while (*str++ !=
'\0');
402 free(
const_cast<char*
>(full_pattern_));
407 return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);
413 return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);
429 const size_t len = strlen(regex);
433 char* buffer =
static_cast<char*
>(malloc(len + 3));
434 full_pattern_ = buffer;
441 memcpy(buffer, regex, len);
444 if (len == 0 || regex[len - 1] !=
'$')
450 #endif // GTEST_USES_POSIX_RE
460 return file_name +
":";
475 const char* file,
int line) {
486 : severity_(severity) {
487 const char*
const marker =
490 severity ==
GTEST_ERROR ?
"[ ERROR ]" :
"[ FATAL ]";
491 GetStream() << ::std::endl << marker <<
" "
506 # pragma warning(push)
507 # pragma warning(disable: 4996)
510 #if GTEST_HAS_STREAM_REDIRECTION
513 class CapturedStream {
516 explicit CapturedStream(
int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
517 # if GTEST_OS_WINDOWS
518 char temp_dir_path[MAX_PATH + 1] = {
'\0' };
519 char temp_file_path[MAX_PATH + 1] = {
'\0' };
521 ::GetTempPathA(
sizeof(temp_dir_path), temp_dir_path);
522 const UINT success = ::GetTempFileNameA(temp_dir_path,
527 <<
"Unable to create a temporary file in " << temp_dir_path;
528 const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
529 GTEST_CHECK_(captured_fd != -1) <<
"Unable to open temporary file "
531 filename_ = temp_file_path;
537 # if GTEST_OS_LINUX_ANDROID
552 char name_template[] =
"/sdcard/gtest_captured_stream.XXXXXX";
554 char name_template[] =
"/tmp/captured_stream.XXXXXX";
555 # endif // GTEST_OS_LINUX_ANDROID
556 const int captured_fd = mkstemp(name_template);
557 filename_ = name_template;
558 # endif // GTEST_OS_WINDOWS
560 dup2(captured_fd, fd_);
565 remove(filename_.c_str());
569 if (uncaptured_fd_ != -1) {
572 dup2(uncaptured_fd_, fd_);
573 close(uncaptured_fd_);
577 FILE*
const file =
posix::FOpen(filename_.c_str(),
"r");
588 static size_t GetFileSize(FILE* file);
599 size_t CapturedStream::GetFileSize(FILE* file) {
600 fseek(file, 0, SEEK_END);
601 return static_cast<size_t>(ftell(file));
605 std::string CapturedStream::ReadEntireFile(FILE* file) {
606 const size_t file_size = GetFileSize(file);
607 char*
const buffer =
new char[file_size];
609 size_t bytes_last_read = 0;
610 size_t bytes_read = 0;
612 fseek(file, 0, SEEK_SET);
617 bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file);
618 bytes_read += bytes_last_read;
619 }
while (bytes_last_read > 0 && bytes_read < file_size);
628 # pragma warning(pop)
631 static CapturedStream* g_captured_stderr = NULL;
632 static CapturedStream* g_captured_stdout = NULL;
635 void CaptureStream(
int fd,
const char* stream_name, CapturedStream** stream) {
636 if (*stream != NULL) {
637 GTEST_LOG_(FATAL) <<
"Only one " << stream_name
638 <<
" capturer can exist at a time.";
640 *stream =
new CapturedStream(fd);
644 std::string GetCapturedStream(CapturedStream** captured_stream) {
645 const std::string content = (*captured_stream)->GetCapturedString();
647 delete *captured_stream;
648 *captured_stream = NULL;
665 return GetCapturedStream(&g_captured_stdout);
670 return GetCapturedStream(&g_captured_stderr);
673 #endif // GTEST_HAS_STREAM_REDIRECTION
675 #if GTEST_HAS_DEATH_TEST
678 ::std::vector<testing::internal::string> g_argvs;
680 static const ::std::vector<testing::internal::string>* g_injected_test_argvs =
683 void SetInjectableArgvs(const ::std::vector<testing::internal::string>* argvs) {
684 if (g_injected_test_argvs != argvs)
685 delete g_injected_test_argvs;
686 g_injected_test_argvs = argvs;
689 const ::std::vector<testing::internal::string>& GetInjectableArgvs() {
690 if (g_injected_test_argvs != NULL) {
691 return *g_injected_test_argvs;
695 #endif // GTEST_HAS_DEATH_TEST
697 #if GTEST_OS_WINDOWS_MOBILE
701 TerminateProcess(GetCurrentProcess(), 1);
704 #endif // GTEST_OS_WINDOWS_MOBILE
714 for (
size_t i = 0; i != full_flag.length(); i++) {
715 env_var <<
ToUpper(full_flag.c_str()[i]);
727 const long long_value = strtol(str, &end, 10);
733 msg <<
"WARNING: " << src_text
734 <<
" is expected to be a 32-bit integer, but actually"
735 <<
" has value \"" << str <<
"\".\n";
742 const Int32 result =
static_cast<Int32>(long_value);
743 if (long_value == LONG_MAX || long_value == LONG_MIN ||
750 msg <<
"WARNING: " << src_text
751 <<
" is expected to be a 32-bit integer, but actually"
752 <<
" has value " << str <<
", which overflows.\n";
768 const char*
const string_value =
posix::GetEnv(env_var.c_str());
769 return string_value == NULL ?
770 default_value : strcmp(string_value,
"0") != 0;
778 const char*
const string_value =
posix::GetEnv(env_var.c_str());
779 if (string_value == NULL) {
781 return default_value;
784 Int32 result = default_value;
786 string_value, &result)) {
787 printf(
"The default value %s is used.\n",
788 (
Message() << default_value).GetString().c_str());
790 return default_value;
801 return value == NULL ? default_value : value;