3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
13 require_once(CONST_LibDir.'/lib.php');
14 require_once(CONST_LibDir.'/ClassTypes.php');
16 class LibTest extends \PHPUnit\Framework\TestCase
19 public function testAddQuotes()
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(''));
26 public function testParseLatLon()
28 // no coordinates expected
29 $this->assertFalse(parseLatLon(''));
30 $this->assertFalse(parseLatLon('abc'));
31 $this->assertFalse(parseLatLon('12 34'));
33 // coordinates expected
34 $this->assertNotNull(parseLatLon('0.0 -0.0'));
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 ');
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] ');
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 ');
51 // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
52 // these all represent the same location
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",
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'",
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"',
75 '40.446° N 79.982° W',
76 'N 40.446° W 79.982°',
83 ' 40.446
\v, -79.982 ',
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]);