the Compartmented Robust Posix C++ Unit Test system |
|
---|
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.
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 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.
See also:
VERIFY_PRED(pred, ...)
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" -------------------------------------------------------------- samples/assert_pred_fails.cpp:41 ASSERT_PRED(std::strspn, haystack, needle) param1 = a haystack string param2 = pqr ------------------------------------------------------------------------------- =============================================================================== 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 VERIFY_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(); ASSERT_PRED([](const char *p) { return ::strcmp(p, "key") == 0; }, refstr); |