]> git.openstreetmap.org Git - nominatim.git/blob - tests-php/Nominatim/NominatimTest.php
allow parameters to be empty
[nominatim.git] / tests-php / Nominatim / NominatimTest.php
1 <?php
2
3 namespace Nominatim;
4 require '../lib/lib.php';
5
6
7 class NominatimTest extends \PHPUnit_Framework_TestCase
8 {
9
10         protected function setUp()
11         {
12         }
13
14
15         public function test_getClassTypesWithImportance()
16         {
17                 $aClasses = getClassTypesWithImportance();
18
19                 $this->assertGreaterThan(
20                         200,
21                         count($aClasses)
22                 );
23
24                 $this->assertEquals(
25                         array(
26                                 'label' => "Country",
27                                 'frequency' => 0,
28                                 'icon' => "poi_boundary_administrative",
29                                 'defzoom' => 6,
30                                 'defdiameter' => 15,
31                                 'importance' => 3
32                         ),
33                         $aClasses['place:country']
34                 );
35         }
36
37
38         public function test_getResultDiameter()
39         {
40                 $aResult = array();
41                 $this->assertEquals(
42                         0.0001,
43                         getResultDiameter($aResult)
44                 );
45
46                 $aResult = array('class' => 'place', 'type' => 'country');
47                 $this->assertEquals(
48                         15,
49                         getResultDiameter($aResult)
50                 );
51
52                 $aResult = array('class' => 'boundary', 'type' => 'administrative', 'admin_level' => 6);
53                 $this->assertEquals(
54                         0.32,
55                         getResultDiameter($aResult)
56                 );
57         }
58
59
60         public function test_addQuotes()
61         {
62                 // FIXME: not quoting existing quote signs is probably a bug
63                 $this->assertSame("'St. John's'", addQuotes("St. John's"));
64                 $this->assertSame("''", addQuotes(''));
65         }
66
67         public function test_looksLikeLatLonPair()
68         {
69                 // no coordinates expected
70                 $this->assertNull(looksLikeLatLonPair(''));
71                 $this->assertNull(looksLikeLatLonPair('abc'));
72                 $this->assertNull(looksLikeLatLonPair('12 34'));
73                 $this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
74
75                 // coordinates expected
76                 $this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
77
78                 $this->assertEquals(
79                                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc   def'),
80                                 looksLikeLatLonPair(' abc 12.456 -78.90 def ')
81                         );
82
83                 $this->assertEquals(
84                                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
85                                 looksLikeLatLonPair(' [12.456,-78.90] ')
86                         );
87
88                 // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
89                 // these all represent the same location
90                 $aQueries = array(
91                                         '40 26.767 N 79 58.933 W',
92                                         '40° 26.767′ N 79° 58.933′ W',
93                                         "40° 26.767' N 79° 58.933' W",
94                                         'N 40 26.767, W 79 58.933',
95                                         'N 40°26.767′, W 79°58.933′',
96                                         "N 40°26.767', W 79°58.933'",
97
98                                         '40 26 46 N 79 58 56 W',
99                                         '40° 26′ 46″ N 79° 58′ 56″ W',
100                                         'N 40 26 46 W 79 58 56',
101                                         'N 40° 26′ 46″, W 79° 58′ 56″',
102                                         'N 40° 26\' 46", W 79° 58\' 56"',
103
104                                         '40.446 -79.982',
105                                         '40.446,-79.982',
106                                         '40.446° N 79.982° W',
107                                         'N 40.446° W 79.982°',
108
109                                         '[40.446 -79.982]',
110                                         '       40.446  ,   -79.982     ',
111                 );
112
113
114                 foreach($aQueries as $sQuery){
115                         $aRes = looksLikeLatLonPair($sQuery);
116                         $this->assertEquals( 40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
117                         $this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
118                 }
119
120         }
121
122
123
124         public function test_getWordSets()
125         {
126
127                 // given an array of arrays like
128                 // array( array('a','b'), array('c','d') )
129                 // returns a summary as string: '(a|b),(c|d)'
130                 function serialize_sets($aSets)
131                 {       
132                         $aParts = array();
133                         foreach($aSets as $aSet){
134                                 $aParts[] = '(' . join('|', $aSet) . ')';
135                         }
136                         return join(',', $aParts);
137                 }
138
139                 $this->assertEquals(
140                         array(array('')),
141                         getWordSets(array(),0)
142                 );
143
144                 $this->assertEquals(
145                         '(a)',
146                         serialize_sets( getWordSets(array("a"),0) )
147                 );
148
149                 $this->assertEquals(
150                         '(a b),(a|b)',
151                         serialize_sets( getWordSets(array('a','b'),0) )
152                 );
153
154                 $this->assertEquals(
155                         '(a b c),(a|b c),(a|b|c),(a b|c)',
156                         serialize_sets( getWordSets(array('a','b','c'),0) )
157                 );
158
159                 $this->assertEquals(
160                         '(a b c d),(a|b c d),(a|b|c d),(a|b|c|d),(a|b c|d),(a b|c d),(a b|c|d),(a b c|d)',
161                         serialize_sets( getWordSets(array('a','b','c','d'),0) )
162                 );
163
164
165                 // Inverse
166                 $this->assertEquals(
167                         '(a b c),(c|a b),(c|b|a),(b c|a)',
168                         serialize_sets( getInverseWordSets(array('a','b','c'),0) )
169                 );
170
171
172                 // make sure we don't create too many sets
173                 // 4 words => 8 sets
174                 // 10 words => 511 sets
175                 // 15 words => 12911 sets
176                 // 18 words => 65536 sets
177                 // 20 words => 169766 sets
178                 // 22 words => 401930 sets
179                 // 28 words => 3505699 sets (needs more than 4GB via 'phpunit -d memory_limit=' to run)
180                 $this->assertEquals(
181                         8,
182                         count( getWordSets(array_fill( 0, 4, 'a'),0) )
183                 );
184
185
186                 $this->assertEquals(
187                         65536,
188                         count( getWordSets(array_fill( 0, 18, 'a'),0) )
189                 );
190         }
191
192
193
194         // you might say we're creating a circle
195         public function test_createPointsAroundCenter()
196         {
197                 $aPoints = createPointsAroundCenter(0, 0, 2);
198
199                 $this->assertEquals(
200                         101,
201                         count($aPoints)
202                 );
203                 $this->assertEquals(
204                         array(
205                                 ['', 0, 2],
206                                 ['', 0.12558103905863, 1.9960534568565],
207                                 ['', 0.25066646712861, 1.984229402629]
208                         ),
209                         array_splice($aPoints, 0, 3)
210                 );
211         }
212
213         public function test_geometryText2Points()
214         {
215                 $fRadius = 1;
216                 // invalid value
217                 $this->assertEquals(
218                         NULL,
219                         geometryText2Points('', $fRadius)
220                 );
221
222                 // POINT
223                 $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
224                 $this->assertEquals(
225                         101,
226                         count($aPoints)
227                 );
228                 $this->assertEquals(
229                         array(
230                                 [10, 21],
231                                 [10.062790519529, 20.998026728428],
232                                 [10.125333233564, 20.992114701314]
233                         ),
234                         array_splice($aPoints, 0,3)
235                 );
236
237                 // POLYGON
238                 $this->assertEquals(
239                         array(
240                                 ['30', '10'],
241                                 ['40', '40'],
242                                 ['20', '40'],
243                                 ['10', '20'],
244                                 ['30', '10']
245                         ),
246                         geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
247                 );
248
249                 // MULTIPOLYGON
250                 $this->assertEquals(
251                         array(
252                                 ['30', '20'], // first polygon only
253                                 ['45', '40'],
254                                 ['10', '40'],
255                                 ['30', '20'],
256                         ),
257                         geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
258                 );
259         }
260
261 }