主要内容

Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds

Polyspace® Test™ supports two approaches for testing and profiling C/C++ code:

  • Project-based approach: Add your source code to a Polyspace Platform project and create tests in a tabular format or as code using the C/C++ xUnit API. Build both source files and tests using the project. You can also enable code profiling to collect metrics such as code coverage, execution time, memory use or run-time error occurrences.

  • Self-managed approach: Author tests using the Polyspace Test xUnit API and build them using your existing toolchain.

In the project-based approach, building sources and tests is managed using options in your project configuration. In contrast, the self-managed approach requires you to handle test builds and code profiling within your current build system. This topic describes the file paths you need for setting up self-managed builds.

File Paths to Include for Testing

The Polyspace Test xUnit API provides the following for test authoring:

  • Macros with names beginning with PST_

  • Classes with names beginning with pst_ (C++)

To build tests authored using the xUnit API, you need the following files and folders with the macro or class definitions.

  • Source file polyspaceroot\polyspace\pstest\pstunit\src\pstunit.c (or polyspaceroot\polyspace\pstest\pstunit\src\pstunit.cpp if you want the macros compiled as C++ code). In the examples on self-managed builds, this path is referred to as <PSTUNIT_SOURCE>.

  • Include folder polyspaceroot\polyspace\pstest\pstunit\include. In the examples on self-managed builds, this path is referred to as <PSTUNIT_INCLUDE>. This folder contains the header pstunit.h, which you will include in all your xUnit test files.

Here, polyspaceroot is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026a on Windows®. The list above uses Windows-style paths. Adjust them as needed for other operating systems.

File Paths to Include for Code Profiling

To collect code profiling metrics such as code coverage, execution time, memory use, or run-time error occurrences, you first instrument your source files using the polyspace-code-profiler command. The instrumentation step injects macros into copies of your source files and then compiles the instrumented source files instead of the original ones. To generate the final executable, you must link the compiled sources to a library containing definitions of the instrumentation macros.

For your convenience, Polyspace Test provides a precompiled library for certain compilers. The library path depends on the compiler you intend to use. In the examples on self-managed builds, this path is referred to as <PSPROFILELIB>.

  • Microsoft® Visual C++ (Windows): polyspaceroot\polyspace\psprofile\lib\win64\import\microsoft\libmwpsprofile_cli_runtime.lib

  • MinGW (Windows): polyspaceroot\polyspace\psprofile\lib\win64\import\mingw64\libmwpsprofile_cli_runtime.lib

  • GCC (Linux®): polyspaceroot/polyspace/psprofile/lib/glnxa64/static/libmwpsprofile_cli_runtime.a

Here, polyspaceroot is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026a on Windows. The list above uses Windows-style paths. Adjust them as needed for other operating systems.

If you do not use one of these compilers or the precompiled libraries do not work for you, you can also create a library by compiling the source files containing the instrumentation macros. For more information, see Create Library for Code Profiling Using Self-Managed Builds.

File Paths to Include for Code Profiling With Test Execution Monitoring

Polyspace Test supports a monitor mode for testing and code profiling where test execution is monitored and results converted into appropriate formats for viewing. This mode is particularly useful for test execution on targets where communication and data retrieval from targets is automatically handled (as long as the data communication happens over serial or TCP/IP ports). You execute tests in monitor mode by using the polyspace-test -monitor command.

To support code profiling in this mode, you need the following files and folders:

  • Source file polyspaceroot\polyspace\psprofile\src\psprofile.c. In the examples on self-managed builds, this file is referred to as <PSPROFILE_SOURCE>.

  • Include folder polyspaceroot\polyspace\psprofile\include. In the examples on self-managed builds, this file is referred to as <PSPROFILE_INCLUDE>.

Here, polyspaceroot is the Polyspace installation folder, for instance, C:\Program Files\Polyspace\R2026a on Windows. The list above uses Windows-style paths. Adjust them as needed for other operating systems.

Adding Files and Folders to Self-Managed Builds

To build your sources for testing, code profiling or test executable monitoring, you can refer to the above files in your compile commands, makefiles, or other build scripts. If you use the CMake package available with Polyspace Test to generate makefiles, you do not need to set these paths explicitly. Instead, you specify link targets and functions available with the CMake package. For more information, see Use CMake to Execute Tests Authored Using Polyspace Test xUnit API.

You can refer to these paths as variables in build scripts. The syntax for defining environment variables differs depending on the operating system and scripts:

  • In Shell scripts on UNIX®-like systems (Linux and Mac OS X), use the export command to define variables, for instance:

    export PSTUNIT_SOURCE="/usr/local/Polyspace/R2026a/polyspace/pstest/pstunit/src/pstunit.c"
    You reference the environment variable by prefixing its name with a dollar sign ($). For example, you can refer to the above environment variable PSTUNIT_SOURCE as $PSTUNIT_SOURCE.

  • In batch files on Windows, use the set command to define variables, for instance:

    set PSTUNIT_SOURCE="C:\Program Files\Polyspace\R2026a\polyspace\pstest\pstunit\src\pstunit.c"

    You reference the environment variable by enclosing its name in percent signs (%). For example, you can refer to the above environment variable PSTUNIT_SOURCE as %PSTUNIT_SOURCE%.

See Also

|

Topics