C 如何为另一个使用标准输入的函数编写测试函数?
作为大学任务的一部分,我具有以下职能:C 如何为另一个使用标准输入的函数编写测试函数?,c,unit-testing,stdin,C,Unit Testing,Stdin,作为大学任务的一部分,我具有以下职能: int readMenuOption() { /* local declarations */ char option[2]; /* read in 1 char from stdin plus 1 char for string termination character */ readStdin(1 + 1, option); return (int)option[0] <= ASCII_OFFSET ? 0 : (
int readMenuOption()
{
/* local declarations */
char option[2];
/* read in 1 char from stdin plus 1 char for string termination character */
readStdin(1 + 1, option);
return (int)option[0] <= ASCII_OFFSET ? 0 : (int)option[0] - ASCII_OFFSET;
}
int readStdin(int limit, char *buffer)
{
char c;
int i = 0;
int read = FALSE;
while ((c = fgetc(stdin)) != '\n') {
/* if the input string buffer has already reached it maximum
limit, then abandon any other excess characters. */
if (i <= limit) {
*(buffer + i) = c;
i++;
read = TRUE;
}
}
/* clear the remaining elements of the input buffer with a null character. */
for (i = i; i < strlen(buffer); i++) {
*(buffer + i) = '\0';
}
return read;
}
int readMenuOption()
{
/*本地声明*/
字符选项[2];
/*从stdin读入1个字符加上1个字符作为字符串终止字符*/
readStdin(1+1,可选);
返回(int)选项[0]查找非标准但非常有用的函数。然后执行以下操作:
int ptyfd;
pid = forkpty(&ptyfd, 0, 0, 0);
if (pid<0) perror("forkpty"), exit(1);
if (!pid) {
/* call your function to be tested */
_exit(1);
} else {
/* write to ptyfd here to generate input for the function */
}
char *fakeStdIn = "";
int myfgetc (FILE *fin) {
if (*fakeStdIn == '\0')
return fgetc (fin);
return *fakeStdIn++;
}
int readStdin(int limit, char *buffer)
{
char c;
int i = 0;
int read = FALSE;
while ((c = myfgetc(stdin)) != '\n') {
/* if the input string buffer has already reached it maximum
limit, then abandon any other excess characters. */
if (i <= limit) {
*(buffer + i) = c;
i++;
read = TRUE;
}
}
/* clear the remaining elements of the input buffer with a null character. */
for (i = i; i < strlen(buffer); i++) {
*(buffer + i) = '\0';
}
return read;
}
./a.out < input.txt
intptyfd;
pid=forkpty(&ptyfd,0,0,0);
如果(pid您可以做的一件事是简单地修改readStdin
,以允许它从实际的标准输入或辅助函数获取数据,例如:
int ptyfd;
pid = forkpty(&ptyfd, 0, 0, 0);
if (pid<0) perror("forkpty"), exit(1);
if (!pid) {
/* call your function to be tested */
_exit(1);
} else {
/* write to ptyfd here to generate input for the function */
}
char *fakeStdIn = "";
int myfgetc (FILE *fin) {
if (*fakeStdIn == '\0')
return fgetc (fin);
return *fakeStdIn++;
}
int readStdin(int limit, char *buffer)
{
char c;
int i = 0;
int read = FALSE;
while ((c = myfgetc(stdin)) != '\n') {
/* if the input string buffer has already reached it maximum
limit, then abandon any other excess characters. */
if (i <= limit) {
*(buffer + i) = c;
i++;
read = TRUE;
}
}
/* clear the remaining elements of the input buffer with a null character. */
for (i = i; i < strlen(buffer); i++) {
*(buffer + i) = '\0';
}
return read;
}
./a.out < input.txt
通过将钩子放在较低的级别,您可以注入自己的字符序列,而不是使用标准输入。如果在任何时候,伪标准输入耗尽,它将恢复为真实输入
显然,这是在使用字符,因此,如果要注入EOF事件,则需要一个整数数组,但这只是对方案的一个小修改。为什么不能使用重定向?类似于:
int ptyfd;
pid = forkpty(&ptyfd, 0, 0, 0);
if (pid<0) perror("forkpty"), exit(1);
if (!pid) {
/* call your function to be tested */
_exit(1);
} else {
/* write to ptyfd here to generate input for the function */
}
char *fakeStdIn = "";
int myfgetc (FILE *fin) {
if (*fakeStdIn == '\0')
return fgetc (fin);
return *fakeStdIn++;
}
int readStdin(int limit, char *buffer)
{
char c;
int i = 0;
int read = FALSE;
while ((c = myfgetc(stdin)) != '\n') {
/* if the input string buffer has already reached it maximum
limit, then abandon any other excess characters. */
if (i <= limit) {
*(buffer + i) = c;
i++;
read = TRUE;
}
}
/* clear the remaining elements of the input buffer with a null character. */
for (i = i; i < strlen(buffer); i++) {
*(buffer + i) = '\0';
}
return read;
}
./a.out < input.txt
/a.out
其中“input.txt”将包含您想要给程序的任何输入。可能重复的