45#ifdef GTEST_OS_WINDOWS
59#include <mach/mach_init.h>
61#include <mach/vm_map.h>
64#if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
65 defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD) || \
66 defined(GTEST_OS_OPENBSD)
67#include <sys/sysctl.h>
68#if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
69 defined(GTEST_OS_GNU_KFREEBSD)
77#include <sys/procfs.h>
85#ifdef GTEST_OS_FUCHSIA
86#include <zircon/process.h>
87#include <zircon/syscalls.h>
99#if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_HURD)
103T ReadProcFileField(
const std::string& filename,
int field) {
105 std::ifstream file(filename.c_str());
106 while (field-- > 0) {
117 const std::string filename =
118 (Message() <<
"/proc/" << getpid() <<
"/stat").GetString();
119 return ReadProcFileField<size_t>(filename, 19);
122#elif defined(GTEST_OS_MAC)
125 const task_t task = mach_task_self();
126 mach_msg_type_number_t thread_count;
127 thread_act_array_t thread_list;
128 const kern_return_t status = task_threads(task, &thread_list, &thread_count);
129 if (status == KERN_SUCCESS) {
132 vm_deallocate(task,
reinterpret_cast<vm_address_t
>(thread_list),
133 sizeof(thread_t) * thread_count);
134 return static_cast<size_t>(thread_count);
140#elif defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
141 defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD)
143#ifdef GTEST_OS_NETBSD
145#define KERN_PROC KERN_PROC2
146#define kinfo_proc kinfo_proc2
149#ifdef GTEST_OS_DRAGONFLY
150#define KP_NLWP(kp) (kp.kp_nthreads)
151#elif defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_GNU_KFREEBSD)
152#define KP_NLWP(kp) (kp.ki_numthreads)
153#elif defined(GTEST_OS_NETBSD)
154#define KP_NLWP(kp) (kp.p_nlwps)
165#ifdef GTEST_OS_NETBSD
166 sizeof(
struct kinfo_proc),
170 u_int miblen =
sizeof(mib) /
sizeof(mib[0]);
171 struct kinfo_proc info;
172 size_t size =
sizeof(info);
173 if (sysctl(mib, miblen, &info, &size, NULL, 0)) {
176 return static_cast<size_t>(KP_NLWP(info));
178#elif defined(GTEST_OS_OPENBSD)
186 KERN_PROC_PID | KERN_PROC_SHOW_THREADS,
188 sizeof(
struct kinfo_proc),
191 u_int miblen =
sizeof(mib) /
sizeof(mib[0]);
195 if (sysctl(mib, miblen, NULL, &size, NULL, 0)) {
199 mib[5] =
static_cast<int>(size /
static_cast<size_t>(mib[4]));
202 std::vector<struct kinfo_proc> info(mib[5]);
203 if (sysctl(mib, miblen, info.data(), &size, NULL, 0)) {
209 for (
size_t i = 0;
i < size /
static_cast<size_t>(mib[4]);
i++) {
210 if (info[
i].p_tid != -1) nthreads++;
215#elif defined(GTEST_OS_QNX)
220 const int fd = open(
"/proc/self/as", O_RDONLY);
224 procfs_info process_info;
226 devctl(fd, DCMD_PROC_INFO, &process_info,
sizeof(process_info),
nullptr);
229 return static_cast<size_t>(process_info.num_threads);
235#elif defined(GTEST_OS_AIX)
238 struct procentry64 entry;
239 pid_t pid = getpid();
240 int status = getprocs64(&entry,
sizeof(entry),
nullptr, 0, &pid, 1);
242 return entry.pi_thcount;
248#elif defined(GTEST_OS_FUCHSIA)
254 zx_object_get_info(zx_process_self(), ZX_INFO_PROCESS_THREADS,
255 &dummy_buffer, 0,
nullptr, &avail);
256 if (status == ZX_OK) {
273#if defined(GTEST_IS_THREADSAFE) && defined(GTEST_OS_WINDOWS)
275AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
277AutoHandle::AutoHandle(Handle handle) : handle_(handle) {}
279AutoHandle::~AutoHandle() { Reset(); }
281AutoHandle::Handle AutoHandle::Get()
const {
return handle_; }
283void AutoHandle::Reset() { Reset(INVALID_HANDLE_VALUE); }
285void AutoHandle::Reset(HANDLE handle) {
287 if (handle_ != handle) {
289 ::CloseHandle(handle_);
294 <<
"Resetting a valid handle to itself is likely a programmer error "
295 "and thus not allowed.";
299bool AutoHandle::IsCloseable()
const {
302 return handle_ !=
nullptr && handle_ != INVALID_HANDLE_VALUE;
306 : owner_thread_id_(0),
308 critical_section_init_phase_(0),
309 critical_section_(new CRITICAL_SECTION) {
310 ::InitializeCriticalSection(critical_section_);
316 if (type_ == kDynamic) {
317 ::DeleteCriticalSection(critical_section_);
318 delete critical_section_;
319 critical_section_ =
nullptr;
324 ThreadSafeLazyInit();
325 ::EnterCriticalSection(critical_section_);
326 owner_thread_id_ = ::GetCurrentThreadId();
329void Mutex::Unlock() {
330 ThreadSafeLazyInit();
334 owner_thread_id_ = 0;
335 ::LeaveCriticalSection(critical_section_);
340void Mutex::AssertHeld() {
341 ThreadSafeLazyInit();
342 GTEST_CHECK_(owner_thread_id_ == ::GetCurrentThreadId())
343 <<
"The current thread is not holding the mutex @" <<
this;
357class MemoryIsNotDeallocated {
359 MemoryIsNotDeallocated() : old_crtdbg_flag_(0) {
360 old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
363 (void)_CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF);
366 ~MemoryIsNotDeallocated() {
368 (void)_CrtSetDbgFlag(old_crtdbg_flag_);
372 int old_crtdbg_flag_;
374 MemoryIsNotDeallocated(
const MemoryIsNotDeallocated&) =
delete;
375 MemoryIsNotDeallocated& operator=(
const MemoryIsNotDeallocated&) =
delete;
382void Mutex::ThreadSafeLazyInit() {
384 if (type_ == kStatic) {
386 ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) {
390 owner_thread_id_ = 0;
394 MemoryIsNotDeallocated memory_is_not_deallocated;
396 critical_section_ =
new CRITICAL_SECTION;
398 ::InitializeCriticalSection(critical_section_);
401 GTEST_CHECK_(::InterlockedCompareExchange(&critical_section_init_phase_,
407 while (::InterlockedCompareExchange(&critical_section_init_phase_, 2L,
420 <<
"Unexpected value of critical_section_init_phase_ "
421 <<
"while initializing a static mutex.";
428class ThreadWithParamSupport :
public ThreadWithParamBase {
430 static HANDLE CreateThread(Runnable* runnable,
431 Notification* thread_can_start) {
432 ThreadMainParam* param =
new ThreadMainParam(runnable, thread_can_start);
434 HANDLE thread_handle = ::CreateThread(
437 &ThreadWithParamSupport::ThreadMain,
442 <<
"CreateThread failed with error " << ::GetLastError() <<
".";
443 if (thread_handle ==
nullptr) {
446 return thread_handle;
450 struct ThreadMainParam {
451 ThreadMainParam(Runnable* runnable, Notification* thread_can_start)
452 : runnable_(runnable), thread_can_start_(thread_can_start) {}
453 std::unique_ptr<Runnable> runnable_;
455 Notification* thread_can_start_;
458 static DWORD WINAPI ThreadMain(
void* ptr) {
460 std::unique_ptr<ThreadMainParam> param(
static_cast<ThreadMainParam*
>(ptr));
461 if (param->thread_can_start_ !=
nullptr)
462 param->thread_can_start_->WaitForNotification();
463 param->runnable_->Run();
468 ThreadWithParamSupport();
470 ThreadWithParamSupport(
const ThreadWithParamSupport&) =
delete;
471 ThreadWithParamSupport& operator=(
const ThreadWithParamSupport&) =
delete;
476ThreadWithParamBase::ThreadWithParamBase(Runnable* runnable,
477 Notification* thread_can_start)
479 ThreadWithParamSupport::CreateThread(runnable, thread_can_start)) {}
481ThreadWithParamBase::~ThreadWithParamBase() { Join(); }
483void ThreadWithParamBase::Join() {
484 GTEST_CHECK_(::WaitForSingleObject(thread_.Get(), INFINITE) == WAIT_OBJECT_0)
485 <<
"Failed to join the thread with error " << ::GetLastError() <<
".";
492class ThreadLocalRegistryImpl {
496 static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
497 const ThreadLocalBase* thread_local_instance) {
499 MemoryIsNotDeallocated memory_is_not_deallocated;
501 DWORD current_thread = ::GetCurrentThreadId();
503 ThreadIdToThreadLocals*
const thread_to_thread_locals =
504 GetThreadLocalsMapLocked();
505 ThreadIdToThreadLocals::iterator thread_local_pos =
506 thread_to_thread_locals->find(current_thread);
507 if (thread_local_pos == thread_to_thread_locals->end()) {
509 thread_to_thread_locals
510 ->insert(std::make_pair(current_thread, ThreadLocalValues()))
512 StartWatcherThreadFor(current_thread);
514 ThreadLocalValues& thread_local_values = thread_local_pos->second;
515 ThreadLocalValues::iterator value_pos =
516 thread_local_values.find(thread_local_instance);
517 if (value_pos == thread_local_values.end()) {
520 .insert(std::make_pair(
521 thread_local_instance,
522 std::shared_ptr<ThreadLocalValueHolderBase>(
523 thread_local_instance->NewValueForCurrentThread())))
526 return value_pos->second.get();
529 static void OnThreadLocalDestroyed(
530 const ThreadLocalBase* thread_local_instance) {
531 std::vector<std::shared_ptr<ThreadLocalValueHolderBase> > value_holders;
536 ThreadIdToThreadLocals*
const thread_to_thread_locals =
537 GetThreadLocalsMapLocked();
538 for (ThreadIdToThreadLocals::iterator it =
539 thread_to_thread_locals->begin();
540 it != thread_to_thread_locals->end(); ++it) {
541 ThreadLocalValues& thread_local_values = it->second;
542 ThreadLocalValues::iterator value_pos =
543 thread_local_values.find(thread_local_instance);
544 if (value_pos != thread_local_values.end()) {
545 value_holders.push_back(value_pos->second);
546 thread_local_values.erase(value_pos);
556 static void OnThreadExit(DWORD thread_id) {
558 std::vector<std::shared_ptr<ThreadLocalValueHolderBase> > value_holders;
563 ThreadIdToThreadLocals*
const thread_to_thread_locals =
564 GetThreadLocalsMapLocked();
565 ThreadIdToThreadLocals::iterator thread_local_pos =
566 thread_to_thread_locals->find(thread_id);
567 if (thread_local_pos != thread_to_thread_locals->end()) {
568 ThreadLocalValues& thread_local_values = thread_local_pos->second;
569 for (ThreadLocalValues::iterator value_pos =
570 thread_local_values.begin();
571 value_pos != thread_local_values.end(); ++value_pos) {
572 value_holders.push_back(value_pos->second);
574 thread_to_thread_locals->erase(thread_local_pos);
583 typedef std::map<
const ThreadLocalBase*,
584 std::shared_ptr<ThreadLocalValueHolderBase> >
588 typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals;
592 typedef std::pair<DWORD, HANDLE> ThreadIdAndHandle;
594 static void StartWatcherThreadFor(DWORD thread_id) {
598 ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION, FALSE, thread_id);
602 DWORD watcher_thread_id;
603 HANDLE watcher_thread = ::CreateThread(
606 &ThreadLocalRegistryImpl::WatcherThreadFunc,
607 reinterpret_cast<LPVOID
>(
new ThreadIdAndHandle(thread_id, thread)),
608 CREATE_SUSPENDED, &watcher_thread_id);
610 <<
"CreateThread failed with error " << ::GetLastError() <<
".";
613 ::SetThreadPriority(watcher_thread,
614 ::GetThreadPriority(::GetCurrentThread()));
615 ::ResumeThread(watcher_thread);
616 ::CloseHandle(watcher_thread);
621 static DWORD WINAPI WatcherThreadFunc(LPVOID param) {
622 const ThreadIdAndHandle* tah =
623 reinterpret_cast<const ThreadIdAndHandle*
>(param);
624 GTEST_CHECK_(::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0);
625 OnThreadExit(tah->first);
626 ::CloseHandle(tah->second);
632 static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() {
635 MemoryIsNotDeallocated memory_is_not_deallocated;
637 static ThreadIdToThreadLocals* map =
new ThreadIdToThreadLocals();
644 static Mutex thread_map_mutex_;
647Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex);
648Mutex ThreadLocalRegistryImpl::thread_map_mutex_(
649 Mutex::kStaticMutex);
651ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(
652 const ThreadLocalBase* thread_local_instance) {
653 return ThreadLocalRegistryImpl::GetValueOnCurrentThread(
654 thread_local_instance);
657void ThreadLocalRegistry::OnThreadLocalDestroyed(
658 const ThreadLocalBase* thread_local_instance) {
659 ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance);
664#ifdef GTEST_USES_POSIX_RE
674 regfree(&partial_regex_);
675 regfree(&full_regex_);
680bool RE::FullMatch(
const char* str,
const RE& re) {
681 if (!re.is_valid_)
return false;
684 return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
689bool RE::PartialMatch(
const char* str,
const RE& re) {
690 if (!re.is_valid_)
return false;
693 return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;
697void RE::Init(
const char* regex) {
702 const size_t full_regex_len = strlen(regex) + 10;
703 char*
const full_pattern =
new char[full_regex_len];
705 snprintf(full_pattern, full_regex_len,
"^(%s)$", regex);
706 is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;
716 const char*
const partial_regex = (*regex ==
'\0') ?
"()" : regex;
717 is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0;
720 <<
"Regular expression \"" << regex
721 <<
"\" is not a valid POSIX Extended regular expression.";
723 delete[] full_pattern;
726#elif defined(GTEST_USES_SIMPLE_RE)
730bool IsInSet(
char ch,
const char* str) {
731 return ch !=
'\0' && strchr(str,
ch) !=
nullptr;
737bool IsAsciiDigit(
char ch) {
return '0' <=
ch &&
ch <=
'9'; }
738bool IsAsciiPunct(
char ch) {
739 return IsInSet(
ch,
"^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~");
741bool IsRepeat(
char ch) {
return IsInSet(
ch,
"?*+"); }
742bool IsAsciiWhiteSpace(
char ch) {
return IsInSet(
ch,
" \f\n\r\t\v"); }
743bool IsAsciiWordChar(
char ch) {
744 return (
'a' <=
ch &&
ch <=
'z') || (
'A' <=
ch &&
ch <=
'Z') ||
745 (
'0' <=
ch &&
ch <=
'9') ||
ch ==
'_';
749bool IsValidEscape(
char c) {
750 return (IsAsciiPunct(c) || IsInSet(c,
"dDfnrsStvwW"));
755bool AtomMatchesChar(
bool escaped,
char pattern_char,
char ch) {
757 switch (pattern_char) {
759 return IsAsciiDigit(
ch);
761 return !IsAsciiDigit(
ch);
769 return IsAsciiWhiteSpace(
ch);
771 return !IsAsciiWhiteSpace(
ch);
777 return IsAsciiWordChar(
ch);
779 return !IsAsciiWordChar(
ch);
781 return IsAsciiPunct(pattern_char) && pattern_char ==
ch;
784 return (pattern_char ==
'.' &&
ch !=
'\n') || pattern_char ==
ch;
788static std::string FormatRegexSyntaxError(
const char* regex,
int index) {
789 return (Message() <<
"Syntax error at index " << index
790 <<
" in simple regular expression \"" << regex <<
"\": ")
796bool ValidateRegex(
const char* regex) {
797 if (regex ==
nullptr) {
798 ADD_FAILURE() <<
"NULL is not a valid simple regular expression.";
802 bool is_valid =
true;
805 bool prev_repeatable =
false;
806 for (
int i = 0; regex[
i];
i++) {
807 if (regex[
i] ==
'\\') {
809 if (regex[
i] ==
'\0') {
811 <<
"'\\' cannot appear at the end.";
815 if (!IsValidEscape(regex[
i])) {
817 <<
"invalid escape sequence \"\\" << regex[
i] <<
"\".";
820 prev_repeatable =
true;
822 const char ch = regex[
i];
824 if (
ch ==
'^' &&
i > 0) {
826 <<
"'^' can only appear at the beginning.";
828 }
else if (
ch ==
'$' && regex[
i + 1] !=
'\0') {
830 <<
"'$' can only appear at the end.";
832 }
else if (IsInSet(
ch,
"()[]{}|")) {
834 <<
"' is unsupported.";
836 }
else if (IsRepeat(
ch) && !prev_repeatable) {
838 <<
"' can only follow a repeatable token.";
842 prev_repeatable = !IsInSet(
ch,
"^$?*+");
856bool MatchRepetitionAndRegexAtHead(
bool escaped,
char c,
char repeat,
857 const char* regex,
const char* str) {
858 const size_t min_count = (repeat ==
'+') ? 1 : 0;
859 const size_t max_count = (repeat ==
'?') ? 1 : static_cast<size_t>(-1) - 1;
863 for (
size_t i = 0;
i <= max_count; ++
i) {
865 if (
i >= min_count && MatchRegexAtHead(regex, str +
i)) {
872 if (str[
i] ==
'\0' || !AtomMatchesChar(escaped, c, str[
i]))
return false;
880bool MatchRegexAtHead(
const char* regex,
const char* str) {
886 if (*regex ==
'$')
return *str ==
'\0';
889 const bool escaped = *regex ==
'\\';
890 if (escaped) ++regex;
891 if (IsRepeat(regex[1])) {
895 return MatchRepetitionAndRegexAtHead(escaped, regex[0], regex[1], regex + 2,
901 return (*str !=
'\0') && AtomMatchesChar(escaped, *regex, *str) &&
902 MatchRegexAtHead(regex + 1, str + 1);
914bool MatchRegexAnywhere(
const char* regex,
const char* str) {
915 if (regex ==
nullptr || str ==
nullptr)
return false;
917 if (*regex ==
'^')
return MatchRegexAtHead(regex + 1, str);
921 if (MatchRegexAtHead(regex, str))
return true;
922 }
while (*str++ !=
'\0');
931bool RE::FullMatch(
const char* str,
const RE& re) {
932 return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_.c_str(), str);
937bool RE::PartialMatch(
const char* str,
const RE& re) {
938 return re.is_valid_ && MatchRegexAnywhere(re.pattern_.c_str(), str);
942void RE::Init(
const char* regex) {
943 full_pattern_.clear();
946 if (regex !=
nullptr) {
950 is_valid_ = ValidateRegex(regex);
958 full_pattern_.reserve(pattern_.size() + 2);
960 if (pattern_.empty() || pattern_.front() !=
'^') {
961 full_pattern_.push_back(
'^');
964 full_pattern_.append(pattern_);
966 if (pattern_.empty() || pattern_.back() !=
'$') {
967 full_pattern_.push_back(
'$');
978 const std::string file_name(file ==
nullptr ?
kUnknownFile : file);
981 return file_name +
":";
984 return file_name +
"(" + StreamableToString(line) +
"):";
986 return file_name +
":" + StreamableToString(line) +
":";
997 const std::string file_name(file ==
nullptr ?
kUnknownFile : file);
1002 return file_name +
":" + StreamableToString(line);
1006 : severity_(severity) {
1007 const char*
const marker = severity ==
GTEST_INFO ?
"[ INFO ]"
1029#if GTEST_HAS_STREAM_REDIRECTION
1032class CapturedStream {
1035 explicit CapturedStream(
int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
1036#ifdef GTEST_OS_WINDOWS
1037 char temp_dir_path[MAX_PATH + 1] = {
'\0'};
1038 char temp_file_path[MAX_PATH + 1] = {
'\0'};
1040 ::GetTempPathA(
sizeof(temp_dir_path), temp_dir_path);
1041 const UINT success = ::GetTempFileNameA(temp_dir_path,
"gtest_redir",
1045 <<
"Unable to create a temporary file in " << temp_dir_path;
1046 const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
1048 <<
"Unable to open temporary file " << temp_file_path;
1049 filename_ = temp_file_path;
1053 std::string name_template;
1055#ifdef GTEST_OS_LINUX_ANDROID
1067 name_template =
"/data/local/tmp/";
1068#elif defined(GTEST_OS_IOS)
1069 char user_temp_dir[PATH_MAX + 1];
1084 ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir,
sizeof(user_temp_dir));
1086 name_template = user_temp_dir;
1090 name_template =
"/tmp/";
1092 name_template.append(
"gtest_captured_stream.XXXXXX");
1100 const int captured_fd = ::mkstemp(
const_cast<char*
>(name_template.data()));
1101 if (captured_fd == -1) {
1103 <<
"Failed to create tmp file " << name_template
1104 <<
" for test; does the test have access to the /tmp directory?";
1106 filename_ = std::move(name_template);
1109 dup2(captured_fd, fd_);
1113 ~CapturedStream() { remove(filename_.c_str()); }
1115 std::string GetCapturedString() {
1116 if (uncaptured_fd_ != -1) {
1119 dup2(uncaptured_fd_, fd_);
1120 close(uncaptured_fd_);
1121 uncaptured_fd_ = -1;
1124 FILE*
const file =
posix::FOpen(filename_.c_str(),
"r");
1125 if (file ==
nullptr) {
1126 GTEST_LOG_(FATAL) <<
"Failed to open tmp file " << filename_
1127 <<
" for capturing stream.";
1138 ::std::string filename_;
1140 CapturedStream(
const CapturedStream&) =
delete;
1141 CapturedStream& operator=(
const CapturedStream&) =
delete;
1146static CapturedStream* g_captured_stderr =
nullptr;
1147static CapturedStream* g_captured_stdout =
nullptr;
1150static
void CaptureStream(
int fd, const
char* stream_name,
1151 CapturedStream** stream) {
1152 if (*stream !=
nullptr) {
1153 GTEST_LOG_(FATAL) <<
"Only one " << stream_name
1154 <<
" capturer can exist at a time.";
1156 *stream =
new CapturedStream(fd);
1160static std::string GetCapturedStream(CapturedStream** captured_stream) {
1161 const std::string content = (*captured_stream)->GetCapturedString();
1163 delete *captured_stream;
1164 *captured_stream =
nullptr;
1169#if defined(_MSC_VER) || defined(__BORLANDC__)
1171const int kStdOutFileno = 1;
1172const int kStdErrFileno = 2;
1174const int kStdOutFileno = STDOUT_FILENO;
1175const int kStdErrFileno = STDERR_FILENO;
1180 CaptureStream(kStdOutFileno,
"stdout", &g_captured_stdout);
1185 CaptureStream(kStdErrFileno,
"stderr", &g_captured_stderr);
1190 return GetCapturedStream(&g_captured_stdout);
1195 return GetCapturedStream(&g_captured_stderr);
1201 fseek(file, 0, SEEK_END);
1202 return static_cast<size_t>(ftell(file));
1207 char*
const buffer =
new char[file_size];
1209 size_t bytes_last_read = 0;
1210 size_t bytes_read = 0;
1212 fseek(file, 0, SEEK_SET);
1218 fread(buffer + bytes_read, 1, file_size - bytes_read, file);
1219 bytes_read += bytes_last_read;
1220 }
while (bytes_last_read > 0 && bytes_read < file_size);
1222 const std::string content(buffer, bytes_read);
1228#ifdef GTEST_HAS_DEATH_TEST
1229static const std::vector<std::string>* g_injected_test_argvs =
1232std::vector<std::string> GetInjectableArgvs() {
1233 if (g_injected_test_argvs !=
nullptr) {
1234 return *g_injected_test_argvs;
1239void SetInjectableArgvs(
const std::vector<std::string>* new_argvs) {
1240 if (g_injected_test_argvs != new_argvs)
delete g_injected_test_argvs;
1241 g_injected_test_argvs = new_argvs;
1244void SetInjectableArgvs(
const std::vector<std::string>& new_argvs) {
1246 new std::vector<std::string>(new_argvs.begin(), new_argvs.end()));
1249void ClearInjectableArgvs() {
1250 delete g_injected_test_argvs;
1251 g_injected_test_argvs =
nullptr;
1255#ifdef GTEST_OS_WINDOWS_MOBILE
1259 TerminateProcess(GetCurrentProcess(), 1);
1268 const std::string full_flag =
1272 for (
size_t i = 0;
i != full_flag.length();
i++) {
1273 env_var <<
ToUpper(full_flag.c_str()[
i]);
1284 char* end =
nullptr;
1285 const long long_value = strtol(str, &end, 10);
1291 msg <<
"WARNING: " << src_text
1292 <<
" is expected to be a 32-bit integer, but actually"
1293 <<
" has value \"" << str <<
"\".\n";
1300 const auto result =
static_cast<int32_t
>(long_value);
1301 if (long_value == LONG_MAX || long_value == LONG_MIN ||
1304 result != long_value
1308 msg <<
"WARNING: " << src_text
1309 <<
" is expected to be a 32-bit integer, but actually"
1310 <<
" has value " << str <<
", which overflows.\n";
1325#if defined(GTEST_GET_BOOL_FROM_ENV_)
1326 return GTEST_GET_BOOL_FROM_ENV_(flag, default_value);
1329 const char*
const string_value =
posix::GetEnv(env_var.c_str());
1330 return string_value ==
nullptr ? default_value
1331 : strcmp(string_value,
"0") != 0;
1339#if defined(GTEST_GET_INT32_FROM_ENV_)
1340 return GTEST_GET_INT32_FROM_ENV_(flag, default_value);
1343 const char*
const string_value =
posix::GetEnv(env_var.c_str());
1344 if (string_value ==
nullptr) {
1346 return default_value;
1349 int32_t result = default_value;
1352 printf(
"The default value %s is used.\n",
1353 (
Message() << default_value).GetString().c_str());
1355 return default_value;
1371 std::string default_value_for_output_flag =
"";
1372 const char* xml_output_file_env =
posix::GetEnv(
"XML_OUTPUT_FILE");
1373 if (
nullptr != xml_output_file_env) {
1374 default_value_for_output_flag = std::string(
"xml:") + xml_output_file_env;
1376 return default_value_for_output_flag;
1382#if defined(GTEST_GET_STRING_FROM_ENV_)
1383 return GTEST_GET_STRING_FROM_ENV_(flag, default_value);
1387 return value ==
nullptr ? default_value :
value;
Definition gtest-message.h:101
std::string GetString() const
Definition gtest.cc:1301
::std::ostream & GetStream()
Definition gtest-port.h:1042
~GTestLog()
Definition gtest-port.cc:1017
int value
Definition gmock-actions_test.cc:1714
int i
Definition gmock-matchers-comparisons_test.cc:603
char ch
Definition gmock-matchers-containers_test.cc:384
#define GTEST_FLAG_PREFIX_
Definition gtest-port.h:331
#define GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
Definition gtest-port.h:373
#define GTEST_LOG_(severity)
Definition gtest-port.h:1053
#define GTEST_DISABLE_MSC_DEPRECATED_POP_()
Definition gtest-port.h:375
#define GTEST_CHECK_(condition)
Definition gtest-port.h:1078
#define GTEST_PATH_SEP_
Definition gtest-port.h:1918
#define EXPECT_TRUE(condition)
Definition gtest.h:1807
#define ADD_FAILURE()
Definition gtest.h:1735
output
Definition gmock_output_test.py:182
std::mutex Mutex
Definition lock.hpp:10
Definition gtest-port.h:1987
void Abort()
Definition gtest-port.h:2147
const char * GetEnv(const char *name)
Definition gtest-port.h:2122
int FClose(FILE *fp)
Definition gtest-port.h:2106
FILE * FOpen(const char *path, const char *mode)
Definition gtest-port.h:2089
GTEST_API_ size_t GetFileSize(FILE *file)
Definition gtest-port.cc:1200
GTestMutexLock MutexLock
Definition gtest-port.h:1892
std::string OutputFlagAlsoCheckEnvVar()
Definition gtest-port.cc:1370
GTEST_API_::std::string FormatCompilerIndependentFileLocation(const char *file, int line)
Definition gtest-port.cc:995
GTEST_API_ std::string ReadEntireFile(FILE *file)
Definition gtest-port.cc:1205
GTEST_API_::std::string FormatFileLocation(const char *file, int line)
Definition gtest-port.cc:977
GTEST_API_ std::string GetCapturedStderr()
GTEST_API_ size_t GetThreadCount()
Definition gtest-port.cc:265
GTEST_API_ bool ParseInt32(const Message &src_text, const char *str, int32_t *value)
Definition gtest-port.cc:1282
bool BoolFromGTestEnv(const char *flag, bool default_val)
Definition gtest-port.cc:1324
static std::string FlagToEnvVar(const char *flag)
Definition gtest-port.cc:1267
const char * StringFromGTestEnv(const char *flag, const char *default_val)
Definition gtest-port.cc:1381
GTEST_API_ void CaptureStderr()
GTEST_API_ std::vector< std::string > GetArgvs()
Definition gtest.cc:624
GTestLogSeverity
Definition gtest-port.h:1030
@ GTEST_ERROR
Definition gtest-port.h:1030
@ GTEST_FATAL
Definition gtest-port.h:1030
@ GTEST_WARNING
Definition gtest-port.h:1030
@ GTEST_INFO
Definition gtest-port.h:1030
const char kUnknownFile[]
Definition gtest-port.cc:973
GTEST_API_ int32_t Int32FromGTestEnv(const char *flag, int32_t default_val)
Definition gtest-port.cc:1338
char ToUpper(char ch)
Definition gtest-port.h:1971
GTEST_API_ void CaptureStdout()
GTEST_API_ std::string GetCapturedStdout()
Definition gmock-actions.h:151