I have the following functions as part of a college assignment:
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;
}
It works perfectly for what I need it to do (take in input from keyboard). I've had to do it using stdin (like I have) because of a number of requirements outlined by my professor.
I want to write a series of "unit tests" for the assignment, but I dont know how to get my testing functions to call readMenuOption()
and pass in input to it (without having to do it at run-time).
Is this possible, and if so, how can I do it? (i.e., is it possible to write to stdin)?