Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过模拟在C中调用的非静态函数来测试静态函数_C - Fatal编程技术网

通过模拟在C中调用的非静态函数来测试静态函数

通过模拟在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

我有

方案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 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的源文件移动到另一个源文件