[英]How to test a stack of n elements?
This seems like a silly question but I would really like your comments and would like to validate my tester thinking.. 这似乎是一个愚蠢的问题,但我真的很想您的评论,并希望验证我的测试人员的想法。
I can think of following test cases:
1) Pop element from empty stack should return error
2) Push n+1 elements in stack should return error during insertion of n+1th element
3) validate empty,full and top functions of stack.
4) if stack is used in multithreaded environment , make sure it is synchronized.
5) validate stack functionality:
push element (x)
pop element: it should return x
Comments? 评论?
If this question is asked in an interview, what could be the possible answers? 如果在面试中提出这个问题,可能的答案是什么? Is my answer valid enough?
我的答案足够有效吗?
I think you got everything covered, except maybe checking in 3) that the n
elements pushed before are how you'd expect them. 我认为您已经涵盖了所有内容,除了可能检查3)之前推送的
n
元素是您期望的方式。 This will catch erroneous boundary condition checking, ie checking whether the stack is full only after writing the new element. 这将捕获错误的边界条件检查,即仅在写入新元素之后检查堆栈是否已满。
Additionally, I'd group your test cases differently and test top,pop,push,empty,full for each: 另外,我将对您的测试用例进行不同的分组,并对每个用例的top,pop,push,empty,full测试:
*Not necessary, but could be a corner case. *不是必需的,但可能是一个小案例。
Looks fine to me. 在我看来很好。 Just make sure when you're validating the functionality, that you make sure multiple items come back in the right order, top works correctly after each pop and push, and it properly reports empty after you pop the last element.
只需确保在验证功能时,确保以正确的顺序返回多个项目,每次弹出并推动后top即可正常工作,并且在弹出最后一个元素后可正确报告为空。
I would also push the numbers 101 through 100+n then pop them all off to ensure they come off in reverse order (will catch sequencing problems such as "sometimes pop will get the second element rather than the first"). 我还将数字101推到100 + n,然后将它们全部弹出以确保它们以相反的顺序出现(将捕获排序问题,例如“有时pop将获得第二个元素,而不是第一个元素”)。
The following sequence should test all but your multithreaded requirement thoroughly: 以下序列应彻底测试除多线程需求以外的所有需求:
I would enhance (5) to read: push N distinct elements, pop N times and ensure the same elements are retrieved in reverse order 我将增强(5)的内容:推入N个不同的元素,弹出N次,并确保以相反的顺序检索相同的元素
It might also be prudent to check that this words for elements from a sorted list, a reverse-sorted list, and an unsorted list. 检查此单词是否包含排序列表,反向排序列表和未排序列表中的元素也可能是明智的选择。
Do you want to get really neurotic about this? 您是否想对此感到神经过敏? It's always interesting to confirm the performance of data structures.
确认数据结构的性能总是很有趣的。 A stack should have constant performance.
堆栈应具有恒定的性能。 So for each k in [0,n-1), you could push k elements, then push and pop a (k+1)st element 1e8 times, and make sure the times are comparable.
因此,对于[0,n-1)中的每个k,您可以推入k个元素,然后将第(k + 1)个元素推入并弹出1e8次,并确保时间可比。 I can imagine several brain dead implementations where element n-1 would take n times longer to push and pop than element 0.
我可以想象几种脑瘫的实现方式,其中元素n-1的推入和弹出要比元素0花费n倍的时间。
If there are any other methods apart from push and pop (eg length and top ), test those too. 如果除了push和pop之外还有其他方法(例如, length和top ),也请进行测试。
For top, test: 对于顶部,测试:
For length, test on: 对于长度,请测试:
Multithreading: it would be good to write a test in which at least 3 threads hammer on the data structure simultaneously. 多线程:最好编写一个测试,其中至少3个线程同时锤击数据结构。 And make sure you run it on a multiprocessor box, so you get true concurrency, not just time slicing.
并确保在多处理器机器上运行它,这样您才能获得真正的并发,而不仅仅是时间分割。
And don't forget the case where: 并且不要忘记以下情况:
if( !stack.Empty )
{
// oops, another thread emptied the stack and now this call will throw an error:
stack.Pop();
}
Okay from the comments 好的,来自评论
Testing of stack may be divided in 堆栈测试可以分为
Functionality testing
Performance testing
Security testing: if data pushed onto stack are strings , then we may need to check for buffer overflows
if you are talking about writing a test case to figure out if a stack is emplty or not, then this is it 如果您正在谈论编写测试用例以弄清堆栈是否是示例性的,那么就是这样
public class LinkStackTest {
protected StackInterface<String> stack;
protected int size;
@Before public void setUp() {
stack = new LinkStack<String>();
}
@Test public void empty() {
assertTrue(stack.isEmpty());
assertEquals(0, stack.size());
}
I think there is one more and the simplest thing that someone should look at. 我认为还有其他事情应该是最简单的事情。 Stack is a LIFO data structure so if you fill up(consequent pushes) the given stack with the sequence of items given "a,b,c,d.......w", you should get the reverse sequence(w,....,d,c,b,a) when you pop all the elements of the stack.
Stack是一种LIFO数据结构,因此,如果您使用给定的序列“ a,b,c,d ....... w”填充(随后推动)给定的堆栈,则应该获得相反的顺序(w ,....,d,c,b,a)弹出堆栈中的所有元素。 Someone should check whether the main property of the stack works correct or not.
有人应该检查堆栈的main属性是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.