Skip to content

Commit d56565f

Browse files
committed
Fix some tests
1 parent 6d5ac66 commit d56565f

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

src/OpenLocationCode.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO
7070

7171
$revCode = '';
7272

73-
$latVal = round(($latitude+self::LATITUDE_MAX)*self::LAT_INTEGER_MULTIPLIER*1e6)/1e6;
74-
$lngVal = round(($longitude+self::LONGITUDE_MAX)*self::LNG_INTEGER_MULTIPLIER*1e6)/1e6;
73+
$latVal = (int) round(($latitude+self::LATITUDE_MAX)*self::LAT_INTEGER_MULTIPLIER*1e6)/1e6;
74+
$lngVal = (int) round(($longitude+self::LONGITUDE_MAX)*self::LNG_INTEGER_MULTIPLIER*1e6)/1e6;
7575

7676
if($codeLength>self::PAIR_CODE_LENGTH){
7777
for($i=0;$i<self::GRID_CODE_LENGTH;$i++){
@@ -83,8 +83,8 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO
8383
$lngVal /= self::GRID_COLUMNS;
8484
}
8585
}else{
86-
$latVal = $latVal/pow(self::GRID_ROWS,self::GRID_CODE_LENGTH);
87-
$lngVal = $lngVal/pow(self::GRID_COLUMNS,self::GRID_CODE_LENGTH);
86+
$latVal = (int) ($latVal/pow(self::GRID_ROWS,self::GRID_CODE_LENGTH));
87+
$lngVal = (int) ($lngVal/pow(self::GRID_COLUMNS,self::GRID_CODE_LENGTH));
8888
}
8989

9090
for($i=0;$i<self::PAIR_CODE_LENGTH/2;$i++){
@@ -146,13 +146,13 @@ public function decode(){
146146
for($i=0;$i<min(strlen($clean),self::PAIR_CODE_LENGTH);$i+=2){
147147
$latPlaceVal /= self::ENCODING_BASE;
148148
$lngPlaceVal /= self::ENCODING_BASE;
149-
$latVal += strpos(self::CODE_ALPHABET,substr($clean,$i,1)) * $latPlaceVal;
150-
$latVal += strpos(self::CODE_ALPHABET,substr($clean,$i+1,1)) * $lngPlaceVal;
149+
$latVal += self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1)) * $latPlaceVal;
150+
$lngVal += self::indexOf(self::CODE_ALPHABET,substr($clean,$i+1,1)) * $lngPlaceVal;
151151
}
152152
for($i=self::PAIR_CODE_LENGTH;$i<min(strlen($clean),self::MAX_DIGIT_COUNT);$i++){
153153
$latPlaceVal /= self::GRID_ROWS;
154154
$lngPlaceVal /= self::GRID_COLUMNS;
155-
$digit = strpos(self::CODE_ALPHABET,substr($clean,$i,1));
155+
$digit = self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1));
156156
$row = $digit/self::GRID_COLUMNS;
157157
$col = $digit%self::GRID_COLUMNS;
158158
$latVal += $row*$latPlaceVal;
@@ -179,7 +179,7 @@ public static function decodeByCode($code){
179179
* @return bool
180180
*/
181181
public function isFull(){
182-
return strpos($this->code,self::SEPARATOR)===self::SEPARATOR_POSITION;
182+
return self::indexOf($this->code,self::SEPARATOR)==self::SEPARATOR_POSITION;
183183
}
184184

185185
/**
@@ -195,7 +195,7 @@ public static function isFullByCode($code){
195195
* @return bool
196196
*/
197197
public function isShort(){
198-
return strpos($this->code,self::SEPARATOR)>=0 && strpos($this->code,self::SEPARATOR)<self::SEPARATOR_POSITION;
198+
return self::indexOf($this->code,self::SEPARATOR)>=0 && self::indexOf($this->code,self::SEPARATOR)<self::SEPARATOR_POSITION;
199199
}
200200

201201
/**
@@ -211,7 +211,7 @@ public static function isShortByCode($code){
211211
* @return bool
212212
*/
213213
public function isPadded(){
214-
return strpos($this->code,self::PADDING_CHARACTER)>=0;
214+
return self::indexOf($this->code,self::PADDING_CHARACTER)>=0;
215215
}
216216

217217
/**
@@ -228,7 +228,7 @@ public function shorten($referenceLatitude,$referenceLongitude){
228228
throw new InvalidArgumentException('shorten() method could only be called on a full code.');
229229
}
230230
if($this->isPadded()){
231-
throw new InvalidArgumentException('shorten() method can not be called on a padded code.');
231+
throw new InvalidArgumentException('shorten() method can not be called on a padded code.');
232232
}
233233

234234
$codeArea = $this->decode();
@@ -254,7 +254,7 @@ public function recover($referenceLatitude,$referenceLongitude){
254254
$referenceLatitude = self::clipLatitude($referenceLatitude);
255255
$referenceLongitude = self::normalizeLongitude($referenceLongitude);
256256

257-
$digitsToRecover = self::SEPARATOR_POSITION - strpos($this->code,self::SEPARATOR);
257+
$digitsToRecover = self::SEPARATOR_POSITION - self::indexOf($this->code,self::SEPARATOR);
258258
$prefixPrecision = pow(self::ENCODING_BASE,2-($digitsToRecover/2));
259259

260260
$recoveredPrefix = substr((new self($referenceLatitude,$referenceLongitude))->getCode(),0,$digitsToRecover);
@@ -305,30 +305,30 @@ public static function isValidCode($code){
305305
}
306306
$code = strtoupper($code);
307307

308-
$separatorPosition = strpos($code,self::SEPARATOR);
309-
if($separatorPosition===false){
308+
$separatorPosition = self::indexOf($code,self::SEPARATOR);
309+
if($separatorPosition==-1){
310310
return false;
311311
}
312-
if($separatorPosition!=strpos($code,self::SEPARATOR)){
312+
if($separatorPosition!==self::indexOf($code,self::SEPARATOR)){
313313
return false;
314314
}
315315
if($separatorPosition%2!==0 || $separatorPosition>self::SEPARATOR_POSITION){
316316
return false;
317317
}
318318

319319
if($separatorPosition==self::SEPARATOR_POSITION){
320-
if(strpos(self::CODE_ALPHABET,substr($code,0,1))>8){
320+
if(self::indexOf(self::CODE_ALPHABET,substr($code,0,1))>8){
321321
return false;
322322
}
323323

324-
if(strpos(self::CODE_ALPHABET,substr($code,1,1))>17){
324+
if(self::indexOf(self::CODE_ALPHABET,substr($code,1,1))>17){
325325
return false;
326326
}
327327
}
328328

329329
$paddingStarted = false;
330330
for($i=0;$i<$separatorPosition;$i++){
331-
if(strpos(self::CODE_ALPHABET,substr($code,$i,1))===false && substr($code,$i,1)!==self::PADDING_CHARACTER){
331+
if(self::indexOf(self::CODE_ALPHABET,substr($code,$i,1))==-1 && substr($code,$i,1)!==self::PADDING_CHARACTER){
332332
return false;
333333
}
334334
if($paddingStarted){
@@ -355,7 +355,7 @@ public static function isValidCode($code){
355355
return false;
356356
}
357357
for($i=$separatorPosition+1;$i<strlen($code);$i++){
358-
if(strpos(self::CODE_ALPHABET,substr($code,$i,1))===false){
358+
if(self::indexOf(self::CODE_ALPHABET,substr($code,$i,1))==-1){
359359
return false;
360360
}
361361
}
@@ -414,9 +414,17 @@ private static function normalizeLongitude($longitude){
414414
*/
415415
private static function computeLatitudePrecision($codeLength){
416416
if($codeLength<=self::CODE_PRECISION_NORMAL){
417-
return pow(self::ENCODING_BASE,($codeLength / -2 + 2));
417+
return self::ENCODING_BASE ** ($codeLength / -2 + 2);
418418
}
419-
return pow(self::ENCODING_BASE,-3) / pow(self::GRID_ROWS,$codeLength - self::PAIR_CODE_LENGTH);
419+
return (self::ENCODING_BASE ** -3) / (self::GRID_ROWS ** ($codeLength - self::PAIR_CODE_LENGTH));
420+
}
421+
422+
private static function indexOf($haystack,$needle,$offset=0){
423+
$pos = strpos($haystack,$needle,$offset);
424+
if($pos===false){
425+
return -1;
426+
}
427+
return $pos;
420428
}
421429

422430
}

tests/DecodingTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function testDecode(){
2828
$decoded = (new OpenLocationCode($testData->code))->decode();
2929

3030
$this->assertEquals($testData->length,$decoded->getLength(),'Wrong length for code '.$testData->code);
31-
$this->assertEquals($testData->decodedLatitudeLo,$decoded->getSouthLatitude(),'Wrong low latitude for code '.$testData->code);
32-
$this->assertEquals($testData->decodedLatitudeHi,$decoded->getNorthLatitude(),'Wrong high latitude for code '.$testData->code);
33-
$this->assertEquals($testData->decodedLongitudeLo,$decoded->getWestLongitude(),'Wrong low longitude for code '.$testData->code);
34-
$this->assertEquals($testData->decodedLongitudeHi,$decoded->getEastLongitude(),'Wrong high longitude for code '.$testData->code);
31+
$this->assertEqualsWithDelta($testData->decodedLatitudeLo,$decoded->getSouthLatitude(),self::PRECISION,'Wrong low latitude for code '.$testData->code);
32+
$this->assertEqualsWithDelta($testData->decodedLatitudeHi,$decoded->getNorthLatitude(),self::PRECISION,'Wrong high latitude for code '.$testData->code);
33+
$this->assertEqualsWithDelta($testData->decodedLongitudeLo,$decoded->getWestLongitude(),self::PRECISION,'Wrong low longitude for code '.$testData->code);
34+
$this->assertEqualsWithDelta($testData->decodedLongitudeHi,$decoded->getEastLongitude(),self::PRECISION,'Wrong high longitude for code '.$testData->code);
3535
}
3636
}
3737

tests/ShorteningTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public function testShortening(){
2828
}
2929
$olc = new OpenLocationCode($testData->code);
3030
$shortened = $olc->shorten($testData->referenceLatitude,$testData->referenceLongitude);
31-
$this->assertEquals($testData->code,$shortened->getCode(),'Wrong shortening of code '.$testData->code);
31+
$this->assertEquals($testData->shortCode,$shortened->getCode(),'Wrong shortening of code '.$testData->code);
3232
}
3333
}
3434

3535
public function testRecovering(){
3636
foreach($this->testDataList AS $testData){
37-
if('B'!==$testData->testType && 'S'!==$testData->testType){
37+
if('B'!==$testData->testType && 'R'!==$testData->testType){
3838
continue;
3939
}
4040
$olc = new OpenLocationCode($testData->shortCode);

tests/ValidityTest_TestData.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public function __construct($line){
1616
throw new InvalidArgumentException('Wrong format of testing data.');
1717
}
1818
$this->code = $parts[0];
19-
$this->isValid = boolval($parts[1]);
20-
$this->isShort = boolval($parts[2]);
21-
$this->isFull = boolval($parts[3]);
19+
$this->isValid = filter_var($parts[1],FILTER_VALIDATE_BOOLEAN);
20+
$this->isShort = filter_var($parts[2],FILTER_VALIDATE_BOOLEAN);
21+
$this->isFull = filter_var($parts[3],FILTER_VALIDATE_BOOLEAN);
22+
// var_dump($this);
2223
}
2324

2425
}

0 commit comments

Comments
 (0)