20 #include <boost/lexical_cast.hpp>
42 CPPUNIT_TEST_SUITE(mitkFloatToStringTestSuite);
47 CPPUNIT_TEST_SUITE_END();
50 template <
typename DATATYPE>
51 void ConfirmNumberToString(DATATYPE number,
const std::string &s)
54 CPPUNIT_ASSERT_EQUAL(boost::lexical_cast<std::string>(number), s);
57 template <
typename DATATYPE>
58 void ConfirmStringToNumber(
const std::string &s, DATATYPE number)
61 CPPUNIT_ASSERT_EQUAL(number, boost::lexical_cast<DATATYPE>(s));
64 template <
typename DATATYPE>
65 void ConfirmStringValues()
69 DATATYPE nan = boost::lexical_cast<DATATYPE>(
"nan");
70 CPPUNIT_ASSERT_MESSAGE(
"nan==nan must be false", !(nan == nan));
71 nan = boost::lexical_cast<DATATYPE>(
"NAN");
72 CPPUNIT_ASSERT_MESSAGE(
"NAN==NAN must be false", !(nan == nan));
74 std::string s_nan = boost::lexical_cast<std::string>(nan);
75 CPPUNIT_ASSERT_EQUAL(std::string(
"nan"), s_nan);
77 ConfirmStringToNumber(
"inf", std::numeric_limits<DATATYPE>::infinity());
78 ConfirmStringToNumber(
"INF", std::numeric_limits<DATATYPE>::infinity());
79 ConfirmStringToNumber(
"infinity", std::numeric_limits<DATATYPE>::infinity());
80 ConfirmStringToNumber(
"INFINITY", std::numeric_limits<DATATYPE>::infinity());
82 ConfirmStringToNumber(
"-inf", -std::numeric_limits<DATATYPE>::infinity());
83 ConfirmStringToNumber(
"-INF", -std::numeric_limits<DATATYPE>::infinity());
84 ConfirmStringToNumber(
"-infinity", -std::numeric_limits<DATATYPE>::infinity());
85 ConfirmStringToNumber(
"-INFINITY", -std::numeric_limits<DATATYPE>::infinity());
87 ConfirmNumberToString(std::numeric_limits<DATATYPE>::infinity(),
"inf");
88 ConfirmNumberToString(-std::numeric_limits<DATATYPE>::infinity(),
"-inf");
91 template <
typename DATATYPE>
92 void CheckRoundTrip(DATATYPE number, DATATYPE tolerance)
94 std::string s = boost::lexical_cast<std::string>(number);
95 DATATYPE number2 = boost::lexical_cast<DATATYPE>(s);
97 CPPUNIT_ASSERT_MESSAGE(std::string(
"Must not parse string ") + s +
" as NaN", number2 == number2);
100 CPPUNIT_ASSERT_EQUAL(number, number2);
104 CPPUNIT_ASSERT(
mitk::Equal(number, number2, tolerance));
108 template <
typename DATATYPE>
109 void CheckRoundTrip(
const std::string &input)
111 DATATYPE number = boost::lexical_cast<DATATYPE>(input);
112 std::string result = boost::lexical_cast<std::string>(number);
116 DATATYPE number2 = boost::lexical_cast<DATATYPE>(result);
120 template <
typename DATATYPE>
121 void TestConversions()
124 CheckRoundTrip<DATATYPE>(std::numeric_limits<DATATYPE>::infinity(), 0.0);
125 CheckRoundTrip<DATATYPE>(-std::numeric_limits<DATATYPE>::infinity(), 0.0);
127 CheckRoundTrip<DATATYPE>(std::numeric_limits<DATATYPE>::denorm_min(),
mitk::eps);
128 CheckRoundTrip<DATATYPE>(std::numeric_limits<DATATYPE>::epsilon(),
mitk::eps);
129 CheckRoundTrip<DATATYPE>(std::numeric_limits<DATATYPE>::lowest(),
mitk::eps);
132 CheckRoundTrip<DATATYPE>(sqrt(2),
mitk::eps);
133 CheckRoundTrip<DATATYPE>(0.000000042,
mitk::eps);
134 CheckRoundTrip<DATATYPE>(422345678.2345678,
mitk::eps);
135 CheckRoundTrip<DATATYPE>(0.0, 0);
136 CheckRoundTrip<DATATYPE>(-0.0, 0);
138 CheckRoundTrip<DATATYPE>(
"1");
139 CheckRoundTrip<DATATYPE>(
"1.1");
140 CheckRoundTrip<DATATYPE>(
"1.12121212");
141 CheckRoundTrip<DATATYPE>(
"1.1e-2");
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
MITKCORE_EXPORT const ScalarType eps