Updated nbfi test suite, tests and a bit of refactoring

This commit is contained in:
Veit Heller
2014-10-12 13:58:25 +02:00
parent 125030c3e0
commit 79cbec14db
3 changed files with 31 additions and 9 deletions

View File

@@ -12,6 +12,8 @@ all:
@echo @echo
@echo Running tests: @echo Running tests:
@echo @echo
make tests
@echo Compilation successfull.
# Runs the tests # Runs the tests
tests: tests:

View File

@@ -45,7 +45,7 @@
* Lets the program die and emits an error message. * Lets the program die and emits an error message.
*/ */
static inline void die(int code, const char* message){ static inline void die(int code, const char* message){
fprintf(stderr, "%s", message); fprintf(stderr, "%s\n", message);
exit(code); exit(code);
} }
@@ -88,12 +88,12 @@ void compile(FILE* fp){
case '[': case '[':
program[pc].operator = JMP_FWD; program[pc].operator = JMP_FWD;
if (FULL()) if (FULL())
die(127, "nbfi:compile: Cannot jump forwards: Stack is full.\n"); die(127, "nbfi:compile: Cannot jump forwards: Stack is full.");
PUSH(pc); PUSH(pc);
break; break;
case ']': case ']':
if (EMPTY()) if (EMPTY())
die(127, "nbfi:compile: Cannot jump backwards: Stack is full.\n"); die(127, "nbfi:compile: Cannot jump backwards: Stack is full.");
jmp_pc = POP(); jmp_pc = POP();
program[pc].operator = JMP_BCK; program[pc].operator = JMP_BCK;
program[pc].operand = jmp_pc; program[pc].operand = jmp_pc;
@@ -108,7 +108,7 @@ void compile(FILE* fp){
pc++; pc++;
} }
if(!EMPTY()) if(!EMPTY())
die(117, "nbfi:compile: Program ends with non-empty stack."); die(127, "nbfi:compile: Program ends with non-empty stack.");
if(pc == MAX_SIZE) if(pc == MAX_SIZE)
die(127, "nbfi:compile: Program exceeds maximum program size."); die(127, "nbfi:compile: Program exceeds maximum program size.");
program[pc].operator = END; program[pc].operator = END;
@@ -133,12 +133,12 @@ void execute(){
case IN: data[ptr] = (unsigned int)getchar(); break; case IN: data[ptr] = (unsigned int)getchar(); break;
case JMP_FWD: if(!data[ptr]) { pc = program[pc].operand; } break; case JMP_FWD: if(!data[ptr]) { pc = program[pc].operand; } break;
case JMP_BCK: if(data[ptr]) { pc = program[pc].operand; } break; case JMP_BCK: if(data[ptr]) { pc = program[pc].operand; } break;
default: die(127, "nbfi:execute: Unknown instruction.\n"); default: die(127, "nbfi:execute: Unknown instruction.");
} }
pc++; pc++;
} }
if(ptr == DATA_SIZE) if(ptr == DATA_SIZE)
die(127, "nbfi:execute: Program used up too much memory.\n"); die(127, "nbfi:execute: Program used up too much memory.");
} }
/* /*
@@ -151,9 +151,9 @@ void execute(){
int main(int argc, const char * argv[]){ int main(int argc, const char * argv[]){
FILE *fp; FILE *fp;
if(argc != 2) if(argc != 2)
die(127, "nbfi:parse: Wrong number of arguments supplied.\n"); die(127, "nbfi:parse: Wrong number of arguments supplied.");
if(!(fp = fopen(argv[1], "r"))) if(!(fp = fopen(argv[1], "r")))
die(1, "nbfi:parse: File could not be opened.\n"); die(127, "nbfi:parse: File could not be opened.");
compile(fp); compile(fp);
fclose(fp); fclose(fp);
execute(); execute();

View File

@@ -1,9 +1,29 @@
#!/bin/bash #!/bin/bash
for i in `ls test` ; do for i in `ls test | grep -v fail` ; do
echo Running test $i: echo Running test $i:
echo --------------------- echo ---------------------
bin/nbfi test/$i bin/nbfi test/$i
return=$?
if [ $return != 0 ] ; then
echo Test $i failed.
exit 1
fi
echo
echo ---------------------
echo End of test $i
echo
done
for i in `ls test/*fail*` ; do
echo Running expected fail test $i:
echo ---------------------
bin/nbfi $i
return=$?
if [ $return != 127 ] ; then
echo Test $i failed.
exit 1
fi
echo echo
echo --------------------- echo ---------------------
echo End of test $i echo End of test $i