Monday, March 29, 2010

The New Number Two

So, tonight I did the second thing on my list of stack tasks:

Write code to deal with cases outside the expected cases.

Basically, I dealt with two situations:
  • More pops than pushes.
  • More than 100 pushes.
Both pieces of testing code work by checking current against base. If there is a problem, it jumps to the following alternative program exit:


EXCEPTION: printf("Error: Exiting.\n");

return 0;


This means that the state of the stack is tested BEFORE any changes are made. Later versions of this, instead of exiting, will modify the stack to allow for additional pushes.

Here's POP:

#define POP if (current == base) goto EXCEPTION; pop(); // deal with extra pop.

I used a compiler directive to tie the test with the pop.

Here's PUSH:

#define PUSH(x) if (current == (base+stackSize)) goto EXCEPTION; push(x);


Next: Expanding the stack.

No comments:

Post a Comment