48 #define TEST_NOTHROW_WITH_MESSAGE( code ) \
50 (out) << "Test that code {"#code";} does not throw : "; \
52 (out) << "passes\n"; \
54 catch (std::exception& theException) { \
57 out << "\nException message for unexpected exception:\n\n"; \
59 Teuchos::OSTab l_tab(out); \
60 out << theException.what() << "\n\n"; \
72 typedef unsigned short unsigned_short_type;
73 typedef unsigned int unsigned_int_type;
74 typedef unsigned long unsigned_long_type;
76 typedef long long long_long_type;
77 typedef unsigned long long unsigned_long_long_type;
81 std::string valToString(
const T &val)
83 const int precision = std::numeric_limits<T>::digits10 + 10;
85 std::ostringstream os;
86 os.precision(precision);
99 const float minF = -std::numeric_limits<float>::max ();
100 const float minusOneF = -1;
101 const float maxF = std::numeric_limits<float>::max ();
103 const double minD = -std::numeric_limits<double>::max ();
104 const double minusOneD = -1;
105 const double maxD = std::numeric_limits<double>::max ();
109 long double valLD = 0;
200 const long double minLD = -std::numeric_limits<long double>::max ();
201 const long double minusOneLD = -1;
202 const long double maxLD = std::numeric_limits<long double>::max ();
289 const float minF = -std::numeric_limits<float>::max ();
290 const float minusOneF = -1;
291 const float maxF = std::numeric_limits<float>::max ();
293 out <<
"minF = " << minF <<
"\n";
294 out <<
"maxF = " << maxF <<
"\n";
296 const double minD = -std::numeric_limits<double>::max ();
297 const double minusOneD = -1;
298 const double maxD = std::numeric_limits<double>::max ();
300 out <<
"minD = " << minD <<
"\n";
301 out <<
"maxD = " << maxD <<
"\n";
324 const long double minLD = -std::numeric_limits<long double>::max ();
325 const long double minusOneLD = -1;
326 const long double maxLD = std::numeric_limits<long double>::max ();
328 out <<
"minLD = " << minLD <<
"\n";
329 out <<
"maxLD = " << maxLD <<
"\n";
333 long double valLD = 0;
336 out <<
"Testing string -> float conversions ...\n";
339 std::ostringstream os;
352 std::ostringstream os;
361 std::ostringstream os;
371 std::ostringstream os;
381 out <<
"Testing string -> float conversions that should throw ...\n";
384 std::ostringstream os;
387 TEST_THROW(valF = asSafe<float> (os.str ()), std::range_error);
390 std::ostringstream os;
393 TEST_THROW(valF = asSafe<float> (os.str ()), std::range_error);
397 out <<
"Testing string -> double conversions ...\n";
400 std::ostringstream os;
409 std::ostringstream os;
418 TEST_NOTHROW(valD = asSafe<double> (valToString(minusOneD)));
420 TEST_NOTHROW(valD = as<double> (valToString(minusOneD)));
428 const int sizeof_long_double =
sizeof(
long double);
429 const int sizeof_double =
sizeof(double);
430 const int max_exponent10_long_double = std::numeric_limits<long double>::max_exponent10;
431 const int max_exponent10_double = std::numeric_limits<double>::max_exponent10;
433 out <<
"sizeof_long_double = " << sizeof_long_double <<
"\n";
434 out <<
"sizeof_double = " << sizeof_double <<
"\n";
435 out <<
"max_exponent10_long_double = " << max_exponent10_long_double <<
"\n";
436 out <<
"max_exponent10_double = " << max_exponent10_double <<
"\n";
438 if (sizeof_long_double > sizeof_double
439 && max_exponent10_long_double > max_exponent10_double)
441 out <<
"Testing converting from 'long double' to 'double' that does not fit ...\n";
443 const long double maxTooBigLD_for_D =
static_cast<long double>(maxD) * 10.0;
444 const long double minTooBigLD_for_D =
static_cast<long double>(minD) * 10.0;
445 out <<
"maxTooBigLD_for_D = " << maxTooBigLD_for_D <<
"\n";
446 out <<
"minTooBigLD_for_D = " << minTooBigLD_for_D <<
"\n";
449 TEST_THROW(valD = asSafe<double>(valToString(minTooBigLD_for_D)), std::range_error);
450 out <<
"valD = " << valD <<
"\n";
453 std::ostringstream os;
455 TEST_THROW(valD = asSafe<double>(valToString(maxTooBigLD_for_D)), std::range_error);
456 out <<
"valD = " << valD <<
"\n";
475 out <<
"Testing string -> long double conversions ...\n";
478 std::ostringstream os;
487 std::ostringstream os;
496 std::ostringstream os;
522 const RealType minVal = -std::numeric_limits<RealType>::max ();
523 const RealType maxVal = std::numeric_limits<RealType>::max ();
525 SignedIntType val = 0;
526 if (
sizeof (SignedIntType) <
sizeof (RealType)) {
527 TEST_THROW(val = asSafe<SignedIntType> (minVal), std::range_error);
528 TEST_THROW(val = asSafe<SignedIntType> (maxVal), std::range_error);
549 const RealType minVal = -std::numeric_limits<RealType>::max ();
550 const RealType maxVal = std::numeric_limits<RealType>::max ();
551 const UnsignedIntType maxUnsignedIntVal =
552 std::numeric_limits<UnsignedIntType>::max ();
556 UnsignedIntType val = 42;
560 val == static_cast<UnsignedIntType> (maxVal),
562 "Dear test author, please pick a different marker value. "
563 "Please report this bug to the Teuchos developers.");
566 TEST_THROW(val = asSafe<UnsignedIntType> (minVal), std::range_error);
567 const RealType minusOne = -1;
568 TEST_THROW(val = asSafe<UnsignedIntType> (minusOne), std::range_error);
571 if (maxUnsignedIntVal < maxVal) {
572 TEST_THROW(val = asSafe<UnsignedIntType> (maxVal), std::range_error);
574 std::cerr << std::endl
575 <<
"*** RealType = " << TypeNameTraits<RealType>::name ()
576 <<
", UnsignedIntType = " << TypeNameTraits<UnsignedIntType>::name ()
577 <<
", maxVal = " << maxVal
578 <<
", maxUnsignedIntVal = " << maxUnsignedIntVal
579 <<
", asSafe (maxVal) = " << asSafe<UnsignedIntType> (maxVal)
582 std::cerr <<
"(asSafe threw an exception)" << std::endl;
593 "Hey, how come val == 42? It should be something completely different. "
595 <<
"FYI, static_cast<" << TypeNameTraits<UnsignedIntType>::name ()
596 <<
"> (minVal) = " << static_cast<UnsignedIntType> (minVal)
599 <<
"static_cast<" << TypeNameTraits<UnsignedIntType>::name ()
600 <<
"> (maxVal) = " << static_cast<UnsignedIntType> (maxVal)
601 <<
". val should be equal to the latter."
603 <<
"As float: minVal = " << minVal <<
", maxVal = " << maxVal <<
".");
694 std::ostringstream os;
695 const FirstIntType origVal = 42;
696 const SecondIntType origValSecond = 42;
698 SecondIntType asVal = 0, asSafeVal = 0;
700 TEST_NOTHROW(asSafeVal = asSafe<SecondIntType> (origVal));
706 FirstIntType backVal = 0, backSafeVal = 0;
708 TEST_NOTHROW(backSafeVal = asSafe<FirstIntType> (asSafeVal));
726 sizeof (SignedIntType) !=
sizeof (UnsignedIntType),
728 "Unit test Teuchos,asSafe,negativeSignedIntToUnsignedInt requires that the "
729 "two template parameters SignedIntType and UnsignedIntType have the same "
732 std::ostringstream os;
733 const SignedIntType origVal = -1;
735 UnsignedIntType asSafeVal = 0;
739 TEST_THROW(asSafeVal = asSafe<UnsignedIntType> (origVal), std::range_error);
744 UnsignedIntType negVal =
static_cast<UnsignedIntType
> (origVal);
745 SignedIntType backSafeVal = 0;
746 TEST_THROW(backSafeVal = asSafe<SignedIntType> (negVal), std::range_error);
759 std::ostringstream os;
760 const FirstSignedIntType origVal = -42;
764 ! std::numeric_limits<FirstSignedIntType>::is_signed ||
765 ! std::numeric_limits<SecondSignedIntType>::is_signed,
767 "Unit test Teuchos,as,negativeSignedIntToSignedInt requires that the "
768 "two template parameters FirstSignedIntType and SecondSignedIntType "
769 "both be signed built-in integer types.");
773 SecondSignedIntType asVal = 0, asSafeVal = 0;
774 TEST_NOTHROW(asVal = as<SecondSignedIntType> (origVal));
775 TEST_NOTHROW(asSafeVal = asSafe<SecondSignedIntType> (origVal));
780 FirstSignedIntType backVal = 0, backSafeVal = 0;
781 TEST_NOTHROW(backVal = as<FirstSignedIntType> (origVal));
782 TEST_NOTHROW(backSafeVal = asSafe<FirstSignedIntType> (origVal));
920 std::ostringstream os;
921 const int maxInt = std::numeric_limits<int>::max ();
925 if (
sizeof (
int) <
sizeof (
long)) {
926 const long maxIntPlusOne =
static_cast<long> (maxInt) + static_cast<long> (1);
932 TEST_THROW(intVal = asSafe<int> (os.str ()), std::range_error);
956 std::ostringstream os;
957 const int minInt = std::numeric_limits<int>::min ();
961 if (
sizeof (
int) <
sizeof (
long)) {
962 const long minIntMinusOne =
static_cast<long> (minInt) - static_cast<long> (1);
963 os << minIntMinusOne;
968 TEST_THROW(intVal = asSafe<int> (os.str ()), std::range_error);
992 std::ostringstream os;
993 os << static_cast<IntegerType> (42);
1004 std::ostringstream os;
1005 os << static_cast<IntegerType> (-42);
1006 IntegerType val = 0;
1016 std::ostringstream os;
1017 os <<
"This string definitely does not contain an integer.";
1018 IntegerType val = 0;
1019 TEST_THROW(val = asSafe<IntegerType> (os.str ()), std::invalid_argument);
1027 #define UNIT_TEST_GROUP_ANY_INTEGER( AnyIntegerType ) \
1028 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( asSafe, stringToIntegerPositive, AnyIntegerType ) \
1029 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( asSafe, stringToIntegerShouldThrow, AnyIntegerType )
1031 #define UNIT_TEST_GROUP_SIGNED_INTEGER( SignedIntegerType ) \
1032 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( asSafe, stringToIntegerNegative, SignedIntegerType )
#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)