PriorityQueueTest.php 4.0 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_Search_Lucene
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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. * PHPUnit test case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Search_Lucene
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Search_Lucene
  37. */
  38. class Zend_Search_Lucene_PriorityQueueTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testCreate()
  41. {
  42. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  43. $this->assertTrue($queue instanceof Zend_Search_Lucene_PriorityQueue);
  44. }
  45. public function testPut()
  46. {
  47. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  48. $queue->put(1);
  49. $queue->put(100);
  50. $queue->put(46);
  51. $queue->put(347);
  52. $queue->put(11);
  53. $queue->put(125);
  54. $queue->put(-10);
  55. $queue->put(100);
  56. }
  57. public function testPop()
  58. {
  59. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  60. $queue->put( 1);
  61. $queue->put( 100);
  62. $queue->put( 46);
  63. $queue->put( 347);
  64. $queue->put( 11);
  65. $queue->put( 125);
  66. $queue->put(-10);
  67. $queue->put( 100);
  68. $this->assertEquals($queue->pop(), -10);
  69. $this->assertEquals($queue->pop(), 1 );
  70. $this->assertEquals($queue->pop(), 11 );
  71. $this->assertEquals($queue->pop(), 46 );
  72. $this->assertEquals($queue->pop(), 100);
  73. $this->assertEquals($queue->pop(), 100);
  74. $this->assertEquals($queue->pop(), 125);
  75. $queue->put( 144);
  76. $queue->put( 546);
  77. $queue->put( 15);
  78. $queue->put( 125);
  79. $queue->put( 325);
  80. $queue->put(-12);
  81. $queue->put( 347);
  82. $this->assertEquals($queue->pop(), -12);
  83. $this->assertEquals($queue->pop(), 15 );
  84. $this->assertEquals($queue->pop(), 125);
  85. $this->assertEquals($queue->pop(), 144);
  86. $this->assertEquals($queue->pop(), 325);
  87. $this->assertEquals($queue->pop(), 347);
  88. $this->assertEquals($queue->pop(), 347);
  89. $this->assertEquals($queue->pop(), 546);
  90. }
  91. public function testClear()
  92. {
  93. $queue = new Zend_Search_Lucene_PriorityQueue_testClass();
  94. $queue->put( 1);
  95. $queue->put( 100);
  96. $queue->put( 46);
  97. $queue->put(-10);
  98. $queue->put( 100);
  99. $this->assertEquals($queue->pop(), -10);
  100. $this->assertEquals($queue->pop(), 1 );
  101. $this->assertEquals($queue->pop(), 46 );
  102. $queue->clear();
  103. $this->assertEquals($queue->pop(), null);
  104. $queue->put( 144);
  105. $queue->put( 546);
  106. $queue->put( 15);
  107. $this->assertEquals($queue->pop(), 15 );
  108. $this->assertEquals($queue->pop(), 144);
  109. $this->assertEquals($queue->pop(), 546);
  110. }
  111. }
  112. class Zend_Search_Lucene_PriorityQueue_testClass extends Zend_Search_Lucene_PriorityQueue
  113. {
  114. /**
  115. * Compare elements
  116. *
  117. * Returns true, if $el1 is less than $el2; else otherwise
  118. *
  119. * @param mixed $el1
  120. * @param mixed $el2
  121. * @return boolean
  122. */
  123. protected function _less($el1, $el2)
  124. {
  125. return ($el1 < $el2);
  126. }
  127. }