the Compartmented Robust Posix C++ Unit Test system | hosted by |
---|
Assert that a predicate evaluates to true
.
Used in: A test function body, the constructor or destructor of a
fixture, or a function called from
them.
See TEST(name, ...)
.
Requirement: pred
is callable with
...
as its parameter list, and returns a value
that can be evaluated as a bool.
...
may be 0
to
9
expressions.
The assertion succeeds if, and only if,
bool(pred(...))
returns a value that compares equal to true
.
On success the test function continues without side effects.
On failure the test is terminated with an error report. The
report includes the predicate and parameter list text, and the
parameter values that are output streamable have a text representation
of their value, and a byte by byte hex dump for the others. If the
pred
itself has an output stream operator, the output
of it is included for extra information.
Example: The test program
#include <crpcut.hpp> #include <cstring> TEST(assert_pred_succeeds) { char non_zero_string[] = "s"; ASSERT_PRED(std::strlen, non_zero_string); } TEST(assert_pred_fails) { char haystack[] = "a haystack string"; char needle[] = "pqr"; ASSERT_PRED(std::strspn, haystack, needle); } int main(int argc, char *argv[]) { return crpcut::run(argc, argv); }
reports one failed test:
FAILED: assert_pred_fails phase="running" -------------------------------------------------------------- /home/bjorn/devel/crpcut/doc-src/samples/assert_pred_fails.cpp:41 ASSERT_PRED(std::strspn, haystack, needle) param1 = a haystack string param2 = pqr ------------------------------------------------------------------------------- =============================================================================== Total 2 test cases selected UNTESTED : 0 PASSED : 1 FAILED : 1
See also: crpcut::match
<matcher>(...) for
floating point comparisons (
crpcut::abs_diff,
crpcut::relative_diff,
crpcut::ulps_diff,)
for matching regular expressions (
crpcut::regex,) and
for string collation (crpcut::collate.)
![]() | Tip |
---|---|
If you have a compiler that supports C++0x
lambda
expressions, lambdas make excellent predicates.
const char *refstr = get_from_a_function(); ASSERT_PRED([](const char *p) { return ::strcmp(p, "key") == 0; }, refstr); |