Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed Sep 20, 2022
1 parent 6d5ac66 commit d56565f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
50 changes: 29 additions & 21 deletions src/OpenLocationCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO

$revCode = '';

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

if($codeLength>self::PAIR_CODE_LENGTH){
for($i=0;$i<self::GRID_CODE_LENGTH;$i++){
Expand All @@ -83,8 +83,8 @@ public function __construct($latitudeOrCode,$longitude=null,$codeLength=self::CO
$lngVal /= self::GRID_COLUMNS;
}
}else{
$latVal = $latVal/pow(self::GRID_ROWS,self::GRID_CODE_LENGTH);
$lngVal = $lngVal/pow(self::GRID_COLUMNS,self::GRID_CODE_LENGTH);
$latVal = (int) ($latVal/pow(self::GRID_ROWS,self::GRID_CODE_LENGTH));
$lngVal = (int) ($lngVal/pow(self::GRID_COLUMNS,self::GRID_CODE_LENGTH));
}

for($i=0;$i<self::PAIR_CODE_LENGTH/2;$i++){
Expand Down Expand Up @@ -146,13 +146,13 @@ public function decode(){
for($i=0;$i<min(strlen($clean),self::PAIR_CODE_LENGTH);$i+=2){
$latPlaceVal /= self::ENCODING_BASE;
$lngPlaceVal /= self::ENCODING_BASE;
$latVal += strpos(self::CODE_ALPHABET,substr($clean,$i,1)) * $latPlaceVal;
$latVal += strpos(self::CODE_ALPHABET,substr($clean,$i+1,1)) * $lngPlaceVal;
$latVal += self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1)) * $latPlaceVal;
$lngVal += self::indexOf(self::CODE_ALPHABET,substr($clean,$i+1,1)) * $lngPlaceVal;
}
for($i=self::PAIR_CODE_LENGTH;$i<min(strlen($clean),self::MAX_DIGIT_COUNT);$i++){
$latPlaceVal /= self::GRID_ROWS;
$lngPlaceVal /= self::GRID_COLUMNS;
$digit = strpos(self::CODE_ALPHABET,substr($clean,$i,1));
$digit = self::indexOf(self::CODE_ALPHABET,substr($clean,$i,1));
$row = $digit/self::GRID_COLUMNS;
$col = $digit%self::GRID_COLUMNS;
$latVal += $row*$latPlaceVal;
Expand All @@ -179,7 +179,7 @@ public static function decodeByCode($code){
* @return bool
*/
public function isFull(){
return strpos($this->code,self::SEPARATOR)===self::SEPARATOR_POSITION;
return self::indexOf($this->code,self::SEPARATOR)==self::SEPARATOR_POSITION;
}

/**
Expand All @@ -195,7 +195,7 @@ public static function isFullByCode($code){
* @return bool
*/
public function isShort(){
return strpos($this->code,self::SEPARATOR)>=0 && strpos($this->code,self::SEPARATOR)<self::SEPARATOR_POSITION;
return self::indexOf($this->code,self::SEPARATOR)>=0 && self::indexOf($this->code,self::SEPARATOR)<self::SEPARATOR_POSITION;
}

/**
Expand All @@ -211,7 +211,7 @@ public static function isShortByCode($code){
* @return bool
*/
public function isPadded(){
return strpos($this->code,self::PADDING_CHARACTER)>=0;
return self::indexOf($this->code,self::PADDING_CHARACTER)>=0;
}

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

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

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

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

$separatorPosition = strpos($code,self::SEPARATOR);
if($separatorPosition===false){
$separatorPosition = self::indexOf($code,self::SEPARATOR);
if($separatorPosition==-1){
return false;
}
if($separatorPosition!=strpos($code,self::SEPARATOR)){
if($separatorPosition!==self::indexOf($code,self::SEPARATOR)){
return false;
}
if($separatorPosition%2!==0 || $separatorPosition>self::SEPARATOR_POSITION){
return false;
}

if($separatorPosition==self::SEPARATOR_POSITION){
if(strpos(self::CODE_ALPHABET,substr($code,0,1))>8){
if(self::indexOf(self::CODE_ALPHABET,substr($code,0,1))>8){
return false;
}

if(strpos(self::CODE_ALPHABET,substr($code,1,1))>17){
if(self::indexOf(self::CODE_ALPHABET,substr($code,1,1))>17){
return false;
}
}

$paddingStarted = false;
for($i=0;$i<$separatorPosition;$i++){
if(strpos(self::CODE_ALPHABET,substr($code,$i,1))===false && substr($code,$i,1)!==self::PADDING_CHARACTER){
if(self::indexOf(self::CODE_ALPHABET,substr($code,$i,1))==-1 && substr($code,$i,1)!==self::PADDING_CHARACTER){
return false;
}
if($paddingStarted){
Expand All @@ -355,7 +355,7 @@ public static function isValidCode($code){
return false;
}
for($i=$separatorPosition+1;$i<strlen($code);$i++){
if(strpos(self::CODE_ALPHABET,substr($code,$i,1))===false){
if(self::indexOf(self::CODE_ALPHABET,substr($code,$i,1))==-1){
return false;
}
}
Expand Down Expand Up @@ -414,9 +414,17 @@ private static function normalizeLongitude($longitude){
*/
private static function computeLatitudePrecision($codeLength){
if($codeLength<=self::CODE_PRECISION_NORMAL){
return pow(self::ENCODING_BASE,($codeLength / -2 + 2));
return self::ENCODING_BASE ** ($codeLength / -2 + 2);
}
return pow(self::ENCODING_BASE,-3) / pow(self::GRID_ROWS,$codeLength - self::PAIR_CODE_LENGTH);
return (self::ENCODING_BASE ** -3) / (self::GRID_ROWS ** ($codeLength - self::PAIR_CODE_LENGTH));
}

private static function indexOf($haystack,$needle,$offset=0){
$pos = strpos($haystack,$needle,$offset);
if($pos===false){
return -1;
}
return $pos;
}

}
8 changes: 4 additions & 4 deletions tests/DecodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function testDecode(){
$decoded = (new OpenLocationCode($testData->code))->decode();

$this->assertEquals($testData->length,$decoded->getLength(),'Wrong length for code '.$testData->code);
$this->assertEquals($testData->decodedLatitudeLo,$decoded->getSouthLatitude(),'Wrong low latitude for code '.$testData->code);
$this->assertEquals($testData->decodedLatitudeHi,$decoded->getNorthLatitude(),'Wrong high latitude for code '.$testData->code);
$this->assertEquals($testData->decodedLongitudeLo,$decoded->getWestLongitude(),'Wrong low longitude for code '.$testData->code);
$this->assertEquals($testData->decodedLongitudeHi,$decoded->getEastLongitude(),'Wrong high longitude for code '.$testData->code);
$this->assertEqualsWithDelta($testData->decodedLatitudeLo,$decoded->getSouthLatitude(),self::PRECISION,'Wrong low latitude for code '.$testData->code);
$this->assertEqualsWithDelta($testData->decodedLatitudeHi,$decoded->getNorthLatitude(),self::PRECISION,'Wrong high latitude for code '.$testData->code);
$this->assertEqualsWithDelta($testData->decodedLongitudeLo,$decoded->getWestLongitude(),self::PRECISION,'Wrong low longitude for code '.$testData->code);
$this->assertEqualsWithDelta($testData->decodedLongitudeHi,$decoded->getEastLongitude(),self::PRECISION,'Wrong high longitude for code '.$testData->code);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ShorteningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function testShortening(){
}
$olc = new OpenLocationCode($testData->code);
$shortened = $olc->shorten($testData->referenceLatitude,$testData->referenceLongitude);
$this->assertEquals($testData->code,$shortened->getCode(),'Wrong shortening of code '.$testData->code);
$this->assertEquals($testData->shortCode,$shortened->getCode(),'Wrong shortening of code '.$testData->code);
}
}

public function testRecovering(){
foreach($this->testDataList AS $testData){
if('B'!==$testData->testType && 'S'!==$testData->testType){
if('B'!==$testData->testType && 'R'!==$testData->testType){
continue;
}
$olc = new OpenLocationCode($testData->shortCode);
Expand Down
7 changes: 4 additions & 3 deletions tests/ValidityTest_TestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public function __construct($line){
throw new InvalidArgumentException('Wrong format of testing data.');
}
$this->code = $parts[0];
$this->isValid = boolval($parts[1]);
$this->isShort = boolval($parts[2]);
$this->isFull = boolval($parts[3]);
$this->isValid = filter_var($parts[1],FILTER_VALIDATE_BOOLEAN);
$this->isShort = filter_var($parts[2],FILTER_VALIDATE_BOOLEAN);
$this->isFull = filter_var($parts[3],FILTER_VALIDATE_BOOLEAN);
// var_dump($this);
}

}

0 comments on commit d56565f

Please sign in to comment.