0

堆栈是通过链表实现的。什么时候有效,什么时候无效?

4

2 回答 2

5

适当的单元测试取决于您的特定接口(黑盒测试)以及您的特定实现(玻璃盒测试)。对于堆栈,我希望测试一些东西:

  1. 可以将项目压入堆栈并将其弹出。
  2. 当将多个不同的项目压入堆栈时,重复弹出以相反的顺序返回它们。
  3. 未初始化的堆栈为空。
  4. 已推送项目的堆栈是非空的。
  5. 已推送并随后移除项目的堆栈是空的。
  6. 在没有项目时尝试弹出会导致记录的失败类型,无论是返回 NULL、抛出异常还是中止程序。

总结一下:

  • 基本功能。
  • 订单属性。
  • 空性不变。
  • 故障模式。

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.

于 2011-02-16T08:04:53.903 回答
1

好吧,想想你想让你的堆栈做什么。构建一个 API(或查看现有堆栈的 API)并测试每个函数是否都做了它应该做的事情。你应该看看单元测试

示例:您的基本堆栈应该有一个push()and pop(),所以:

  • 创建一个元素A,
  • 将 A 压入堆栈,
  • 弹出一个元素
  • 看看是不是元素A。

特别是测试边缘情况:删除最后一个元素,插入一个元素两次,插入无效对象,对堆栈本身的引用,删除堆栈上的对象。

于 2011-02-16T08:03:16.307 回答