2 ms·
Although hacky, assert(test_foo() /* Checks the sanity of Foo */); does the job for the error messages, and replacing abort() with a custom function like
by vortico 10y ago
Although hacky,
assert(test_foo() /* Checks the sanity of Foo */);
does the job for the error messages, and replacing abort() with a custom function like `void check(int condition, const char *format, ...);` can make it not abort the program on an error.
I prefer the regular assert() though because I want the build to fail by an aborting test program, and all I care is the line number of failure. Another benefit is that abort() will pause gdb in the exact place that it crashed, with the stack intact if you want to inspect it.
- cormacrelf 10y agoYou can also use the comma operator to document your assert()s, if you don't like using comments. assert(("test_foo() should be true", test_foo())); https://en.wikipedia.org/wiki/Comma_operator#Asserts https://en.wikipedia.org/wiki/Comma_operator#Asserts
- gpderetta 10y agoI have used often and also seen in the wild the following form: assert(condition && "message");