]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/LibTest.php
Merge remote-tracking branch 'upstream/master'
[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                      'N 40 26.767, W 79 58.933',
70                      'N 40°26.767′, W 79°58.933′',
71                      "N 40°26.767', W 79°58.933'",
72  
73                      '40 26 46 N 79 58 56 W',
74                      '40° 26′ 46″ N 79° 58′ 56″ W',
75                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
76                      '40°26′46″N 79°58′56″W',
77                      'N 40 26 46 W 79 58 56',
78                      'N 40° 26′ 46″, W 79° 58′ 56″',
79                      'N 40° 26\' 46", W 79° 58\' 56"',
80  
81                      '40.446 -79.982',
82                      '40.446,-79.982',
83                      '40.446° N 79.982° W',
84                      'N 40.446° W 79.982°',
85  
86                      '[40.446 -79.982]',
87                      '       40.446  ,   -79.982     ',
88                     );
89
90
91         foreach ($aQueries as $sQuery) {
92             $aRes = parseLatLon($sQuery);
93             $this->assertEquals(40.446, $aRes[1], 'degrees decimal ' . $sQuery, 0.01);
94             $this->assertEquals(-79.982, $aRes[2], 'degrees decimal ' . $sQuery, 0.01);
95             $this->assertEquals($sQuery, $aRes[0]);
96         }
97     }
98
99     private function closestHouseNumberEvenOddOther($startnumber, $endnumber, $fraction, $aExpected)
100     {
101         foreach (array('even', 'odd', 'other') as $itype) {
102             $this->assertEquals(
103                 $aExpected[$itype],
104                 closestHouseNumber(array(
105                                     'startnumber' => $startnumber,
106                                     'endnumber' => $endnumber,
107                                     'fraction' => $fraction,
108                                     'interpolationtype' => $itype
109                                    )),
110                 "$startnumber => $endnumber, $fraction, $itype"
111             );
112         }
113     }
114
115     public function testClosestHouseNumber()
116     {
117         $this->closestHouseNumberEvenOddOther(50, 100, 0.5, array('even' => 76, 'odd' => 75, 'other' => 75));
118         // upper bound
119         $this->closestHouseNumberEvenOddOther(50, 100, 1.5, array('even' => 100, 'odd' => 100, 'other' => 100));
120         // lower bound
121         $this->closestHouseNumberEvenOddOther(50, 100, -0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
122         // fraction 0
123         $this->closestHouseNumberEvenOddOther(50, 100, 0, array('even' => 50, 'odd' => 51, 'other' => 50));
124         // start == end
125         $this->closestHouseNumberEvenOddOther(50, 50, 0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
126     }
127
128     public function testGetSearchRankLabel()
129     {
130         $this->assertEquals('unknown', getSearchRankLabel(null));
131         $this->assertEquals('continent', getSearchRankLabel(0));
132         $this->assertEquals('continent', getSearchRankLabel(1));
133         $this->assertEquals('other: 30', getSearchRankLabel(30));
134     }
135 }