1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
<?php
/**
* Test for filter utilities.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
class SearchSuggestionSetTest extends \MediaWikiUnitTestCase {
/**
* Test that adding a new suggestion at the end
* will keep proper score ordering
* @covers SearchSuggestionSet::append
*/
public function testAppend() {
$set = SearchSuggestionSet::emptySuggestionSet();
$this->assertSame( 0, $set->getSize() );
$set->append( new SearchSuggestion( 3 ) );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$suggestion = new SearchSuggestion( 4 );
$set->append( $suggestion );
$this->assertEquals( 2, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$this->assertEquals( 2, $suggestion->getScore() );
$suggestion = new SearchSuggestion( 2 );
$set->append( $suggestion );
$this->assertSame( 1, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$this->assertSame( 1, $suggestion->getScore() );
$scores = $set->map( function ( $s ) {
return $s->getScore();
} );
$sorted = $scores;
asort( $sorted );
$this->assertEquals( $sorted, $scores );
}
/**
* Test that adding a new best suggestion will keep proper score
* ordering
* @covers SearchSuggestionSet::getWorstScore
* @covers SearchSuggestionSet::getBestScore
* @covers SearchSuggestionSet::prepend
*/
public function testInsertBest() {
$set = SearchSuggestionSet::emptySuggestionSet();
$this->assertSame( 0, $set->getSize() );
$set->prepend( new SearchSuggestion( 3 ) );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 3, $set->getBestScore() );
$suggestion = new SearchSuggestion( 4 );
$set->prepend( $suggestion );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 4, $set->getBestScore() );
$this->assertEquals( 4, $suggestion->getScore() );
$suggestion = new SearchSuggestion( 0 );
$set->prepend( $suggestion );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 5, $set->getBestScore() );
$this->assertEquals( 5, $suggestion->getScore() );
$suggestion = new SearchSuggestion( 2 );
$set->prepend( $suggestion );
$this->assertEquals( 3, $set->getWorstScore() );
$this->assertEquals( 6, $set->getBestScore() );
$this->assertEquals( 6, $suggestion->getScore() );
$scores = $set->map( function ( $s ) {
return $s->getScore();
} );
$sorted = $scores;
asort( $sorted );
$this->assertEquals( $sorted, $scores );
}
/**
* @covers SearchSuggestionSet::shrink
*/
public function testShrink() {
$set = SearchSuggestionSet::emptySuggestionSet();
for ( $i = 0; $i < 100; $i++ ) {
$set->append( new SearchSuggestion( 0 ) );
}
$set->shrink( 10 );
$this->assertEquals( 10, $set->getSize() );
$set->shrink( 0 );
$this->assertSame( 0, $set->getSize() );
}
// TODO: test for fromTitles
}
|