]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/LibTest.php
Merge pull request #1804 from lonvia/ranking-improvement-germany
[nominatim.git] / test / php / Nominatim / LibTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/lib.php');
6 require_once(CONST_BasePath.'/lib/ClassTypes.php');
7
8 class LibTest extends \PHPUnit\Framework\TestCase
9 {
10
11     public function testAddQuotes()
12     {
13         // FIXME: not quoting existing quote signs is probably a bug
14         $this->assertSame("'St. John's'", addQuotes("St. John's"));
15         $this->assertSame("''", addQuotes(''));
16     }
17
18
19     public function testCreatePointsAroundCenter()
20     {
21         // you might say we're creating a circle
22         $aPoints = createPointsAroundCenter(0, 0, 2);
23
24         $this->assertEquals(
25             101,
26             count($aPoints)
27         );
28         $this->assertEquals(
29             array(
30              array('', 0, 2),
31              array('', 0.12558103905863, 1.9960534568565),
32              array('', 0.25066646712861, 1.984229402629)
33             ),
34             array_splice($aPoints, 0, 3)
35         );
36     }
37
38     public function testParseLatLon()
39     {
40         // no coordinates expected
41         $this->assertFalse(parseLatLon(''));
42         $this->assertFalse(parseLatLon('abc'));
43         $this->assertFalse(parseLatLon('12 34'));
44
45         // coordinates expected
46         $this->assertNotNull(parseLatLon('0.0 -0.0'));
47
48         $aRes = parseLatLon(' abc 12.456 -78.90 def ');
49         $this->assertEquals($aRes[1], 12.456);
50         $this->assertEquals($aRes[2], -78.90);
51         $this->assertEquals($aRes[0], ' 12.456 -78.90 ');
52
53         $aRes = parseLatLon(' [12.456,-78.90] ');
54         $this->assertEquals($aRes[1], 12.456);
55         $this->assertEquals($aRes[2], -78.90);
56         $this->assertEquals($aRes[0], ' [12.456,-78.90] ');
57
58         $aRes = parseLatLon(' -12.456,-78.90 ');
59         $this->assertEquals($aRes[1], -12.456);
60         $this->assertEquals($aRes[2], -78.90);
61         $this->assertEquals($aRes[0], ' -12.456,-78.90 ');
62
63         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
64         // these all represent the same location
65         $aQueries = array(
66                      '40 26.767 N 79 58.933 W',
67                      '40° 26.767′ N 79° 58.933′ W',
68                      "40° 26.767' N 79° 58.933' W",
69                      "40° 26.767'
70                          N 79° 58.933' W",
71                      'N 40 26.767, W 79 58.933',
72                      'N 40°26.767′, W 79°58.933′',
73                      '  N 40°26.767′, W 79°58.933′',
74                      "N 40°26.767', W 79°58.933'",
75  
76                      '40 26 46 N 79 58 56 W',
77                      '40° 26′ 46″ N 79° 58′ 56″ W',
78                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
79                      '40°26′46″N 79°58′56″W',
80                      'N 40 26 46 W 79 58 56',
81                      'N 40° 26′ 46″, W 79° 58′ 56″',
82                      'N 40° 26\' 46", W 79° 58\' 56"',
83                      'N 40° 26\' 46", W 79° 58\' 56"',
84  
85                      '40.446 -79.982',
86                      '40.446,-79.982',
87                      '40.446° N 79.982° W',
88                      'N 40.446° W 79.982°',
89  
90                      '[40.446 -79.982]',
91                      '[40.446,\v-79.982]',
92                      '       40.446  ,   -79.982     ',
93                      '       40.446  ,   -79.982     ',
94                      '       40.446     ,   -79.982     ',
95                      '       40.446\v,   -79.982 ',
96                     );
97
98
99         foreach ($aQueries as $sQuery) {
100             $aRes = parseLatLon($sQuery);
101             $this->assertEqualsWithDelta(40.446, $aRes[1], 0.01, 'degrees decimal ' . $sQuery);
102             $this->assertEqualsWithDelta(-79.982, $aRes[2], 0.01, 'degrees decimal ' . $sQuery);
103             $this->assertEquals($sQuery, $aRes[0]);
104         }
105     }
106
107     private function closestHouseNumberEvenOddOther($startnumber, $endnumber, $fraction, $aExpected)
108     {
109         foreach (array('even', 'odd', 'other') as $itype) {
110             $this->assertEquals(
111                 $aExpected[$itype],
112                 closestHouseNumber(array(
113                                     'startnumber' => $startnumber,
114                                     'endnumber' => $endnumber,
115                                     'fraction' => $fraction,
116                                     'interpolationtype' => $itype
117                                    )),
118                 "$startnumber => $endnumber, $fraction, $itype"
119             );
120         }
121     }
122
123     public function testClosestHouseNumber()
124     {
125         $this->closestHouseNumberEvenOddOther(50, 100, 0.5, array('even' => 76, 'odd' => 75, 'other' => 75));
126         // upper bound
127         $this->closestHouseNumberEvenOddOther(50, 100, 1.5, array('even' => 100, 'odd' => 100, 'other' => 100));
128         // lower bound
129         $this->closestHouseNumberEvenOddOther(50, 100, -0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
130         // fraction 0
131         $this->closestHouseNumberEvenOddOther(50, 100, 0, array('even' => 50, 'odd' => 51, 'other' => 50));
132         // start == end
133         $this->closestHouseNumberEvenOddOther(50, 50, 0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
134     }
135
136     public function testGetSearchRankLabel()
137     {
138         $this->assertEquals('unknown', getSearchRankLabel(null));
139         $this->assertEquals('continent', getSearchRankLabel(0));
140         $this->assertEquals('continent', getSearchRankLabel(1));
141         $this->assertEquals('other: 30', getSearchRankLabel(30));
142     }
143 }