Submit and vote on feature ideas.

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.

How to add a stub for a Class member function?

sorel Posts: 1

For example, class A has a public function foo().

class A {
int Foo();

I add the stub like this:

void CppTest_Stub_Foo() {
int __return = 0;
CPPTEST_STUB_CALLBACK(int __return, const A __this);

But it throws execution error. The detail in real project execution is as follows.

1. Test execution: error compiling file.
during IPA pass: profile
TEST_HARNESS: In member function ‘float ehy::ObjectMotion::GetAx() const’:
TEST_HARNESS:6:71: internal compiler error: in coverage_begin_function, at coverage.c:656
0x7f22b0d67082 __libc_start_main
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <file:///usr/share/doc/gcc-9/README.Bugs> for instructions.

Best Answer

  • Bogdan Czwartkowski
    Bogdan Czwartkowski Posts: 157 admin
    Answer ✓

    @sorel ,

    The stub code that you provided does not look right, but I assume that some of the issues came from the stub code being interpreted by HTML renderer of this forum post. In any case, I created a stub like this and it works fine:

    int (::A::CppTest_Stub_Foo) (void) 
        int __return = 0;
            CPPTEST_STUB_CALLBACK_PARAMS(int* __return, ::A* __this);
            CPPTEST_STUB_INVOKE_CALLBACK(&__return, this);
        return __return;

    A few issues that I can point out in your stub:
    1. The stub signature. When stubbing a method of a class, the stub itself has to be a method as well. In your case it is a function: "void CppTest_Stub_Foo()". It would not work as a stub for a class method.
    2. The macro "CPPTEST_STUB_CALLBACK" does not exist. It should be "CPPTEST_STUB_CALLBACK_PARAMS".
    3. Parameters to "CPPTEST_STUB_CALLBACK_PARAMS" should be pointers - see my example above.

    While I could not reproduce the internal compiler error issue with my GNU GCC 9.4.0, I think that fixing the stub code should help.

    I would also advise to create user stubs using C/C++test's "Create User Stub" functionality available from "Stubs" view instead of adding stubs manually (see C/C++test will create all the boilerplate code exactly as needed, with all the proper declarations and stub structure. You can always trim down the auto-created stub code later, after the stub is proven to work.