]> git.openstreetmap.org Git - nominatim.git/blob - tests-php/Nominatim/NominatimTest.php
adapt to code style conventions
[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_addQuotes()
16         {
17                 // FIXME: not quoting existing quote signs is probably a bug
18                 $this->assertSame("'St. John's'", addQuotes("St. John's"));
19                 $this->assertSame("''", addQuotes(''));
20         }
21
22         public function test_looksLikeLatLonPair()
23         {
24                 // no coordinates expected
25                 $this->assertNull(looksLikeLatLonPair(''));
26                 $this->assertNull(looksLikeLatLonPair('abc'));
27                 $this->assertNull(looksLikeLatLonPair('12 34'));
28                 $this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
29
30                 // coordinates expected
31                 $this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
32
33                 $this->assertEquals(
34                                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc   def'),
35                                 looksLikeLatLonPair(' abc 12.456 -78.90 def ')
36                         );
37
38                 $this->assertEquals(
39                                 array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
40                                 looksLikeLatLonPair(' [12.456,-78.90] ')
41                         );
42
43                 // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
44                 // these all represent the same location
45                 $aQueries = array(
46                                         '40 26.767 N 79 58.933 W',
47                                         '40° 26.767′ N 79° 58.933′ W',
48                                         "40° 26.767' N 79° 58.933' W",
49                                         'N 40 26.767, W 79 58.933',
50                                         'N 40°26.767′, W 79°58.933′',
51                                         "N 40°26.767', W 79°58.933'",
52
53                                         '40 26 46 N 79 58 56 W',
54                                         '40° 26′ 46″ N 79° 58′ 56″ W',
55                                         'N 40 26 46 W 79 58 56',
56                                         'N 40° 26′ 46″, W 79° 58′ 56″',
57                                         'N 40° 26\' 46", W 79° 58\' 56"',
58
59                                         '40.446 -79.982',
60                                         '40.446,-79.982',
61                                         '40.446° N 79.982° W',
62                                         'N 40.446° W 79.982°',
63
64                                         '[40.446 -79.982]',
65                                         '       40.446  ,   -79.982     ',
66                 );
67
68
69                 foreach($aQueries as $sQuery){
70                         $aRes = looksLikeLatLonPair($sQuery);
71                         $this->assertEquals( 40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
72                         $this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
73                 }
74
75         }
76
77
78
79         public function test_getWordSets()
80         {
81
82                 // given an array of arrays like
83                 // array( array('a','b'), array('c','d') )
84                 // returns a summary as string: '(a|b),(c|d)'
85                 function serialize_sets($aSets)
86                 {       
87                         $aParts = array();
88                         foreach($aSets as $aSet){
89                                 $aParts[] = '(' . join('|', $aSet) . ')';
90                         }
91                         return join(',', $aParts);
92                 }
93
94                 $this->assertEquals(
95                         array(array('')),
96                         getWordSets(array(),0)
97                 );
98
99                 $this->assertEquals(
100                         '(a)',
101                         serialize_sets( getWordSets(array("a"),0) )
102                 );
103
104                 $this->assertEquals(
105                         '(a b),(a|b)',
106                         serialize_sets( getWordSets(array('a','b'),0) )
107                 );
108
109                 $this->assertEquals(
110                         '(a b c),(a|b c),(a|b|c),(a b|c)',
111                         serialize_sets( getWordSets(array('a','b','c'),0) )
112                 );
113
114                 $this->assertEquals(
115                         '(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)',
116                         serialize_sets( getWordSets(array('a','b','c','d'),0) )
117                 );
118
119
120                 // Inverse
121                 $this->assertEquals(
122                         '(a b c),(c|a b),(c|b|a),(b c|a)',
123                         serialize_sets( getInverseWordSets(array('a','b','c'),0) )
124                 );
125
126
127                 // make sure we don't create too many sets
128                 // 4 words => 8 sets
129                 // 10 words => 511 sets
130                 // 15 words => 12911 sets
131                 // 18 words => 65536 sets
132                 // 20 words => 169766 sets
133                 // 22 words => 401930 sets
134                 // 28 words => 3505699 sets (needs more than 4GB via 'phpunit -d memory_limit=' to run)
135                 $this->assertEquals(
136                         8,
137                         count( getWordSets(array_fill( 0, 4, 'a'),0) )
138                 );
139
140
141                 $this->assertEquals(
142                         65536,
143                         count( getWordSets(array_fill( 0, 18, 'a'),0) )
144                 );
145
146         }
147
148
149
150
151         public function test_geometryText2Points()
152         {
153                 $fRadius = 1;
154
155                 // invalid value
156                 $this->assertEquals(
157                         NULL,
158                         geometryText2Points('', $fRadius)
159                 );
160
161
162                 // POINT
163                 $aPoints = geometryText2Points('POINT(10 20)', $fRadius);
164                 $this->assertEquals(
165                         101,
166                         count($aPoints)
167                 );
168
169                 $this->assertEquals(
170
171                 array(
172                         ['', 10, 21],
173                         ['', 10.062790519529, 20.998026728428],
174                                 ['', 10.125333233564, 20.992114701314]
175                         ),
176                         array_splice($aPoints, 0,3)
177                 );
178
179                 // POLYGON
180                 $this->assertEquals(
181                         array(
182                                 ['30 10', '30', '10'],
183                                 ['40 40', '40', '40'],
184                                 ['20 40', '20', '40'],
185                                 ['10 20', '10', '20'],
186                                 ['30 10', '30', '10']
187                         ),
188                         geometryText2Points('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', $fRadius)
189                 );
190
191                 // MULTIPOLYGON
192                 // only the first polygon is used
193                 $this->assertEquals(
194                         array(
195                                 ['30 20', '30', '20'],
196                                 ['45 40', '45', '40'],
197                                 ['10 40', '10', '40'],
198                                 ['30 20', '30', '20'],
199
200                                 // ['15 5' , '15', '5' ],
201                                 // ['45 10', '45', '10'],
202                                 // ['10 20', '10', '20'],
203                                 // ['5 10' , '5' , '10'],
204                                 // ['15 5' , '15', '5' ]
205                         ),
206                         geometryText2Points('MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', $fRadius)
207                 );
208
209
210         }
211
212
213 }