Ports gtest to C++Builder, by Josh Kelley.

This commit is contained in:
zhanyong.wan
2009-04-28 00:28:09 +00:00
parent f2334aa195
commit c78ae6196d
13 changed files with 810 additions and 203 deletions

View File

@@ -383,7 +383,7 @@ class FloatingPoint {
// around may change its bits, although the new value is guaranteed
// to be also a NAN. Therefore, don't expect this constructor to
// preserve the bits in x when x is a NAN.
explicit FloatingPoint(const RawType& x) : value_(x) {}
explicit FloatingPoint(const RawType& x) { u_.value_ = x; }
// Static methods
@@ -392,8 +392,8 @@ class FloatingPoint {
// This function is needed to test the AlmostEquals() method.
static RawType ReinterpretBits(const Bits bits) {
FloatingPoint fp(0);
fp.bits_ = bits;
return fp.value_;
fp.u_.bits_ = bits;
return fp.u_.value_;
}
// Returns the floating-point number that represent positive infinity.
@@ -404,16 +404,16 @@ class FloatingPoint {
// Non-static methods
// Returns the bits that represents this number.
const Bits &bits() const { return bits_; }
const Bits &bits() const { return u_.bits_; }
// Returns the exponent bits of this number.
Bits exponent_bits() const { return kExponentBitMask & bits_; }
Bits exponent_bits() const { return kExponentBitMask & u_.bits_; }
// Returns the fraction bits of this number.
Bits fraction_bits() const { return kFractionBitMask & bits_; }
Bits fraction_bits() const { return kFractionBitMask & u_.bits_; }
// Returns the sign bit of this number.
Bits sign_bit() const { return kSignBitMask & bits_; }
Bits sign_bit() const { return kSignBitMask & u_.bits_; }
// Returns true iff this is NAN (not a number).
bool is_nan() const {
@@ -433,10 +433,17 @@ class FloatingPoint {
// a NAN must return false.
if (is_nan() || rhs.is_nan()) return false;
return DistanceBetweenSignAndMagnitudeNumbers(bits_, rhs.bits_) <= kMaxUlps;
return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_)
<= kMaxUlps;
}
private:
// The data type used to store the actual floating-point number.
union FloatingPointUnion {
RawType value_; // The raw floating-point number.
Bits bits_; // The bits that represent the number.
};
// Converts an integer from the sign-and-magnitude representation to
// the biased representation. More precisely, let N be 2 to the
// power of (kBitCount - 1), an integer x is represented by the
@@ -471,10 +478,7 @@ class FloatingPoint {
return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
}
union {
RawType value_; // The raw floating-point number.
Bits bits_; // The bits that represent the number.
};
FloatingPointUnion u_;
};
// Typedefs the instances of the FloatingPoint template class that we

View File

@@ -220,13 +220,15 @@
// Defines GTEST_HAS_EXCEPTIONS to 1 if exceptions are enabled, or 0
// otherwise.
#ifdef _MSC_VER // Compiled by MSVC?
#if defined(_MSC_VER) || defined(__BORLANDC__)
// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
// macro to enable exceptions, so we'll do the same.
// Assumes that exceptions are enabled by default.
#ifndef _HAS_EXCEPTIONS // MSVC uses this macro to enable exceptions.
#ifndef _HAS_EXCEPTIONS
#define _HAS_EXCEPTIONS 1
#endif // _HAS_EXCEPTIONS
#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
#else // The compiler is not MSVC.
#else // The compiler is not MSVC or C++Builder.
// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. For
// other compilers, we assume exceptions are disabled to be
// conservative.
@@ -235,7 +237,7 @@
#else
#define GTEST_HAS_EXCEPTIONS 0
#endif // defined(__GNUC__) && __EXCEPTIONS
#endif // _MSC_VER
#endif // defined(_MSC_VER) || defined(__BORLANDC__)
// Determines whether ::std::string and ::string are available.
@@ -756,13 +758,22 @@ namespace posix {
typedef struct _stat StatStruct;
inline int FileNo(FILE* file) { return _fileno(file); }
#ifdef __BORLANDC__
inline int IsATTY(int fd) { return isatty(fd); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return stricmp(s1, s2);
}
inline char* StrDup(const char* src) { return strdup(src); }
#else
inline int IsATTY(int fd) { return _isatty(fd); }
inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return _stricmp(s1, s2);
}
inline char* StrDup(const char* src) { return _strdup(src); }
#endif // __BORLANDC__
inline int FileNo(FILE* file) { return _fileno(file); }
inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
inline int RmDir(const char* dir) { return _rmdir(dir); }
inline bool IsDir(const StatStruct& st) {
return (_S_IFDIR & st.st_mode) != 0;
@@ -778,7 +789,7 @@ inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
inline int StrCaseCmp(const char* s1, const char* s2) {
return strcasecmp(s1, s2);
}
inline char* StrDup(const char* src) { return ::strdup(src); }
inline char* StrDup(const char* src) { return strdup(src); }
inline int RmDir(const char* dir) { return rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
@@ -815,6 +826,11 @@ inline const char* StrError(int errnum) { return strerror(errnum); }
inline const char* GetEnv(const char* name) {
#ifdef _WIN32_WCE // We are on Windows CE, which has no environment variables.
return NULL;
#elif defined(__BORLANDC__)
// Environment variables which we programmatically clear will be set to the
// empty string rather than unset (NULL). Handle that case.
const char* const env = getenv(name);
return (env != NULL && env[0] != '\0') ? env : NULL;
#else
return getenv(name);
#endif