From: Scott Worley Date: Mon, 8 May 2017 03:27:05 +0000 (-0700) Subject: Accept a test data file as a command line argument X-Git-Url: http://git.scottworley.com/overonion/commitdiff_plain/622af8d7465b204a817a06a4962e5aaee69c4954?ds=sidebyside Accept a test data file as a command line argument --- diff --git a/reverse_test.c b/reverse_test.c index 0b82f19..46c7a97 100644 --- a/reverse_test.c +++ b/reverse_test.c @@ -8,8 +8,6 @@ #include #include -static const char test_file[] = "reverse.c"; - /* Reverse input_file. Dump the result in a temporary file. Return the temp * file's name. The caller must free the filename. */ char* reverse_to_temp_file(const char* input_file) { @@ -45,11 +43,11 @@ void compare(const char* filename1, const char* filename2) { if (fclose(f2) != 0) err(EX_IOERR, "Couldn't close file %s", filename2); } -void test_reverse_twice_from_files_is_the_same() { - char* intermediate = reverse_to_temp_file(test_file); +void test_reverse_twice_from_files_is_the_same(const char* test_filename) { + char* intermediate = reverse_to_temp_file(test_filename); char* back_to_normal = reverse_to_temp_file(intermediate); - compare(test_file, back_to_normal); + compare(test_filename, back_to_normal); if (unlink(intermediate) == -1) err(EX_IOERR, "Couldn't remove intermediate temp file"); if (unlink(back_to_normal) == -1) err(EX_IOERR, "Couldn't remove twice-reversed temp file"); @@ -57,7 +55,7 @@ void test_reverse_twice_from_files_is_the_same() { free(back_to_normal); } -void test_reverse_from_file_then_from_stream_is_the_same() { +void test_reverse_from_file_then_from_stream_is_the_same(const char* test_filename) { int pipefd[2]; if (pipe(pipefd) == -1) err(EX_OSERR, "Couldn't create pipe"); @@ -67,7 +65,7 @@ void test_reverse_from_file_then_from_stream_is_the_same() { if (close(pipefd[0]) == -1) err(EX_OSERR, "Couldn't close unneeded pipe descriptor"); FILE* to_second = fdopen(pipefd[1], "w"); if (to_second == NULL) err(EX_IOERR, "Couldn't open pipe for writing"); - reverse_file(test_file, to_second); + reverse_file(test_filename, to_second); exit(0); } if (close(pipefd[1]) == -1) err(EX_OSERR, "Couldn't close unneeded pipe descriptor"); @@ -79,15 +77,23 @@ void test_reverse_from_file_then_from_stream_is_the_same() { reverse_stream(from_first, out_file); if (fclose(out_file) == EOF) err(EX_IOERR, "Couldn't close temporary file"); - compare(test_file, out_temp_filename); + compare(test_filename, out_temp_filename); if (unlink(out_temp_filename) == -1) err(EX_IOERR, "Couldn't remove temp output file"); free(out_temp_filename); } -int main() { - test_reverse_twice_from_files_is_the_same(); - test_reverse_from_file_then_from_stream_is_the_same(); +int main(int argc, char** argv) { + char* test_filename = "reverse.c"; + if (argc > 2) { + errx(EX_USAGE, "usage: reverse_test [datafile]"); + } else if (argc == 2) { + test_filename = argv[1]; + } + + test_reverse_twice_from_files_is_the_same(test_filename); + test_reverse_from_file_then_from_stream_is_the_same(test_filename); + puts("PASS"); return 0; }