Working through extra credit for ch20 ex19

This commit is contained in:
Dan Buch 2011-10-26 04:21:16 -04:00
parent 874eb4f3a2
commit 341d626870
6 changed files with 99 additions and 8 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
!*.py !*.py
!*.h !*.h
!*.mk !*.mk
!test_*.sh

View File

@ -7,9 +7,13 @@ all: $(EXERCISES)
$(MAKE) -f ex19.mk $(MAKE) -f ex19.mk
test: all
$(MAKE) -f ex19.mk test
clean: clean:
rm -f $(EXERCISES) rm -f $(EXERCISES)
$(MAKE) -f ex19.mk clean $(MAKE) -f ex19.mk clean
.PHONY: all clean .PHONY: all test clean

10
ex19.c
View File

@ -46,7 +46,6 @@ Object MonsterProto = {
void *Room_move(void *self, Direction direction) void *Room_move(void *self, Direction direction)
{ {
assert(self != NULL); assert(self != NULL);
assert(direction);
Room *room = self; Room *room = self;
Room *next = NULL; Room *next = NULL;
@ -70,6 +69,8 @@ void *Room_move(void *self, Direction direction)
if(next) { if(next) {
next->_(describe)(next); next->_(describe)(next);
} else {
printf("mumble mumble.\n");
} }
return next; return next;
@ -103,7 +104,6 @@ Object RoomProto = {
void *Map_move(void *self, Direction direction) void *Map_move(void *self, Direction direction)
{ {
assert(self != NULL); assert(self != NULL);
assert(direction);
Map *map = self; Map *map = self;
Room *location = map->location; Room *location = map->location;
@ -113,6 +113,8 @@ void *Map_move(void *self, Direction direction)
if(next) { if(next) {
map->location = next; map->location = next;
} else {
map->location = location;
} }
return next; return next;
@ -176,9 +178,6 @@ int process_input(Map *game)
printf("\n> "); printf("\n> ");
char ch = getchar(); char ch = getchar();
assert(ch != EOF);
getchar(); // eat ENTER
int damage = rand() % 4; int damage = rand() % 4;
switch(ch) { switch(ch) {
@ -219,6 +218,7 @@ int process_input(Map *game)
printf("What?: %d\n", ch); printf("What?: %d\n", ch);
} }
getchar(); // eat ENTER
return 1; return 1;
} }

12
ex19.mk
View File

@ -2,8 +2,16 @@ CFLAGS=-Wall -g
all: ex19 all: ex19
ex19: object.o ex19: object.o
test:
./test_ex19.sh
clean: clean:
rm -f ex19 rm -f ex19 object.o
rm -f object.o
.PHONY: all test clean

View File

@ -85,6 +85,7 @@ void *Object_new(size_t size, Object proto, char *description)
return NULL; return NULL;
} else { } else {
// all done, we made an object of any type // all done, we made an object of any type
assert(el != NULL);
return el; return el;
} }
} }

77
test_ex19.sh Executable file
View File

@ -0,0 +1,77 @@
#!/bin/bash
EXIT_CODE=0
run_test()
{
echo -n " $1"
cat $2 | ./ex19 >/dev/null
if [[ $? -eq 0 ]]
then
echo " ... OK"
else
echo " ... FAIL"
EXIT_CODE=1
fi
rm -f $2
}
tmp_01=`mktemp`
cat > $tmp_01 <<EOF
l
n
w
n
w
n
w
e
w
e
e
e
s
s
w
w
n
a
a
a
a
a
a
a
a
a
a
a
a
EOF
tmp_02=`mktemp`
cat > $tmp_02 <<EOF
l
a
a
a
a
a
n
hutesoahutesohas
999999999999
l
n
n
n
e
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
EOF
run_test '01' $tmp_01
run_test '02' $tmp_02
exit $EXIT_CODE