Generate C/C++ Tests for Boundary Values of Function Inputs
Using Polyspace® Test™, you can generate C/C++ unit tests with specific objectives instead of manually authoring tests. For instance, you can generate tests that call functions with boundary values for inputs, or tests that cover true and false outcomes of all conditions in a function (full condition coverage).
This topic shows how to generate tests that exercise boundary values of inputs. For information on how to generate tests for code coverage objectives, see Generate C/C++ Tests for Code Coverage Objectives.
Boundary-Value Testing
Boundary-value testing divides test input values into distinct regions and tests functions with representative values from each region. Typically, these values are taken from the boundary of the region because the functions to test are most likely to fail at the boundary.
Using Polyspace Test, you can generate a common class of boundary-value tests:
If a function has a single input, the generated boundary-value tests call the function with the minimum and maximum value of the input.
If a function has multiple inputs, the generated boundary-value tests call the function with minimum and maximum values of each input:
In the exhaustive testing strategy, the generated tests cover all possible combinations of minimum and maximum input values.
In the minimal testing strategy, the generated tests cover the minimum and maximum value of each input at least once.
The minimum and maximum values of an input are determined by the input data type and sizes of types that you define in the project configuration. For instance, if you generate tests for a function func with this signature:
void func (char x) {
//....
}char type, two boundary-value tests are generated: one that calls func with value -128 (or -28-1) and another with value 127 (or 28-1-1). For more information on how to specify sizes of types, see Processor.Generate Boundary-Value Tests
In the Polyspace Platform user interface, after you add files to a project and parse the source code, you can start generating tests. To generate tests for a function:
On the Projects pane, right-click the function name, below the Code node of the project and select Generate Tests (boundary values).
Change the default options for test generation if needed and click OK.
Option Description Inputs
Pointer Targets
Use these sections if you want to constrain the generated tests in some way. For instance, you can fix some of the input values while generating values for others, or fix the sizes of pointer targets. For more information, see Constrain Values and Target Sizes in Generated C/C++ Tests. Boundary Testing Mode Choose one of the following:
Minimal– Generate as few tests as possible while covering all boundary values. For multi-input functions, the generated tests cover boundary values of each function input at least once.Exhaustive– Generate enough tests to cover all boundary value combinations. For multi-input functions, the generated tests cover all possible boundary value combinations of the function inputs.
For instance, if you generate tests for a function
funcwith this signature:For an 8-bitvoid func(char x, unsigned char y) { //....; }chartype:In the minimal testing strategy, at least one generated test calls the function with
xset to -128 and another withxset to 127. At least one test calls the function withyset to 0 and another withyset to255.For instance, the generated tests might call the function with these input values:
x = -128, y = 0
x = 127, y = 255
In the exhaustive testing strategy, the generated tests call the function with these input values:
x = -128, y = 0
x = -128, y = 255
x = 127, y = 0
x = 127, y = 255
Include zero parameter values Generate tests that cover the zero value of each input parameter.
If you select this option:
For each function input with a signed type, the generated tests cover values in
{.min, 0,max}For each function input with an unsigned type, the generated tests cover values in
{0,.max}
Here,
is the minimum possible value andminis the maximum possible value of the function input.maxInclude smallest float values For each input with a floating-point type, generate tests that cover the smallest representable floating-point values. The smallest representable value is the smallest value
epssuch that1.0 + eps != 1.0.If you select this option, the generated tests cover values in
{for each floating-point input. Here,min, -eps,eps,max}is the minimum possible value andminis the maximum possible value of the function input.maxis the smallest representable floating-point value for the given input type.epsInclude off-by-one values For each input with an integer type, generate tests that cover values that are off-by-one from the boundary values.
If you select this option, the generated tests cover values in
{for each function input. Here,min,min+ 1,max- 1,max}is the minimum possible value andminis the maximum possible value of the function input.maxFollow the progress of test generation on the Generating tests window.
If tests are successfully generated, they appear below the Tests node of the project. The Logs pane states:
How many tests were generated.
How many test generation objectives were satisfied. For instance, if a function has two parameters and the minimal test generation strategy is used, the test generation attempts to satisfy four objectives. There must be at least one generated test where:
The first parameter takes its minimum value.
The first parameter takes its maximum value.
The second parameter takes its minimum value.
The second parameter takes its maximum value.
The four objectives might be satisfied by four or fewer tests.
The automatic test generation fills in the test inputs but leaves the assessments undefined. Fill the Assessments section of each generated test. See Specify Test Assessments in Polyspace Platform User Interface.
You can now build and run the generated tests just like manually authored tests. See Build and Run Tests in Polyspace Platform User Interface.