Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Handling errno in unit tests

ScottE33
ScottE33 Posts: 4

I have a routine that checks errno after calling exp(). The code compiles fine and works for static analysis. When I do a unit test, writing errno = 0 before the call generates a CPPTEST_ACCESS_VIOLATION error. Using MinGW compiler. Any suggestions?

Comments

  • ScottE33
    ScottE33 Posts: 4

    My stub:

    int errno; (I have also tried this as an extern with the same result)
    int errnoVal = 0;
    float expVal = 0.0;
    void CppTest_StubCallback_exp_sigmoid(CppTest_StubCallInfo* stubCallInfo, float *__return, float *x )
    {
    *__return = expVal;
    errno = errnoVal;
    }

  • ScottE33
    ScottE33 Posts: 4
    edited January 14

    Figured it out. Looked in errno.h. The definition of errno is:
    _CRTIMP __cdecl __MINGW_NOTHROW int *_errno(void);

    define errno (*_errno())

    This means I needed to do this as a stub:
    int errnoVal = 0;
    int errnoLoad = 0;
    void CppTest_StubCallback_errno(CppTest_StubCallInfo *stubCallInfo, int **__return)
    {
    *__return = &errnoVal;
    }

    Put in stub function to set errno: errnoVal = errnoLoad;

    and implement in the actual test:
    errnoVal = 0; // clear errrno
    errnoLoad = 100;
    CPPTEST_REGISTER_STUB_CALLBACK("_errno", &CppTest_StubCallback_errno);