开发者

How to write php unit test to test the stack functionality?

开发者 https://www.devze.com 2023-02-28 01:52 出处:网络
开发者_StackOverflow中文版I want to write a unit test for a class handling a stack of positive integers.
开发者_StackOverflow中文版

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);
0

精彩评论

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