]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/LibTest.php
Merge pull request #2181 from lonvia/port-more-tool-functions-to-python
[nominatim.git] / test / php / Nominatim / LibTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_LibDir.'/lib.php');
6 require_once(CONST_LibDir.'/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     public function testParseLatLon()
19     {
20         // no coordinates expected
21         $this->assertFalse(parseLatLon(''));
22         $this->assertFalse(parseLatLon('abc'));
23         $this->assertFalse(parseLatLon('12 34'));
24
25         // coordinates expected
26         $this->assertNotNull(parseLatLon('0.0 -0.0'));
27
28         $aRes = parseLatLon(' abc 12.456 -78.90 def ');
29         $this->assertEquals($aRes[1], 12.456);
30         $this->assertEquals($aRes[2], -78.90);
31         $this->assertEquals($aRes[0], ' 12.456 -78.90 ');
32
33         $aRes = parseLatLon(' [12.456,-78.90] ');
34         $this->assertEquals($aRes[1], 12.456);
35         $this->assertEquals($aRes[2], -78.90);
36         $this->assertEquals($aRes[0], ' [12.456,-78.90] ');
37
38         $aRes = parseLatLon(' -12.456,-78.90 ');
39         $this->assertEquals($aRes[1], -12.456);
40         $this->assertEquals($aRes[2], -78.90);
41         $this->assertEquals($aRes[0], ' -12.456,-78.90 ');
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                      "40° 26.767'
50                          N 79° 58.933' W",
51                      'N 40 26.767, W 79 58.933',
52                      'N 40°26.767′, W 79°58.933′',
53                      '  N 40°26.767′, W 79°58.933′',
54                      "N 40°26.767', W 79°58.933'",
55  
56                      '40 26 46 N 79 58 56 W',
57                      '40° 26′ 46″ N 79° 58′ 56″ W',
58                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
59                      '40°26′46″N 79°58′56″W',
60                      'N 40 26 46 W 79 58 56',
61                      'N 40° 26′ 46″, W 79° 58′ 56″',
62                      'N 40° 26\' 46", W 79° 58\' 56"',
63                      'N 40° 26\' 46", W 79° 58\' 56"',
64  
65                      '40.446 -79.982',
66                      '40.446,-79.982',
67                      '40.446° N 79.982° W',
68                      'N 40.446° W 79.982°',
69  
70                      '[40.446 -79.982]',
71                      '[40.446,\v-79.982]',
72                      '       40.446  ,   -79.982     ',
73                      '       40.446  ,   -79.982     ',
74                      '       40.446     ,   -79.982     ',
75                      '       40.446\v,   -79.982 ',
76                     );
77
78
79         foreach ($aQueries as $sQuery) {
80             $aRes = parseLatLon($sQuery);
81             $this->assertEqualsWithDelta(40.446, $aRes[1], 0.01, 'degrees decimal ' . $sQuery);
82             $this->assertEqualsWithDelta(-79.982, $aRes[2], 0.01, 'degrees decimal ' . $sQuery);
83             $this->assertEquals($sQuery, $aRes[0]);
84         }
85     }
86
87     private function closestHouseNumberEvenOddOther($startnumber, $endnumber, $fraction, $aExpected)
88     {
89         foreach (array('even', 'odd', 'other') as $itype) {
90             $this->assertEquals(
91                 $aExpected[$itype],
92                 closestHouseNumber(array(
93                                     'startnumber' => $startnumber,
94                                     'endnumber' => $endnumber,
95                                     'fraction' => $fraction,
96                                     'interpolationtype' => $itype
97                                    )),
98                 "$startnumber => $endnumber, $fraction, $itype"
99             );
100         }
101     }
102
103     public function testClosestHouseNumber()
104     {
105         $this->closestHouseNumberEvenOddOther(50, 100, 0.5, array('even' => 76, 'odd' => 75, 'other' => 75));
106         // upper bound
107         $this->closestHouseNumberEvenOddOther(50, 100, 1.5, array('even' => 100, 'odd' => 100, 'other' => 100));
108         // lower bound
109         $this->closestHouseNumberEvenOddOther(50, 100, -0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
110         // fraction 0
111         $this->closestHouseNumberEvenOddOther(50, 100, 0, array('even' => 50, 'odd' => 51, 'other' => 50));
112         // start == end
113         $this->closestHouseNumberEvenOddOther(50, 50, 0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
114     }
115 }