Provide test utilities for components above bsl.
Outline
Purpose
Provide test utilities for components above bsl.
Classes
Macros
- BSLIM_TESTUTIL_ASSERT(X): record and print error if
!X
- BSLIM_TESTUTIL_LOOP_ASSERT(I, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP2_ASSERT(I, J, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP3_ASSERT(I, J, K, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP4_ASSERT(I, J, K, L, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP5_ASSERT(I, J, K, L, M, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP6_ASSERT(I, J, K, L, M, N, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP7_ASSERT(I, J, K, L, M, N,O, X): print arguments if
!X
- BSLIM_TESTUTIL_LOOP8_ASSERT(I, J, K, L,M,N,O,V, X): print arguments if
!X
- BSLIM_TESTUTIL_ASSERTV(..., X): print generic arguments if
!X
- BSLIM_TESTUTIL_Q(X): quote identifier literally
- BSLIM_TESTUTIL_P(X): print identifier and value
- BSLIM_TESTUTIL_P_(X): print identifier and value without '
'
- BSLIM_TESTUTIL_L_: current line number
- BSLIM_TESTUTIL_T_: print tab without '
'
- See also
- bsls_bsltestutil
Description
This component provides the standard print macros used in BDE-style test drivers (ASSERT, LOOP_ASSERT, ASSERTV, P, Q, L, and T) for components above the bsl package group.
This component also defines a set of overloads for the insertion operator (<<) to support the streaming of test types defined in the bsltf package. These overloads are required for test drivers above the bsl package group in order to print objects of the bsltf types to bsl::cout.
This component also defines a pair of methods, setFunc and callFunc, that allow a test driver to set and call a function by going through another compilation unit to preclude the optimizer from inlining the function call.
Note that the 'bsltf' package resides below 'bsl+bslhdrs', in which 'bsl::cout' is defined; therefore, the components in 'bsltf' cannot directly define the overloads of the insertion operator to support printing the test types. Instead, an alternate method supplied in bsls_bsltestutil is used for test drivers in the 'bsl' package group.
Usage
This section illustrates intended use of this component.
Example 1: Writing a Test Driver
First, we write an elided component to test, which provides a utility class:
namespace bdlabc {
struct ExampleUtil {
static int fortyTwo();
};
inline
int ExampleUtil::fortyTwo()
{
return 42;
}
}
Then, we can write an elided test driver for this component. We start by providing the standard BDE assert test macro:
static int testStatus = 0;
static void aSsErT(bool b, const char *s, int i)
{
if (b) {
printf("Error " __FILE__ "(%d): %s (failed)\n", i, s);
fflush(stdout);
if (testStatus >= 0 && testStatus <= 100) ++testStatus;
}
}
Next, we define the standard print and 'LOOP_ASSERT' macros, as aliases to the macros defined by this component:
#define ASSERT BSLIM_TESTUTIL_ASSERT
#define LOOP_ASSERT BSLIM_TESTUTIL_LOOP_ASSERT
#define LOOP0_ASSERT BSLIM_TESTUTIL_LOOP0_ASSERT
#define LOOP1_ASSERT BSLIM_TESTUTIL_LOOP1_ASSERT
#define LOOP2_ASSERT BSLIM_TESTUTIL_LOOP2_ASSERT
#define LOOP3_ASSERT BSLIM_TESTUTIL_LOOP3_ASSERT
#define LOOP4_ASSERT BSLIM_TESTUTIL_LOOP4_ASSERT
#define LOOP5_ASSERT BSLIM_TESTUTIL_LOOP5_ASSERT
#define LOOP6_ASSERT BSLIM_TESTUTIL_LOOP6_ASSERT
#define LOOP7_ASSERT BSLIM_TESTUTIL_LOOP7_ASSERT
#define LOOP8_ASSERT BSLIM_TESTUTIL_LOOP8_ASSERT
#define ASSERTV BSLIM_TESTUTIL_ASSERTV
#define Q BSLIM_TESTUTIL_Q
#define P BSLIM_TESTUTIL_P
#define P_ BSLIM_TESTUTIL_P_
#define T_ BSLIM_TESTUTIL_T_
#define L_ BSLIM_TESTUTIL_L_
Now, using the (standard) abbreviated macro names we have just defined, we write a test function for the 'static' 'fortyTwo' method, to be called from a test case in the test driver:
void testFortyTwo(bool verbose)
{
const int value = bdlabc::ExampleUtil::fortyTwo();
if (verbose) P(value);
LOOP_ASSERT(value, 42 == value);
}
Finally, when 'testFortyTwo' is called from a test case in verbose mode we observe the console output:
◆ BSLIM_TESTUTIL_ASSERT
| #define BSLIM_TESTUTIL_ASSERT |
( |
|
X | ) |
aSsErT(!(X), #X, __LINE__); |
◆ BSLIM_TESTUTIL_ASSERTV
| #define BSLIM_TESTUTIL_ASSERTV |
( |
|
... | ) |
|
Value:
#define BSLIM_TESTUTIL_LOOPN_ASSERT(N,...)
Definition bslim_testutil.h:294
#define BSLIM_TESTUTIL_NUM_ARGS(...)
Definition bslim_testutil.h:287
◆ BSLIM_TESTUTIL_DEBUG_REP
| #define BSLIM_TESTUTIL_DEBUG_REP |
( |
|
X | ) |
BloombergLP::bslim::TestUtil::debugRep(X) |
◆ BSLIM_TESTUTIL_EXPAND
| #define BSLIM_TESTUTIL_EXPAND |
( |
|
X | ) |
X |
◆ BSLIM_TESTUTIL_L_
| #define BSLIM_TESTUTIL_L_ __LINE__ |
◆ BSLIM_TESTUTIL_LOOP0_ASSERT
◆ BSLIM_TESTUTIL_LOOP1_ASSERT
◆ BSLIM_TESTUTIL_LOOP2_ASSERT
| #define BSLIM_TESTUTIL_LOOP2_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
#define BSLIM_TESTUTIL_DEBUG_REP(X)
Definition bslim_testutil.h:200
◆ BSLIM_TESTUTIL_LOOP3_ASSERT
| #define BSLIM_TESTUTIL_LOOP3_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
K, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOP4_ASSERT
| #define BSLIM_TESTUTIL_LOOP4_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
K, |
|
|
|
L, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOP5_ASSERT
| #define BSLIM_TESTUTIL_LOOP5_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
K, |
|
|
|
L, |
|
|
|
M, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOP6_ASSERT
| #define BSLIM_TESTUTIL_LOOP6_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
K, |
|
|
|
L, |
|
|
|
M, |
|
|
|
N, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOP7_ASSERT
| #define BSLIM_TESTUTIL_LOOP7_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
K, |
|
|
|
L, |
|
|
|
M, |
|
|
|
N, |
|
|
|
O, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOP8_ASSERT
| #define BSLIM_TESTUTIL_LOOP8_ASSERT |
( |
|
I, |
|
|
|
J, |
|
|
|
K, |
|
|
|
L, |
|
|
|
M, |
|
|
|
N, |
|
|
|
O, |
|
|
|
V, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOP_ASSERT
| #define BSLIM_TESTUTIL_LOOP_ASSERT |
( |
|
I, |
|
|
|
X |
|
) |
| |
Value:
" (context)\n"; \
aSsErT(1, #X, __LINE__); }
◆ BSLIM_TESTUTIL_LOOPN_ASSERT
◆ BSLIM_TESTUTIL_LOOPN_ASSERT_IMPL
| #define BSLIM_TESTUTIL_LOOPN_ASSERT_IMPL |
( |
|
N, |
|
|
|
... |
|
) |
| BSLIM_TESTUTIL_EXPAND(BSLIM_TESTUTIL_LOOP ## N ## _ASSERT(__VA_ARGS__)) |
◆ BSLIM_TESTUTIL_NUM_ARGS
| #define BSLIM_TESTUTIL_NUM_ARGS |
( |
|
... | ) |
|
Value:
__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0, ""))
#define BSLIM_TESTUTIL_NUM_ARGS_IMPL(X8, X7, X6, X5, X4, X3, X2, X1, X0, N,...)
Definition bslim_testutil.h:283
#define BSLIM_TESTUTIL_EXPAND(X)
Definition bslim_testutil.h:280
◆ BSLIM_TESTUTIL_NUM_ARGS_IMPL
| #define BSLIM_TESTUTIL_NUM_ARGS_IMPL |
( |
|
X8, |
|
|
|
X7, |
|
|
|
X6, |
|
|
|
X5, |
|
|
|
X4, |
|
|
|
X3, |
|
|
|
X2, |
|
|
|
X1, |
|
|
|
X0, |
|
|
|
N, |
|
|
|
... |
|
) |
| N |
◆ BSLIM_TESTUTIL_P
Print identifier and its value.
◆ BSLIM_TESTUTIL_P_
◆ BSLIM_TESTUTIL_Q
| #define BSLIM_TESTUTIL_Q |
( |
|
X | ) |
bsl::cout << "<| " #X " |>" << bsl::endl; |
Quote identifier literally.
◆ BSLIM_TESTUTIL_T_
| #define BSLIM_TESTUTIL_T_ bsl::cout << "\t" << bsl::flush; |