nlib

Defines a predicate assertion. More...

Go to the source code of this file.

Namespaces

 nn::nlib
 Implements stream-related classes usually commonly used, various containers, and other gadget classes. nlib_ns is an alias.
 
 nn::nlib::testing
 Framework to test C++ code. View the description of List of testing Library Features.
 

Macros

Test of Existing Boolean Functions (ASSERT_PREDn)

For functions that return a bool type, you can view the function arguments when the function fails by providing the function to the following macros.

Description
A simple example is provided below.
bool IsEven(int n) { return n % 2 == 0; }
TEST(Pred, test) {
ASSERT_PRED1(IsEven, 1 + 2);
}
/*
Output:
[ RUN ] pred.test
IsEven(1 + 2) evaluates to false, where
1 + 2 evaluates to 3
[ FAILED ] pred.test (0 ms)
*/
#define ASSERT_PRED1(pred, v1)   NLIB_TESTING_PRED1_(pred, v1, NLIB_TESTING_FATAL_)
 Evaluates whether pred(v1) is true. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED2(pred, v1, v2)   NLIB_TESTING_PRED2_(pred, v1, v2, NLIB_TESTING_FATAL_)
 Evaluates whether pred(v1, v2) is true. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED3(pred, v1, v2, v3)   NLIB_TESTING_PRED3_(pred, v1, v2, v3, NLIB_TESTING_FATAL_)
 Evaluates whether pred(v1, v2, v3) is true. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED4(pred, v1, v2, v3, v4)   NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, NLIB_TESTING_FATAL_)
 Evaluates whether pred(v1, v2, v3, v4) is true. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED5(pred, v1, v2, v3, v4, v5)   NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, NLIB_TESTING_FATAL_)
 Evaluates whether pred(v1, v2, v3, v4, v5) is true. If the test fails, control leaves the currently running function.
 
Test of Existing Boolean Functions (EXPECT_PREDn)

For functions that return a bool type, you can view the function arguments when the function fails by providing the function to the following macros.

#define EXPECT_PRED1(pred, v1)   NLIB_TESTING_PRED1_(pred, v1, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED1.
 
#define EXPECT_PRED2(pred, v1, v2)   NLIB_TESTING_PRED2_(pred, v1, v2, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED2.
 
#define EXPECT_PRED3(pred, v1, v2, v3)   NLIB_TESTING_PRED3_(pred, v1, v2, v3, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED3.
 
#define EXPECT_PRED4(pred, v1, v2, v3, v4)   NLIB_TESTING_PRED4_(pred, v1, v2, v3, v4, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED4.
 
#define EXPECT_PRED5(pred, v1, v2, v3, v4, v5)   NLIB_TESTING_PRED5_(pred, v1, v2, v3, v4, v5, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED5.
 
Customizing the Display by Defining a Test-Only Boolean Function (ASSERT_PRED_FORMATn)

Use the following macros to customize the message when ASSERT_PREDn fails.

Description
In the following simply written example, a function that returns the AssertionResult type is defined and tested using ASSERT_TRUE, etc.
::testing::AssertionResult IsEven(int n) {
if (n % 2 != 0)
return ::testing::AssertionFailure() << n << " is not even";
return ::testing::AssertionSuccess();
}
TEST(PredAssertionResult, test) {
ASSERT_TRUE(IsEven(1 + 2));
}
/*
Output:
[ RUN ] pred.test
Value of: IsEven(1 + 2)
Actual: false (3 is not even)
Expected: true
[ FAILED ] pred.test (0 ms)
*/
If you wish to further customize the code, use ASSERT_PREDn() as follows.
::testing::AssertionResult IsEven(const char* n_expr, int n) {
if (n % 2 != 0)
return ::testing::AssertionFailure() << n_expr << " is not even";
return ::testing::AssertionSuccess();
}
TEST(PredFormat, test) {
ASSERT_PRED_FORMAT1(IsEven, (1 + 2));
}
/*
Output:
[ RUN ] PredFormat.test
(1 + 2) is not even
[ FAILED ] PredFormat.test (0 ms)
[----------] 1 tests from PredFormat (0 ms total)
*/
#define ASSERT_PRED_FORMAT1(pred_format, v1)   NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, NLIB_TESTING_FATAL_)
 ASSERT_PRED1 may be used by the user to define the display at failure. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED_FORMAT2(pred_format, v1, v2)   NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, NLIB_TESTING_FATAL_)
 ASSERT_PRED2 may be used by the user to define the display at failure. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3)   NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, NLIB_TESTING_FATAL_)
 ASSERT_PRED3 may be used by the user to define the display at failure. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4)   NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, NLIB_TESTING_FATAL_)
 ASSERT_PRED4 may be used by the user to define the display at failure. If the test fails, control leaves the currently running function.
 
#define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5)   NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, NLIB_TESTING_FATAL_)
 ASSERT_PRED5 may be used by the user to define the display at failure. If the test fails, control leaves the currently running function.
 
Customizing the Display by Defining a Test-Only Boolean Function (EXPECT_PRED_FORMATn)

Use the following macros to customize the message when EXPECT_PREDn() fails.

#define EXPECT_PRED_FORMAT1(pred_format, v1)   NLIB_TESTING_PRED_FORMAT1_(pred_format, v1, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just likeASSERT_PRED_FORMAT1.
 
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)   NLIB_TESTING_PRED_FORMAT2_(pred_format, v1, v2, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just likeASSERT_PRED_FORMAT2.
 
#define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3)   NLIB_TESTING_PRED_FORMAT3_(pred_format, v1, v2, v3, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED_FORMAT3.
 
#define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4)   NLIB_TESTING_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED_FORMAT4.
 
#define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5)   NLIB_TESTING_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, NLIB_TESTING_NONFATAL_)
 Even if the test failed, control does not leave the currently running function, just like ASSERT_PRED_FORMAT5.
 

Detailed Description

Defines a predicate assertion.

Definition in file Pred.h.