PriorityQueueTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Stdlib
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. if (!defined('PHPUnit_MAIN_METHOD')) {
  22. define('PHPUnit_MAIN_METHOD', 'Zend_Stdlib_PriorityQueueTest::main');
  23. }
  24. require_once 'Zend/Stdlib/PriorityQueue.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Stdlib
  28. * @subpackage UnitTests
  29. * @group Zend_Stdlib
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Stdlib_PriorityQueueTest extends PHPUnit_Framework_TestCase
  34. {
  35. public static function main()
  36. {
  37. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  38. $result = PHPUnit_TextUI_TestRunner::run($suite);
  39. }
  40. public function setUp()
  41. {
  42. $this->queue = new Zend_Stdlib_PriorityQueue();
  43. $this->queue->insert('foo', 3);
  44. $this->queue->insert('bar', 4);
  45. $this->queue->insert('baz', 2);
  46. $this->queue->insert('bat', 1);
  47. }
  48. public function testSerializationAndDeserializationShouldMaintainState()
  49. {
  50. $s = serialize($this->queue);
  51. $unserialized = unserialize($s);
  52. $count = count($this->queue);
  53. $this->assertSame($count, count($unserialized), 'Expected count ' . $count . '; received ' . count($unserialized));
  54. $expected = array();
  55. foreach ($this->queue as $item) {
  56. $expected[] = $item;
  57. }
  58. $test = array();
  59. foreach ($unserialized as $item) {
  60. $test[] = $item;
  61. }
  62. $this->assertSame($expected, $test, 'Expected: ' . var_export($expected, 1) . "\nReceived:" . var_export($test, 1));
  63. }
  64. public function testRetrievingQueueAsArrayReturnsDataOnlyByDefault()
  65. {
  66. $expected = array(
  67. 'foo',
  68. 'bar',
  69. 'baz',
  70. 'bat',
  71. );
  72. $test = $this->queue->toArray();
  73. $this->assertSame($expected, $test, var_export($test, 1));
  74. }
  75. public function testCanCastToArrayOfPrioritiesOnly()
  76. {
  77. $expected = array(
  78. 3,
  79. 4,
  80. 2,
  81. 1,
  82. );
  83. $test = $this->queue->toArray(Zend_Stdlib_PriorityQueue::EXTR_PRIORITY);
  84. $this->assertSame($expected, $test, var_export($test, 1));
  85. }
  86. public function testCanCastToArrayOfDataPriorityPairs()
  87. {
  88. $expected = array(
  89. array('data' => 'foo', 'priority' => 3),
  90. array('data' => 'bar', 'priority' => 4),
  91. array('data' => 'baz', 'priority' => 2),
  92. array('data' => 'bat', 'priority' => 1),
  93. );
  94. $test = $this->queue->toArray(Zend_Stdlib_PriorityQueue::EXTR_BOTH);
  95. $this->assertSame($expected, $test, var_export($test, 1));
  96. }
  97. public function testCanIterateMultipleTimesAndReceiveSameResults()
  98. {
  99. $expected = array('bar', 'foo', 'baz', 'bat');
  100. for ($i = 1; $i < 3; $i++) {
  101. $test = array();
  102. foreach ($this->queue as $item) {
  103. $test[] = $item;
  104. }
  105. $this->assertEquals($expected, $test, 'Failed at iteration ' . $i);
  106. }
  107. }
  108. public function testCanRemoveItemFromQueue()
  109. {
  110. $this->queue->remove('baz');
  111. $expected = array('bar', 'foo', 'bat');
  112. $test = array();
  113. foreach ($this->queue as $item) {
  114. $test[] = $item;
  115. }
  116. $this->assertEquals($expected, $test);
  117. }
  118. public function testCanTestForExistenceOfItemInQueue()
  119. {
  120. $this->assertTrue($this->queue->contains('foo'));
  121. $this->assertFalse($this->queue->contains('foobar'));
  122. }
  123. public function testCanTestForExistenceOfPriorityInQueue()
  124. {
  125. $this->assertTrue($this->queue->hasPriority(3));
  126. $this->assertFalse($this->queue->hasPriority(1000));
  127. }
  128. }
  129. if (PHPUnit_MAIN_METHOD == 'Zend_Stdlib_PriorityQueueTest::main') {
  130. Zend_Stdlib_PriorityQueueTest::main();
  131. }