Skip to content

Commit

Permalink
Actual unit tests for parseoptions_vstudio.h
Browse files Browse the repository at this point in the history
  • Loading branch information
pzembrod committed Jan 13, 2024
1 parent 3d9cdf2 commit f3313ec
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 15 deletions.
106 changes: 106 additions & 0 deletions RunCPM/parseopt_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,113 @@
#include "cheat.h"
#include "parseoptions_testsetup.h"

CHEAT_SET_UP(setupParseoptionsTest();)

CHEAT_TEST(dummy,
cheat_assert(1);
)

CHEAT_TEST(no_opts,
char *myargv[] = {"runcpm"};
_parse_options(1, myargv);
cheat_assert_not(streamInputActive);
cheat_assert(consoleOutputActive);
cheat_assert(streamInputFile == NULL);
cheat_assert(streamOutputFile == NULL);
cheat_assert_not(exit_value);
cheat_assert(cheat_compare(mystderr, ""));
)

CHEAT_TEST(dash_i,
char *myargv[] = {"runcpm", "-i", "inputfile"};
_parse_options(3, myargv);
cheat_assert(streamInputActive);
cheat_assert(consoleOutputActive);
cheat_assert(streamInputFile == myinfile);
cheat_assert(streamOutputFile == NULL);
cheat_assert(cheat_compare(infilename, "inputfile"));
cheat_assert(cheat_compare(outfilename, ""));
cheat_assert_not(exit_value);
cheat_assert(cheat_compare(mystderr, ""));
)

CHEAT_TEST(dash_o,
char *myargv[] = {"runcpm", "-o", "outputfile"};
_parse_options(3, myargv);
cheat_assert_not(streamInputActive);
cheat_assert(consoleOutputActive);
cheat_assert(streamInputFile == NULL);
cheat_assert(streamOutputFile == myoutfile);
cheat_assert(cheat_compare(infilename, ""));
cheat_assert(cheat_compare(outfilename, "outputfile"));
cheat_assert_not(exit_value);
cheat_assert(cheat_compare(mystderr, ""));
)

CHEAT_TEST(dash_i_o,
char *myargv[] = {"runcpm", "-i", "inputfile", "-o", "outputfile"};
_parse_options(5, myargv);
cheat_assert(streamInputActive);
cheat_assert(consoleOutputActive);
cheat_assert(streamInputFile == myinfile);
cheat_assert(streamOutputFile == myoutfile);
cheat_assert(cheat_compare(infilename, "inputfile"));
cheat_assert(cheat_compare(outfilename, "outputfile"));
cheat_assert_not(exit_value);
cheat_assert(cheat_compare(mystderr, ""));
)

CHEAT_TEST(dash_o_i,
char *myargv[] = {"runcpm", "-o", "outputfile", "-i", "inputfile"};
_parse_options(5, myargv);
cheat_assert(streamInputActive);
cheat_assert(consoleOutputActive);
cheat_assert(streamInputFile == myinfile);
cheat_assert(streamOutputFile == myoutfile);
cheat_assert(cheat_compare(infilename, "inputfile"));
cheat_assert(cheat_compare(outfilename, "outputfile"));
cheat_assert_not(exit_value);
cheat_assert(cheat_compare(mystderr, ""));
)

CHEAT_TEST(dash_s,
char *myargv[] = {"runcpm", "-s"};
_parse_options(2, myargv);
cheat_assert(streamInputActive);
cheat_assert_not(consoleOutputActive);
cheat_assert(streamInputFile == mystdin);
cheat_assert(streamOutputFile == mystdout);
cheat_assert(cheat_compare(infilename, ""));
cheat_assert(cheat_compare(outfilename, ""));
cheat_assert_not(exit_value);
cheat_assert(cheat_compare(mystderr, ""));
)

CHEAT_TEST(dash_i_no_ifile,
char *myargv[] = {"myruncpm", "-i"};
_parse_options(2, myargv);
cheat_assert(exit_value);
cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0);
)

CHEAT_TEST(dash_o_no_ofile,
char *myargv[] = {"myruncpm", "-o"};
_parse_options(2, myargv);
cheat_assert(exit_value);
cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0);
)

CHEAT_TEST(dash_i_o_no_ifile,
char *myargv[] = {"myruncpm", "-o", "outfile", "-i"};
_parse_options(4, myargv);
cheat_assert(exit_value);
cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0);
)

CHEAT_TEST(dash_i_o_no_ofile,
char *myargv[] = {"myruncpm", "-i", "infile", "-o"};
_parse_options(4, myargv);
cheat_assert(exit_value);
cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0);
)

21 changes: 15 additions & 6 deletions RunCPM/parseoptions_testsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
FILE mystdin[1];
FILE mystdout[1];
FILE mystderr[2000];
FILE myinfile[1];
FILE myoutfile[1];

void myfprintf(FILE *out, const char *fmt, ...) {
va_list va;
Expand All @@ -23,19 +25,16 @@ char infilename[50];
char outfilename[50];
char unknownfilename[50];

void clearfilenames(void) {
*infilename = 0;
*outfilename = 0;
*unknownfilename = 0;
}

char *myfopen(const char *fname, const char *mode) {
if (*mode == 'r') {
strcpy(infilename, fname);
return myinfile;
} else if (*mode == 'w') {
strcpy(outfilename, fname);
return myoutfile;
} else {
strcpy(unknownfilename, fname);
return NULL;
}
}

Expand Down Expand Up @@ -67,4 +66,14 @@ void _file_failure_exit(char *argv[], char* fmt, char* filename)
{
}

void setupParseoptionsTest(void) {
*infilename = 0;
*outfilename = 0;
*unknownfilename = 0;
streamInputFile = NULL;
streamOutputFile = NULL;
streamInputActive = FALSE;
consoleOutputActive = TRUE;
}

#include "parseoptions_vstudio.h"
6 changes: 5 additions & 1 deletion RunCPM/parseoptions_testsetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
extern char mystdin[];
extern char mystdout[];
extern char mystderr[];
extern char myinfile[];
extern char myoutfile[];

extern char infilename[];
extern char outfilename[];
Expand All @@ -15,7 +17,9 @@ extern char *streamOutputFile;
extern char streamInputActive;
extern char consoleOutputActive;

void clearfilenames(void);
extern int exit_value;

void setupParseoptionsTest(void);

void _usage(char *argv[]);

Expand Down
13 changes: 5 additions & 8 deletions RunCPM/parseoptions_vstudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ void _usage(char *argv[]) {
"usage: %s [-i input_file] [-o output_file] [-s]\n", argv[0]);
fprintf(stderr,
" -i input_file: console input will be read from the file "
"with the\ngiven name. "
"After input file's EOF, further console input\nwill be read "
"with the\n given name. "
"After input file's EOF, further console input\n will be read "
"from the keyboard.\n");
fprintf(stderr,
" -o output_file: console output will be written to the file "
"with the\ngiven name, in addition to the screen.\n");
"with the\n given name, in addition to the screen.\n");
fprintf(stderr,
" -s: console input and output is connected directly to "
"stdin and stdout.\nSince on Posix keyboard input is read from "
"stdin, switching from\nstdin to keyboard on stdin EOF is a "
"no-op. Therefore stdin EOF is an\nerror condition on Posix in "
"this case.\n");
"stdin and stdout.\n");
}

#define SET_OPTARG ++i; if (i >= argc) {++errflg; break;} optarg = argv[i];

void _parse_options(int argc, char *argv[]) {
int errflg = 0;
char *optarg;
for (int i = 0; i < argc && errflg == 0; ++i) {
for (int i = 1; i < argc && errflg == 0; ++i) {
if (strcmp("-i", argv[i]) == 0) {
/* ++i;
if (i >= argc) {
Expand Down

0 comments on commit f3313ec

Please sign in to comment.