[Cppunit-devel] CPPUNIT_ASSERT_EQUAL and different types
Brought to you by:
blep
From: King D. <Ki...@tc...> - 2001-10-19 18:53:52
|
I submitted bug #472263 about an issue like the following: CPPUNIT_ASSERT_EQUAL( 255, longVariable ); This will not compile. The problem is that 255 is an int constant and therefore has a different type than the long variable. The implementation under the macro is a template method that expects the expected and actual to have the same type. Since they have different types it will not use the template method. One can make it work by making the constant a long constant by appending an L, but it would be nice to not require users to have to remember this. I was told that I should discuss this issue on the mailing list as it has come up before. Therefore here is my proposal for a fix. My proposal is to split the template method to have 2 class template parameters. In my proposal I will use Te and Ta for the expected type and the actual type. Replace them with better names. This entails doing the same to the assertion_traits template. In that case I would split out the toString stuff from the equals test as follows: template <class Te, class Ta> struct assertion_traits { static bool equal( const Te& x, const Ta& y ) { return x == y; } }; template <class T> struct assertion_tostring { static std::string toString( const T& x ) { OStringStream ost; ost << x; return ost.str(); } }; Then you change the template method as follows: template <class Te, class Ta> void assertEquals( const Te& expected, const Ta& actual, long lineNumber = Exception::UNKNOWNLINENUMBER, std::string fileName = Exception::UNKNOWNFILENAME ) { if ( !assertion_traits<Te,Ta>::equal(expected,actual) ) { assertNotEqualImplementation( assertion_tostring<Te>::toString(expected), assertion_tostring<Ta>::toString(actual), lineNumber, fileName ); } } You also have to change the toString stuff in the double version. That seems to solve the issue. This could probably be cleaned up further, but hopefully this gets the idea across. |