/tests/Zend/Stdlib/PriorityQueueTest.php

https://github.com/necrogami/zf2 · PHP · 168 lines · 117 code · 21 blank · 30 comment · 3 complexity · 3372b92dc120d8b7a855b3f163101eb0 MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. namespace ZendTest\Stdlib;
  22. use Zend\Stdlib\PriorityQueue,
  23. SplPriorityQueue;
  24. /**
  25. * @category Zend
  26. * @package Zend_Stdlib
  27. * @subpackage UnitTests
  28. * @group Zend_Stdlib
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class PriorityQueueTest extends \PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * @var PriorityQueue
  36. */
  37. protected $queue;
  38. public function setUp()
  39. {
  40. $this->queue = new PriorityQueue();
  41. $this->queue->insert('foo', 3);
  42. $this->queue->insert('bar', 4);
  43. $this->queue->insert('baz', 2);
  44. $this->queue->insert('bat', 1);
  45. }
  46. public function testSerializationAndDeserializationShouldMaintainState()
  47. {
  48. $s = serialize($this->queue);
  49. $unserialized = unserialize($s);
  50. $count = count($this->queue);
  51. $this->assertSame($count, count($unserialized), 'Expected count ' . $count . '; received ' . count($unserialized));
  52. $expected = array();
  53. foreach ($this->queue as $item) {
  54. $expected[] = $item;
  55. }
  56. $test = array();
  57. foreach ($unserialized as $item) {
  58. $test[] = $item;
  59. }
  60. $this->assertSame($expected, $test, 'Expected: ' . var_export($expected, 1) . "\nReceived:" . var_export($test, 1));
  61. }
  62. public function testRetrievingQueueAsArrayReturnsDataOnlyByDefault()
  63. {
  64. $expected = array(
  65. 'foo',
  66. 'bar',
  67. 'baz',
  68. 'bat',
  69. );
  70. $test = $this->queue->toArray();
  71. $this->assertSame($expected, $test, var_export($test, 1));
  72. }
  73. public function testCanCastToArrayOfPrioritiesOnly()
  74. {
  75. $expected = array(
  76. 3,
  77. 4,
  78. 2,
  79. 1,
  80. );
  81. $test = $this->queue->toArray(PriorityQueue::EXTR_PRIORITY);
  82. $this->assertSame($expected, $test, var_export($test, 1));
  83. }
  84. public function testCanCastToArrayOfDataPriorityPairs()
  85. {
  86. $expected = array(
  87. array('data' => 'foo', 'priority' => 3),
  88. array('data' => 'bar', 'priority' => 4),
  89. array('data' => 'baz', 'priority' => 2),
  90. array('data' => 'bat', 'priority' => 1),
  91. );
  92. $test = $this->queue->toArray(PriorityQueue::EXTR_BOTH);
  93. $this->assertSame($expected, $test, var_export($test, 1));
  94. }
  95. public function testCanIterateMultipleTimesAndReceiveSameResults()
  96. {
  97. $expected = array('bar', 'foo', 'baz', 'bat');
  98. for ($i = 1; $i < 3; $i++) {
  99. $test = array();
  100. foreach ($this->queue as $item) {
  101. $test[] = $item;
  102. }
  103. $this->assertEquals($expected, $test, 'Failed at iteration ' . $i);
  104. }
  105. }
  106. public function testCanRemoveItemFromQueue()
  107. {
  108. $this->queue->remove('baz');
  109. $expected = array('bar', 'foo', 'bat');
  110. $test = array();
  111. foreach ($this->queue as $item) {
  112. $test[] = $item;
  113. }
  114. $this->assertEquals($expected, $test);
  115. }
  116. public function testCanTestForExistenceOfItemInQueue()
  117. {
  118. $this->assertTrue($this->queue->contains('foo'));
  119. $this->assertFalse($this->queue->contains('foobar'));
  120. }
  121. public function testCanTestForExistenceOfPriorityInQueue()
  122. {
  123. $this->assertTrue($this->queue->hasPriority(3));
  124. $this->assertFalse($this->queue->hasPriority(1000));
  125. }
  126. public function testCloningAlsoClonesQueue()
  127. {
  128. $foo = new \stdClass();
  129. $foo->name = 'bar';
  130. $queue = new PriorityQueue();
  131. $queue->insert($foo, 1);
  132. $queue->insert($foo, 2);
  133. $queueClone = clone $queue;
  134. while (!$queue->isEmpty()) {
  135. $this->assertSame($foo, $queue->top());
  136. $queue->remove($queue->top());
  137. }
  138. $this->assertTrue($queue->isEmpty());
  139. $this->assertFalse($queueClone->isEmpty());
  140. $this->assertEquals(2, $queueClone->count());
  141. while (!$queueClone->isEmpty()) {
  142. $this->assertSame($foo, $queueClone->top());
  143. $queueClone->remove($queueClone->top());
  144. }
  145. $this->assertTrue($queueClone->isEmpty());
  146. }
  147. }