通过模拟在C中调用的非静态函数来测试静态函数
我有 方案h:通过模拟在C中调用的非静态函数来测试静态函数,c,C,我有 方案h: static int doSomething(int input); 方案c: #include "program.h" void sendSomethingToSomewhere(int things); static int doSomething(int input) { // other code sendSomethingToSomewhere(things); return x; } void sendSomethingToSo
static int doSomething(int input);
方案c:
#include "program.h"
void sendSomethingToSomewhere(int things);
static int doSomething(int input)
{
// other code
sendSomethingToSomewhere(things);
return x;
}
void sendSomethingToSomewhere(int things)
{
// bad stuff
}
我想通过创建文件test.c来测试doSomething函数。程序.h和程序.c不得修改。我想在test.c中替换program.c中sendSomethingToSomewhere的实现,这样每当doSomething调用sendSomethingToSomewhere时,就会执行“good stuff”而不是“bad stuff”。我该怎么做
测试c:
#include "program.h"
void test1();
void sendSomethingToSomewhere(int things);
void test1()
{
int output = doSomething(4);
printf("%d", output);
}
void sendSomethingToSomewhere(int things)
{
// good stuff
}
你所要求的是不可能的 要测试的函数与它调用的不希望调用的函数位于同一文件中。如果program.c无法修改,则无法调用
doSomething
而不调用sendSomethingToSomewhere
如果您希望能够单独测试doSomething
,则必须将其从包含sendsomething的源文件移动到另一个源文件