summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitattributes1
-rw-r--r--tests/unit/Collections/AllTests.php2
-rw-r--r--tests/unit/Collections/TStackTest.php131
3 files changed, 134 insertions, 0 deletions
diff --git a/.gitattributes b/.gitattributes
index c69d2018..850fb4f6 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2799,6 +2799,7 @@ tests/unit/Collections/AllTests.php -text
tests/unit/Collections/TListTest.php -text
tests/unit/Collections/TMapTest.php -text
tests/unit/Collections/TQueueTest.php -text
+tests/unit/Collections/TStackTest.php -text
tests/unit/Data/TDbCommandTest.php -text
tests/unit/Data/TDbConnectionTest.php -text
tests/unit/Data/TDbDataReaderTest.php -text
diff --git a/tests/unit/Collections/AllTests.php b/tests/unit/Collections/AllTests.php
index d9158f3a..a639a42c 100644
--- a/tests/unit/Collections/AllTests.php
+++ b/tests/unit/Collections/AllTests.php
@@ -8,6 +8,7 @@ if(!defined('PHPUnit_MAIN_METHOD')) {
require_once 'TListTest.php';
require_once 'TMapTest.php';
require_once 'TQueueTest.php';
+require_once 'TStackTest.php';
class Collections_AllTests {
public static function main() {
@@ -20,6 +21,7 @@ class Collections_AllTests {
$suite->addTestSuite('TListTest');
$suite->addTestSuite('TMapTest');
$suite->addTestSuite('TQueueTest');
+ $suite->addTestSuite('TStackTest');
return $suite;
}
diff --git a/tests/unit/Collections/TStackTest.php b/tests/unit/Collections/TStackTest.php
new file mode 100644
index 00000000..d9cf5fa7
--- /dev/null
+++ b/tests/unit/Collections/TStackTest.php
@@ -0,0 +1,131 @@
+<?php
+require_once dirname(__FILE__).'/../phpunit.php';
+
+Prado::using('System.Collections.TStack');
+
+/**
+ * @package System.Collections
+ */
+class TStackTest extends PHPUnit_Framework_TestCase {
+
+ public function setUp() {
+ }
+
+ public function tearDown() {
+ }
+
+ public function testConstruct() {
+ $stack = new TStack();
+ self::assertEquals(array(), $stack->toArray());
+ $stack = new TStack(array(1, 2, 3));
+ self::assertEquals(array(1, 2, 3), $stack->toArray());
+ }
+
+ public function testToArray() {
+ $stack = new TStack(array(1, 2, 3));
+ self::assertEquals(array(1, 2, 3), $stack->toArray());
+ }
+
+ public function testCopyFrom() {
+ $stack = new TStack(array(1, 2, 3));
+ $data = array(4, 5, 6);
+ $stack->copyFrom($data);
+ self::assertEquals(array(4, 5, 6), $stack->toArray());
+ }
+
+ public function testCanNotCopyFromNonTraversableTypes() {
+ $stack = new TStack();
+ $data = new stdClass();
+ try {
+ $stack->copyFrom($data);
+ } catch(TInvalidDataTypeException $e) {
+ return;
+ }
+ self::fail('An expected TInvalidDataTypeException was not raised');
+ }
+
+ public function testClear() {
+ $stack = new TStack(array(1, 2, 3));
+ $stack->clear();
+ self::assertEquals(array(), $stack->toArray());
+ }
+
+ public function testContains() {
+ $stack = new TStack(array(1, 2, 3));
+ self::assertEquals(true, $stack->contains(2));
+ self::assertEquals(false, $stack->contains(4));
+ }
+
+ public function testPeek() {
+ $stack = new TStack(array(1));
+ self::assertEquals(1, $stack->peek());
+ }
+
+ public function testCanNotPeekAnEmptyStack() {
+ $stack = new TStack();
+ try {
+ $item = $stack->peek();
+ } catch(TInvalidOperationException $e) {
+ return;
+ }
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+
+ public function testPop() {
+ $stack = new TStack(array(1, 2, 3));
+ $last = $stack->pop();
+ self::assertEquals(3, $last);
+ self::assertEquals(array(1, 2), $stack->toArray());
+ }
+
+ public function testCanNotPopAnEmptyStack() {
+ $stack = new TStack();
+ try {
+ $item = $stack->pop();
+ } catch(TInvalidOperationException $e) {
+ return;
+ }
+ self::fail('An expected TInvalidOperationException was not raised');
+ }
+
+ public function testPush() {
+ $stack = new TStack();
+ $stack->push(1);
+ self::assertEquals(array(1), $stack->toArray());
+ }
+
+ public function testGetIterator() {
+ $stack = new TStack(array(1, 2));
+ self::assertType('TStackIterator', $stack->getIterator());
+ $n = 0;
+ $found = 0;
+ foreach($stack as $index => $item) {
+ foreach($stack 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() {
+ $stack = new TStack();
+ self::assertEquals(0, $stack->getCount());
+ $stack = new TStack(array(1, 2, 3));
+ self::assertEquals(3, $stack->getCount());
+ }
+
+ public function testCount() {
+ $stack = new TStack();
+ self::assertEquals(0, $stack->count());
+ $stack = new TStack(array(1, 2, 3));
+ self::assertEquals(3, $stack->count());
+ }
+
+}
+
+?>