#3080

Reporting of Test

Openmr3screenopened 7mo ago

Describe the bug I catched a bug in my code by some test, but the error message was a little misleading:

FAILED:
[ctest]   CHECK_THAT( some_uint8_t, WithinAbs(5.5, 0.5) )
[ctest] with expansion:
[ctest]   '2' is within 0.5 of 5.5

I wondered, where the 2 could have possibly come from, until I ran the test in the VSCode debugger, which automatically prints next to each uint8_t variable its numeric value and it's ascii character.

'2' is a character, but we're doing a number comparison, so printing a character is only misleading.

Expected behavior

FAILED:
[ctest]   CHECK_THAT( some_uint8_t, WithinAbs(5.5, 0.5) )
[ctest] with expansion:
[ctest]   50 is within 0.5 of 5.5

Reproduction steps Something like

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
TEST("misleading error message", "") {
    uint8_t var = 50;
    CHECK_THAT( var, WithinAbs(5.5, 0.5) );
}

Platform information:

  • OS: ubuntu 24.04
  • Compiler+version: gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
  • Catch version: Catch2 v3.4.0

Activity

4
ms-10-us7mo ago

I am interested in working on this bug. Please assign it to me. Thanks!

mr3screen7mo ago

I am interested in working on this bug. Please assign it to me. Thanks!

Hello @ms-10-us, if you are asking me: I am not able to assign bugs to someone because I am not related to this project. If you're asking a maintainer of this project, you might want to mention them directly, like I mentioned you.

Regardless of that - not knowing any specifics about the development rules of catch2 - I believe you don't have to wait until you get this issue assigned to you to start working on it. Just start and create a pull request referencing this issue.

andrewauclair7mo ago

std::uint8_t is typically a typedef for unsigned char which is in turn interpreted as a character by operator<<. To avoid this display issue, cast to an int in the check.

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
TEST("misleading error message", "") {
    uint8_t var = 50;
    CHECK_THAT( (int)var, WithinAbs(5.5, 0.5) );
}
mr3screen7mo ago

That's unfortunate. My only idea is to add a warning to the Catch2 docs for this case.