主要内容

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) {
   //....
}
For an 8-bit 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:

  1. On the Projects pane, right-click the function name, below the Code node of the project and select Generate Tests (boundary values).

  2. Change the default options for test generation if needed and click OK.

    OptionDescription
    • 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 func with this signature:

    void func(char x, unsigned char y) {
       //....;
    }
    
    For an 8-bit char type:

    • In the minimal testing strategy, at least one generated test calls the function with x set to -128 and another with x set to 127. At least one test calls the function with y set to 0 and another with y set to 255.

      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, min is the minimum possible value and max is the maximum possible value of the function input.

    Include 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 eps such that 1.0 + eps != 1.0.

    If you select this option, the generated tests cover values in {min, -eps, eps, max} for each floating-point input. Here, min is the minimum possible value and max is the maximum possible value of the function input. eps is the smallest representable floating-point value for the given input type.

    Include 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 {min, min + 1, max - 1, max} for each function input. Here, min is the minimum possible value and max is the maximum possible value of the function input.

  3. Follow 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.

  4. 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.

See Also

Topics