gtest-port.cc
Go to the documentation of this file.
00001 // Copyright 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 #include "gtest/internal/gtest-port.h"
00033 
00034 #include <limits.h>
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 #if GTEST_OS_WINDOWS
00040 # include <windows.h>
00041 # include <io.h>
00042 # include <sys/stat.h>
00043 # include <map>  // Used in ThreadLocal.
00044 #else
00045 # include <unistd.h>
00046 #endif  // GTEST_OS_WINDOWS
00047 
00048 #if GTEST_OS_MAC
00049 # include <mach/mach_init.h>
00050 # include <mach/task.h>
00051 # include <mach/vm_map.h>
00052 #endif  // GTEST_OS_MAC
00053 
00054 #if GTEST_OS_QNX
00055 # include <devctl.h>
00056 # include <fcntl.h>
00057 # include <sys/procfs.h>
00058 #endif  // GTEST_OS_QNX
00059 
00060 #include "gtest/gtest-spi.h"
00061 #include "gtest/gtest-message.h"
00062 #include "gtest/internal/gtest-internal.h"
00063 #include "gtest/internal/gtest-string.h"
00064 
00065 // Indicates that this translation unit is part of Google Test's
00066 // implementation.  It must come before gtest-internal-inl.h is
00067 // included, or there will be a compiler error.  This trick exists to
00068 // prevent the accidental inclusion of gtest-internal-inl.h in the
00069 // user's code.
00070 #define GTEST_IMPLEMENTATION_ 1
00071 #include "src/gtest-internal-inl.h"
00072 #undef GTEST_IMPLEMENTATION_
00073 
00074 namespace testing {
00075 namespace internal {
00076 
00077 #if defined(_MSC_VER) || defined(__BORLANDC__)
00078 // MSVC and C++Builder do not provide a definition of STDERR_FILENO.
00079 const int kStdOutFileno = 1;
00080 const int kStdErrFileno = 2;
00081 #else
00082 const int kStdOutFileno = STDOUT_FILENO;
00083 const int kStdErrFileno = STDERR_FILENO;
00084 #endif  // _MSC_VER
00085 
00086 #if GTEST_OS_MAC
00087 
00088 // Returns the number of threads running in the process, or 0 to indicate that
00089 // we cannot detect it.
00090 size_t GetThreadCount() {
00091   const task_t task = mach_task_self();
00092   mach_msg_type_number_t thread_count;
00093   thread_act_array_t thread_list;
00094   const kern_return_t status = task_threads(task, &thread_list, &thread_count);
00095   if (status == KERN_SUCCESS) {
00096     // task_threads allocates resources in thread_list and we need to free them
00097     // to avoid leaks.
00098     vm_deallocate(task,
00099                   reinterpret_cast<vm_address_t>(thread_list),
00100                   sizeof(thread_t) * thread_count);
00101     return static_cast<size_t>(thread_count);
00102   } else {
00103     return 0;
00104   }
00105 }
00106 
00107 #elif GTEST_OS_QNX
00108 
00109 // Returns the number of threads running in the process, or 0 to indicate that
00110 // we cannot detect it.
00111 size_t GetThreadCount() {
00112   const int fd = open("/proc/self/as", O_RDONLY);
00113   if (fd < 0) {
00114     return 0;
00115   }
00116   procfs_info process_info;
00117   const int status =
00118       devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), NULL);
00119   close(fd);
00120   if (status == EOK) {
00121     return static_cast<size_t>(process_info.num_threads);
00122   } else {
00123     return 0;
00124   }
00125 }
00126 
00127 #else
00128 
00129 size_t GetThreadCount() {
00130   // There's no portable way to detect the number of threads, so we just
00131   // return 0 to indicate that we cannot detect it.
00132   return 0;
00133 }
00134 
00135 #endif  // GTEST_OS_MAC
00136 
00137 #if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
00138 
00139 void SleepMilliseconds(int n) {
00140   ::Sleep(n);
00141 }
00142 
00143 AutoHandle::AutoHandle()
00144     : handle_(INVALID_HANDLE_VALUE) {}
00145 
00146 AutoHandle::AutoHandle(Handle handle)
00147     : handle_(handle) {}
00148 
00149 AutoHandle::~AutoHandle() {
00150   Reset();
00151 }
00152 
00153 AutoHandle::Handle AutoHandle::Get() const {
00154   return handle_;
00155 }
00156 
00157 void AutoHandle::Reset() {
00158   Reset(INVALID_HANDLE_VALUE);
00159 }
00160 
00161 void AutoHandle::Reset(HANDLE handle) {
00162   // Resetting with the same handle we already own is invalid.
00163   if (handle_ != handle) {
00164     if (IsCloseable()) {
00165       ::CloseHandle(handle_);
00166     }
00167     handle_ = handle;
00168   } else {
00169     GTEST_CHECK_(!IsCloseable())
00170         << "Resetting a valid handle to itself is likely a programmer error "
00171             "and thus not allowed.";
00172   }
00173 }
00174 
00175 bool AutoHandle::IsCloseable() const {
00176   // Different Windows APIs may use either of these values to represent an
00177   // invalid handle.
00178   return handle_ != NULL && handle_ != INVALID_HANDLE_VALUE;
00179 }
00180 
00181 Notification::Notification()
00182     : event_(::CreateEvent(NULL,   // Default security attributes.
00183                            TRUE,   // Do not reset automatically.
00184                            FALSE,  // Initially unset.
00185                            NULL)) {  // Anonymous event.
00186   GTEST_CHECK_(event_.Get() != NULL);
00187 }
00188 
00189 void Notification::Notify() {
00190   GTEST_CHECK_(::SetEvent(event_.Get()) != FALSE);
00191 }
00192 
00193 void Notification::WaitForNotification() {
00194   GTEST_CHECK_(
00195       ::WaitForSingleObject(event_.Get(), INFINITE) == WAIT_OBJECT_0);
00196 }
00197 
00198 Mutex::Mutex()
00199     : type_(kDynamic),
00200       owner_thread_id_(0),
00201       critical_section_init_phase_(0),
00202       critical_section_(new CRITICAL_SECTION) {
00203   ::InitializeCriticalSection(critical_section_);
00204 }
00205 
00206 Mutex::~Mutex() {
00207   // Static mutexes are leaked intentionally. It is not thread-safe to try
00208   // to clean them up.
00209   // TODO(yukawa): Switch to Slim Reader/Writer (SRW) Locks, which requires
00210   // nothing to clean it up but is available only on Vista and later.
00211   // http://msdn.microsoft.com/en-us/library/windows/desktop/aa904937.aspx
00212   if (type_ == kDynamic) {
00213     ::DeleteCriticalSection(critical_section_);
00214     delete critical_section_;
00215     critical_section_ = NULL;
00216   }
00217 }
00218 
00219 void Mutex::Lock() {
00220   ThreadSafeLazyInit();
00221   ::EnterCriticalSection(critical_section_);
00222   owner_thread_id_ = ::GetCurrentThreadId();
00223 }
00224 
00225 void Mutex::Unlock() {
00226   ThreadSafeLazyInit();
00227   // We don't protect writing to owner_thread_id_ here, as it's the
00228   // caller's responsibility to ensure that the current thread holds the
00229   // mutex when this is called.
00230   owner_thread_id_ = 0;
00231   ::LeaveCriticalSection(critical_section_);
00232 }
00233 
00234 // Does nothing if the current thread holds the mutex. Otherwise, crashes
00235 // with high probability.
00236 void Mutex::AssertHeld() {
00237   ThreadSafeLazyInit();
00238   GTEST_CHECK_(owner_thread_id_ == ::GetCurrentThreadId())
00239       << "The current thread is not holding the mutex @" << this;
00240 }
00241 
00242 // Initializes owner_thread_id_ and critical_section_ in static mutexes.
00243 void Mutex::ThreadSafeLazyInit() {
00244   // Dynamic mutexes are initialized in the constructor.
00245   if (type_ == kStatic) {
00246     switch (
00247         ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) {
00248       case 0:
00249         // If critical_section_init_phase_ was 0 before the exchange, we
00250         // are the first to test it and need to perform the initialization.
00251         owner_thread_id_ = 0;
00252         critical_section_ = new CRITICAL_SECTION;
00253         ::InitializeCriticalSection(critical_section_);
00254         // Updates the critical_section_init_phase_ to 2 to signal
00255         // initialization complete.
00256         GTEST_CHECK_(::InterlockedCompareExchange(
00257                           &critical_section_init_phase_, 2L, 1L) ==
00258                       1L);
00259         break;
00260       case 1:
00261         // Somebody else is already initializing the mutex; spin until they
00262         // are done.
00263         while (::InterlockedCompareExchange(&critical_section_init_phase_,
00264                                             2L,
00265                                             2L) != 2L) {
00266           // Possibly yields the rest of the thread's time slice to other
00267           // threads.
00268           ::Sleep(0);
00269         }
00270         break;
00271 
00272       case 2:
00273         break;  // The mutex is already initialized and ready for use.
00274 
00275       default:
00276         GTEST_CHECK_(false)
00277             << "Unexpected value of critical_section_init_phase_ "
00278             << "while initializing a static mutex.";
00279     }
00280   }
00281 }
00282 
00283 namespace {
00284 
00285 class ThreadWithParamSupport : public ThreadWithParamBase {
00286  public:
00287   static HANDLE CreateThread(Runnable* runnable,
00288                              Notification* thread_can_start) {
00289     ThreadMainParam* param = new ThreadMainParam(runnable, thread_can_start);
00290     DWORD thread_id;
00291     // TODO(yukawa): Consider to use _beginthreadex instead.
00292     HANDLE thread_handle = ::CreateThread(
00293         NULL,    // Default security.
00294         0,       // Default stack size.
00295         &ThreadWithParamSupport::ThreadMain,
00296         param,   // Parameter to ThreadMainStatic
00297         0x0,     // Default creation flags.
00298         &thread_id);  // Need a valid pointer for the call to work under Win98.
00299     GTEST_CHECK_(thread_handle != NULL) << "CreateThread failed with error "
00300                                         << ::GetLastError() << ".";
00301     if (thread_handle == NULL) {
00302       delete param;
00303     }
00304     return thread_handle;
00305   }
00306 
00307  private:
00308   struct ThreadMainParam {
00309     ThreadMainParam(Runnable* runnable, Notification* thread_can_start)
00310         : runnable_(runnable),
00311           thread_can_start_(thread_can_start) {
00312     }
00313     scoped_ptr<Runnable> runnable_;
00314     // Does not own.
00315     Notification* thread_can_start_;
00316   };
00317 
00318   static DWORD WINAPI ThreadMain(void* ptr) {
00319     // Transfers ownership.
00320     scoped_ptr<ThreadMainParam> param(static_cast<ThreadMainParam*>(ptr));
00321     if (param->thread_can_start_ != NULL)
00322       param->thread_can_start_->WaitForNotification();
00323     param->runnable_->Run();
00324     return 0;
00325   }
00326 
00327   // Prohibit instantiation.
00328   ThreadWithParamSupport();
00329 
00330   GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParamSupport);
00331 };
00332 
00333 }  // namespace
00334 
00335 ThreadWithParamBase::ThreadWithParamBase(Runnable *runnable,
00336                                          Notification* thread_can_start)
00337       : thread_(ThreadWithParamSupport::CreateThread(runnable,
00338                                                      thread_can_start)) {
00339 }
00340 
00341 ThreadWithParamBase::~ThreadWithParamBase() {
00342   Join();
00343 }
00344 
00345 void ThreadWithParamBase::Join() {
00346   GTEST_CHECK_(::WaitForSingleObject(thread_.Get(), INFINITE) == WAIT_OBJECT_0)
00347       << "Failed to join the thread with error " << ::GetLastError() << ".";
00348 }
00349 
00350 // Maps a thread to a set of ThreadIdToThreadLocals that have values
00351 // instantiated on that thread and notifies them when the thread exits.  A
00352 // ThreadLocal instance is expected to persist until all threads it has
00353 // values on have terminated.
00354 class ThreadLocalRegistryImpl {
00355  public:
00356   // Registers thread_local_instance as having value on the current thread.
00357   // Returns a value that can be used to identify the thread from other threads.
00358   static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
00359       const ThreadLocalBase* thread_local_instance) {
00360     DWORD current_thread = ::GetCurrentThreadId();
00361     MutexLock lock(&mutex_);
00362     ThreadIdToThreadLocals* const thread_to_thread_locals =
00363         GetThreadLocalsMapLocked();
00364     ThreadIdToThreadLocals::iterator thread_local_pos =
00365         thread_to_thread_locals->find(current_thread);
00366     if (thread_local_pos == thread_to_thread_locals->end()) {
00367       thread_local_pos = thread_to_thread_locals->insert(
00368           std::make_pair(current_thread, ThreadLocalValues())).first;
00369       StartWatcherThreadFor(current_thread);
00370     }
00371     ThreadLocalValues& thread_local_values = thread_local_pos->second;
00372     ThreadLocalValues::iterator value_pos =
00373         thread_local_values.find(thread_local_instance);
00374     if (value_pos == thread_local_values.end()) {
00375       value_pos =
00376           thread_local_values
00377               .insert(std::make_pair(
00378                   thread_local_instance,
00379                   linked_ptr<ThreadLocalValueHolderBase>(
00380                       thread_local_instance->NewValueForCurrentThread())))
00381               .first;
00382     }
00383     return value_pos->second.get();
00384   }
00385 
00386   static void OnThreadLocalDestroyed(
00387       const ThreadLocalBase* thread_local_instance) {
00388     std::vector<linked_ptr<ThreadLocalValueHolderBase> > value_holders;
00389     // Clean up the ThreadLocalValues data structure while holding the lock, but
00390     // defer the destruction of the ThreadLocalValueHolderBases.
00391     {
00392       MutexLock lock(&mutex_);
00393       ThreadIdToThreadLocals* const thread_to_thread_locals =
00394           GetThreadLocalsMapLocked();
00395       for (ThreadIdToThreadLocals::iterator it =
00396           thread_to_thread_locals->begin();
00397           it != thread_to_thread_locals->end();
00398           ++it) {
00399         ThreadLocalValues& thread_local_values = it->second;
00400         ThreadLocalValues::iterator value_pos =
00401             thread_local_values.find(thread_local_instance);
00402         if (value_pos != thread_local_values.end()) {
00403           value_holders.push_back(value_pos->second);
00404           thread_local_values.erase(value_pos);
00405           // This 'if' can only be successful at most once, so theoretically we
00406           // could break out of the loop here, but we don't bother doing so.
00407         }
00408       }
00409     }
00410     // Outside the lock, let the destructor for 'value_holders' deallocate the
00411     // ThreadLocalValueHolderBases.
00412   }
00413 
00414   static void OnThreadExit(DWORD thread_id) {
00415     GTEST_CHECK_(thread_id != 0) << ::GetLastError();
00416     std::vector<linked_ptr<ThreadLocalValueHolderBase> > value_holders;
00417     // Clean up the ThreadIdToThreadLocals data structure while holding the
00418     // lock, but defer the destruction of the ThreadLocalValueHolderBases.
00419     {
00420       MutexLock lock(&mutex_);
00421       ThreadIdToThreadLocals* const thread_to_thread_locals =
00422           GetThreadLocalsMapLocked();
00423       ThreadIdToThreadLocals::iterator thread_local_pos =
00424           thread_to_thread_locals->find(thread_id);
00425       if (thread_local_pos != thread_to_thread_locals->end()) {
00426         ThreadLocalValues& thread_local_values = thread_local_pos->second;
00427         for (ThreadLocalValues::iterator value_pos =
00428             thread_local_values.begin();
00429             value_pos != thread_local_values.end();
00430             ++value_pos) {
00431           value_holders.push_back(value_pos->second);
00432         }
00433         thread_to_thread_locals->erase(thread_local_pos);
00434       }
00435     }
00436     // Outside the lock, let the destructor for 'value_holders' deallocate the
00437     // ThreadLocalValueHolderBases.
00438   }
00439 
00440  private:
00441   // In a particular thread, maps a ThreadLocal object to its value.
00442   typedef std::map<const ThreadLocalBase*,
00443                    linked_ptr<ThreadLocalValueHolderBase> > ThreadLocalValues;
00444   // Stores all ThreadIdToThreadLocals having values in a thread, indexed by
00445   // thread's ID.
00446   typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals;
00447 
00448   // Holds the thread id and thread handle that we pass from
00449   // StartWatcherThreadFor to WatcherThreadFunc.
00450   typedef std::pair<DWORD, HANDLE> ThreadIdAndHandle;
00451 
00452   static void StartWatcherThreadFor(DWORD thread_id) {
00453     // The returned handle will be kept in thread_map and closed by
00454     // watcher_thread in WatcherThreadFunc.
00455     HANDLE thread = ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION,
00456                                  FALSE,
00457                                  thread_id);
00458     GTEST_CHECK_(thread != NULL);
00459     // We need to to pass a valid thread ID pointer into CreateThread for it
00460     // to work correctly under Win98.
00461     DWORD watcher_thread_id;
00462     HANDLE watcher_thread = ::CreateThread(
00463         NULL,   // Default security.
00464         0,      // Default stack size
00465         &ThreadLocalRegistryImpl::WatcherThreadFunc,
00466         reinterpret_cast<LPVOID>(new ThreadIdAndHandle(thread_id, thread)),
00467         CREATE_SUSPENDED,
00468         &watcher_thread_id);
00469     GTEST_CHECK_(watcher_thread != NULL);
00470     // Give the watcher thread the same priority as ours to avoid being
00471     // blocked by it.
00472     ::SetThreadPriority(watcher_thread,
00473                         ::GetThreadPriority(::GetCurrentThread()));
00474     ::ResumeThread(watcher_thread);
00475     ::CloseHandle(watcher_thread);
00476   }
00477 
00478   // Monitors exit from a given thread and notifies those
00479   // ThreadIdToThreadLocals about thread termination.
00480   static DWORD WINAPI WatcherThreadFunc(LPVOID param) {
00481     const ThreadIdAndHandle* tah =
00482         reinterpret_cast<const ThreadIdAndHandle*>(param);
00483     GTEST_CHECK_(
00484         ::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0);
00485     OnThreadExit(tah->first);
00486     ::CloseHandle(tah->second);
00487     delete tah;
00488     return 0;
00489   }
00490 
00491   // Returns map of thread local instances.
00492   static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() {
00493     mutex_.AssertHeld();
00494     static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals;
00495     return map;
00496   }
00497 
00498   // Protects access to GetThreadLocalsMapLocked() and its return value.
00499   static Mutex mutex_;
00500   // Protects access to GetThreadMapLocked() and its return value.
00501   static Mutex thread_map_mutex_;
00502 };
00503 
00504 Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex);
00505 Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex);
00506 
00507 ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(
00508       const ThreadLocalBase* thread_local_instance) {
00509   return ThreadLocalRegistryImpl::GetValueOnCurrentThread(
00510       thread_local_instance);
00511 }
00512 
00513 void ThreadLocalRegistry::OnThreadLocalDestroyed(
00514       const ThreadLocalBase* thread_local_instance) {
00515   ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance);
00516 }
00517 
00518 #endif  // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
00519 
00520 #if GTEST_USES_POSIX_RE
00521 
00522 // Implements RE.  Currently only needed for death tests.
00523 
00524 RE::~RE() {
00525   if (is_valid_) {
00526     // regfree'ing an invalid regex might crash because the content
00527     // of the regex is undefined. Since the regex's are essentially
00528     // the same, one cannot be valid (or invalid) without the other
00529     // being so too.
00530     regfree(&partial_regex_);
00531     regfree(&full_regex_);
00532   }
00533   free(const_cast<char*>(pattern_));
00534 }
00535 
00536 // Returns true iff regular expression re matches the entire str.
00537 bool RE::FullMatch(const char* str, const RE& re) {
00538   if (!re.is_valid_) return false;
00539 
00540   regmatch_t match;
00541   return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
00542 }
00543 
00544 // Returns true iff regular expression re matches a substring of str
00545 // (including str itself).
00546 bool RE::PartialMatch(const char* str, const RE& re) {
00547   if (!re.is_valid_) return false;
00548 
00549   regmatch_t match;
00550   return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;
00551 }
00552 
00553 // Initializes an RE from its string representation.
00554 void RE::Init(const char* regex) {
00555   pattern_ = posix::StrDup(regex);
00556 
00557   // Reserves enough bytes to hold the regular expression used for a
00558   // full match.
00559   const size_t full_regex_len = strlen(regex) + 10;
00560   char* const full_pattern = new char[full_regex_len];
00561 
00562   snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
00563   is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;
00564   // We want to call regcomp(&partial_regex_, ...) even if the
00565   // previous expression returns false.  Otherwise partial_regex_ may
00566   // not be properly initialized can may cause trouble when it's
00567   // freed.
00568   //
00569   // Some implementation of POSIX regex (e.g. on at least some
00570   // versions of Cygwin) doesn't accept the empty string as a valid
00571   // regex.  We change it to an equivalent form "()" to be safe.
00572   if (is_valid_) {
00573     const char* const partial_regex = (*regex == '\0') ? "()" : regex;
00574     is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0;
00575   }
00576   EXPECT_TRUE(is_valid_)
00577       << "Regular expression \"" << regex
00578       << "\" is not a valid POSIX Extended regular expression.";
00579 
00580   delete[] full_pattern;
00581 }
00582 
00583 #elif GTEST_USES_SIMPLE_RE
00584 
00585 // Returns true iff ch appears anywhere in str (excluding the
00586 // terminating '\0' character).
00587 bool IsInSet(char ch, const char* str) {
00588   return ch != '\0' && strchr(str, ch) != NULL;
00589 }
00590 
00591 // Returns true iff ch belongs to the given classification.  Unlike
00592 // similar functions in <ctype.h>, these aren't affected by the
00593 // current locale.
00594 bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; }
00595 bool IsAsciiPunct(char ch) {
00596   return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~");
00597 }
00598 bool IsRepeat(char ch) { return IsInSet(ch, "?*+"); }
00599 bool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); }
00600 bool IsAsciiWordChar(char ch) {
00601   return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
00602       ('0' <= ch && ch <= '9') || ch == '_';
00603 }
00604 
00605 // Returns true iff "\\c" is a supported escape sequence.
00606 bool IsValidEscape(char c) {
00607   return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW"));
00608 }
00609 
00610 // Returns true iff the given atom (specified by escaped and pattern)
00611 // matches ch.  The result is undefined if the atom is invalid.
00612 bool AtomMatchesChar(bool escaped, char pattern_char, char ch) {
00613   if (escaped) {  // "\\p" where p is pattern_char.
00614     switch (pattern_char) {
00615       case 'd': return IsAsciiDigit(ch);
00616       case 'D': return !IsAsciiDigit(ch);
00617       case 'f': return ch == '\f';
00618       case 'n': return ch == '\n';
00619       case 'r': return ch == '\r';
00620       case 's': return IsAsciiWhiteSpace(ch);
00621       case 'S': return !IsAsciiWhiteSpace(ch);
00622       case 't': return ch == '\t';
00623       case 'v': return ch == '\v';
00624       case 'w': return IsAsciiWordChar(ch);
00625       case 'W': return !IsAsciiWordChar(ch);
00626     }
00627     return IsAsciiPunct(pattern_char) && pattern_char == ch;
00628   }
00629 
00630   return (pattern_char == '.' && ch != '\n') || pattern_char == ch;
00631 }
00632 
00633 // Helper function used by ValidateRegex() to format error messages.
00634 std::string FormatRegexSyntaxError(const char* regex, int index) {
00635   return (Message() << "Syntax error at index " << index
00636           << " in simple regular expression \"" << regex << "\": ").GetString();
00637 }
00638 
00639 // Generates non-fatal failures and returns false if regex is invalid;
00640 // otherwise returns true.
00641 bool ValidateRegex(const char* regex) {
00642   if (regex == NULL) {
00643     // TODO(wan@google.com): fix the source file location in the
00644     // assertion failures to match where the regex is used in user
00645     // code.
00646     ADD_FAILURE() << "NULL is not a valid simple regular expression.";
00647     return false;
00648   }
00649 
00650   bool is_valid = true;
00651 
00652   // True iff ?, *, or + can follow the previous atom.
00653   bool prev_repeatable = false;
00654   for (int i = 0; regex[i]; i++) {
00655     if (regex[i] == '\\') {  // An escape sequence
00656       i++;
00657       if (regex[i] == '\0') {
00658         ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
00659                       << "'\\' cannot appear at the end.";
00660         return false;
00661       }
00662 
00663       if (!IsValidEscape(regex[i])) {
00664         ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
00665                       << "invalid escape sequence \"\\" << regex[i] << "\".";
00666         is_valid = false;
00667       }
00668       prev_repeatable = true;
00669     } else {  // Not an escape sequence.
00670       const char ch = regex[i];
00671 
00672       if (ch == '^' && i > 0) {
00673         ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
00674                       << "'^' can only appear at the beginning.";
00675         is_valid = false;
00676       } else if (ch == '$' && regex[i + 1] != '\0') {
00677         ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
00678                       << "'$' can only appear at the end.";
00679         is_valid = false;
00680       } else if (IsInSet(ch, "()[]{}|")) {
00681         ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
00682                       << "'" << ch << "' is unsupported.";
00683         is_valid = false;
00684       } else if (IsRepeat(ch) && !prev_repeatable) {
00685         ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
00686                       << "'" << ch << "' can only follow a repeatable token.";
00687         is_valid = false;
00688       }
00689 
00690       prev_repeatable = !IsInSet(ch, "^$?*+");
00691     }
00692   }
00693 
00694   return is_valid;
00695 }
00696 
00697 // Matches a repeated regex atom followed by a valid simple regular
00698 // expression.  The regex atom is defined as c if escaped is false,
00699 // or \c otherwise.  repeat is the repetition meta character (?, *,
00700 // or +).  The behavior is undefined if str contains too many
00701 // characters to be indexable by size_t, in which case the test will
00702 // probably time out anyway.  We are fine with this limitation as
00703 // std::string has it too.
00704 bool MatchRepetitionAndRegexAtHead(
00705     bool escaped, char c, char repeat, const char* regex,
00706     const char* str) {
00707   const size_t min_count = (repeat == '+') ? 1 : 0;
00708   const size_t max_count = (repeat == '?') ? 1 :
00709       static_cast<size_t>(-1) - 1;
00710   // We cannot call numeric_limits::max() as it conflicts with the
00711   // max() macro on Windows.
00712 
00713   for (size_t i = 0; i <= max_count; ++i) {
00714     // We know that the atom matches each of the first i characters in str.
00715     if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
00716       // We have enough matches at the head, and the tail matches too.
00717       // Since we only care about *whether* the pattern matches str
00718       // (as opposed to *how* it matches), there is no need to find a
00719       // greedy match.
00720       return true;
00721     }
00722     if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i]))
00723       return false;
00724   }
00725   return false;
00726 }
00727 
00728 // Returns true iff regex matches a prefix of str.  regex must be a
00729 // valid simple regular expression and not start with "^", or the
00730 // result is undefined.
00731 bool MatchRegexAtHead(const char* regex, const char* str) {
00732   if (*regex == '\0')  // An empty regex matches a prefix of anything.
00733     return true;
00734 
00735   // "$" only matches the end of a string.  Note that regex being
00736   // valid guarantees that there's nothing after "$" in it.
00737   if (*regex == '$')
00738     return *str == '\0';
00739 
00740   // Is the first thing in regex an escape sequence?
00741   const bool escaped = *regex == '\\';
00742   if (escaped)
00743     ++regex;
00744   if (IsRepeat(regex[1])) {
00745     // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so
00746     // here's an indirect recursion.  It terminates as the regex gets
00747     // shorter in each recursion.
00748     return MatchRepetitionAndRegexAtHead(
00749         escaped, regex[0], regex[1], regex + 2, str);
00750   } else {
00751     // regex isn't empty, isn't "$", and doesn't start with a
00752     // repetition.  We match the first atom of regex with the first
00753     // character of str and recurse.
00754     return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
00755         MatchRegexAtHead(regex + 1, str + 1);
00756   }
00757 }
00758 
00759 // Returns true iff regex matches any substring of str.  regex must be
00760 // a valid simple regular expression, or the result is undefined.
00761 //
00762 // The algorithm is recursive, but the recursion depth doesn't exceed
00763 // the regex length, so we won't need to worry about running out of
00764 // stack space normally.  In rare cases the time complexity can be
00765 // exponential with respect to the regex length + the string length,
00766 // but usually it's must faster (often close to linear).
00767 bool MatchRegexAnywhere(const char* regex, const char* str) {
00768   if (regex == NULL || str == NULL)
00769     return false;
00770 
00771   if (*regex == '^')
00772     return MatchRegexAtHead(regex + 1, str);
00773 
00774   // A successful match can be anywhere in str.
00775   do {
00776     if (MatchRegexAtHead(regex, str))
00777       return true;
00778   } while (*str++ != '\0');
00779   return false;
00780 }
00781 
00782 // Implements the RE class.
00783 
00784 RE::~RE() {
00785   free(const_cast<char*>(pattern_));
00786   free(const_cast<char*>(full_pattern_));
00787 }
00788 
00789 // Returns true iff regular expression re matches the entire str.
00790 bool RE::FullMatch(const char* str, const RE& re) {
00791   return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);
00792 }
00793 
00794 // Returns true iff regular expression re matches a substring of str
00795 // (including str itself).
00796 bool RE::PartialMatch(const char* str, const RE& re) {
00797   return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);
00798 }
00799 
00800 // Initializes an RE from its string representation.
00801 void RE::Init(const char* regex) {
00802   pattern_ = full_pattern_ = NULL;
00803   if (regex != NULL) {
00804     pattern_ = posix::StrDup(regex);
00805   }
00806 
00807   is_valid_ = ValidateRegex(regex);
00808   if (!is_valid_) {
00809     // No need to calculate the full pattern when the regex is invalid.
00810     return;
00811   }
00812 
00813   const size_t len = strlen(regex);
00814   // Reserves enough bytes to hold the regular expression used for a
00815   // full match: we need space to prepend a '^', append a '$', and
00816   // terminate the string with '\0'.
00817   char* buffer = static_cast<char*>(malloc(len + 3));
00818   full_pattern_ = buffer;
00819 
00820   if (*regex != '^')
00821     *buffer++ = '^';  // Makes sure full_pattern_ starts with '^'.
00822 
00823   // We don't use snprintf or strncpy, as they trigger a warning when
00824   // compiled with VC++ 8.0.
00825   memcpy(buffer, regex, len);
00826   buffer += len;
00827 
00828   if (len == 0 || regex[len - 1] != '$')
00829     *buffer++ = '$';  // Makes sure full_pattern_ ends with '$'.
00830 
00831   *buffer = '\0';
00832 }
00833 
00834 #endif  // GTEST_USES_POSIX_RE
00835 
00836 const char kUnknownFile[] = "unknown file";
00837 
00838 // Formats a source file path and a line number as they would appear
00839 // in an error message from the compiler used to compile this code.
00840 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
00841   const std::string file_name(file == NULL ? kUnknownFile : file);
00842 
00843   if (line < 0) {
00844     return file_name + ":";
00845   }
00846 #ifdef _MSC_VER
00847   return file_name + "(" + StreamableToString(line) + "):";
00848 #else
00849   return file_name + ":" + StreamableToString(line) + ":";
00850 #endif  // _MSC_VER
00851 }
00852 
00853 // Formats a file location for compiler-independent XML output.
00854 // Although this function is not platform dependent, we put it next to
00855 // FormatFileLocation in order to contrast the two functions.
00856 // Note that FormatCompilerIndependentFileLocation() does NOT append colon
00857 // to the file location it produces, unlike FormatFileLocation().
00858 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(
00859     const char* file, int line) {
00860   const std::string file_name(file == NULL ? kUnknownFile : file);
00861 
00862   if (line < 0)
00863     return file_name;
00864   else
00865     return file_name + ":" + StreamableToString(line);
00866 }
00867 
00868 
00869 GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line)
00870     : severity_(severity) {
00871   const char* const marker =
00872       severity == GTEST_INFO ?    "[  INFO ]" :
00873       severity == GTEST_WARNING ? "[WARNING]" :
00874       severity == GTEST_ERROR ?   "[ ERROR ]" : "[ FATAL ]";
00875   GetStream() << ::std::endl << marker << " "
00876               << FormatFileLocation(file, line).c_str() << ": ";
00877 }
00878 
00879 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
00880 GTestLog::~GTestLog() {
00881   GetStream() << ::std::endl;
00882   if (severity_ == GTEST_FATAL) {
00883     fflush(stderr);
00884     posix::Abort();
00885   }
00886 }
00887 // Disable Microsoft deprecation warnings for POSIX functions called from
00888 // this class (creat, dup, dup2, and close)
00889 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
00890 
00891 #if GTEST_HAS_STREAM_REDIRECTION
00892 
00893 // Object that captures an output stream (stdout/stderr).
00894 class CapturedStream {
00895  public:
00896   // The ctor redirects the stream to a temporary file.
00897   explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
00898 # if GTEST_OS_WINDOWS
00899     char temp_dir_path[MAX_PATH + 1] = { '\0' };  // NOLINT
00900     char temp_file_path[MAX_PATH + 1] = { '\0' };  // NOLINT
00901 
00902     ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path);
00903     const UINT success = ::GetTempFileNameA(temp_dir_path,
00904                                             "gtest_redir",
00905                                             0,  // Generate unique file name.
00906                                             temp_file_path);
00907     GTEST_CHECK_(success != 0)
00908         << "Unable to create a temporary file in " << temp_dir_path;
00909     const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
00910     GTEST_CHECK_(captured_fd != -1) << "Unable to open temporary file "
00911                                     << temp_file_path;
00912     filename_ = temp_file_path;
00913 # else
00914     // There's no guarantee that a test has write access to the current
00915     // directory, so we create the temporary file in the /tmp directory
00916     // instead. We use /tmp on most systems, and /sdcard on Android.
00917     // That's because Android doesn't have /tmp.
00918 #  if GTEST_OS_LINUX_ANDROID
00919     // Note: Android applications are expected to call the framework's
00920     // Context.getExternalStorageDirectory() method through JNI to get
00921     // the location of the world-writable SD Card directory. However,
00922     // this requires a Context handle, which cannot be retrieved
00923     // globally from native code. Doing so also precludes running the
00924     // code as part of a regular standalone executable, which doesn't
00925     // run in a Dalvik process (e.g. when running it through 'adb shell').
00926     //
00927     // The location /sdcard is directly accessible from native code
00928     // and is the only location (unofficially) supported by the Android
00929     // team. It's generally a symlink to the real SD Card mount point
00930     // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or
00931     // other OEM-customized locations. Never rely on these, and always
00932     // use /sdcard.
00933     char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
00934 #  else
00935     char name_template[] = "/tmp/captured_stream.XXXXXX";
00936 #  endif  // GTEST_OS_LINUX_ANDROID
00937     const int captured_fd = mkstemp(name_template);
00938     filename_ = name_template;
00939 # endif  // GTEST_OS_WINDOWS
00940     fflush(NULL);
00941     dup2(captured_fd, fd_);
00942     close(captured_fd);
00943   }
00944 
00945   ~CapturedStream() {
00946     remove(filename_.c_str());
00947   }
00948 
00949   std::string GetCapturedString() {
00950     if (uncaptured_fd_ != -1) {
00951       // Restores the original stream.
00952       fflush(NULL);
00953       dup2(uncaptured_fd_, fd_);
00954       close(uncaptured_fd_);
00955       uncaptured_fd_ = -1;
00956     }
00957 
00958     FILE* const file = posix::FOpen(filename_.c_str(), "r");
00959     const std::string content = ReadEntireFile(file);
00960     posix::FClose(file);
00961     return content;
00962   }
00963 
00964  private:
00965   // Reads the entire content of a file as an std::string.
00966   static std::string ReadEntireFile(FILE* file);
00967 
00968   // Returns the size (in bytes) of a file.
00969   static size_t GetFileSize(FILE* file);
00970 
00971   const int fd_;  // A stream to capture.
00972   int uncaptured_fd_;
00973   // Name of the temporary file holding the stderr output.
00974   ::std::string filename_;
00975 
00976   GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream);
00977 };
00978 
00979 // Returns the size (in bytes) of a file.
00980 size_t CapturedStream::GetFileSize(FILE* file) {
00981   fseek(file, 0, SEEK_END);
00982   return static_cast<size_t>(ftell(file));
00983 }
00984 
00985 // Reads the entire content of a file as a string.
00986 std::string CapturedStream::ReadEntireFile(FILE* file) {
00987   const size_t file_size = GetFileSize(file);
00988   char* const buffer = new char[file_size];
00989 
00990   size_t bytes_last_read = 0;  // # of bytes read in the last fread()
00991   size_t bytes_read = 0;       // # of bytes read so far
00992 
00993   fseek(file, 0, SEEK_SET);
00994 
00995   // Keeps reading the file until we cannot read further or the
00996   // pre-determined file size is reached.
00997   do {
00998     bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file);
00999     bytes_read += bytes_last_read;
01000   } while (bytes_last_read > 0 && bytes_read < file_size);
01001 
01002   const std::string content(buffer, bytes_read);
01003   delete[] buffer;
01004 
01005   return content;
01006 }
01007 
01008 GTEST_DISABLE_MSC_WARNINGS_POP_()
01009 
01010 static CapturedStream* g_captured_stderr = NULL;
01011 static CapturedStream* g_captured_stdout = NULL;
01012 
01013 // Starts capturing an output stream (stdout/stderr).
01014 void CaptureStream(int fd, const char* stream_name, CapturedStream** stream) {
01015   if (*stream != NULL) {
01016     GTEST_LOG_(FATAL) << "Only one " << stream_name
01017                       << " capturer can exist at a time.";
01018   }
01019   *stream = new CapturedStream(fd);
01020 }
01021 
01022 // Stops capturing the output stream and returns the captured string.
01023 std::string GetCapturedStream(CapturedStream** captured_stream) {
01024   const std::string content = (*captured_stream)->GetCapturedString();
01025 
01026   delete *captured_stream;
01027   *captured_stream = NULL;
01028 
01029   return content;
01030 }
01031 
01032 // Starts capturing stdout.
01033 void CaptureStdout() {
01034   CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout);
01035 }
01036 
01037 // Starts capturing stderr.
01038 void CaptureStderr() {
01039   CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr);
01040 }
01041 
01042 // Stops capturing stdout and returns the captured string.
01043 std::string GetCapturedStdout() {
01044   return GetCapturedStream(&g_captured_stdout);
01045 }
01046 
01047 // Stops capturing stderr and returns the captured string.
01048 std::string GetCapturedStderr() {
01049   return GetCapturedStream(&g_captured_stderr);
01050 }
01051 
01052 #endif  // GTEST_HAS_STREAM_REDIRECTION
01053 
01054 #if GTEST_HAS_DEATH_TEST
01055 
01056 // A copy of all command line arguments.  Set by InitGoogleTest().
01057 ::std::vector<testing::internal::string> g_argvs;
01058 
01059 static const ::std::vector<testing::internal::string>* g_injected_test_argvs =
01060                                         NULL;  // Owned.
01061 
01062 void SetInjectableArgvs(const ::std::vector<testing::internal::string>* argvs) {
01063   if (g_injected_test_argvs != argvs)
01064     delete g_injected_test_argvs;
01065   g_injected_test_argvs = argvs;
01066 }
01067 
01068 const ::std::vector<testing::internal::string>& GetInjectableArgvs() {
01069   if (g_injected_test_argvs != NULL) {
01070     return *g_injected_test_argvs;
01071   }
01072   return g_argvs;
01073 }
01074 #endif  // GTEST_HAS_DEATH_TEST
01075 
01076 #if GTEST_OS_WINDOWS_MOBILE
01077 namespace posix {
01078 void Abort() {
01079   DebugBreak();
01080   TerminateProcess(GetCurrentProcess(), 1);
01081 }
01082 }  // namespace posix
01083 #endif  // GTEST_OS_WINDOWS_MOBILE
01084 
01085 // Returns the name of the environment variable corresponding to the
01086 // given flag.  For example, FlagToEnvVar("foo") will return
01087 // "GTEST_FOO" in the open-source version.
01088 static std::string FlagToEnvVar(const char* flag) {
01089   const std::string full_flag =
01090       (Message() << GTEST_FLAG_PREFIX_ << flag).GetString();
01091 
01092   Message env_var;
01093   for (size_t i = 0; i != full_flag.length(); i++) {
01094     env_var << ToUpper(full_flag.c_str()[i]);
01095   }
01096 
01097   return env_var.GetString();
01098 }
01099 
01100 // Parses 'str' for a 32-bit signed integer.  If successful, writes
01101 // the result to *value and returns true; otherwise leaves *value
01102 // unchanged and returns false.
01103 bool ParseInt32(const Message& src_text, const char* str, Int32* value) {
01104   // Parses the environment variable as a decimal integer.
01105   char* end = NULL;
01106   const long long_value = strtol(str, &end, 10);  // NOLINT
01107 
01108   // Has strtol() consumed all characters in the string?
01109   if (*end != '\0') {
01110     // No - an invalid character was encountered.
01111     Message msg;
01112     msg << "WARNING: " << src_text
01113         << " is expected to be a 32-bit integer, but actually"
01114         << " has value \"" << str << "\".\n";
01115     printf("%s", msg.GetString().c_str());
01116     fflush(stdout);
01117     return false;
01118   }
01119 
01120   // Is the parsed value in the range of an Int32?
01121   const Int32 result = static_cast<Int32>(long_value);
01122   if (long_value == LONG_MAX || long_value == LONG_MIN ||
01123       // The parsed value overflows as a long.  (strtol() returns
01124       // LONG_MAX or LONG_MIN when the input overflows.)
01125       result != long_value
01126       // The parsed value overflows as an Int32.
01127       ) {
01128     Message msg;
01129     msg << "WARNING: " << src_text
01130         << " is expected to be a 32-bit integer, but actually"
01131         << " has value " << str << ", which overflows.\n";
01132     printf("%s", msg.GetString().c_str());
01133     fflush(stdout);
01134     return false;
01135   }
01136 
01137   *value = result;
01138   return true;
01139 }
01140 
01141 // Reads and returns the Boolean environment variable corresponding to
01142 // the given flag; if it's not set, returns default_value.
01143 //
01144 // The value is considered true iff it's not "0".
01145 bool BoolFromGTestEnv(const char* flag, bool default_value) {
01146   const std::string env_var = FlagToEnvVar(flag);
01147   const char* const string_value = posix::GetEnv(env_var.c_str());
01148   return string_value == NULL ?
01149       default_value : strcmp(string_value, "0") != 0;
01150 }
01151 
01152 // Reads and returns a 32-bit integer stored in the environment
01153 // variable corresponding to the given flag; if it isn't set or
01154 // doesn't represent a valid 32-bit integer, returns default_value.
01155 Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
01156   const std::string env_var = FlagToEnvVar(flag);
01157   const char* const string_value = posix::GetEnv(env_var.c_str());
01158   if (string_value == NULL) {
01159     // The environment variable is not set.
01160     return default_value;
01161   }
01162 
01163   Int32 result = default_value;
01164   if (!ParseInt32(Message() << "Environment variable " << env_var,
01165                   string_value, &result)) {
01166     printf("The default value %s is used.\n",
01167            (Message() << default_value).GetString().c_str());
01168     fflush(stdout);
01169     return default_value;
01170   }
01171 
01172   return result;
01173 }
01174 
01175 // Reads and returns the string environment variable corresponding to
01176 // the given flag; if it's not set, returns default_value.
01177 const char* StringFromGTestEnv(const char* flag, const char* default_value) {
01178   const std::string env_var = FlagToEnvVar(flag);
01179   const char* const value = posix::GetEnv(env_var.c_str());
01180   return value == NULL ? default_value : value;
01181 }
01182 
01183 }  // namespace internal
01184 }  // namespace testing


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03