开发者

Test cases of stack

A stack is implemented via linked list. What are the tes开发者_运维知识库t cases when it works and when it doesn't?


The appropriate unit tests depend on your particular interface (blackbox testing) as well as on your particular implementation (glassbox testing). For a stack, some things I would expect to test:

  1. It is possible to push an item on the stack and pop it off.
  2. When pushing multiple distinct items on the stack, repeated popping returns them in reverse order.
  3. An uninitialized stack is empty.
  4. A stack that has an item pushed is non-empty.
  5. A stack that has items pushed and subsequently removed is empty.
  6. Attempting to pop when there are no items results in the documented type of failure, whether that is returning NULL, throwing an exception, or aborting the program.

To summarize:

  • Basic functionality.
  • Order property.
  • Emptiness invariant.
  • Failure mode.

Note that what needs to be tested would be different for other datastructures. Typically, the way to go about creating this set of tests is to create a unit test for each function that confirms that each guarantee made by the documentation is upheld, and that it is upheld for all code paths through the function. Furthermore, one should test documented failure modes to confirm they fail in the expected manner.


Well, think about what you want your Stack to do. Construct an API (or look at the API of an existing stack) and test if every function does what it should. You should take a look at Unit Testing.

Example: your basic stack should have a push() and pop(), so:

  • create an element A,
  • push A on the stack,
  • pop an element
  • see if it is the element A.

Especially test the edge cases: removing the last element, inserting an element twice, inserting invalid objects, references to the stack itself, deleting objects that are on the stack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜