Below is a problem my SQA engineer was having using
CPPUNIT_ASSERT_EQUAL with a numeric literal and a long
variable. I show how to eliminate this problem, but I
am submitting this bug report to see if maybe
something could be done to eliminate this potential
source of confusion. It is probably a common
occurrence for people to not be correct about int
versus long literals.
One way perhaps is to use 2 different types for the
template. There is no real requirement that the 2 be
exactly the same type, just that an == comparison is
valid, which it will be between and int and a long.
> I just got my simple versiontest example running. I
> couldn't get CPPUNIT_ASSERT_EQUAL to work. I think
> some sort of type cast problem. For example:
> CPPUNIT_ASSERT_EQUAL( 255,submin );
> produces the following preprocessor/compiler error
> H:\testversion.cpp(31) : error C2664: 'void __cdecl
> ass std::basic_string<char,struct
> std::char_traits<char>,class std::allocator<char>
> cannot convert parameter 4 from 'char ' to 'long'
> This conversion requires a reinterpret_cast,
> C-style cast or function-style cast
> Error executing cl.exe.
> Not sure why... Let me know if you have any ideas.
That is because what you are testing are longs and the
macros are calling a function that is overloaded to
have two different sets of parameters. One is a
template function, the other is for double. The
template version (which is the one you want in this
case) expects the two parameters to be the same type.
It may seem strange, but what you have there are two
different types. In C and C++ there is a difference
between int literals and long literals. What you have
done is give it an int literal and a long variable. It
says they are not the same so decides that you do not
want the template version and instead tries to use the
double version, but it doesn't have the right
parameters for that so you get a compiler error.
To get it to use the correct method you need to make
the values have the same type. There are 3 ways to do
that. The easiest way is simply to use a long literal
CPPUNIT_ASSERT_EQUAL( 255L, submin );
You can store the expected value in a variable as in
the following to guarantee it is long type.
long expected = 255;
CPPUNIT_ASSERT_EQUAL( expected,submin );
Or cast the second parameter to int so they are both
CPPUNIT_ASSERT_EQUAL( 255, (int)submin );
But I will report this as a bug to the cppunit project.
Log in to post a comment.