1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/*
-------------------------------------------------------------------------
CxxTest: A lightweight C++ unit testing library.
Copyright (c) 2008 Sandia Corporation.
This software is distributed under the LGPL License v3
For more information, see the COPYING file in the top CxxTest directory.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
*/
#ifndef __cxxtest__ValueTraits_h__
#define __cxxtest__ValueTraits_h__
//
// ValueTraits are used by CxxTest to convert arbitrary
// values used in TS_ASSERT_EQUALS() to a string representation.
//
// This header file contains value traits for builtin integral types.
// To declare value traits for new types you should instantiate the class
// ValueTraits<YourClass>.
//
#include <cxxtest/Flags.h>
#ifdef _CXXTEST_OLD_TEMPLATE_SYNTAX
# define CXXTEST_TEMPLATE_INSTANTIATION
#else // !_CXXTEST_OLD_TEMPLATE_SYNTAX
# define CXXTEST_TEMPLATE_INSTANTIATION template<>
#endif // _CXXTEST_OLD_TEMPLATE_SYNTAX
#ifdef _CXXTEST_HAVE_STD
#include <cmath>
#else
#include <math.h>
#endif
namespace CxxTest
{
//
// This is how we use the value traits
//
# define TS_AS_STRING(x) CxxTest::traits(x).asString()
//
// Char representation of a digit
//
char digitToChar(unsigned digit);
//
// Convert byte value to hex digits
// Returns pointer to internal buffer
//
const char *byteToHex(unsigned char byte);
//
// Convert byte values to string
// Returns one past the copied data
//
char *bytesToString(const unsigned char *bytes, unsigned numBytes, unsigned maxBytes, char *s);
//
// Copy a string.
// Returns one past the end of the destination string
// Remember -- we can't use the standard library!
//
char *copyString(char *dst, const char *src);
//
// Compare two strings.
// Remember -- we can't use the standard library!
//
bool stringsEqual(const char *s1, const char *s2);
//
// Represent a character value as a string
// Returns one past the end of the string
// This will be the actual char if printable or '\xXXXX' otherwise
//
char *charToString(unsigned long c, char *s);
//
// Prevent problems with negative (signed char)s
//
char *charToString(char c, char *s);
//
// The default ValueTraits class dumps up to 8 bytes as hex values
//
template <class T>
class ValueTraits
{
enum { MAX_BYTES = 8 };
char _asString[sizeof("{ ") + sizeof("XX ") * MAX_BYTES + sizeof("... }")];
public:
ValueTraits(const T &t) { bytesToString((const unsigned char *)&t, sizeof(T), MAX_BYTES, _asString); }
const char *asString(void) const { return _asString; }
};
//
// traits( T t )
// Creates an object of type ValueTraits<T>
//
template <class T>
inline ValueTraits<T> traits(T t)
{
return ValueTraits<T>(t);
}
//
// You can duplicate the implementation of an existing ValueTraits
//
# define CXXTEST_COPY_TRAITS(CXXTEST_NEW_CLASS, CXXTEST_OLD_CLASS) \
CXXTEST_TEMPLATE_INSTANTIATION \
class ValueTraits< CXXTEST_NEW_CLASS > \
{ \
ValueTraits< CXXTEST_OLD_CLASS > _old; \
public: \
ValueTraits( CXXTEST_NEW_CLASS n ) : _old( (CXXTEST_OLD_CLASS)n ) {} \
const char *asString( void ) const { return _old.asString(); } \
}
//
// Certain compilers need separate declarations for T and const T
//
# ifdef _CXXTEST_NO_COPY_CONST
# define CXXTEST_COPY_CONST_TRAITS(CXXTEST_CLASS)
# else // !_CXXTEST_NO_COPY_CONST
# define CXXTEST_COPY_CONST_TRAITS(CXXTEST_CLASS) CXXTEST_COPY_TRAITS(CXXTEST_CLASS, const CXXTEST_CLASS)
# endif // _CXXTEST_NO_COPY_CONST
//
// Avoid compiler warnings about unsigned types always >= 0
//
template<class N> inline bool negative(N n) { return n < 0; }
template<class N> inline N abs(N n) { return negative(n) ? -n : n; }
# define CXXTEST_NON_NEGATIVE(Type) \
CXXTEST_TEMPLATE_INSTANTIATION \
inline bool negative<Type>( Type ) { return false; } \
CXXTEST_TEMPLATE_INSTANTIATION \
inline Type abs<Type>( Type value ) { return value; }
CXXTEST_NON_NEGATIVE(bool)
CXXTEST_NON_NEGATIVE(unsigned char)
CXXTEST_NON_NEGATIVE(unsigned short int)
CXXTEST_NON_NEGATIVE(unsigned int)
CXXTEST_NON_NEGATIVE(unsigned long int)
# ifdef _CXXTEST_LONGLONG
CXXTEST_NON_NEGATIVE(unsigned _CXXTEST_LONGLONG)
# endif // _CXXTEST_LONGLONG
//
// Represent (integral) number as a string
// Returns one past the end of the string
// Remember -- we can't use the standard library!
//
template<class N>
char *numberToString(N n, char *s,
N base = 10,
unsigned skipDigits = 0,
unsigned maxDigits = (unsigned) - 1)
{
if (negative(n))
{
*s++ = '-';
n = abs(n);
}
N digit = 1;
while (digit <= (n / base))
{
digit *= base;
}
N digitValue;
for (; digit >= 1 && skipDigits; n -= digit * digitValue, digit /= base, -- skipDigits)
{
digitValue = (unsigned)(n / digit);
}
for (; digit >= 1 && maxDigits; n -= digit * digitValue, digit /= base, -- maxDigits)
{
*s++ = digitToChar((unsigned)(digitValue = (unsigned)(n / digit)));
}
*s = '\0';
return s;
}
//
// All the specific ValueTraits follow.
// You can #define CXXTEST_USER_VALUE_TRAITS if you don't want them
//
#ifndef CXXTEST_USER_VALUE_TRAITS
//
// ValueTraits: const char * const &
// This is used for printing strings, as in TS_FAIL( "Message" )
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const char * const &>
{
ValueTraits &operator=(const ValueTraits &);
const char *_asString;
public:
ValueTraits(const char * const &value) : _asString(value) {}
ValueTraits(const ValueTraits &other) : _asString(other._asString) {}
const char *asString(void) const { return _asString; }
#if 0
const char *asString(void) const
{
std::string tmp(1, '"');
for (char const* src = _asString; src && *src; ++src)
{
switch (*src)
{
case '\\': tmp += "\\\\"; break;
case '\n': tmp += "\\n"; break;
case '\r': tmp += "\\r"; break;
case '\t': tmp += "\\t"; break;
case '"': tmp += "\\\""; break;
default: tmp += *src; break;
}
}
tmp += '"';
char* res = new char[tmp.size() + 1];
return strcpy(res, tmp.c_str());
}
#endif
};
CXXTEST_COPY_TRAITS(const char *, const char * const &);
CXXTEST_COPY_TRAITS(char *, const char * const &);
//
// ValueTraits: bool
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const bool>
{
bool _value;
public:
ValueTraits(const bool value) : _value(value) {}
const char *asString(void) const { return _value ? "true" : "false"; }
};
CXXTEST_COPY_CONST_TRAITS(bool);
# ifdef _CXXTEST_LONGLONG
//
// ValueTraits: signed long long
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const signed _CXXTEST_LONGLONG>
{
typedef _CXXTEST_LONGLONG T;
char _asString[2 + 3 * sizeof(T)];
public:
ValueTraits(T t) { numberToString<T>(t, _asString); }
const char *asString(void) const { return _asString; }
};
CXXTEST_COPY_CONST_TRAITS(signed _CXXTEST_LONGLONG);
//
// ValueTraits: unsigned long long
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const unsigned _CXXTEST_LONGLONG>
{
typedef unsigned _CXXTEST_LONGLONG T;
char _asString[1 + 3 * sizeof(T)];
public:
ValueTraits(T t) { numberToString<T>(t, _asString); }
const char *asString(void) const { return _asString; }
};
CXXTEST_COPY_CONST_TRAITS(unsigned _CXXTEST_LONGLONG);
# endif // _CXXTEST_LONGLONG
//
// ValueTraits: signed long
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const signed long int>
{
typedef signed long int T;
char _asString[2 + 3 * sizeof(T)];
public:
ValueTraits(T t) { numberToString<T>(t, _asString); }
const char *asString(void) const { return _asString; }
};
CXXTEST_COPY_CONST_TRAITS(signed long int);
//
// ValueTraits: unsigned long
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const unsigned long int>
{
typedef unsigned long int T;
char _asString[1 + 3 * sizeof(T)];
public:
ValueTraits(T t) { numberToString<T>(t, _asString); }
const char *asString(void) const { return _asString; }
};
CXXTEST_COPY_CONST_TRAITS(unsigned long int);
//
// All decimals are the same as the long version
//
CXXTEST_COPY_TRAITS(const signed int, const signed long int);
CXXTEST_COPY_TRAITS(const unsigned int, const unsigned long int);
CXXTEST_COPY_TRAITS(const signed short int, const signed long int);
CXXTEST_COPY_TRAITS(const unsigned short int, const unsigned long int);
CXXTEST_COPY_TRAITS(const unsigned char, const unsigned long int);
CXXTEST_COPY_CONST_TRAITS(signed int);
CXXTEST_COPY_CONST_TRAITS(unsigned int);
CXXTEST_COPY_CONST_TRAITS(signed short int);
CXXTEST_COPY_CONST_TRAITS(unsigned short int);
CXXTEST_COPY_CONST_TRAITS(unsigned char);
//
// ValueTraits: char
// Returns 'x' for printable chars, '\x??' for others
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const char>
{
char _asString[sizeof("'\\xXX'")];
public:
ValueTraits(char c) { copyString(charToString(c, copyString(_asString, "'")), "'"); }
const char *asString(void) const { return _asString; }
};
CXXTEST_COPY_CONST_TRAITS(char);
//
// ValueTraits: signed char
// Same as char, some compilers need it
//
CXXTEST_COPY_TRAITS(const signed char, const char);
CXXTEST_COPY_CONST_TRAITS(signed char);
//
// ValueTraits: double
//
CXXTEST_TEMPLATE_INSTANTIATION
class ValueTraits<const double>
{
public:
ValueTraits(double t)
{
if ((t != t) || (t >= HUGE_VAL) || (t == -HUGE_VAL))
{
nonFiniteNumber(t);
}
else if (requiredDigitsOnLeft(t) > MAX_DIGITS_ON_LEFT)
{
hugeNumber(t);
}
else
{
normalNumber(t);
}
}
const char *asString(void) const { return _asString; }
private:
enum { MAX_DIGITS_ON_LEFT = 24, DIGITS_ON_RIGHT = 4, BASE = 10 };
char _asString[1 + MAX_DIGITS_ON_LEFT + 1 + DIGITS_ON_RIGHT + 1];
static unsigned requiredDigitsOnLeft(double t);
char *doNegative(double &t);
void hugeNumber(double t);
void normalNumber(double t);
void nonFiniteNumber(double t);
char *doubleToString(double t, char *s, unsigned skip = 0, unsigned max = (unsigned) - 1);
};
CXXTEST_COPY_CONST_TRAITS(double);
//
// ValueTraits: float
//
CXXTEST_COPY_TRAITS(const float, const double);
CXXTEST_COPY_CONST_TRAITS(float);
#endif // !CXXTEST_USER_VALUE_TRAITS
}
#ifdef _CXXTEST_HAVE_STD
# include <cxxtest/StdValueTraits.h>
#endif // _CXXTEST_HAVE_STD
namespace dummy_enum_ns {}
//
// CXXTEST_ENUM_TRAITS
//
#define CXXTEST_ENUM_TRAITS( TYPE, VALUES ) \
namespace CxxTest \
{ \
CXXTEST_TEMPLATE_INSTANTIATION \
class ValueTraits<TYPE> \
{ \
TYPE _value; \
char _fallback[sizeof("(" #TYPE ")") + 3 * sizeof(TYPE)]; \
public: \
ValueTraits( TYPE value ) { \
_value = value; \
numberToString<unsigned long int>( _value, copyString( _fallback, "(" #TYPE ")" ) ); \
} \
const char *asString( void ) const \
{ \
switch ( _value ) \
{ \
VALUES \
default: return _fallback; \
} \
} \
}; \
} using namespace dummy_enum_ns
#define CXXTEST_ENUM_MEMBER( MEMBER ) \
case MEMBER: return #MEMBER;
#endif // __cxxtest__ValueTraits_h__
|