2

I need your suggestion to wrap my existing function.

I am from testing team I need to write unit test cases, so I don't want to depend on original definition so trying to write my own definiton.

Following is the source code which should not be changed.

source.c:

#include <stdio.h>
const char *getObjectName (int *anObject);

void func()
{
    int *p;
    getObjectName(p);
}

const char *getObjectName (int *anObject)
{
    printf("i am in original\n");
}

From the above code I want to wrap getObjectName() function so that I can give my own definition.

I have googled a lot and tried following methods but didn't work out:

I cannot use above 3 methods because calling function and called function are in same file.

So please suggest me any other methods to write my own defintion for getObjectName().

Community
  • 1
  • 1
  • I cannot use above 3 methods because calling function and called function are in same file. - Do you mean test function and original source are in the same file? – Jay Apr 04 '17 at 10:14
  • func() and getObjectName() is in same file ... i want to test func() function so i will write my test cases in other file but i want to mock getObjectName() function with my own defintion. – venkatesh kambakana Apr 04 '17 at 10:16
  • This is a duplicate of this questions I guess: http://stackoverflow.com/questions/8959132/function-mocking-for-testing-in-c – Heyji Apr 04 '17 at 11:53
  • @Heyji that is not my question.... if you feel that is similar to my question, please share your answer so that it is helpful to me... but dont simply say it is duplicate without analyzing.... – venkatesh kambakana Apr 05 '17 at 03:22
  • @venkatesh kambakana you want to test fun() with your onw definition of getObjectName, and in the link I provided the OP wants to test myfunction() with its own implementation of square(). In both cases the two functions are in the same .c file. I don't see any major difference. Maybe you can explain why it is not the same question. I have spent some time analyzing your case btw. – Heyji Apr 05 '17 at 21:35
  • if the prototype of getObjectName() were in a separate header file, and if you were using GCC, you could use the weak attribute. – Heyji Apr 05 '17 at 21:37

0 Answers0