Skip to content

Commit

Permalink
mask checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Sep 15, 2017
1 parent 18e838f commit d095524
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ var_dump($error->isWarningsShown());

// value may be set by mask
// E_USER_ERROR | E_USER_WARNING is 256 + 512;
$error->setBitsByMask(E_USER_ERROR + E_USER_WARNING);
$error->setBitsByMask(E_USER_ERROR | E_USER_WARNING);
```
56 changes: 47 additions & 9 deletions src/Bitmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,30 @@ public function __construct($bitmap = 0)
*/
public function isBitSet($index)
{
return (bool) ($this->bitmap & (1 << $index));
return (bool)($this->bitmap & (1 << $index));
}

/**
* @param int $mask
* @return bool
*/
public function isAnyMaskBitSet($mask)
{
return ($this->bitmap & $mask) > 0;
}

/**
* @param int $mask
* @return bool
*/
public function isAllMaskBitsSet($mask)
{
return $mask === ($this->bitmap & $mask);
}

/**
* @param int $index
* @return $this
* @return Bitmap
*/
public function setBit($index)
{
Expand All @@ -47,8 +65,8 @@ public function setBit($index)
}

/**
* @param [] $indexList
* @return $this
* @param int[] $indexList
* @return Bitmap
*/
public function setBits(array $indexList)
{
Expand All @@ -64,7 +82,7 @@ public function setBits(array $indexList)

/**
* @param int $mask
* @return $this
* @return Bitmap
*/
public function setBitsByMask($mask)
{
Expand All @@ -74,7 +92,7 @@ public function setBitsByMask($mask)

/**
* @param int $index
* @return $this
* @return Bitmap
*/
public function unsetBit($index)
{
Expand All @@ -84,7 +102,7 @@ public function unsetBit($index)

/**
* @param int[] $indexList
* @return $this
* @return Bitmap
*/
public function unsetBits(array $indexList)
{
Expand All @@ -100,7 +118,7 @@ public function unsetBits(array $indexList)

/**
* @param int $mask
* @return $this
* @return Bitmap
*/
public function unsetBitsByMask($mask)
{
Expand All @@ -109,7 +127,7 @@ public function unsetBitsByMask($mask)
}

/**
* @return $this
* @return Bitmap
*/
public function invert()
{
Expand All @@ -132,4 +150,24 @@ public function getBinary()
{
return decbin($this->bitmap);
}

/**
* @param Bitmap $bitmap
* @return bool
*/
public function equals(Bitmap $bitmap)
{
return $this->bitmap === $bitmap->getInt();
}

/**
* Add two bitmaps.
*
* @param Bitmap $bitmap
* @return Bitmap
*/
public function add(Bitmap $bitmap)
{
return new Bitmap($this->bitmap + $bitmap->getInt());
}
}
113 changes: 93 additions & 20 deletions tests/BitmapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@

class BitmapTest extends \PHPUnit_Framework_TestCase
{
public function testIsAnyBitSetByMask()
{
$bitmap = new Bitmap(E_USER_ERROR | E_USER_WARNING | E_USER_DEPRECATED);
$this->assertTrue($bitmap->isAnyMaskBitSet(E_USER_ERROR | E_ERROR));
$this->assertFalse($bitmap->isAnyMaskBitSet(E_NOTICE | E_ERROR));
}

public function testIsAllBitsSetByMask()
{
$bitmap = new Bitmap(E_USER_ERROR | E_USER_WARNING | E_USER_DEPRECATED);
$this->assertTrue($bitmap->isAllMaskBitsSet(E_USER_ERROR | E_USER_WARNING));
$this->assertFalse($bitmap->isAllMaskBitsSet(E_USER_ERROR | E_ERROR));
}

public function testIsBitSet()
{
// 5 => 101
Expand All @@ -15,68 +29,127 @@ public function testIsBitSet()
public function testSetBit()
{
$bitmap = new Bitmap();

$this->assertEquals(8, $bitmap->setBit(3)->getInt());

// 8 => 1000
$this->assertEquals(
8,
$bitmap->setBit(3)->getInt()
);
}

public function testSetBits()
{
$bitmap = new Bitmap();

$this->assertEquals(10, $bitmap->setBits(array(1, 3))->getInt());

// 10 => 1010
$this->assertEquals(
10,
$bitmap->setBits(array(1, 3))->getInt()
);
}

public function testSetBitsByMask()
{
$bitmap = new Bitmap();

$this->assertEquals(10, $bitmap->setBitsByMask(10)->getInt());

// 10 => 1010
$this->assertEquals(
10,
$bitmap->setBitsByMask(10)->getInt()
);
}

public function testUnsetBit()
{
$bitmap = new Bitmap(5);

$this->assertEquals(4, $bitmap->unsetBit(0)->getInt());

// 4 => 100
$this->assertEquals(
4,
$bitmap->unsetBit(0)->getInt()
);
}

public function testUnsetBits()
{
// 5 => 101
$bitmap = new Bitmap(5);

$this->assertEquals(4, $bitmap->unsetBits(array(0, 1))->getInt());

// 4 => 100
$this->assertEquals(
4,
$bitmap->unsetBits(array(0, 1))->getInt()
);
}

public function testUnsetBitsByMask()
{
// 5 => 101
// 4 => 100
$bitmap = new Bitmap(5);

$this->assertEquals(1, $bitmap->unsetBitsByMask(4)->getInt());
$this->assertEquals(
1,
$bitmap->unsetBitsByMask(4)->getInt()
);
}

public function testInvert()
{
// 5 => 101
$bitmap = new Bitmap(5);
$bitmap->invert();

$this->assertEquals(-6, $bitmap->getInt());

// -6 => 1111111111111111111111111111111111111111111111111111111111111010 (64 bit)
$this->assertEquals(
-6,
$bitmap->getInt()
);
}

public function testGetInt()
{
$bitmap = new Bitmap();
$bitmap->setBit(0);
$bitmap->setBit(2);
$bitmap->setBit(0); // 1
$bitmap->setBit(2); // 101 => 5

$this->assertEquals(5, $bitmap->getInt());
$this->assertEquals(
5,
$bitmap->getInt()
);
}

public function testBitmap()
public function testGetBinary()
{
$bitmap = new Bitmap();
$bitmap->setBit(0);
$bitmap->setBit(2);
$bitmap->setBit(0); // 1
$bitmap->setBit(2); // 101

$this->assertEquals('101', $bitmap->getBinary());
$this->assertEquals(
'101',
$bitmap->getBinary()
);
}

public function testEquals()
{
$bitmap42 = new Bitmap(42);

$this->assertTrue(
$bitmap42->equals(new Bitmap(42))
);

$this->assertFalse(
$bitmap42->equals(new Bitmap(43))
);
}

public function testAdd()
{
$bitmap10 = new Bitmap(10);
$bitmap42 = new Bitmap(42);
$bitmap52 = $bitmap10->add($bitmap42);

$this->assertEquals(52, $bitmap52->getInt());
}
}

0 comments on commit d095524

Please sign in to comment.