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