gtest-port.h revision 12a92c26fc0e0de81f687dbe739a6aa24f37f9dd
1// Copyright 2005, 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: wan@google.com (Zhanyong Wan) 31// 32// Low-level types and utilities for porting Google Test to various 33// platforms. They are subject to change without notice. DO NOT USE 34// THEM IN USER CODE. 35 36#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 37#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 38 39// The user can define the following macros in the build script to 40// control Google Test's behavior. If the user doesn't define a macro 41// in this list, Google Test will define it. 42// 43// GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) 44// is/isn't available. 45// GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions 46// are enabled. 47// GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string 48// is/isn't available (some systems define 49// ::string, which is different to std::string). 50// GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string 51// is/isn't available (some systems define 52// ::wstring, which is different to std::wstring). 53// GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h> 54// is/isn't available. 55// GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't 56// enabled. 57// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 58// std::wstring does/doesn't work (Google Test can 59// be used where std::wstring is unavailable). 60// GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple 61// is/isn't available. 62// GTEST_HAS_SEH - Define it to 1/0 to indicate whether the 63// compiler supports Microsoft's "Structured 64// Exception Handling". 65// GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google 66// Test's own tr1 tuple implementation should be 67// used. Unused when the user sets 68// GTEST_HAS_TR1_TUPLE to 0. 69 70// This header defines the following utilities: 71// 72// Macros indicating the current platform (defined to 1 if compiled on 73// the given platform; otherwise undefined): 74// GTEST_OS_CYGWIN - Cygwin 75// GTEST_OS_LINUX - Linux 76// GTEST_OS_MAC - Mac OS X 77// GTEST_OS_SOLARIS - Sun Solaris 78// GTEST_OS_SYMBIAN - Symbian 79// GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) 80// GTEST_OS_WINDOWS_DESKTOP - Windows Desktop 81// GTEST_OS_WINDOWS_MINGW - MinGW 82// GTEST_OS_WINDOWS_MOBILE - Windows Mobile 83// GTEST_OS_ZOS - z/OS 84// 85// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the 86// most stable support. Since core members of the Google Test project 87// don't have access to other platforms, support for them may be less 88// stable. If you notice any problems on your platform, please notify 89// googletestframework@googlegroups.com (patches for fixing them are 90// even more welcome!). 91// 92// Note that it is possible that none of the GTEST_OS_* macros are defined. 93// 94// Macros indicating available Google Test features (defined to 1 if 95// the corresponding feature is supported; otherwise undefined): 96// GTEST_HAS_COMBINE - the Combine() function (for value-parameterized 97// tests) 98// GTEST_HAS_DEATH_TEST - death tests 99// GTEST_HAS_PARAM_TEST - value-parameterized tests 100// GTEST_HAS_TYPED_TEST - typed tests 101// GTEST_HAS_TYPED_TEST_P - type-parameterized tests 102// GTEST_USES_POSIX_RE - enhanced POSIX regex is used. 103// GTEST_USES_SIMPLE_RE - our own simple regex is used; 104// the above two are mutually exclusive. 105// 106// Macros for basic C++ coding: 107// GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. 108// GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a 109// variable don't have to be used. 110// GTEST_DISALLOW_ASSIGN_ - disables operator=. 111// GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=. 112// GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. 113// 114// Synchronization: 115// Mutex, MutexLock, ThreadLocal, GetThreadCount() 116// - synchronization primitives. 117// GTEST_IS_THREADSAFE - defined to 1 to indicate that the above 118// synchronization primitives have real implementations 119// and Google Test is thread-safe; or 0 otherwise. 120// 121// Template meta programming: 122// is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only. 123// 124// Smart pointers: 125// scoped_ptr - as in TR2. 126// 127// Regular expressions: 128// RE - a simple regular expression class using the POSIX 129// Extended Regular Expression syntax. Not available on 130// Windows. 131// 132// Logging: 133// GTEST_LOG_() - logs messages at the specified severity level. 134// LogToStderr() - directs all log messages to stderr. 135// FlushInfoLog() - flushes informational log messages. 136// 137// Stdout and stderr capturing: 138// CaptureStdout() - starts capturing stdout. 139// GetCapturedStdout() - stops capturing stdout and returns the captured 140// string. 141// CaptureStderr() - starts capturing stderr. 142// GetCapturedStderr() - stops capturing stderr and returns the captured 143// string. 144// 145// Integer types: 146// TypeWithSize - maps an integer to a int type. 147// Int32, UInt32, Int64, UInt64, TimeInMillis 148// - integers of known sizes. 149// BiggestInt - the biggest signed integer type. 150// 151// Command-line utilities: 152// GTEST_FLAG() - references a flag. 153// GTEST_DECLARE_*() - declares a flag. 154// GTEST_DEFINE_*() - defines a flag. 155// GetArgvs() - returns the command line as a vector of strings. 156// 157// Environment variable utilities: 158// GetEnv() - gets the value of an environment variable. 159// BoolFromGTestEnv() - parses a bool environment variable. 160// Int32FromGTestEnv() - parses an Int32 environment variable. 161// StringFromGTestEnv() - parses a string environment variable. 162 163#include <stddef.h> // For ptrdiff_t 164#include <stdlib.h> 165#include <stdio.h> 166#include <string.h> 167#ifndef _WIN32_WCE 168#include <sys/stat.h> 169#endif // !_WIN32_WCE 170 171#include <iostream> // NOLINT 172#include <sstream> // NOLINT 173#include <string> // NOLINT 174 175#define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" 176#define GTEST_FLAG_PREFIX_ "gtest_" 177#define GTEST_FLAG_PREFIX_DASH_ "gtest-" 178#define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" 179#define GTEST_NAME_ "Google Test" 180#define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/" 181 182// Determines the version of gcc that is used to compile this. 183#ifdef __GNUC__ 184// 40302 means version 4.3.2. 185#define GTEST_GCC_VER_ \ 186 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__) 187#endif // __GNUC__ 188 189// Determines the platform on which Google Test is compiled. 190#ifdef __CYGWIN__ 191#define GTEST_OS_CYGWIN 1 192#elif defined __SYMBIAN32__ 193#define GTEST_OS_SYMBIAN 1 194#elif defined _WIN32 195#define GTEST_OS_WINDOWS 1 196#ifdef _WIN32_WCE 197#define GTEST_OS_WINDOWS_MOBILE 1 198#elif defined(__MINGW__) || defined(__MINGW32__) 199#define GTEST_OS_WINDOWS_MINGW 1 200#else 201#define GTEST_OS_WINDOWS_DESKTOP 1 202#endif // _WIN32_WCE 203#elif defined __APPLE__ 204#define GTEST_OS_MAC 1 205#elif defined __linux__ 206#define GTEST_OS_LINUX 1 207#elif defined __MVS__ 208#define GTEST_OS_ZOS 1 209#elif defined(__sun) && defined(__SVR4) 210#define GTEST_OS_SOLARIS 1 211#endif // __CYGWIN__ 212 213#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_SYMBIAN || \ 214 GTEST_OS_SOLARIS 215 216// On some platforms, <regex.h> needs someone to define size_t, and 217// won't compile otherwise. We can #include it here as we already 218// included <stdlib.h>, which is guaranteed to define size_t through 219// <stddef.h>. 220#include <regex.h> // NOLINT 221#include <strings.h> // NOLINT 222#include <sys/types.h> // NOLINT 223#include <time.h> // NOLINT 224#include <unistd.h> // NOLINT 225 226#define GTEST_USES_POSIX_RE 1 227 228#elif GTEST_OS_WINDOWS 229 230#if !GTEST_OS_WINDOWS_MOBILE 231#include <direct.h> // NOLINT 232#include <io.h> // NOLINT 233#endif 234 235// <regex.h> is not available on Windows. Use our own simple regex 236// implementation instead. 237#define GTEST_USES_SIMPLE_RE 1 238 239#else 240 241// <regex.h> may not be available on this platform. Use our own 242// simple regex implementation instead. 243#define GTEST_USES_SIMPLE_RE 1 244 245#endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || 246 // GTEST_OS_SYMBIAN || GTEST_OS_SOLARIS 247 248#ifndef GTEST_HAS_EXCEPTIONS 249// The user didn't tell us whether exceptions are enabled, so we need 250// to figure it out. 251#if defined(_MSC_VER) || defined(__BORLANDC__) 252// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS 253// macro to enable exceptions, so we'll do the same. 254// Assumes that exceptions are enabled by default. 255#ifndef _HAS_EXCEPTIONS 256#define _HAS_EXCEPTIONS 1 257#endif // _HAS_EXCEPTIONS 258#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 259#elif defined(__GNUC__) && __EXCEPTIONS 260// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. 261#define GTEST_HAS_EXCEPTIONS 1 262#elif defined(__SUNPRO_CC) 263// Sun Pro CC supports exceptions. However, there is no compile-time way of 264// detecting whether they are enabled or not. Therefore, we assume that 265// they are enabled unless the user tells us otherwise. 266#define GTEST_HAS_EXCEPTIONS 1 267#else 268// For other compilers, we assume exceptions are disabled to be 269// conservative. 270#define GTEST_HAS_EXCEPTIONS 0 271#endif // defined(_MSC_VER) || defined(__BORLANDC__) 272#endif // GTEST_HAS_EXCEPTIONS 273 274#if !defined(GTEST_HAS_STD_STRING) 275// Even though we don't use this macro any longer, we keep it in case 276// some clients still depend on it. 277#define GTEST_HAS_STD_STRING 1 278#elif !GTEST_HAS_STD_STRING 279// The user told us that ::std::string isn't available. 280#error "Google Test cannot be used where ::std::string isn't available." 281#endif // !defined(GTEST_HAS_STD_STRING) 282 283#ifndef GTEST_HAS_GLOBAL_STRING 284// The user didn't tell us whether ::string is available, so we need 285// to figure it out. 286 287#define GTEST_HAS_GLOBAL_STRING 0 288 289#endif // GTEST_HAS_GLOBAL_STRING 290 291#ifndef GTEST_HAS_STD_WSTRING 292// The user didn't tell us whether ::std::wstring is available, so we need 293// to figure it out. 294// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring 295// is available. 296 297// Cygwin 1.5 and below doesn't support ::std::wstring. 298// Cygwin 1.7 might add wstring support; this should be updated when clear. 299// Solaris' libc++ doesn't support it either. 300#define GTEST_HAS_STD_WSTRING (!(GTEST_OS_CYGWIN || GTEST_OS_SOLARIS)) 301 302#endif // GTEST_HAS_STD_WSTRING 303 304#ifndef GTEST_HAS_GLOBAL_WSTRING 305// The user didn't tell us whether ::wstring is available, so we need 306// to figure it out. 307#define GTEST_HAS_GLOBAL_WSTRING \ 308 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING) 309#endif // GTEST_HAS_GLOBAL_WSTRING 310 311// Determines whether RTTI is available. 312#ifndef GTEST_HAS_RTTI 313// The user didn't tell us whether RTTI is enabled, so we need to 314// figure it out. 315 316#ifdef _MSC_VER 317 318#ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled. 319#define GTEST_HAS_RTTI 1 320#else 321#define GTEST_HAS_RTTI 0 322#endif // _CPPRTTI 323 324#elif defined(__GNUC__) 325 326// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled. 327#if GTEST_GCC_VER_ >= 40302 328#ifdef __GXX_RTTI 329#define GTEST_HAS_RTTI 1 330#else 331#define GTEST_HAS_RTTI 0 332#endif // __GXX_RTTI 333#else 334// For gcc versions smaller than 4.3.2, we assume RTTI is enabled. 335#define GTEST_HAS_RTTI 1 336#endif // GTEST_GCC_VER >= 40302 337 338#else 339 340// Unknown compiler - assume RTTI is enabled. 341#define GTEST_HAS_RTTI 1 342 343#endif // _MSC_VER 344 345#endif // GTEST_HAS_RTTI 346 347// Determines whether Google Test can use the pthreads library. 348#ifndef GTEST_HAS_PTHREAD 349// The user didn't tell us explicitly, so we assume pthreads support is 350// available on Linux and Mac. 351// 352// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 353// to your compiler flags. 354#define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC) 355#endif // GTEST_HAS_PTHREAD 356 357// Determines whether Google Test can use tr1/tuple. You can define 358// this macro to 0 to prevent Google Test from using tuple (any 359// feature depending on tuple with be disabled in this mode). 360#ifndef GTEST_HAS_TR1_TUPLE 361// The user didn't tell us not to do it, so we assume it's OK. 362#define GTEST_HAS_TR1_TUPLE 1 363#endif // GTEST_HAS_TR1_TUPLE 364 365// Determines whether Google Test's own tr1 tuple implementation 366// should be used. 367#ifndef GTEST_USE_OWN_TR1_TUPLE 368// The user didn't tell us, so we need to figure it out. 369 370// We use our own TR1 tuple if we aren't sure the user has an 371// implementation of it already. At this time, GCC 4.0.0+ and MSVC 372// 2010 are the only mainstream compilers that come with a TR1 tuple 373// implementation. NVIDIA's CUDA NVCC compiler pretends to be GCC by 374// defining __GNUC__ and friends, but cannot compile GCC's tuple 375// implementation. MSVC 2008 (9.0) provides TR1 tuple in a 323 MB 376// Feature Pack download, which we cannot assume the user has. 377#if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000)) \ 378 || _MSC_VER >= 1600 379#define GTEST_USE_OWN_TR1_TUPLE 0 380#else 381#define GTEST_USE_OWN_TR1_TUPLE 1 382#endif 383 384#endif // GTEST_USE_OWN_TR1_TUPLE 385 386// To avoid conditional compilation everywhere, we make it 387// gtest-port.h's responsibility to #include the header implementing 388// tr1/tuple. 389#if GTEST_HAS_TR1_TUPLE 390 391#if GTEST_USE_OWN_TR1_TUPLE 392#include <gtest/internal/gtest-tuple.h> 393#elif GTEST_OS_SYMBIAN 394 395// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to 396// use STLport's tuple implementation, which unfortunately doesn't 397// work as the copy of STLport distributed with Symbian is incomplete. 398// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to 399// use its own tuple implementation. 400#ifdef BOOST_HAS_TR1_TUPLE 401#undef BOOST_HAS_TR1_TUPLE 402#endif // BOOST_HAS_TR1_TUPLE 403 404// This prevents <boost/tr1/detail/config.hpp>, which defines 405// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>. 406#define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED 407#include <tuple> 408 409#elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000) 410// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does 411// not conform to the TR1 spec, which requires the header to be <tuple>. 412 413#if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302 414// Until version 4.3.2, gcc has a bug that causes <tr1/functional>, 415// which is #included by <tr1/tuple>, to not compile when RTTI is 416// disabled. _TR1_FUNCTIONAL is the header guard for 417// <tr1/functional>. Hence the following #define is a hack to prevent 418// <tr1/functional> from being included. 419#define _TR1_FUNCTIONAL 1 420#include <tr1/tuple> 421#undef _TR1_FUNCTIONAL // Allows the user to #include 422 // <tr1/functional> if he chooses to. 423#else 424#include <tr1/tuple> // NOLINT 425#endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302 426 427#else 428// If the compiler is not GCC 4.0+, we assume the user is using a 429// spec-conforming TR1 implementation. 430#include <tuple> // NOLINT 431#endif // GTEST_USE_OWN_TR1_TUPLE 432 433#endif // GTEST_HAS_TR1_TUPLE 434 435// Determines whether clone(2) is supported. 436// Usually it will only be available on Linux, excluding 437// Linux on the Itanium architecture. 438// Also see http://linux.die.net/man/2/clone. 439#ifndef GTEST_HAS_CLONE 440// The user didn't tell us, so we need to figure it out. 441 442#if GTEST_OS_LINUX && !defined(__ia64__) 443#define GTEST_HAS_CLONE 1 444#else 445#define GTEST_HAS_CLONE 0 446#endif // GTEST_OS_LINUX && !defined(__ia64__) 447 448#endif // GTEST_HAS_CLONE 449 450// Determines whether to support stream redirection. This is used to test 451// output correctness and to implement death tests. 452#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN 453#define GTEST_HAS_STREAM_REDIRECTION_ 1 454#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN 455 456// Determines whether to support death tests. 457// Google Test does not support death tests for VC 7.1 and earlier as 458// abort() in a VC 7.1 application compiled as GUI in debug config 459// pops up a dialog window that cannot be suppressed programmatically. 460#if (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \ 461 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || GTEST_OS_WINDOWS_MINGW) 462#define GTEST_HAS_DEATH_TEST 1 463#include <vector> // NOLINT 464#endif 465 466// We don't support MSVC 7.1 with exceptions disabled now. Therefore 467// all the compilers we care about are adequate for supporting 468// value-parameterized tests. 469#define GTEST_HAS_PARAM_TEST 1 470 471// Determines whether to support type-driven tests. 472 473// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0, and 474// Sun Pro CC support. 475#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) 476#define GTEST_HAS_TYPED_TEST 1 477#define GTEST_HAS_TYPED_TEST_P 1 478#endif 479 480// Determines whether to support Combine(). This only makes sense when 481// value-parameterized tests are enabled. 482#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 483#define GTEST_HAS_COMBINE 1 484#endif // GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 485 486// Determines whether the system compiler uses UTF-16 for encoding wide strings. 487#define GTEST_WIDE_STRING_USES_UTF16_ \ 488 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN) 489 490// Defines some utility macros. 491 492// The GNU compiler emits a warning if nested "if" statements are followed by 493// an "else" statement and braces are not used to explicitly disambiguate the 494// "else" binding. This leads to problems with code like: 495// 496// if (gate) 497// ASSERT_*(condition) << "Some message"; 498// 499// The "switch (0) case 0:" idiom is used to suppress this. 500#ifdef __INTEL_COMPILER 501#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 502#else 503#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT 504#endif 505 506// Use this annotation at the end of a struct/class definition to 507// prevent the compiler from optimizing away instances that are never 508// used. This is useful when all interesting logic happens inside the 509// c'tor and / or d'tor. Example: 510// 511// struct Foo { 512// Foo() { ... } 513// } GTEST_ATTRIBUTE_UNUSED_; 514// 515// Also use it after a variable or parameter declaration to tell the 516// compiler the variable/parameter does not have to be used. 517#if defined(__GNUC__) && !defined(COMPILER_ICC) 518#define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) 519#else 520#define GTEST_ATTRIBUTE_UNUSED_ 521#endif 522 523// A macro to disallow operator= 524// This should be used in the private: declarations for a class. 525#define GTEST_DISALLOW_ASSIGN_(type)\ 526 void operator=(type const &) 527 528// A macro to disallow copy constructor and operator= 529// This should be used in the private: declarations for a class. 530#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\ 531 type(type const &);\ 532 GTEST_DISALLOW_ASSIGN_(type) 533 534// Tell the compiler to warn about unused return values for functions declared 535// with this macro. The macro should be used on function declarations 536// following the argument list: 537// 538// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 539#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC) 540#define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result)) 541#else 542#define GTEST_MUST_USE_RESULT_ 543#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC 544 545// Determine whether the compiler supports Microsoft's Structured Exception 546// Handling. This is supported by several Windows compilers but generally 547// does not exist on any other system. 548#ifndef GTEST_HAS_SEH 549// The user didn't tell us, so we need to figure it out. 550 551#if defined(_MSC_VER) || defined(__BORLANDC__) 552// These two compilers are known to support SEH. 553#define GTEST_HAS_SEH 1 554#else 555// Assume no SEH. 556#define GTEST_HAS_SEH 0 557#endif 558 559#endif // GTEST_HAS_SEH 560 561namespace testing { 562 563class Message; 564 565namespace internal { 566 567class String; 568 569typedef ::std::stringstream StrStream; 570 571// A helper for suppressing warnings on constant condition. It just 572// returns 'condition'. 573bool IsTrue(bool condition); 574 575// Defines scoped_ptr. 576 577// This implementation of scoped_ptr is PARTIAL - it only contains 578// enough stuff to satisfy Google Test's need. 579template <typename T> 580class scoped_ptr { 581 public: 582 typedef T element_type; 583 584 explicit scoped_ptr(T* p = NULL) : ptr_(p) {} 585 ~scoped_ptr() { reset(); } 586 587 T& operator*() const { return *ptr_; } 588 T* operator->() const { return ptr_; } 589 T* get() const { return ptr_; } 590 591 T* release() { 592 T* const ptr = ptr_; 593 ptr_ = NULL; 594 return ptr; 595 } 596 597 void reset(T* p = NULL) { 598 if (p != ptr_) { 599 if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type. 600 delete ptr_; 601 } 602 ptr_ = p; 603 } 604 } 605 private: 606 T* ptr_; 607 608 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr); 609}; 610 611// Defines RE. 612 613// A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended 614// Regular Expression syntax. 615class RE { 616 public: 617 // Constructs an RE from a string. 618 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 619 620#if GTEST_HAS_GLOBAL_STRING 621 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT 622#endif // GTEST_HAS_GLOBAL_STRING 623 624 RE(const char* regex) { Init(regex); } // NOLINT 625 ~RE(); 626 627 // Returns the string representation of the regex. 628 const char* pattern() const { return pattern_; } 629 630 // FullMatch(str, re) returns true iff regular expression re matches 631 // the entire str. 632 // PartialMatch(str, re) returns true iff regular expression re 633 // matches a substring of str (including str itself). 634 // 635 // TODO(wan@google.com): make FullMatch() and PartialMatch() work 636 // when str contains NUL characters. 637 static bool FullMatch(const ::std::string& str, const RE& re) { 638 return FullMatch(str.c_str(), re); 639 } 640 static bool PartialMatch(const ::std::string& str, const RE& re) { 641 return PartialMatch(str.c_str(), re); 642 } 643 644#if GTEST_HAS_GLOBAL_STRING 645 static bool FullMatch(const ::string& str, const RE& re) { 646 return FullMatch(str.c_str(), re); 647 } 648 static bool PartialMatch(const ::string& str, const RE& re) { 649 return PartialMatch(str.c_str(), re); 650 } 651#endif // GTEST_HAS_GLOBAL_STRING 652 653 static bool FullMatch(const char* str, const RE& re); 654 static bool PartialMatch(const char* str, const RE& re); 655 656 private: 657 void Init(const char* regex); 658 659 // We use a const char* instead of a string, as Google Test may be used 660 // where string is not available. We also do not use Google Test's own 661 // String type here, in order to simplify dependencies between the 662 // files. 663 const char* pattern_; 664 bool is_valid_; 665#if GTEST_USES_POSIX_RE 666 regex_t full_regex_; // For FullMatch(). 667 regex_t partial_regex_; // For PartialMatch(). 668#else // GTEST_USES_SIMPLE_RE 669 const char* full_pattern_; // For FullMatch(); 670#endif 671 672 GTEST_DISALLOW_COPY_AND_ASSIGN_(RE); 673}; 674 675// Defines logging utilities: 676// GTEST_LOG_(severity) - logs messages at the specified severity level. The 677// message itself is streamed into the macro. 678// LogToStderr() - directs all log messages to stderr. 679// FlushInfoLog() - flushes informational log messages. 680 681enum GTestLogSeverity { 682 GTEST_INFO, 683 GTEST_WARNING, 684 GTEST_ERROR, 685 GTEST_FATAL 686}; 687 688// Formats log entry severity, provides a stream object for streaming the 689// log message, and terminates the message with a newline when going out of 690// scope. 691class GTestLog { 692 public: 693 GTestLog(GTestLogSeverity severity, const char* file, int line); 694 695 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. 696 ~GTestLog(); 697 698 ::std::ostream& GetStream() { return ::std::cerr; } 699 700 private: 701 const GTestLogSeverity severity_; 702 703 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog); 704}; 705 706#define GTEST_LOG_(severity) \ 707 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ 708 __FILE__, __LINE__).GetStream() 709 710inline void LogToStderr() {} 711inline void FlushInfoLog() { fflush(NULL); } 712 713// INTERNAL IMPLEMENTATION - DO NOT USE. 714// 715// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 716// is not satisfied. 717// Synopsys: 718// GTEST_CHECK_(boolean_condition); 719// or 720// GTEST_CHECK_(boolean_condition) << "Additional message"; 721// 722// This checks the condition and if the condition is not satisfied 723// it prints message about the condition violation, including the 724// condition itself, plus additional message streamed into it, if any, 725// and then it aborts the program. It aborts the program irrespective of 726// whether it is built in the debug mode or not. 727#define GTEST_CHECK_(condition) \ 728 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 729 if (::testing::internal::IsTrue(condition)) \ 730 ; \ 731 else \ 732 GTEST_LOG_(FATAL) << "Condition " #condition " failed. " 733 734// An all-mode assert to verify that the given POSIX-style function 735// call returns 0 (indicating success). Known limitation: this 736// doesn't expand to a balanced 'if' statement, so enclose the macro 737// in {} if you need to use it as the only statement in an 'if' 738// branch. 739#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ 740 if (const int gtest_error = (posix_call)) \ 741 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \ 742 << gtest_error 743 744#if GTEST_HAS_STREAM_REDIRECTION_ 745 746// Defines the stderr capturer: 747// CaptureStdout - starts capturing stdout. 748// GetCapturedStdout - stops capturing stdout and returns the captured string. 749// CaptureStderr - starts capturing stderr. 750// GetCapturedStderr - stops capturing stderr and returns the captured string. 751// 752void CaptureStdout(); 753String GetCapturedStdout(); 754void CaptureStderr(); 755String GetCapturedStderr(); 756 757#endif // GTEST_HAS_STREAM_REDIRECTION_ 758 759 760#if GTEST_HAS_DEATH_TEST 761 762// A copy of all command line arguments. Set by InitGoogleTest(). 763extern ::std::vector<String> g_argvs; 764 765// GTEST_HAS_DEATH_TEST implies we have ::std::string. 766const ::std::vector<String>& GetArgvs(); 767 768#endif // GTEST_HAS_DEATH_TEST 769 770// Defines synchronization primitives. 771 772#if GTEST_HAS_PTHREAD 773 774// Sleeps for (roughly) n milli-seconds. This function is only for 775// testing Google Test's own constructs. Don't use it in user tests, 776// either directly or indirectly. 777inline void SleepMilliseconds(int n) { 778 const timespec time = { 779 0, // 0 seconds. 780 n * 1000L * 1000L, // And n ms. 781 }; 782 nanosleep(&time, NULL); 783} 784 785// Allows a controller thread to pause execution of newly created 786// threads until notified. Instances of this class must be created 787// and destroyed in the controller thread. 788// 789// This class is only for testing Google Test's own constructs. Do not 790// use it in user tests, either directly or indirectly. 791class Notification { 792 public: 793 Notification() : notified_(false) {} 794 795 // Notifies all threads created with this notification to start. Must 796 // be called from the controller thread. 797 void Notify() { notified_ = true; } 798 799 // Blocks until the controller thread notifies. Must be called from a test 800 // thread. 801 void WaitForNotification() { 802 while(!notified_) { 803 SleepMilliseconds(10); 804 } 805 } 806 807 private: 808 volatile bool notified_; 809 810 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); 811}; 812 813// Helper class for testing Google Test's multi-threading constructs. 814// To use it, derive a class template ThreadWithParam<T> from 815// ThreadWithParamBase<T> and implement thread creation and startup in 816// the constructor and joining the thread in JoinUnderlyingThread(). 817// Then you can write: 818// 819// void ThreadFunc(int param) { /* Do things with param */ } 820// Notification thread_can_start; 821// ... 822// // The thread_can_start parameter is optional; you can supply NULL. 823// ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start); 824// thread_can_start.Notify(); 825// 826// These classes are only for testing Google Test's own constructs. Do 827// not use them in user tests, either directly or indirectly. 828template <typename T> 829class ThreadWithParamBase { 830 public: 831 typedef void (*UserThreadFunc)(T); 832 833 ThreadWithParamBase( 834 UserThreadFunc func, T param, Notification* thread_can_start) 835 : func_(func), 836 param_(param), 837 thread_can_start_(thread_can_start), 838 finished_(false) {} 839 virtual ~ThreadWithParamBase() {} 840 841 void Join() { 842 if (!finished_) { 843 JoinUnderlyingThread(); 844 finished_ = true; 845 } 846 } 847 848 virtual void JoinUnderlyingThread() = 0; 849 850 void ThreadMain() { 851 if (thread_can_start_ != NULL) 852 thread_can_start_->WaitForNotification(); 853 func_(param_); 854 } 855 856 protected: 857 const UserThreadFunc func_; // User-supplied thread function. 858 const T param_; // User-supplied parameter to the thread function. 859 // When non-NULL, used to block execution until the controller thread 860 // notifies. 861 Notification* const thread_can_start_; 862 bool finished_; // true iff we know that the thread function has finished. 863}; 864 865// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is 866// true. 867#include <pthread.h> 868 869// MutexBase and Mutex implement mutex on pthreads-based platforms. They 870// are used in conjunction with class MutexLock: 871// 872// Mutex mutex; 873// ... 874// MutexLock lock(&mutex); // Acquires the mutex and releases it at the end 875// // of the current scope. 876// 877// MutexBase implements behavior for both statically and dynamically 878// allocated mutexes. Do not use MutexBase directly. Instead, write 879// the following to define a static mutex: 880// 881// GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); 882// 883// You can forward declare a static mutex like this: 884// 885// GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); 886// 887// To create a dynamic mutex, just define an object of type Mutex. 888class MutexBase { 889 public: 890 // Acquires this mutex. 891 void Lock() { 892 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); 893 owner_ = pthread_self(); 894 } 895 896 // Releases this mutex. 897 void Unlock() { 898 // We don't protect writing to owner_ here, as it's the caller's 899 // responsibility to ensure that the current thread holds the 900 // mutex when this is called. 901 owner_ = 0; 902 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); 903 } 904 905 // Does nothing if the current thread holds the mutex. Otherwise, crashes 906 // with high probability. 907 void AssertHeld() const { 908 GTEST_CHECK_(owner_ == pthread_self()) 909 << "The current thread is not holding the mutex @" << this; 910 } 911 912 // A static mutex may be used before main() is entered. It may even 913 // be used before the dynamic initialization stage. Therefore we 914 // must be able to initialize a static mutex object at link time. 915 // This means MutexBase has to be a POD and its member variables 916 // have to be public. 917 public: 918 pthread_mutex_t mutex_; // The underlying pthread mutex. 919 pthread_t owner_; // The thread holding the mutex; 0 means no one holds it. 920}; 921 922// Forward-declares a static mutex. 923#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 924 extern ::testing::internal::MutexBase mutex 925 926// Defines and statically (i.e. at link time) initializes a static mutex. 927#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 928 ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 } 929 930// The Mutex class can only be used for mutexes created at runtime. It 931// shares its API with MutexBase otherwise. 932class Mutex : public MutexBase { 933 public: 934 Mutex() { 935 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL)); 936 owner_ = 0; 937 } 938 ~Mutex() { 939 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); 940 } 941 942 private: 943 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); 944}; 945 946// We cannot name this class MutexLock as the ctor declaration would 947// conflict with a macro named MutexLock, which is defined on some 948// platforms. Hence the typedef trick below. 949class GTestMutexLock { 950 public: 951 explicit GTestMutexLock(MutexBase* mutex) 952 : mutex_(mutex) { mutex_->Lock(); } 953 954 ~GTestMutexLock() { mutex_->Unlock(); } 955 956 private: 957 MutexBase* const mutex_; 958 959 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); 960}; 961 962typedef GTestMutexLock MutexLock; 963 964// Implements thread-local storage on pthreads-based systems. 965// 966// // Thread 1 967// ThreadLocal<int> tl(100); // 100 is the default value for each thread. 968// 969// // Thread 2 970// tl.set(150); // Changes the value for thread 2 only. 971// EXPECT_EQ(150, tl.get()); 972// 973// // Thread 1 974// EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. 975// tl.set(200); 976// EXPECT_EQ(200, tl.get()); 977// 978// The template type argument T must have a public copy constructor. 979// In addition, the default ThreadLocal constructor requires T to have 980// a public default constructor. An object managed by a ThreadLocal 981// instance for a thread is guaranteed to exist at least until the 982// earliest of the two events: (a) the thread terminates or (b) the 983// ThreadLocal object is destroyed. 984template <typename T> 985class ThreadLocal { 986 public: 987 ThreadLocal() : key_(CreateKey()), 988 default_() {} 989 explicit ThreadLocal(const T& value) : key_(CreateKey()), 990 default_(value) {} 991 992 ~ThreadLocal() { 993 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); 994 } 995 996 T* pointer() { return GetOrCreateValue(); } 997 const T* pointer() const { return GetOrCreateValue(); } 998 const T& get() const { return *pointer(); } 999 void set(const T& value) { *pointer() = value; } 1000 1001 private: 1002 static pthread_key_t CreateKey() { 1003 pthread_key_t key; 1004 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_create(&key, &DeleteData)); 1005 return key; 1006 } 1007 1008 T* GetOrCreateValue() const { 1009 T* const value = static_cast<T*>(pthread_getspecific(key_)); 1010 if (value != NULL) 1011 return value; 1012 1013 T* const new_value = new T(default_); 1014 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, new_value)); 1015 return new_value; 1016 } 1017 1018 static void DeleteData(void* data) { delete static_cast<T*>(data); } 1019 1020 // A key pthreads uses for looking up per-thread values. 1021 const pthread_key_t key_; 1022 const T default_; // The default value for each thread. 1023 1024 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); 1025}; 1026 1027// Helper class for testing Google Test's multi-threading constructs. 1028template <typename T> 1029class ThreadWithParam : public ThreadWithParamBase<T> { 1030 public: 1031 ThreadWithParam(void (*func)(T), T param, Notification* thread_can_start) 1032 : ThreadWithParamBase<T>(func, param, thread_can_start) { 1033 // The thread can be created only after all fields except thread_ 1034 // have been initialized. 1035 GTEST_CHECK_POSIX_SUCCESS_( 1036 pthread_create(&thread_, 0, ThreadMainStatic, this)); 1037 } 1038 virtual ~ThreadWithParam() { this->Join(); } 1039 1040 virtual void JoinUnderlyingThread() { 1041 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0)); 1042 } 1043 1044 private: 1045 static void* ThreadMainStatic(void* thread_with_param) { 1046 static_cast<ThreadWithParam<T>*>(thread_with_param)->ThreadMain(); 1047 return NULL; // We are not interested in the thread exit code. 1048 } 1049 1050 pthread_t thread_; // The native thread object. 1051 1052 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); 1053}; 1054 1055#define GTEST_IS_THREADSAFE 1 1056 1057#else // GTEST_HAS_PTHREAD 1058 1059// A dummy implementation of synchronization primitives (mutex, lock, 1060// and thread-local variable). Necessary for compiling Google Test where 1061// mutex is not supported - using Google Test in multiple threads is not 1062// supported on such platforms. 1063 1064class Mutex { 1065 public: 1066 Mutex() {} 1067 void AssertHeld() const {} 1068}; 1069 1070#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1071 extern ::testing::internal::Mutex mutex 1072 1073#define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex 1074 1075class GTestMutexLock { 1076 public: 1077 explicit GTestMutexLock(Mutex*) {} // NOLINT 1078}; 1079 1080typedef GTestMutexLock MutexLock; 1081 1082template <typename T> 1083class ThreadLocal { 1084 public: 1085 ThreadLocal() : value_() {} 1086 explicit ThreadLocal(const T& value) : value_(value) {} 1087 T* pointer() { return &value_; } 1088 const T* pointer() const { return &value_; } 1089 const T& get() const { return value_; } 1090 void set(const T& value) { value_ = value; } 1091 private: 1092 T value_; 1093}; 1094 1095// The above synchronization primitives have dummy implementations. 1096// Therefore Google Test is not thread-safe. 1097#define GTEST_IS_THREADSAFE 0 1098 1099#endif // GTEST_HAS_PTHREAD 1100 1101// Returns the number of threads running in the process, or 0 to indicate that 1102// we cannot detect it. 1103size_t GetThreadCount(); 1104 1105// Passing non-POD classes through ellipsis (...) crashes the ARM 1106// compiler and generates a warning in Sun Studio. The Nokia Symbian 1107// and the IBM XL C/C++ compiler try to instantiate a copy constructor 1108// for objects passed through ellipsis (...), failing for uncopyable 1109// objects. We define this to ensure that only POD is passed through 1110// ellipsis on these systems. 1111#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC) 1112#define GTEST_ELLIPSIS_NEEDS_POD_ 1 1113#endif 1114 1115// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between 1116// const T& and const T* in a function template. These compilers 1117// _can_ decide between class template specializations for T and T*, 1118// so a tr1::type_traits-like is_pointer works. 1119#if defined(__SYMBIAN32__) || defined(__IBMCPP__) 1120#define GTEST_NEEDS_IS_POINTER_ 1 1121#endif 1122 1123template <bool bool_value> 1124struct bool_constant { 1125 typedef bool_constant<bool_value> type; 1126 static const bool value = bool_value; 1127}; 1128template <bool bool_value> const bool bool_constant<bool_value>::value; 1129 1130typedef bool_constant<false> false_type; 1131typedef bool_constant<true> true_type; 1132 1133template <typename T> 1134struct is_pointer : public false_type {}; 1135 1136template <typename T> 1137struct is_pointer<T*> : public true_type {}; 1138 1139#if GTEST_OS_WINDOWS 1140#define GTEST_PATH_SEP_ "\\" 1141#define GTEST_HAS_ALT_PATH_SEP_ 1 1142// The biggest signed integer type the compiler supports. 1143typedef __int64 BiggestInt; 1144#else 1145#define GTEST_PATH_SEP_ "/" 1146#define GTEST_HAS_ALT_PATH_SEP_ 0 1147typedef long long BiggestInt; // NOLINT 1148#endif // GTEST_OS_WINDOWS 1149 1150// The testing::internal::posix namespace holds wrappers for common 1151// POSIX functions. These wrappers hide the differences between 1152// Windows/MSVC and POSIX systems. Since some compilers define these 1153// standard functions as macros, the wrapper cannot have the same name 1154// as the wrapped function. 1155 1156namespace posix { 1157 1158// Functions with a different name on Windows. 1159 1160#if GTEST_OS_WINDOWS 1161 1162typedef struct _stat StatStruct; 1163 1164#ifdef __BORLANDC__ 1165inline int IsATTY(int fd) { return isatty(fd); } 1166inline int StrCaseCmp(const char* s1, const char* s2) { 1167 return stricmp(s1, s2); 1168} 1169inline char* StrDup(const char* src) { return strdup(src); } 1170#else // !__BORLANDC__ 1171#if GTEST_OS_WINDOWS_MOBILE 1172inline int IsATTY(int /* fd */) { return 0; } 1173#else 1174inline int IsATTY(int fd) { return _isatty(fd); } 1175#endif // GTEST_OS_WINDOWS_MOBILE 1176inline int StrCaseCmp(const char* s1, const char* s2) { 1177 return _stricmp(s1, s2); 1178} 1179inline char* StrDup(const char* src) { return _strdup(src); } 1180#endif // __BORLANDC__ 1181 1182#if GTEST_OS_WINDOWS_MOBILE 1183inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } 1184// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this 1185// time and thus not defined there. 1186#else 1187inline int FileNo(FILE* file) { return _fileno(file); } 1188inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } 1189inline int RmDir(const char* dir) { return _rmdir(dir); } 1190inline bool IsDir(const StatStruct& st) { 1191 return (_S_IFDIR & st.st_mode) != 0; 1192} 1193#endif // GTEST_OS_WINDOWS_MOBILE 1194 1195#else 1196 1197typedef struct stat StatStruct; 1198 1199inline int FileNo(FILE* file) { return fileno(file); } 1200inline int IsATTY(int fd) { return isatty(fd); } 1201inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } 1202inline int StrCaseCmp(const char* s1, const char* s2) { 1203 return strcasecmp(s1, s2); 1204} 1205inline char* StrDup(const char* src) { return strdup(src); } 1206inline int RmDir(const char* dir) { return rmdir(dir); } 1207inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 1208 1209#endif // GTEST_OS_WINDOWS 1210 1211// Functions deprecated by MSVC 8.0. 1212 1213#ifdef _MSC_VER 1214// Temporarily disable warning 4996 (deprecated function). 1215#pragma warning(push) 1216#pragma warning(disable:4996) 1217#endif 1218 1219inline const char* StrNCpy(char* dest, const char* src, size_t n) { 1220 return strncpy(dest, src, n); 1221} 1222 1223// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and 1224// StrError() aren't needed on Windows CE at this time and thus not 1225// defined there. 1226 1227#if !GTEST_OS_WINDOWS_MOBILE 1228inline int ChDir(const char* dir) { return chdir(dir); } 1229#endif 1230inline FILE* FOpen(const char* path, const char* mode) { 1231 return fopen(path, mode); 1232} 1233#if !GTEST_OS_WINDOWS_MOBILE 1234inline FILE *FReopen(const char* path, const char* mode, FILE* stream) { 1235 return freopen(path, mode, stream); 1236} 1237inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } 1238#endif 1239inline int FClose(FILE* fp) { return fclose(fp); } 1240#if !GTEST_OS_WINDOWS_MOBILE 1241inline int Read(int fd, void* buf, unsigned int count) { 1242 return static_cast<int>(read(fd, buf, count)); 1243} 1244inline int Write(int fd, const void* buf, unsigned int count) { 1245 return static_cast<int>(write(fd, buf, count)); 1246} 1247inline int Close(int fd) { return close(fd); } 1248inline const char* StrError(int errnum) { return strerror(errnum); } 1249#endif 1250inline const char* GetEnv(const char* name) { 1251#if GTEST_OS_WINDOWS_MOBILE 1252 // We are on Windows CE, which has no environment variables. 1253 return NULL; 1254#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) 1255 // Environment variables which we programmatically clear will be set to the 1256 // empty string rather than unset (NULL). Handle that case. 1257 const char* const env = getenv(name); 1258 return (env != NULL && env[0] != '\0') ? env : NULL; 1259#else 1260 return getenv(name); 1261#endif 1262} 1263 1264#ifdef _MSC_VER 1265#pragma warning(pop) // Restores the warning state. 1266#endif 1267 1268#if GTEST_OS_WINDOWS_MOBILE 1269// Windows CE has no C library. The abort() function is used in 1270// several places in Google Test. This implementation provides a reasonable 1271// imitation of standard behaviour. 1272void Abort(); 1273#else 1274inline void Abort() { abort(); } 1275#endif // GTEST_OS_WINDOWS_MOBILE 1276 1277} // namespace posix 1278 1279// The maximum number a BiggestInt can represent. This definition 1280// works no matter BiggestInt is represented in one's complement or 1281// two's complement. 1282// 1283// We cannot rely on numeric_limits in STL, as __int64 and long long 1284// are not part of standard C++ and numeric_limits doesn't need to be 1285// defined for them. 1286const BiggestInt kMaxBiggestInt = 1287 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); 1288 1289// This template class serves as a compile-time function from size to 1290// type. It maps a size in bytes to a primitive type with that 1291// size. e.g. 1292// 1293// TypeWithSize<4>::UInt 1294// 1295// is typedef-ed to be unsigned int (unsigned integer made up of 4 1296// bytes). 1297// 1298// Such functionality should belong to STL, but I cannot find it 1299// there. 1300// 1301// Google Test uses this class in the implementation of floating-point 1302// comparison. 1303// 1304// For now it only handles UInt (unsigned int) as that's all Google Test 1305// needs. Other types can be easily added in the future if need 1306// arises. 1307template <size_t size> 1308class TypeWithSize { 1309 public: 1310 // This prevents the user from using TypeWithSize<N> with incorrect 1311 // values of N. 1312 typedef void UInt; 1313}; 1314 1315// The specialization for size 4. 1316template <> 1317class TypeWithSize<4> { 1318 public: 1319 // unsigned int has size 4 in both gcc and MSVC. 1320 // 1321 // As base/basictypes.h doesn't compile on Windows, we cannot use 1322 // uint32, uint64, and etc here. 1323 typedef int Int; 1324 typedef unsigned int UInt; 1325}; 1326 1327// The specialization for size 8. 1328template <> 1329class TypeWithSize<8> { 1330 public: 1331#if GTEST_OS_WINDOWS 1332 typedef __int64 Int; 1333 typedef unsigned __int64 UInt; 1334#else 1335 typedef long long Int; // NOLINT 1336 typedef unsigned long long UInt; // NOLINT 1337#endif // GTEST_OS_WINDOWS 1338}; 1339 1340// Integer types of known sizes. 1341typedef TypeWithSize<4>::Int Int32; 1342typedef TypeWithSize<4>::UInt UInt32; 1343typedef TypeWithSize<8>::Int Int64; 1344typedef TypeWithSize<8>::UInt UInt64; 1345typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. 1346 1347// Utilities for command line flags and environment variables. 1348 1349// Macro for referencing flags. 1350#define GTEST_FLAG(name) FLAGS_gtest_##name 1351 1352// Macros for declaring flags. 1353#define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name) 1354#define GTEST_DECLARE_int32_(name) \ 1355 extern ::testing::internal::Int32 GTEST_FLAG(name) 1356#define GTEST_DECLARE_string_(name) \ 1357 extern ::testing::internal::String GTEST_FLAG(name) 1358 1359// Macros for defining flags. 1360#define GTEST_DEFINE_bool_(name, default_val, doc) \ 1361 bool GTEST_FLAG(name) = (default_val) 1362#define GTEST_DEFINE_int32_(name, default_val, doc) \ 1363 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) 1364#define GTEST_DEFINE_string_(name, default_val, doc) \ 1365 ::testing::internal::String GTEST_FLAG(name) = (default_val) 1366 1367// Parses 'str' for a 32-bit signed integer. If successful, writes the result 1368// to *value and returns true; otherwise leaves *value unchanged and returns 1369// false. 1370// TODO(chandlerc): Find a better way to refactor flag and environment parsing 1371// out of both gtest-port.cc and gtest.cc to avoid exporting this utility 1372// function. 1373bool ParseInt32(const Message& src_text, const char* str, Int32* value); 1374 1375// Parses a bool/Int32/string from the environment variable 1376// corresponding to the given Google Test flag. 1377bool BoolFromGTestEnv(const char* flag, bool default_val); 1378Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); 1379const char* StringFromGTestEnv(const char* flag, const char* default_val); 1380 1381} // namespace internal 1382} // namespace testing 1383 1384#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 1385