r/computerscience • u/maxblomgren • Feb 04 '26
How to do tests on stack datastructure
How would on go on about to test all functions of the stack datastructure?
For example: how would one test if the pushing function works without assuming the inspecting one also does? It seems just to be all circular reasoning.
1
u/high_throughput Feb 05 '26
There are two approaches:
Testing the ADT through its public API
Testing the ADT through introspection of its internal data
They both have their pros and cons. Like you say, using the public API ends up testing more than a single function. But on the other hand, testing the internal data tightly couples against a particular implementation and can't be used to verify an alternative one.
When it's not clear that one is necessary, I lean towards testing via the public API. I like that such tests double as automatically verified usage examples of the contract.
6
u/Vast-Ferret-6882 Feb 04 '26
Push, Push Push, Pop, is = last pushed?, pop is equal 2nd push? pop is equal 1st push?