BDE 4.14.0 Production release
|
Provide utilities to help with testing templates.
bslmf
typesWhen testing a container template having a type parameter, we need to ensure that the template supports its contractually specified categories of parameter types. The bsltf
package provides a representative set of types intended for testing that can be used as template parameters for doing this kind of verification.
Creating a separate test for each category of types supported by a template would be cumbersome. Instead, writing a single templatized test is usually preferable. Unfortunately, different types often require different syntaxes for constructing an object and getting an object's value. This inconsistency makes writing generic code rather difficult.
This component provides a solution with a utility struct
, TemplateTestFacility
, that defines two class method templates, create
and getIdentifier
, that respectively have consistent syntaxes for creating objects and getting a integer value representing the state of objects of a parameterized type.
This component also provides a macro, BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
, that serves as a convenient way to instantiate and invoke a template (for testing) having a type parameter for a specified list of types. In addition, this component provides a set of macros referring to commonly useful lists of types intended for testing that can be used as arguments to BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
.
The utilities and macros provided by this component, along with the types defined in the bsltf
package, are explained in more detail in the following sections.
The TemplateTestFacility
struct
provides the following static (class) method templates to construct objects and get the states of objects of a supported parameterized type (supported types are those types intended for testing defined in the bsltf
package):
create
: Return an object of the parameterized TYPE
whose value is uniquely associated with a specified integer identifier.getIdentifier
: Return the integer identifier used to create a specified object of the parameterized TYPE
.The BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
macro instantiates a specified class template for a specified list of types and call a specified class method of each instantiation. The macro takes in arguments in the following order:
This component also defines a set of macros, each providing a list of types, that can be used as the last argument to BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
. The following is a brief synopsis of these macros (note that all macros names have the BSLTF_TEMPLATETESTFACILITY_
prefix, which is omitted for layout efficiency):
TEST_TYPES_PRIMITIVE
: list of primitive typesTEST_TYPES_USER_DEFINED
: list of user-defined typesTEST_TYPES_REGULAR
: list of typically used typesTEST_TYPES_AWKWARD
: list of types with odd behaviorsTEST_TYPES_ALL
: list of all of the typesThe BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_PRIMITIVE
macro refers to a representative set of primitive types that are useful for testing:
The BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_USER_DEFINED
macro refers to a representative set of user-defined types that are useful for testing (note that all types described here belong to the bsltf
namespace, which is not explicitly qualified for layout efficiency):
The BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_REGULAR
macro refers to the union of the types provided by BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_PRIMITIVE
and BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_USER_DEFINED
. These types are designed to work within the regular operating conditions of a typical template. Typically, a test driver for a template instantiates its tests (using the BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
macro) for all of the types referred by BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_REGULAR
.
The BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_AWKWARD
macro refers to a set of types that have certain attributes that make them unlikely to work for all of the operations of a template. Typically, not all methods of a template are instantiable with these types, so these types are most often used independently in tests explicitly designed for a (single) type.
The BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_ALL
refers to all the test types provided in the bsltf
package.
This section illustrates intended use of this component.
In this example, we demonstrate how to use BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
to call a class method of a template for a list of types.
First, we define a struct
template TestTemplate
taking in a parameterized TYPE
that has a class method, printTypeName
:
Now, we can instantiate the TestTemplate
class for each of the types int
, char
, and double
, and call the printTypeName
class method of each instantiation using the BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
macro:
Finally, we observe the console output:
In this example, we demonstrate using the TemplateTestFacility
struct
and the macros provided by this component to test the default constructor and primary manipulator of a class template in the context of a typical BDE-style test driver. Note that a goal of the demonstrated test is to validate the class template with a broad range of types emulating those with which the template might be instantiated.
First, we define a simple class template, MyNullableValue
, that we will later need to test:
Then, we define some aliases for the micros that will be used by the test driver:
Next, we define a struct
template, MyTestDriver
, that provides a namespace containing the test cases (here, only testCase2
is defined for brevity) of the test driver:
Now, we define the implementation of MyTestDriver::testCase2
:
Notice that, we create objects of the parameterized TYPE
using the TemplateTestFacility::create
class method template specifying an integer identifier; the created object has a value that is uniquely associated with the integer identifier.
Also notice that we verified that an object of the parameterized TYPE
has the expected value in two ways:
TemplateTestFacility::getIdentifier
class method template (specifying the object), and (2) the integer identifier uniquely associated with the expected state of the object.TYPE
. Note that the equality-comparison operator is defined for all types intended for testing in the bsltf
package except for bsltf::NonEqualComparableTestType
.Finally, we use the BSLTF_TEMPLATETESTFACILITY_RUN_EACH_TYPE
macro to instantiate MyTestDriver
for each of the types listed in BSLTF_TEMPLATETESTFACILITY_TEST_TYPES_REGULAR
and invoke the testCase2
class method of each instantiation: