Browse Source

refactoring: made input injectable for testing purposes

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
a05cf45c9f
  1. 6
      src/fakeinput.h
  2. 13
      src/userinput.c

6
src/fakeinput.h

@ -0,0 +1,6 @@
// fakeinput.h exists solely for testing purposes
// fakeInput is set by unit tests to inject specific inputs for testing
#ifndef FAKEINPUT_H
#define FAKEINPUT_H
extern char **fakeInput;
#endif

13
src/userinput.c

@ -1,4 +1,5 @@
#include "userinput.h" #include "userinput.h"
#include "fakeinput.h"
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
@ -9,6 +10,8 @@
#include <limits.h> #include <limits.h>
#include <float.h> #include <float.h>
char **fakeInput = NULL;
#define LD(name)\ #define LD(name)\
long double name##ldvalue;\ long double name##ldvalue;\
long double *name##ld = NULL;\ long double *name##ld = NULL;\
@ -39,6 +42,16 @@ void trimRight(char *input) {
} }
char *readInput() { char *readInput() {
if (fakeInput != NULL) {
if (fakeInput[0] == NULL) {
printf("The given list of inputs in the test was exceeded!\n");
exit(1);
}
char *result = malloc(strlen(fakeInput[0])*sizeof(char) + 1);
strcpy(result, fakeInput[0]);
fakeInput = fakeInput + 1;
return result;
}
size_t bufferSize = 100; size_t bufferSize = 100;
char *buffer = malloc(bufferSize*sizeof(char)); char *buffer = malloc(bufferSize*sizeof(char));
char c; char c;

Loading…
Cancel
Save