BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslim_testutil

Detailed Description

Provide test utilities for components above bsl.

Outline

Purpose

Provide test utilities for components above bsl.

Classes

Macros

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 {
// This utility class provides sample functionality to demonstrate how
// a test driver might be written validating its only method.
// CLASS METHODS
static int fortyTwo();
// Return the integer value 42.
};
// CLASS METHODS
inline
int ExampleUtil::fortyTwo()
{
return 42;
}
} // close package namespace

Then, we can write an elided test driver for this component. We start by providing the standard BDE assert test macro:

//=========================================================================
// 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:

//=========================================================================
// STANDARD BDE TEST DRIVER MACROS
//-------------------------------------------------------------------------
#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 // Quote identifier literally.
#define P BSLIM_TESTUTIL_P // Print identifier and value.
#define P_ BSLIM_TESTUTIL_P_ // P(X) without '\n'.
#define T_ BSLIM_TESTUTIL_T_ // Print a tab (w/o newline).
#define L_ BSLIM_TESTUTIL_L_ // current Line number

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)
// Test 'bdlabc::ExampleUtil::fortyTwo' in the specified 'verbose'
// verbosity level.
{
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:

value = 42

Macro Definition Documentation

◆ BSLIM_TESTUTIL_ASSERT

#define BSLIM_TESTUTIL_ASSERT (   X)     aSsErT(!(X), #X, __LINE__);

◆ BSLIM_TESTUTIL_ASSERTV

#define BSLIM_TESTUTIL_ASSERTV (   ...)
Value:
BSLIM_TESTUTIL_NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
#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__

current Line number

◆ BSLIM_TESTUTIL_LOOP0_ASSERT

#define BSLIM_TESTUTIL_LOOP0_ASSERT    BSLIM_TESTUTIL_ASSERT

◆ BSLIM_TESTUTIL_LOOP1_ASSERT

#define BSLIM_TESTUTIL_LOOP1_ASSERT    BSLIM_TESTUTIL_LOOP_ASSERT

◆ BSLIM_TESTUTIL_LOOP2_ASSERT

#define BSLIM_TESTUTIL_LOOP2_ASSERT (   I,
  J,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << \
" (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,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << "\t" \
<< #K ": " << BSLIM_TESTUTIL_DEBUG_REP(K) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOP4_ASSERT

#define BSLIM_TESTUTIL_LOOP4_ASSERT (   I,
  J,
  K,
  L,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << "\t" \
<< #K ": " << BSLIM_TESTUTIL_DEBUG_REP(K) << "\t" \
<< #L ": " << BSLIM_TESTUTIL_DEBUG_REP(L) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOP5_ASSERT

#define BSLIM_TESTUTIL_LOOP5_ASSERT (   I,
  J,
  K,
  L,
  M,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << "\t" \
<< #K ": " << BSLIM_TESTUTIL_DEBUG_REP(K) << "\t" \
<< #L ": " << BSLIM_TESTUTIL_DEBUG_REP(L) << "\t" \
<< #M ": " << BSLIM_TESTUTIL_DEBUG_REP(M) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOP6_ASSERT

#define BSLIM_TESTUTIL_LOOP6_ASSERT (   I,
  J,
  K,
  L,
  M,
  N,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << "\t" \
<< #K ": " << BSLIM_TESTUTIL_DEBUG_REP(K) << "\t" \
<< #L ": " << BSLIM_TESTUTIL_DEBUG_REP(L) << "\t" \
<< #M ": " << BSLIM_TESTUTIL_DEBUG_REP(M) << "\t" \
<< #N ": " << BSLIM_TESTUTIL_DEBUG_REP(N) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOP7_ASSERT

#define BSLIM_TESTUTIL_LOOP7_ASSERT (   I,
  J,
  K,
  L,
  M,
  N,
  O,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << "\t" \
<< #K ": " << BSLIM_TESTUTIL_DEBUG_REP(K) << "\t" \
<< #L ": " << BSLIM_TESTUTIL_DEBUG_REP(L) << "\t" \
<< #M ": " << BSLIM_TESTUTIL_DEBUG_REP(M) << "\t" \
<< #N ": " << BSLIM_TESTUTIL_DEBUG_REP(N) << "\t" \
<< #O ": " << BSLIM_TESTUTIL_DEBUG_REP(O) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOP8_ASSERT

#define BSLIM_TESTUTIL_LOOP8_ASSERT (   I,
  J,
  K,
  L,
  M,
  N,
  O,
  V,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << "\t" \
<< #J ": " << BSLIM_TESTUTIL_DEBUG_REP(J) << "\t" \
<< #K ": " << BSLIM_TESTUTIL_DEBUG_REP(K) << "\t" \
<< #L ": " << BSLIM_TESTUTIL_DEBUG_REP(L) << "\t" \
<< #M ": " << BSLIM_TESTUTIL_DEBUG_REP(M) << "\t" \
<< #N ": " << BSLIM_TESTUTIL_DEBUG_REP(N) << "\t" \
<< #O ": " << BSLIM_TESTUTIL_DEBUG_REP(O) << "\t" \
<< #V ": " << BSLIM_TESTUTIL_DEBUG_REP(V) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOP_ASSERT

#define BSLIM_TESTUTIL_LOOP_ASSERT (   I,
 
)
Value:
if (!(X)) { bsl::cout << #I ": " << BSLIM_TESTUTIL_DEBUG_REP(I) << \
" (context)\n"; \
aSsErT(1, #X, __LINE__); }

◆ BSLIM_TESTUTIL_LOOPN_ASSERT

#define BSLIM_TESTUTIL_LOOPN_ASSERT (   N,
  ... 
)     BSLIM_TESTUTIL_LOOPN_ASSERT_IMPL(N, __VA_ARGS__)

◆ 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

#define BSLIM_TESTUTIL_P (   X)     bsl::cout << #X " = " << BSLIM_TESTUTIL_DEBUG_REP(X) << bsl::endl;

Print identifier and its value.

◆ BSLIM_TESTUTIL_P_

#define BSLIM_TESTUTIL_P_ (   X)     bsl::cout << #X " = " << BSLIM_TESTUTIL_DEBUG_REP(X) << ", " << bsl::flush;

P(X) without \n

◆ 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;

Print tab (w/o newline).