From 2e0a5d5c993a1ae53b003dda0a04a5f464eefd39 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 11 Nov 2011 08:22:26 -0500 Subject: [PATCH] Breaking out ex24 iofunctions files and writing a shell-based test thingy --- .gitignore | 1 + Makefile | 1 + ex24_iofunctions01.c | 19 ++++++++ ex24_iofunctions.c => ex24_iofunctions02.c | 35 ++------------ test_ex19.sh | 18 +++---- test_ex24_iofunctions.sh | 56 ++++++++++++++++++++++ 6 files changed, 89 insertions(+), 41 deletions(-) create mode 100644 ex24_iofunctions01.c rename ex24_iofunctions.c => ex24_iofunctions02.c (52%) create mode 100755 test_ex24_iofunctions.sh diff --git a/.gitignore b/.gitignore index 184c4f7..5ba293d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ !*.h !*.mk !test_*.sh +!*-input diff --git a/Makefile b/Makefile index 1ddb5c8..03350c8 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ all: $(EXERCISES) test: all $(MAKE) -f ex19.mk test + ./test_ex24_iofunctions.sh clean: diff --git a/ex24_iofunctions01.c b/ex24_iofunctions01.c new file mode 100644 index 0000000..3de50fa --- /dev/null +++ b/ex24_iofunctions01.c @@ -0,0 +1,19 @@ +#include +#include "dbg.h" + + +int main(int argc, char *argv[]) +{ + int a, b, rc; + char c; + printf("Reading from stdin: \n> "); + rc = fscanf(stdin, "%d %d %c", &a, &b, &c); + check(rc == 3, "Failed to read from stdin."); + + printf("Read in: a=%d b=%d c=%c\n", a, b, c); + + return 0; + +error: + return 1; +} diff --git a/ex24_iofunctions.c b/ex24_iofunctions02.c similarity index 52% rename from ex24_iofunctions.c rename to ex24_iofunctions02.c index 0cd249c..e1e21db 100644 --- a/ex24_iofunctions.c +++ b/ex24_iofunctions02.c @@ -2,28 +2,11 @@ #include "dbg.h" -int fscanf_example01() -{ - int a, b, rc; - char c; - printf("Reading from stdin: \n> "); - rc = fscanf(stdin, "%d %d %c", &a, &b, &c); - check(rc == 3, "Failed to read from stdin."); - - printf("Read in: a=%d b=%d c=%c\n", a, b, c); - - return 1; - -error: - return 0; -} - - -int fscanf_example02() +int main(int argc, char *argv[]) { int a, b, rc; char c; - char filename[1024]; + char filename[1024 * 2]; FILE *fp; printf("Provide filename from which to inputs\n> "); @@ -32,7 +15,7 @@ int fscanf_example02() fp = fopen(filename, "r"); check(fp != NULL, "Failed to open file %s", filename); - printf("Reading from \"%s\": \n", filename); + printf("Reading from file: \n"); rc = fscanf(fp, "%d %d %c", &a, &b, &c); check(rc == 3, "Failed to read from \"%s\".", filename); @@ -40,18 +23,6 @@ int fscanf_example02() rc = fclose(fp); check(rc != EOF, "Failed to close file \"%s\"", filename); - return 1; - -error: - return 0; -} - - -int main(int argc, char *argv[]) -{ - check(fscanf_example01() == 1, "Fail!"); - check(fscanf_example02() == 1, "Fail!"); - return 0; error: diff --git a/test_ex19.sh b/test_ex19.sh index 6e2abb0..0455133 100755 --- a/test_ex19.sh +++ b/test_ex19.sh @@ -5,20 +5,20 @@ EXIT_CODE=0 run_test() { echo -n " $1" - cat $2 | ./ex19 >/dev/null - if [[ $? -eq 0 ]] + cat "$2" | ./ex19 >/dev/null + if [[ "$?" -eq "0" ]] then echo " ... OK" else echo " ... FAIL" EXIT_CODE=1 fi - rm -f $2 + rm -f "$2" } -tmp_01=`mktemp` -cat > $tmp_01 < "$tmp_01" < $tmp_02 < "$tmp_02" < "$out_tmp" + diff_out="$(mktemp)" + diff -u "$3" "$out_tmp" > "$diff_out" + diff_count="$(cat "$diff_out" | wc -l)" + if [[ "$?" == "0" ]] && [[ "$diff_count" == "0" ]] + then + echo " ... OK" + else + echo " ... FAIL" + echo " diff count => $diff_count" + cat "$diff_out" + EXIT_CODE=1 + fi + rm -f "$2" "$out_tmp" "$diff_out" +} + + +tmp_01="$(mktemp)" +cat > "$tmp_01" < "$expected_01" < +> Read in: a=2 b=8 c=n +EOF + +tmp_02="$(mktemp)" +tmp_02in="$(mktemp)" +cat > "$tmp_02in" < "$tmp_02" < "$expected_02" < Reading from file: +Read in: a=1 b=4 c=a +EOF + + +run_test '01' "$tmp_01" "$expected_01" +run_test '02' "$tmp_02" "$expected_02" + + +exit $EXIT_CODE