Breaking out ex24 iofunctions files and writing a shell-based test thingy

This commit is contained in:
Dan Buch 2011-11-11 08:22:26 -05:00
parent b5b0213c5d
commit 2e0a5d5c99
6 changed files with 89 additions and 41 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@
!*.h
!*.mk
!test_*.sh
!*-input

View File

@ -10,6 +10,7 @@ all: $(EXERCISES)
test: all
$(MAKE) -f ex19.mk test
./test_ex24_iofunctions.sh
clean:

19
ex24_iofunctions01.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include "dbg.h"
int main(int argc, char *argv[])
{
int a, b, rc;
char c;
printf("Reading from stdin: <a:number> <b:number> <c:letter>\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;
}

View File

@ -2,28 +2,11 @@
#include "dbg.h"
int fscanf_example01()
int main(int argc, char *argv[])
{
int a, b, rc;
char c;
printf("Reading from stdin: <a:number> <b:number> <c:letter>\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 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\": <a:number> <b:number> <c:letter>\n", filename);
printf("Reading from file: <a:number> <b:number> <c:letter>\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:

View File

@ -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 <<EOF
tmp_01="`mktemp`"
cat > "$tmp_01" <<EOF
l
n
w
@ -50,8 +50,8 @@ a
a
EOF
tmp_02=`mktemp`
cat > $tmp_02 <<EOF
tmp_02="`mktemp`"
cat > "$tmp_02" <<EOF
l
a
a
@ -70,8 +70,8 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
EOF
run_test '01' $tmp_01
run_test '02' $tmp_02
run_test '01' "$tmp_01"
run_test '02' "$tmp_02"
exit $EXIT_CODE

56
test_ex24_iofunctions.sh Executable file
View File

@ -0,0 +1,56 @@
#!/bin/bash
EXIT_CODE=0
run_test()
{
echo -n " $1"
out_tmp="$(mktemp)"
./ex24_iofunctions$1 < "$2" > "$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" <<EOF
2 8 n
EOF
expected_01="$(mktemp)"
cat > "$expected_01" <<EOF
Reading from stdin: <a:number> <b:number> <c:letter>
> Read in: a=2 b=8 c=n
EOF
tmp_02="$(mktemp)"
tmp_02in="$(mktemp)"
cat > "$tmp_02in" <<EOF
1 4 a
EOF
cat > "$tmp_02" <<EOF
$tmp_02in
EOF
expected_02="$(mktemp)"
cat > "$expected_02" <<EOF
Provide filename from which to inputs
> Reading from file: <a:number> <b:number> <c:letter>
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