How to write php unit test to test the stack functionality?
I want to write a unit test for a class handling a stack of positive integers.
There are 4 methods in such class: push($x), pop(), getSize(), purge()
.
Please help me.
Class looks like :-
class Stack {
private $stack = array();
public function __construct() {
}
public function push($data) {
array_push($this->stack, $data);
}
public function pop() {
return array_pop($this->stack);
}
public function getSize() {
return count($this->stack);
}
public function purge($element) {
unset $this->stack[$element];
}
}
$s = new Stack();
One test would be
$s = new Stack();
$s->push(1);
assertEquals($s->getSize(), 1);
$s->push(2);
assertEquals($s->getSize(), 2);
assertEquals($s->pop(), 2);
assertEquals($s->getSize(), 1);
assertEquals($s->pop(), 1);
assertEquals($s->getSize(), 0);
精彩评论