16 #define TEST_NOTHROW_WITH_MESSAGE( code ) \
18 (out) << "Test that code {"#code";} does not throw : "; \
20 (out) << "passes\n"; \
22 catch (std::exception& theException) { \
25 out << "\nException message for unexpected exception:\n\n"; \
27 Teuchos::OSTab l_tab(out); \
28 out << theException.what() << "\n\n"; \
40 typedef unsigned short unsigned_short_type;
41 typedef unsigned int unsigned_int_type;
42 typedef unsigned long unsigned_long_type;
44 typedef long long long_long_type;
45 typedef unsigned long long unsigned_long_long_type;
49 std::string valToString(
const T &val)
51 const int precision = std::numeric_limits<T>::digits10 + 10;
53 std::ostringstream os;
54 os.precision(precision);
67 const float minF = -std::numeric_limits<float>::max ();
68 const float minusOneF = -1;
69 const float maxF = std::numeric_limits<float>::max ();
71 const double minD = -std::numeric_limits<double>::max ();
72 const double minusOneD = -1;
73 const double maxD = std::numeric_limits<double>::max ();
77 long double valLD = 0;
168 const long double minLD = -std::numeric_limits<long double>::max ();
169 const long double minusOneLD = -1;
170 const long double maxLD = std::numeric_limits<long double>::max ();
257 const float minF = -std::numeric_limits<float>::max ();
258 const float minusOneF = -1;
259 const float maxF = std::numeric_limits<float>::max ();
261 out <<
"minF = " << minF <<
"\n";
262 out <<
"maxF = " << maxF <<
"\n";
264 const double minD = -std::numeric_limits<double>::max ();
265 const double minusOneD = -1;
266 const double maxD = std::numeric_limits<double>::max ();
268 out <<
"minD = " << minD <<
"\n";
269 out <<
"maxD = " << maxD <<
"\n";
292 const long double minLD = -std::numeric_limits<long double>::max ();
293 const long double minusOneLD = -1;
294 const long double maxLD = std::numeric_limits<long double>::max ();
296 out <<
"minLD = " << minLD <<
"\n";
297 out <<
"maxLD = " << maxLD <<
"\n";
301 long double valLD = 0;
304 out <<
"Testing string -> float conversions ...\n";
307 std::ostringstream os;
320 std::ostringstream os;
329 std::ostringstream os;
339 std::ostringstream os;
349 out <<
"Testing string -> float conversions that should throw ...\n";
352 std::ostringstream os;
355 TEST_THROW(valF = asSafe<float> (os.str ()), std::range_error);
358 std::ostringstream os;
361 TEST_THROW(valF = asSafe<float> (os.str ()), std::range_error);
365 out <<
"Testing string -> double conversions ...\n";
368 std::ostringstream os;
377 std::ostringstream os;
386 TEST_NOTHROW(valD = asSafe<double> (valToString(minusOneD)));
388 TEST_NOTHROW(valD = as<double> (valToString(minusOneD)));
396 const int sizeof_long_double =
sizeof(
long double);
397 const int sizeof_double =
sizeof(double);
398 const int max_exponent10_long_double = std::numeric_limits<long double>::max_exponent10;
399 const int max_exponent10_double = std::numeric_limits<double>::max_exponent10;
401 out <<
"sizeof_long_double = " << sizeof_long_double <<
"\n";
402 out <<
"sizeof_double = " << sizeof_double <<
"\n";
403 out <<
"max_exponent10_long_double = " << max_exponent10_long_double <<
"\n";
404 out <<
"max_exponent10_double = " << max_exponent10_double <<
"\n";
406 if (sizeof_long_double > sizeof_double
407 && max_exponent10_long_double > max_exponent10_double)
409 out <<
"Testing converting from 'long double' to 'double' that does not fit ...\n";
411 const long double maxTooBigLD_for_D =
static_cast<long double>(maxD) * 10.0;
412 const long double minTooBigLD_for_D =
static_cast<long double>(minD) * 10.0;
413 out <<
"maxTooBigLD_for_D = " << maxTooBigLD_for_D <<
"\n";
414 out <<
"minTooBigLD_for_D = " << minTooBigLD_for_D <<
"\n";
417 TEST_THROW(valD = asSafe<double>(valToString(minTooBigLD_for_D)), std::range_error);
418 out <<
"valD = " << valD <<
"\n";
421 std::ostringstream os;
423 TEST_THROW(valD = asSafe<double>(valToString(maxTooBigLD_for_D)), std::range_error);
424 out <<
"valD = " << valD <<
"\n";
443 out <<
"Testing string -> long double conversions ...\n";
446 std::ostringstream os;
455 std::ostringstream os;
464 std::ostringstream os;
490 const RealType minVal = -std::numeric_limits<RealType>::max ();
491 const RealType maxVal = std::numeric_limits<RealType>::max ();
493 SignedIntType val = 0;
494 if (
sizeof (SignedIntType) <
sizeof (RealType)) {
495 TEST_THROW(val = asSafe<SignedIntType> (minVal), std::range_error);
496 TEST_THROW(val = asSafe<SignedIntType> (maxVal), std::range_error);
517 const RealType minVal = -std::numeric_limits<RealType>::max ();
518 const RealType maxVal = std::numeric_limits<RealType>::max ();
519 const UnsignedIntType maxUnsignedIntVal =
520 std::numeric_limits<UnsignedIntType>::max ();
524 UnsignedIntType val = 42;
528 val == static_cast<UnsignedIntType> (maxVal),
530 "Dear test author, please pick a different marker value. "
531 "Please report this bug to the Teuchos developers.");
534 TEST_THROW(val = asSafe<UnsignedIntType> (minVal), std::range_error);
535 const RealType minusOne = -1;
536 TEST_THROW(val = asSafe<UnsignedIntType> (minusOne), std::range_error);
539 if (static_cast<RealType>(maxUnsignedIntVal) < maxVal) {
540 TEST_THROW(val = asSafe<UnsignedIntType> (maxVal), std::range_error);
542 std::cerr << std::endl
543 <<
"*** RealType = " << TypeNameTraits<RealType>::name ()
544 <<
", UnsignedIntType = " << TypeNameTraits<UnsignedIntType>::name ()
545 <<
", maxVal = " << maxVal
546 <<
", maxUnsignedIntVal = " << maxUnsignedIntVal
547 <<
", asSafe (maxVal) = " << asSafe<UnsignedIntType> (maxVal)
550 std::cerr <<
"(asSafe threw an exception)" << std::endl;
561 "Hey, how come val == 42? It should be something completely different. "
563 <<
"FYI, static_cast<" << TypeNameTraits<UnsignedIntType>::name ()
564 <<
"> (minVal) = " << static_cast<UnsignedIntType> (minVal)
567 <<
"static_cast<" << TypeNameTraits<UnsignedIntType>::name ()
568 <<
"> (maxVal) = " << static_cast<UnsignedIntType> (maxVal)
569 <<
". val should be equal to the latter."
571 <<
"As float: minVal = " << minVal <<
", maxVal = " << maxVal <<
".");
662 std::ostringstream os;
663 const FirstIntType origVal = 42;
664 const SecondIntType origValSecond = 42;
666 SecondIntType asVal = 0, asSafeVal = 0;
668 TEST_NOTHROW(asSafeVal = asSafe<SecondIntType> (origVal));
674 FirstIntType backVal = 0, backSafeVal = 0;
676 TEST_NOTHROW(backSafeVal = asSafe<FirstIntType> (asSafeVal));
694 sizeof (SignedIntType) !=
sizeof (UnsignedIntType),
696 "Unit test Teuchos,asSafe,negativeSignedIntToUnsignedInt requires that the "
697 "two template parameters SignedIntType and UnsignedIntType have the same "
700 std::ostringstream os;
701 const SignedIntType origVal = -1;
703 UnsignedIntType asSafeVal = 0;
707 TEST_THROW(asSafeVal = asSafe<UnsignedIntType> (origVal), std::range_error);
712 UnsignedIntType negVal =
static_cast<UnsignedIntType
> (origVal);
713 SignedIntType backSafeVal = 0;
714 TEST_THROW(backSafeVal = asSafe<SignedIntType> (negVal), std::range_error);
727 std::ostringstream os;
728 const FirstSignedIntType origVal = -42;
732 ! std::numeric_limits<FirstSignedIntType>::is_signed ||
733 ! std::numeric_limits<SecondSignedIntType>::is_signed,
735 "Unit test Teuchos,as,negativeSignedIntToSignedInt requires that the "
736 "two template parameters FirstSignedIntType and SecondSignedIntType "
737 "both be signed built-in integer types.");
741 SecondSignedIntType asVal = 0, asSafeVal = 0;
742 TEST_NOTHROW(asVal = as<SecondSignedIntType> (origVal));
743 TEST_NOTHROW(asSafeVal = asSafe<SecondSignedIntType> (origVal));
748 FirstSignedIntType backVal = 0, backSafeVal = 0;
749 TEST_NOTHROW(backVal = as<FirstSignedIntType> (origVal));
750 TEST_NOTHROW(backSafeVal = asSafe<FirstSignedIntType> (origVal));
888 std::ostringstream os;
889 const int maxInt = std::numeric_limits<int>::max ();
893 if (
sizeof (
int) <
sizeof (
long)) {
894 const long maxIntPlusOne =
static_cast<long> (maxInt) + static_cast<long> (1);
900 TEST_THROW(intVal = asSafe<int> (os.str ()), std::range_error);
924 std::ostringstream os;
925 const int minInt = std::numeric_limits<int>::min ();
929 if (
sizeof (
int) <
sizeof (
long)) {
930 const long minIntMinusOne =
static_cast<long> (minInt) - static_cast<long> (1);
931 os << minIntMinusOne;
936 TEST_THROW(intVal = asSafe<int> (os.str ()), std::range_error);
960 std::ostringstream os;
961 os << static_cast<IntegerType> (42);
972 std::ostringstream os;
973 os << static_cast<IntegerType> (-42);
984 std::ostringstream os;
985 os <<
"This string definitely does not contain an integer.";
987 TEST_THROW(val = asSafe<IntegerType> (os.str ()), std::invalid_argument);
995 #define UNIT_TEST_GROUP_ANY_INTEGER( AnyIntegerType ) \
996 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( asSafe, stringToIntegerPositive, AnyIntegerType ) \
997 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( asSafe, stringToIntegerShouldThrow, AnyIntegerType )
999 #define UNIT_TEST_GROUP_SIGNED_INTEGER( SignedIntegerType ) \
1000 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( asSafe, stringToIntegerNegative, SignedIntegerType )
1029 #ifdef HAVE_TEUCHOS_COMPLEX
1040 const std::complex<float> iF = std::complex<float>(0,1);
1041 const std::complex<double> iD = std::complex<double>(0,1);
1043 const std::complex<float> minF = -std::numeric_limits<float>::max ();
1044 const std::complex<float> minImagF = -iF*std::numeric_limits<float>::max ();
1045 const std::complex<float> minusOneF = -1;
1046 const std::complex<float> minusOneImagF = -iF;
1047 const std::complex<float> maxF = std::numeric_limits<float>::max ();
1048 const std::complex<float> maxImagF = iF*std::numeric_limits<float>::max ();
1050 const std::complex<double> minD = -std::numeric_limits<double>::max ();
1051 const std::complex<double> minImagD = -iD*std::numeric_limits<double>::max ();
1052 const std::complex<double> minusOneD = -1;
1053 const std::complex<double> minusOneImagD = -iD;
1054 const std::complex<double> maxD = std::numeric_limits<double>::max ();
1055 const std::complex<double> maxImagD = iD*std::numeric_limits<double>::max ();
1057 std::complex<float> valF = 0;
1059 std::complex<double> valD = 0;
1135 #endif // HAVE_TEUCHOS_COMPLEX
#define TEUCHOS_UNIT_TEST_TEMPLATE_2_INSTANT(TEST_GROUP, TEST_NAME, TYPE1, TYPE2)
Instantiate a templated unit test with two template parameters.
#define TEST_NOTHROW(code)
Asserr that the statement 'code' does not thrown any excpetions.
TypeTo asSafe(const TypeFrom &t)
Convert from one value type to another, with validity checks if appropriate.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST_TEMPLATE_2_DECL(TEST_GROUP, TEST_NAME, TYPE1, TYPE2)
Macro for defining a templated unit test with two template parameters.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
#define TEST_NOTHROW_WITH_MESSAGE(code)
#define UNIT_TEST_GROUP_ANY_INTEGER(AnyIntegerType)
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Default traits class that just returns typeid(T).name().
Definition of Teuchos::as, for conversions between types.
#define UNIT_TEST_GROUP_SIGNED_INTEGER(SignedIntegerType)