]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/LibTest.php
add consistent SPDX copyright headers
[nominatim.git] / test / php / Nominatim / LibTest.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim;
12
13 require_once(CONST_LibDir.'/lib.php');
14 require_once(CONST_LibDir.'/ClassTypes.php');
15
16 class LibTest extends \PHPUnit\Framework\TestCase
17 {
18
19     public function testAddQuotes()
20     {
21         // FIXME: not quoting existing quote signs is probably a bug
22         $this->assertSame("'St. John's'", addQuotes("St. John's"));
23         $this->assertSame("''", addQuotes(''));
24     }
25
26     public function testParseLatLon()
27     {
28         // no coordinates expected
29         $this->assertFalse(parseLatLon(''));
30         $this->assertFalse(parseLatLon('abc'));
31         $this->assertFalse(parseLatLon('12 34'));
32
33         // coordinates expected
34         $this->assertNotNull(parseLatLon('0.0 -0.0'));
35
36         $aRes = parseLatLon(' abc 12.456 -78.90 def ');
37         $this->assertEquals($aRes[1], 12.456);
38         $this->assertEquals($aRes[2], -78.90);
39         $this->assertEquals($aRes[0], ' 12.456 -78.90 ');
40
41         $aRes = parseLatLon(' [12.456,-78.90] ');
42         $this->assertEquals($aRes[1], 12.456);
43         $this->assertEquals($aRes[2], -78.90);
44         $this->assertEquals($aRes[0], ' [12.456,-78.90] ');
45
46         $aRes = parseLatLon(' -12.456,-78.90 ');
47         $this->assertEquals($aRes[1], -12.456);
48         $this->assertEquals($aRes[2], -78.90);
49         $this->assertEquals($aRes[0], ' -12.456,-78.90 ');
50
51         // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
52         // these all represent the same location
53         $aQueries = array(
54                      '40 26.767 N 79 58.933 W',
55                      '40° 26.767′ N 79° 58.933′ W',
56                      "40° 26.767' N 79° 58.933' W",
57                      "40° 26.767'
58                          N 79° 58.933' W",
59                      'N 40 26.767, W 79 58.933',
60                      'N 40°26.767′, W 79°58.933′',
61                      '  N 40°26.767′, W 79°58.933′',
62                      "N 40°26.767', W 79°58.933'",
63  
64                      '40 26 46 N 79 58 56 W',
65                      '40° 26′ 46″ N 79° 58′ 56″ W',
66                      '40° 26′ 46.00″ N 79° 58′ 56.00″ W',
67                      '40°26′46″N 79°58′56″W',
68                      'N 40 26 46 W 79 58 56',
69                      'N 40° 26′ 46″, W 79° 58′ 56″',
70                      'N 40° 26\' 46", W 79° 58\' 56"',
71                      'N 40° 26\' 46", W 79° 58\' 56"',
72  
73                      '40.446 -79.982',
74                      '40.446,-79.982',
75                      '40.446° N 79.982° W',
76                      'N 40.446° W 79.982°',
77  
78                      '[40.446 -79.982]',
79                      '[40.446,\v-79.982]',
80                      '       40.446  ,   -79.982     ',
81                      '       40.446  ,   -79.982     ',
82                      '       40.446     ,   -79.982     ',
83                      '       40.446\v,   -79.982 ',
84                     );
85
86
87         foreach ($aQueries as $sQuery) {
88             $aRes = parseLatLon($sQuery);
89             $this->assertEqualsWithDelta(40.446, $aRes[1], 0.01, 'degrees decimal ' . $sQuery);
90             $this->assertEqualsWithDelta(-79.982, $aRes[2], 0.01, 'degrees decimal ' . $sQuery);
91             $this->assertEquals($sQuery, $aRes[0]);
92         }
93     }
94
95     private function closestHouseNumberEvenOddOther($startnumber, $endnumber, $fraction, $aExpected)
96     {
97         foreach (array('even', 'odd', 'other') as $itype) {
98             $this->assertEquals(
99                 $aExpected[$itype],
100                 closestHouseNumber(array(
101                                     'startnumber' => $startnumber,
102                                     'endnumber' => $endnumber,
103                                     'fraction' => $fraction,
104                                     'interpolationtype' => $itype
105                                    )),
106                 "$startnumber => $endnumber, $fraction, $itype"
107             );
108         }
109     }
110
111     public function testClosestHouseNumber()
112     {
113         $this->closestHouseNumberEvenOddOther(50, 100, 0.5, array('even' => 76, 'odd' => 75, 'other' => 75));
114         // upper bound
115         $this->closestHouseNumberEvenOddOther(50, 100, 1.5, array('even' => 100, 'odd' => 100, 'other' => 100));
116         // lower bound
117         $this->closestHouseNumberEvenOddOther(50, 100, -0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
118         // fraction 0
119         $this->closestHouseNumberEvenOddOther(50, 100, 0, array('even' => 50, 'odd' => 51, 'other' => 50));
120         // start == end
121         $this->closestHouseNumberEvenOddOther(50, 50, 0.5, array('even' => 50, 'odd' => 50, 'other' => 50));
122     }
123 }