Sub topic 15
Testing every implementation against one specification
This chapter assumes you know the Stack ADT from earlier chapters, can read Python, and understand exceptions. You will write one behavior check from the specification and run it against every implementation.
Test the promise, not the representation
A specification says what an ADT promises. An implementation is one way to keep that promise. A test written against ListStack internals, such as _items, only checks that one representation. It breaks when you switch to LinkedStack, even if the new class is correct.
Write tests against the public operations only. Then one test serves every implementation.
Turn the specification into checks
The Stack specification gives these promises:
- A new stack is empty.
push(x)makes the stack non-empty, andpeek()then returnsx.pop()returns the most recently pushed item first (last in, first out).pop()andpeek()on an empty stack raiseIndexError.
Each promise becomes one assertion. The test never reads a private field.
One check, many implementations
The function takes a factory, a callable that builds a fresh stack. Passing the class itself works because calling a class creates an instance.
def check_stack_behavior(make_stack):
s = make_stack()
assert s.is_empty()
s.push(7)
assert not s.is_empty()
assert s.peek() == 7
s.push(9)
assert s.pop() == 9
assert s.pop() == 7
assert s.is_empty()
try:
s.pop()
except IndexError:
pass
else:
raise AssertionError("pop on empty stack must raise IndexError")
check_stack_behavior(ListStack)
check_stack_behavior(LinkedStack)The else branch matters. Without it, a pop that quietly returns None on an empty stack would pass the test.
Adding a third implementation
Suppose you write ArrayStack later. You do not write new tests. You add one line:
check_stack_behavior(ArrayStack)If it fails, the failure points to a broken promise, not to a difference in storage.
What the check cannot show
A behavior check samples the specification. It does not prove the implementation correct. Common gaps:
- It covers only the inputs you try. Add cases for many pushes, or for push after a pop.
- It does not check cost. A
LinkedStackand aListStackpass the same test but differ in memory use and speed. - It does not check the representation invariant. For
IntSet, call_check_rep()inside the implementation, as the earlier chapter did, so internal damage is caught where it happens. - It does not detect representation exposure. A separate check can mutate a returned value and confirm the object is unchanged, as in the
SafeRosterexample.
Common errors
| Error | Effect | Fix |
|---|---|---|
| Test reads `_items` or `_head` | Fails for correct implementations with other storage | Use only public operations |
| Test omits the empty-stack error case | A wrong implementation passes | Assert the exception and use `else` to fail without it |
| Test reuses one stack across checks | One failure hides or causes others | Build a fresh stack from the factory for each check |
| Test passes an instance, not a factory | Cannot create fresh instances | Pass the class or a function that returns a new object |
Exercise
You add a peek check to check_stack_behavior that reads s._items[-1]. It passes for ListStack and crashes for LinkedStack. What is wrong, and how do you fix it?
Show answer
The test depends on the list representation, so it checks ListStack's storage and not the Stack specification. LinkedStack has no _items, so the test fails even though that class is correct. Replace it with s.peek() and compare the result to the expected item, so only public operations are used.
Exercise
Write a short check that confirms peek on an empty stack raises IndexError, usable with any implementation.
Show answer
def checkpeekempty(makestack): s = makestack() try: s.peek() except IndexError: pass else: raise AssertionError("peek on empty stack must raise IndexError")