PriorityQueueTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_Search_Lucene
  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. * @version $Id$
  21. */
  22. /**
  23. * Zend_Search_Lucene_PriorityQueue
  24. */
  25. require_once 'Zend/Search/Lucene/PriorityQueue.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Search_Lucene
  29. * @subpackage UnitTests
  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. * @group Zend_Search_Lucene
  33. */
  34. class Zend_Search_Lucene_PriorityQueueTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testCreate()
  37. {
  38. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  39. $this->assertTrue($queue instanceof Zend_Search_Lucene_PriorityQueue);
  40. }
  41. public function testPut()
  42. {
  43. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  44. $queue->put(1);
  45. $queue->put(100);
  46. $queue->put(46);
  47. $queue->put(347);
  48. $queue->put(11);
  49. $queue->put(125);
  50. $queue->put(-10);
  51. $queue->put(100);
  52. }
  53. public function testPop()
  54. {
  55. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  56. $queue->put( 1);
  57. $queue->put( 100);
  58. $queue->put( 46);
  59. $queue->put( 347);
  60. $queue->put( 11);
  61. $queue->put( 125);
  62. $queue->put(-10);
  63. $queue->put( 100);
  64. $this->assertEquals($queue->pop(), -10);
  65. $this->assertEquals($queue->pop(), 1 );
  66. $this->assertEquals($queue->pop(), 11 );
  67. $this->assertEquals($queue->pop(), 46 );
  68. $this->assertEquals($queue->pop(), 100);
  69. $this->assertEquals($queue->pop(), 100);
  70. $this->assertEquals($queue->pop(), 125);
  71. $queue->put( 144);
  72. $queue->put( 546);
  73. $queue->put( 15);
  74. $queue->put( 125);
  75. $queue->put( 325);
  76. $queue->put(-12);
  77. $queue->put( 347);
  78. $this->assertEquals($queue->pop(), -12);
  79. $this->assertEquals($queue->pop(), 15 );
  80. $this->assertEquals($queue->pop(), 125);
  81. $this->assertEquals($queue->pop(), 144);
  82. $this->assertEquals($queue->pop(), 325);
  83. $this->assertEquals($queue->pop(), 347);
  84. $this->assertEquals($queue->pop(), 347);
  85. $this->assertEquals($queue->pop(), 546);
  86. }
  87. public function testClear()
  88. {
  89. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  90. $queue->put( 1);
  91. $queue->put( 100);
  92. $queue->put( 46);
  93. $queue->put(-10);
  94. $queue->put( 100);
  95. $this->assertEquals($queue->pop(), -10);
  96. $this->assertEquals($queue->pop(), 1 );
  97. $this->assertEquals($queue->pop(), 46 );
  98. $queue->clear();
  99. $this->assertEquals($queue->pop(), null);
  100. $queue->put( 144);
  101. $queue->put( 546);
  102. $queue->put( 15);
  103. $this->assertEquals($queue->pop(), 15 );
  104. $this->assertEquals($queue->pop(), 144);
  105. $this->assertEquals($queue->pop(), 546);
  106. }
  107. }
  108. class Zend_Search_Lucene_PriorityQueue_testClass extends Zend_Search_Lucene_PriorityQueue
  109. {
  110. /**
  111. * Compare elements
  112. *
  113. * Returns true, if $el1 is less than $el2; else otherwise
  114. *
  115. * @param mixed $el1
  116. * @param mixed $el2
  117. * @return boolean
  118. */
  119. protected function _less($el1, $el2)
  120. {
  121. return ($el1 < $el2);
  122. }
  123. }