assertSame(0, $heap->count()); $this->assertNull($heap->pop()); $heap->push(1); $this->assertSame(1, $heap->count()); $this->assertSame(1, $heap->pop()); $this->assertSame(0, $heap->count()); $this->assertNull($heap->pop()); $heap->push(2); $heap->push(1); $heap->push(4); $heap->push(3); $this->assertSame(4, $heap->count()); $this->assertSame(1, $heap->pop()); $this->assertSame(3, $heap->count()); $this->assertSame(2, $heap->pop()); $this->assertSame(2, $heap->count()); $heap->push(-1); $this->assertSame(3, $heap->count()); $this->assertSame(-1, $heap->pop()); $this->assertSame(2, $heap->count()); $this->assertSame(3, $heap->pop()); $this->assertSame(1, $heap->count()); $this->assertSame(4, $heap->pop()); $this->assertSame(0, $heap->count()); $this->assertNull($heap->pop()); } public function testHeapIt() { $heap = new PlHeap(array('HeapTest', 'compare')); $heap->push(2); $heap->push(1); $heap->push(4); $heap->push(3); $it = $heap->iterator(); $this->assertSame(4, $it->total()); $this->assertSame(1, $it->next()); $this->assertTrue($it->first()); $this->assertFalse($it->last()); $this->assertSame(2, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(3, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(4, $it->next()); $this->assertFalse($it->first()); $this->assertTrue($it->last()); $this->assertNull($it->next()); $this->assertSame(4, $heap->count()); } public function testMergeSortedIterator() { $its = array(); $its[] = PlIteratorUtils::fromArray(array(2, 4, 8, 16), 1, true); $its[] = PlIteratorUtils::fromArray(array(3, 9, 27), 1, true); $its[] = PlIteratorUtils::fromArray(array(4, 16, 32), 1, true); $it = PlIteratorUtils::merge($its, array('HeapTest', 'compare')); $this->assertSame(10, $it->total()); $this->assertSame(2, $it->next()); $this->assertTrue($it->first()); $this->assertFalse($it->last()); $this->assertSame(3, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(4, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(4, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(8, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(9, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(16, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(16, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(27, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(32, $it->next()); $this->assertFalse($it->first()); $this->assertTrue($it->last()); $this->assertNull($it->next()); } public function testMergeUnsortedIterator() { $its = array(); $its[] = PlIteratorUtils::fromArray(array(8, 4, 16, 2), 1, true); $its[] = PlIteratorUtils::fromArray(array(3, 27, 9), 1, true); $its[] = PlIteratorUtils::fromArray(array(32, 4, 16), 1, true); $it = PlIteratorUtils::merge($its, array('HeapTest', 'compare'), false); $this->assertSame(10, $it->total()); $this->assertSame(2, $it->next()); $this->assertTrue($it->first()); $this->assertFalse($it->last()); $this->assertSame(3, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(4, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(4, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(8, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(9, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(16, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(16, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(27, $it->next()); $this->assertFalse($it->first()); $this->assertFalse($it->last()); $this->assertSame(32, $it->next()); $this->assertFalse($it->first()); $this->assertTrue($it->last()); $this->assertNull($it->next()); } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?>