[TESTS] Add tests for the bitmap utility and fix implementation

This commit is contained in:
Hugo Sales 2021-04-05 13:44:25 +00:00
parent 8d25859de7
commit 88e4044d02
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 71 additions and 8 deletions

View File

@ -24,11 +24,11 @@ use App\Util\Exception\ServerException;
abstract class Bitmap
{
/**
* Convert an to or from an integer and an array of constants for
* each bit. If $instance, return an object with the corresponding
* Convert from an integer to an onject or an array of constants for
* set each bit. If $instance, return an object with the corresponding
* properties set
*/
private static function _do(int $r, bool $instance)
private static function _to(int $r, bool $instance)
{
$init = $r;
$class = get_called_class();
@ -38,8 +38,12 @@ abstract class Bitmap
$vals = [];
}
$consts = (new \ReflectionClass($class))->getConstants();
unset($consts['PREFIX']);
$consts = (new \ReflectionClass($class))->getConstants();
$have_prefix = false;
if (isset($consts['PREFIX'])) {
$have_prefix = true;
unset($consts['PREFIX']);
}
foreach ($consts as $c => $v) {
$b = ($r & $v) !== 0;
@ -50,7 +54,7 @@ abstract class Bitmap
if ($b) {
$r -= $v;
if (!$instance) {
$vals[] = $class::PREFIX . $c;
$vals[] = ($have_prefix ? $class::PREFIX : '') . $c;
}
}
}
@ -69,11 +73,11 @@ abstract class Bitmap
public static function create(int $r): self
{
return self::_do($r, true);
return self::_to($r, true);
}
public static function toArray(int $r): array
{
return self::_do($r, false);
return self::_to($r, false);
}
}

59
tests/Util/BitmapTest.php Normal file
View File

@ -0,0 +1,59 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Tests\Util;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class FooBitmap extends \App\Util\Bitmap
{
public const FOO = 1;
public const BAR = 2;
public const QUUX = 4;
}
class BarBitmap extends \App\Util\Bitmap
{
public const HYDROGEN = 1;
public const HELIUM = 2;
public const PREFIX = 'BAR_';
}
class BitmapTest extends KernelTestCase
{
public function testObj()
{
$a = FooBitmap::create(FooBitmap::FOO | FooBitmap::BAR);
static::assertTrue($a->foo);
static::assertTrue($a->bar);
static::assertFalse($a->quux);
}
public function testArray()
{
$b = FooBitmap::toArray(FooBitmap::FOO | FooBitmap::QUUX);
static::assertSame(['FOO', 'QUUX'], $b);
}
public function testPrefix()
{
$b = BarBitmap::toArray(BarBitmap::HYDROGEN | BarBitmap::HELIUM);
static::assertSame(['BAR_HYDROGEN', 'BAR_HELIUM'], $b);
}
}