summaryrefslogtreecommitdiff
path: root/tests/unit/Collections/TQueueTest.php
blob: 531214aa931c58bfd89e8562073f48e1c0f12fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

use Prado\Collections\TQueue;
use Prado\Exceptions\TInvalidDataTypeException;
use Prado\Exceptions\TInvalidOperationException;


/**
 * @package System.Collections
 */
class TQueueTest extends PHPUnit_Framework_TestCase {

	public function setUp() {
	}

	public function tearDown() {
	}

	public function testConstruct() {
		$queue = new TQueue();
		self::assertEquals(array(), $queue->toArray());
		$queue = new TQueue(array(1, 2, 3));
		self::assertEquals(array(1, 2, 3), $queue->toArray());
	}

	public function testToArray() {
		$queue = new TQueue(array(1, 2, 3));
		self::assertEquals(array(1, 2, 3), $queue->toArray());
	}

	public function testCopyFrom() {
		$queue = new TQueue(array(1, 2, 3));
		$data = array(4, 5, 6);
		$queue->copyFrom($data);
		self::assertEquals(array(4, 5, 6), $queue->toArray());
	}

	public function testCanNotCopyFromNonTraversableTypes() {
		$queue = new TQueue();
		$data = new stdClass();
		try {
			$queue->copyFrom($data);
		} catch(TInvalidDataTypeException $e) {
			return;
		}
		self::fail('An expected TInvalidDataTypeException was not raised');
	}

	public function testClear() {
		$queue = new TQueue(array(1, 2, 3));
		$queue->clear();
		self::assertEquals(array(), $queue->toArray());
	}

	public function testContains() {
		$queue = new TQueue(array(1, 2, 3));
		self::assertEquals(true, $queue->contains(2));
		self::assertEquals(false, $queue->contains(4));
	}

	public function testPeek() {
		$queue = new TQueue(array(1,2,3));
		self::assertEquals(1, $queue->peek());
	}

	public function testCanNotPeekAnEmptyQueue() {
		$queue = new TQueue();
		try {
			$item = $queue->peek();
		} catch(TInvalidOperationException $e) {
			return;
		}
		self::fail('An expected TInvalidOperationException was not raised');
	}

	public function testDequeue() {
		$queue = new TQueue(array(1, 2, 3));
		$first = $queue->dequeue();
		self::assertEquals(1, $first);
		self::assertEquals(array(2, 3), $queue->toArray());
	}

	public function testCanNotDequeueAnEmptyQueue() {
		$queue = new TQueue();
		try {
			$item = $queue->dequeue();
		} catch(TInvalidOperationException $e) {
			return;
		}
		self::fail('An expected TInvalidOperationException was not raised');
	}

	public function testEnqueue() {
		$queue = new TQueue();
		$queue->enqueue(1);
		self::assertEquals(array(1), $queue->toArray());
	}

 	public function testGetIterator() {
		$queue = new TQueue(array(1, 2));
		self::assertInstanceOf('ArrayIterator', $queue->getIterator());
		$n = 0;
		$found = 0;
		foreach($queue as $index => $item) {
			foreach($queue as $a => $b); // test of iterator
			$n++;
			if($index === 0 && $item === 1) {
				$found++;
			}
			if($index === 1 && $item === 2) {
				$found++;
			}
		}
		self::assertTrue($n == 2 && $found == 2);
	}

	public function testGetCount() {
    	$queue = new TQueue();
		self::assertEquals(0, $queue->getCount());
		$queue = new TQueue(array(1, 2, 3));
		self::assertEquals(3, $queue->getCount());
	}

	public function testCountable() {
		$queue = new TQueue();
		self::assertEquals(0, count($queue));
		$queue = new TQueue(array(1, 2, 3));
		self::assertEquals(3, count($queue));
	}

}