Discussion:
Boost.Test - how to print the value of a variable in case of error?
Attila Feher F
2010-06-24 05:19:08 UTC
Permalink
Hi,

I am testing "stuff" in a loop, and the "failure message" from Boost.Test is not too useful, because it does not tell me the "loop variable" value. I have hit this "problem" several times already, not only in loops, but any times a variable is involved in a tested expression. And I was not able to figure out what to do. I've looked at the sources etc., but I am too stupid to see if there is a solution in there. I haven't seen anything in the documentation. :(

Imagine this:

BOOST_CHECK_EQUAL(cont.erase(key),1u);

If it fails, it tells me this:

basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]

I cannot see the value of the 'key' variable, so it quite hard to know what that error is. In case of simple code, it may not be a problem to track down what key is, but when it is a loop of over 100000 iterations... :(

..._CHECKPOINT does not work, because it is only printed in case of an exception, but not other failures. :(

I was hoping to find something like:

BOOST_CHECK_EQUAL_WITH(cont.erase(key),1u, (key) );

which would say:

basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1] with key == 12

Is there a way I could do that? I have tried to ask in #BOOST on IRC, but it seems there are only bots there. :(

TIA,
Attila
Steven Watanabe
2010-06-24 14:08:02 UTC
Permalink
AMDG
Post by Attila Feher F
I am testing "stuff" in a loop, and the "failure message" from Boost.Test is not too useful, because it does not tell me the "loop variable" value. I have hit this "problem" several times already, not only in loops, but any times a variable is involved in a tested expression. And I was not able to figure out what to do. I've looked at the sources etc., but I am too stupid to see if there is a solution in there. I haven't seen anything in the documentation. :(
BOOST_CHECK_EQUAL(cont.erase(key),1u);
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]
I cannot see the value of the 'key' variable, so it quite hard to know what that error is. In case of simple code, it may not be a problem to track down what key is, but when it is a loop of over 100000 iterations... :(
..._CHECKPOINT does not work, because it is only printed in case of an exception, but not other failures. :(
BOOST_CHECK_EQUAL_WITH(cont.erase(key),1u, (key) );
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1] with key == 12
Is there a way I could do that? I have tried to ask in #BOOST on IRC, but it seems there are only bots there. :(
Have you looked at
http://www.boost.org/libs/test/doc/html/utf/testing-tools/custom-predicate.html

In Christ,
Steven Watanabe
Attila Feher F
2010-06-28 03:53:42 UTC
Permalink
Steven Watanabe wrote:
[SNIP]
Post by Steven Watanabe
Post by Attila Feher F
..._CHECKPOINT does not work, because it is only printed in case of
an exception, but not other failures. :(
BOOST_CHECK_EQUAL_WITH(cont.erase(key),1u, (key) );
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u
failed [0 != 1] with key == 12
Is there a way I could do that? I have tried to ask in #BOOST on
IRC, but it seems there are only bots there. :(
Have you looked at
http://www.boost.org/libs/test/doc/html/utf/testing-tools/custom-predicate.html
I have, but I am looking for a simple solution that would not change the look of the test code too much. That is something I wish to avoid. Especially if it means changing the test asserts to something that needs to be documented and taught to all (150+ people).

Attila
legalize+ (Richard)
2010-06-24 15:34:28 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Attila Feher F
BOOST_CHECK_EQUAL(cont.erase(key),1u);
First, I recommend use of BOOST_REQUIRES_xxx instead of BOOST_CHECK_xxx.
Post by Attila Feher F
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]
If you use BOOST_CHECK_MESSAGE(predicate, message), you can create a
custom failure message that contains additional context:

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

#include <cmath>

BOOST_AUTO_TEST_CASE( test )
{
double res = std::sin( 45. );

BOOST_WARN_MESSAGE( res > 1,
"sin(45){" << res << "} is <= 1. Hmm.. Strange. " );
}
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
Attila Feher F
2010-06-28 05:08:56 UTC
Permalink
Richard wrote:
[SNIP]
Post by legalize+ (Richard)
Post by Attila Feher F
basics.cpp(61): error in "test_case": check cont.erase(key) == 1u failed [0 != 1]
If you use BOOST_CHECK_MESSAGE(predicate, message), you can create a
#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
#include <cmath>
BOOST_AUTO_TEST_CASE( test )
{
double res = std::sin( 45. );
BOOST_WARN_MESSAGE( res > 1,
"sin(45){" << res << "} is <= 1. Hmm.. Strange. " ); }
Thanks! I was doing that earlier, but I was hoping there is a "better" solution. With this, I have to do all the things "by hand", so for example the spelling out of the two sides of an EQUAL check. :( It seems that I have to "duplicate" all existing macros and create something like a BOOST_CHECK_..._WITH(...) series.

Eg.:

BOOST_CHECK_EQUAL_WITH(lhs,rhs,v1,v2)

used as:

BOOST_CHECK_EQUAL_WITH(a[i],b[j],i,j)

output:

tests.cpp(99): error in "test_case": check a[i] == b[j] [with i==1, j==42] failed [0 != 1]

BR,
Attila aka WW
legalize+ (Richard)
2010-06-28 05:11:26 UTC
Permalink
[Please do not mail me a copy of your followup]
Post by Attila Feher F
Post by legalize+ (Richard)
BOOST_WARN_MESSAGE( res > 1,
"sin(45){" << res << "} is <= 1. Hmm.. Strange. " ); }
Thanks! I was doing that earlier, but I was hoping there is a "better"
solution.
Yeah, most assertion frameworks have some way to optionally provide a
message. Since Boost.Test's assertions are done with macros, there
isn't a way to provide an overloaded version that supplies a
contextual message. I'm afraid the best you can do is use
BOOST_REQUIRE_MESSAGE() or provide your own assertion macros on top of
it.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
Continue reading on narkive:
Loading...