주요 콘텐츠

Test C++ Member Functions of Classes with Non-Default Initialization

This example shows how to author graphical tests for C++ member functions when the class default constructor does not initialize all data members. For instance, you might want to test member functions of classes that require one or more of the following initialization strategies:

  • A non-default constructor might be used for initialization.

  • Some public data members might be initialized after the constructor call.

Prerequisites

The example assumes that you are familiar with the basics of graphical test authoring in the Polyspace® Platform user interface. For more information on:

Example Files

This tutorial uses the files in the folder polyspaceroot\polyspace\examples\doc_pstest\cpp_tabular_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.

This folder contains source code defining a class Sensor with member functions readData() and checkStatus():

  • Sensor.hpp:

    #ifndef SENSOR_H
    #define SENSOR_H
    
    #include <string>
    
    class Sensor {
    public:
        bool calibrated;
        double minThreshold;
        double maxThreshold;
        std::string sensorID;
        std::string sensorType;
        // Constructor
        Sensor();
        Sensor(const std::string& id, const std::string& type);
    
        // Method to read current sensor value
        void readData(double data);
        
        // Method to check status
        bool checkStatus(void);
        
    private:
        double currentVal;
    };
    
    #endif // SENSOR_H

  • Sensor.cpp:

    #include "Sensor.hpp"
    
    // Default constructor
    Sensor::Sensor() {}
    // Constructor that initializes mandatory fields
    Sensor::Sensor(const std::string& id, const std::string& type)
        : sensorID(id), sensorType(type), currentVal(0.0), calibrated(false), 
          minThreshold(0.0), maxThreshold(0.0) {}
    
    // Simulate reading data from the sensor
    void Sensor::readData(double data) {
        currentVal = data;
    }
    
    // Check status before proceeding
    bool Sensor::checkStatus(void) {
        if(!calibrated || currentVal < minThreshold || currentVal > maxThreshold) {
            return false;
        }
        return true;
    }

Note that the constructor Sensor::Sensor(const std::string&, const std::string&) requires some arguments to set the values of some data members but default-initializes the other members. Since some of the class members are public, you can also initialize them directly after object construction.

Write Test with Non-Default Initialization of Object

Write a test that initializes an object of the Sensor class using the constructor Sensor::Sensor(const std::string&, const std::string&).

  1. On the Projects pane, right-click the function Sensor::checkStatus() and select Add Test Case

  2. Set the inputs and assessments in the newly created test.

    1. In the Inputs section of the test, set the value of the object containing the member function Sensor::checkStatus() as follows:

      pst_obj (Sensor) = Sensor("TS-1001", "Temperature")

      • calibrated (bool) = true

      • minThreshold (double) = -10

      • maxThreshold (double) = 50

      • sensorID (std::string) = <<default>>

      • sensorType (std::string) = <<default>>

      Inputs section of test

      These inputs imply that an object of class Sensor is created with a call to the constructor:

      Sensor::Sensor(const std::string&, const std::string&)
      with values "TS-1001" and "Temperature". The public members calibrated, minThreshold and maxThreshold are then set to the values true, -10 and 50 respectively, while the members sensorID and sensorType are left at the default values set by the constructor call.

    2. In the Assessments section of the test, set the following assessment value:

      • pst_call_out (bool) == true

Build and run the tests in the project to see passing test results.

See Also

Topics