Main Content

Incorrect test registration or definition

Test or test suite not defined, test not registered, and so on

Since R2023b

Description

This defect checker looks for the following issues related to test registration and definition:

  • Test defined but not registered

  • Test suite with no tests

  • Test suite that is neither declared nor defined

  • Test with a configuration but without a test body

  • Test that is registered but neither declared or defined

This checker is enabled if you specify the value pstunit for the option Libraries used (-library) or find defects in a test file from the Polyspace Platform (Polyspace Test) user interface. For more information, see Check for Bugs and Run-Time Errors in C/C++ Tests and Functions Under Test (Polyspace Test).

Risk

All these issues indicate high likelihood of a programming error. They indicate some omission when adding new tests or removing existing tests. For instance:

  • You might have defined a new test but forgot to register the test using PST_ADD_TEST (when using the C macro-based API).

  • You might have removed all tests in a suite but forgot to remove the suite itself leading to an empty suite.

Some of these issues can lead to link errors with a compiler error message that is difficult to understand. In some cases, the tests might not behave as expected. For instance, if you define a test but forget to register the test, the test will not run during execution of the corresponding test suite.

Fix

Depending on the root cause of the issue, follow one of these suggested fixes.

IssueSuggested Fix

Test defined but not registered

Register the test in a registration function using PST_ADD_TEST.

For more information, see Test Defined But Not Registered.

Test suite with no tests

Unless you meant to add a test, remove the test suite. Otherwise, define a test using PST_TEST_CONFIG and PST_TEST_BODY.

For more information, see Test Suite With No Tests.

Test suite that is neither declared nor defined

Define the test suite using PST_SUITE (suite with no configuration), PST_SUITE_CONFIG (suite with a configuration) or PST_SUITE_EXTERN (suite defined in a file different from the current test).

For more information, see Test Suite Neither Declared Nor Defined.

Test with a configuration but without a test body

Define a test body using PST_TEST_BODY.

For more information, see Test With Configuration But No Body.

Test that is registered but neither declared or defined

Unless you meant to define a test, remove the test registration. Otherwise, define a test using PST_TEST_CONFIG and PST_TEST_BODY.

For more information, see Test Registered But Not Declared or Defined.

Examples

expand all

In this example, the test TestCaseThree in the suite gSumTests is defined but the test is not registered in the registration function myRegFcn. This issue is flagged only in C code.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

Correction — Register Test

Register the test in the registration function myRegFcn using the PST_ADD_TEST macro. The corrected test file is shown below.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

In this example, the test suite gSumTestsSaturation is defined using PST_SUITE_CONFIG, but the test suite is not associated with any of the registered tests. Perhaps, the developer migrated all tests from this suite to the suite gSumTests but forgot to remove the suite itself.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_SUITE_CONFIG(gSumTestsSaturation) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    
    
Correction — Remove Empty Test Suite

Remove the test suite gSumTestsSaturation that is not associated with any test. The corrected test file is shown below.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

In this example, the test suite gSumTests is used in the definition of several tests, but the suite is never declared or defined.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    
    
Correction — Define Test Suite

If the test suite does not require suite-level configuration, declare the suite using PST_SUITE. Otherwise, define a suite configuration using PST_SUITE_CONFIG. The corrected test file shown below defines a suite configuration with a setup function setGSum() and a teardown function resetGSum().

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

In this example, a test configuration is defined for the test TestCaseThree in the suite gSumTests but the test does not have a body. This issue manifests as a compilation error in C++ but not in C code.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    
    
    
    
Correction — Define Test Body

If you did not mean to define the test, remove the test configuration. Otherwise, define a test body using the PST_TEST_BODY macro. The corrected test file below shows a test body for the test TestCaseThree in the suite gSumTests.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

In this example, the test TestCaseThree in the suite gSumTests is added in the registration function myRegFcn, but the test is neither declared nor defined. his issue manifests as a compilation error in C++ but not in C code.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    
    
Correction — Add Test Definition

If you did not mean to define the test, remove the test registration. Otherwise, define a test using the PST_TEST_CONFIG and PST_TEST_BODY macro. The corrected test file below shows a test definition for the test TestCaseThree in the suite gSumTests.

  • Header with type definitions (named example.h):

    #include <limits.h>
    extern unsigned gSum;
    
    typedef enum {
        NOT_SATURATED = 0,
        SATURATED = 1
    } SUM_STATUS;
    
    SUM_STATUS globalSum(unsigned x);
  • Source:

    #include "example.h"
    
    unsigned gSum;
    
    SUM_STATUS globalSum(unsigned x) {
        if (x > UINT_MAX - gSum) {
            gSum = UINT_MAX;
            return SATURATED;
        }
        gSum += x;
        return NOT_SATURATED;
    }
  • Tests:

    #include <pstunit.h>
    #include "example.h"
    
    //Test utilities
    void setGSum (void) {
        gSum = 0;
    }
    
    void resetGSum (void) {
        gSum = 0;
    }
    
    
    PST_SUITE_CONFIG(gSumTests) {
        PST_SUITE_TEST_SETUP(setGSum);
        PST_SUITE_TEST_TEARDOWN(resetGSum);
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseOne){}
    PST_TEST_BODY(gSumTests, TestCaseOne) {
        unsigned test_data = 1;
        unsigned expected_gSum = 1;
        int  i;
        
        for(i = 1; i<= 1; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in nonsaturating sum.");
          
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseTwo){}
    PST_TEST_BODY(gSumTests, TestCaseTwo) {
        unsigned test_data = 1;
        unsigned expected_gSum = 2;
        int i;
        
        for(i = 1; i<= 2; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is just above limit.");
    
    }
    
    PST_TEST_CONFIG(gSumTests, TestCaseThree){}
    PST_TEST_BODY(gSumTests, TestCaseThree) {
        unsigned test_data = 1;
        unsigned expected_gSum = 3;
        int i;
            
        for(i = 1; i<= 3; i++)    
                globalSum(test_data);
        
        PST_VERIFY_EQ_INT_MSG(gSum, expected_gSum, "Issue in sum that is well above limit.");
    }
    
    
    PST_REGFCN(myRegFcn) {
        PST_ADD_TEST(gSumTests, TestCaseOne);
        PST_ADD_TEST(gSumTests, TestCaseTwo);
        PST_ADD_TEST(gSumTests, TestCaseThree);
    }
    
    #ifndef PSTEST_BUILD
    int main(int argc, char *argv[]) {
        PST_REGFCN_CALL(myRegFcn);
        return PST_MAIN(argc, argv);
    }
    #endif
    

Result Information

Group: Libraries Misuse
Language: C | C++
Default: Off
Command-Line Syntax: PSTUNIT_MISUSE_REGISTRATION
Impact: Medium

Version History

Introduced in R2023b