print unsigned character in expansion

56 views
Skip to first unread message

Daniel Kotik

unread,
Jun 18, 2017, 7:41:33 AM6/18/17
to CATCH

Hello,


within my test application some variables are of type uint8_t which is a typedef for unsigned char. Now, if an assertion fails the expansion prints the character with the ASCII Code rather than the numeric value.
To overcome this issue I tried to overload the operator<< functions as follows

std::ostream & operator<<(std::ostream & os, std::uint8_t val)
{
    return os << static_cast<int>(val);
}

TEST_CASE( "print unsigned char", "")
{
    std::uint8_t val = 123;
    std::cout << val << std::endl;  // prints number 123 due to overloading operator<< --> great!
    CHECK( val == 124);              // expansion, however, gives corresponding  ASCII character --> still bad
}

where I have declared the operator<< function before including Catch's header. The code above produces the output

123
-------------------------------------------------------------------------------
print unsigned char
-------------------------------------------------------------------------------

tests.cpp:32
...............................................................................

tests.cpp:36: FAILED:
  CHECK( val == 124 )
with expansion:
  '{' == 124

The problem is sill present and I don't know how to workaround it. Any suggestions? Many thanks in advance.

// Daniel Kotik

Espen Albrektsen

unread,
Jun 29, 2017, 6:54:32 AM6/29/17
to CATCH
The workaround is to cast inside the CHECK:

CHECK((int)val == 124)

Catch also has support for operator overloading, but I was not successful when I tried that in my project. (That was a pretty old version of Catch, so picture may be different now...)
Message has been deleted

Daniel Kotik

unread,
Jul 3, 2017, 8:44:44 AM7/3/17
to CATCH
Hi,

im aware of this and other workarounds. My question, however, actually concerns whether it would be possible to achieve printing the numbers rather than the related ASCII characters for uin8_t integers in any expansion within Catch with juste a simple operator<< overload or with some other advanced technique. I have a lot of different uint8_t variables and I prefer not to clutter my code with annoying and likely redundant pieces of code.
Reply all
Reply to author
Forward
0 new messages