the Compartmented Robust Posix C++ Unit Test system |
|
---|
Verify 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.
Note | |
---|---|
If you use GCC version 4.3 or higher, and compile your test sources with -std=c++0x, there is no limit to the number of parameters. |
The check 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 marked as failed, but continues execution
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.
See also:
ASSERT_PRED(pred, ...)
Example: The test program
#include <crpcut.hpp> #include <cstring> TEST(verify_pred_succeeds) { char non_zero_string[] = "s"; VERIFY_PRED(std::strlen, non_zero_string); } TEST(verify_pred_fails) { char haystack[] = "a haystack string"; char needle[] = "pqr"; VERIFY_PRED(std::strspn, haystack, needle); VERIFY_PRED(std::strspn, haystack, "hey"); } int main(int argc, char *argv[]) { return crpcut::run(argc, argv); }
reports one failed test with two failed
VERIFY_PRED
() checks:
FAILED!: verify_pred_fails fail--------------------------------------------------------------------------- samples/verify_pred_fails.cpp:41 VERIFY_PRED(std::strspn, haystack, needle) param1 = a haystack string param2 = pqr ------------------------------------------------------------------------------- fail--------------------------------------------------------------------------- samples/verify_pred_fails.cpp:42 VERIFY_PRED(std::strspn, haystack, "hey") param1 = a haystack string param2 = hey ------------------------------------------------------------------------------- phase="running" -------------------------------------------------------------- samples/verify_pred_fails.cpp:37 Earlier VERIFY failed ------------------------------------------------------------------------------- =============================================================================== 2 test cases selected Sum Critical Non-critical PASSED : 1 1 0 FAILED : 1 1 0
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,)
and
ASSERT_PRED(pred, ...)
.
Tip | |
---|---|
If you have a compiler that supports C++0x
lambda
expressions, lambdas make excellent predicates.
const char *refstr = get_from_a_function(); VERIFY_PRED([](const char *p) { return ::strcmp(p, "key") == 0; }, refstr); |