Main Content

Generate C/C++ Tests for Scripts Calling Functions

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). If a function is not supported for automatic test generation due to incompatibility with graphical test authoring, you can write a C/C++ script calling the function and generate scripted tests from that script.

This example shows how to generate scripted tests from a C/C++ script. For an introduction to scripted tests, see Test C/C++ Functions by Using Scripts in Graphical Tests.

Example Files

This tutorial uses the files in the folder polyspaceroot\polyspace\examples\doc_pstest\coverage_tests\src. Here, polyspaceroot is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026a. To continue with this tutorial:

  1. Create a new Polyspace Platform project and add the folder to the project.

  2. Select Parse Code on the toolstrip to analyze the files in the folder.

  3. Open the project configuration. On the Projects tab, add the folder polyspaceroot\polyspace\examples\doc_pstest\coverage_tests\src for the option Include paths.

    Even though the source folder is automatically added to the include search path for building sources, it is not added to the search path for building scripts in tests. In this example, you will be using definitions from source headers in test scripts. Therefore, you must add the source folder explicitly as an include path so that it is used for include search when building test scripts.

In this example, you generate scripted tests for the function checkAgainstSpeedLimit() in the file helpers.c:

#include "decls.h"

bool checkAgainstSpeedLimit(uint32_t speedReading, uint32_t limit) {
    bool speedOk;
    speedOk = 1;
    if(speedReading > SPEED_UPPER_BOUND || limit > SPEED_UPPER_BOUND) {
        return 0;
    }
    return speedOk;
}

Specify Script and Generate Tests

Generate scripted tests for the function checkAgainstSpeedLimit() by specifying the script body and appropriate constraints on the generated tests:

  1. On the Polyspace Platform toolstrip, select Add Test > Generate Test(s) From Script (Coverage Metrics).

  2. On the Generate Tests dialog box, specify a script for test generation:

    • Preamble: Add the following include:

      #include "decls.h"
      
      This include file provides all function and type declarations that are required by your script, in particular, the declaration of the function checkAgainstSpeedLimit and the type bool.

    • Test Script Body: Add the following script:

      bool res = checkAgainstSpeedLimit(speed, limit);

  3. Add inputs for test generation:

    1. In the Inputs section, select Add input(s) button.

    2. Enter speed for Variable Name and select unsigned int for Variable Type.

    3. Repeat the previous steps for the other input limit (also an unsigned int type).

  4. Constrain the values of the inputs:

    • For the input speed, erase the entry in the Value column. Add 0u in the Min column and 200u in the Max column.

    • For the input limit, enter 70u in the Value column.

    The complete Generate Tests (coverage metrics) window looks like this:

    Generate Tests window with Preamble, Test Script Body and Inputs section filled.

  5. Set the Coverage level to Decision. For more information on coverage levels, see Coverage metrics (-cov-metric-level).

  6. Click OK. The Generating tests window shows the progress of test generation.

The test generation step first analyzes the script and ensures that all variables in the script are defined either in the Preamble, the Test Script Body, or the Inputs section. If a definition is not found, the test generation fails. Otherwise, once test generation is complete, below the Tests node of your project, you see one or two generated tests with names such as Generated Scripted Test 1.

Review Generated Tests

The test generation in the previous section generates scripted tests with appropriate input values to satisfy decision coverage objectives. To complete the generated scripted tests, add observables and assessments to the tests:

  1. Double-click a generated test name below the Tests node.

  2. In the Observables section of the test, select Add observable(s) button. Enter res for Variable Name and select unsigned int for Variable Type.

    This action enters the variable in the Assessments section of the test.

  3. In the Assessments section of the test, enter the value 1u if the value of the input speed is less than the input limit, otherwise enter 0u.

Repeat these steps for the other generated test (or the other step of the generated test). Built and run the generated tests to see passing test results.

See Also

Topics