Skip to content

Commit d095524

Browse files
committed
mask checks
1 parent 18e838f commit d095524

File tree

3 files changed

+141
-30
lines changed

3 files changed

+141
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ var_dump($error->isWarningsShown());
133133

134134
// value may be set by mask
135135
// E_USER_ERROR | E_USER_WARNING is 256 + 512;
136-
$error->setBitsByMask(E_USER_ERROR + E_USER_WARNING);
136+
$error->setBitsByMask(E_USER_ERROR | E_USER_WARNING);
137137
```

src/Bitmap.php

+47-9
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,30 @@ public function __construct($bitmap = 0)
3333
*/
3434
public function isBitSet($index)
3535
{
36-
return (bool) ($this->bitmap & (1 << $index));
36+
return (bool)($this->bitmap & (1 << $index));
37+
}
38+
39+
/**
40+
* @param int $mask
41+
* @return bool
42+
*/
43+
public function isAnyMaskBitSet($mask)
44+
{
45+
return ($this->bitmap & $mask) > 0;
46+
}
47+
48+
/**
49+
* @param int $mask
50+
* @return bool
51+
*/
52+
public function isAllMaskBitsSet($mask)
53+
{
54+
return $mask === ($this->bitmap & $mask);
3755
}
3856

3957
/**
4058
* @param int $index
41-
* @return $this
59+
* @return Bitmap
4260
*/
4361
public function setBit($index)
4462
{
@@ -47,8 +65,8 @@ public function setBit($index)
4765
}
4866

4967
/**
50-
* @param [] $indexList
51-
* @return $this
68+
* @param int[] $indexList
69+
* @return Bitmap
5270
*/
5371
public function setBits(array $indexList)
5472
{
@@ -64,7 +82,7 @@ public function setBits(array $indexList)
6482

6583
/**
6684
* @param int $mask
67-
* @return $this
85+
* @return Bitmap
6886
*/
6987
public function setBitsByMask($mask)
7088
{
@@ -74,7 +92,7 @@ public function setBitsByMask($mask)
7492

7593
/**
7694
* @param int $index
77-
* @return $this
95+
* @return Bitmap
7896
*/
7997
public function unsetBit($index)
8098
{
@@ -84,7 +102,7 @@ public function unsetBit($index)
84102

85103
/**
86104
* @param int[] $indexList
87-
* @return $this
105+
* @return Bitmap
88106
*/
89107
public function unsetBits(array $indexList)
90108
{
@@ -100,7 +118,7 @@ public function unsetBits(array $indexList)
100118

101119
/**
102120
* @param int $mask
103-
* @return $this
121+
* @return Bitmap
104122
*/
105123
public function unsetBitsByMask($mask)
106124
{
@@ -109,7 +127,7 @@ public function unsetBitsByMask($mask)
109127
}
110128

111129
/**
112-
* @return $this
130+
* @return Bitmap
113131
*/
114132
public function invert()
115133
{
@@ -132,4 +150,24 @@ public function getBinary()
132150
{
133151
return decbin($this->bitmap);
134152
}
153+
154+
/**
155+
* @param Bitmap $bitmap
156+
* @return bool
157+
*/
158+
public function equals(Bitmap $bitmap)
159+
{
160+
return $this->bitmap === $bitmap->getInt();
161+
}
162+
163+
/**
164+
* Add two bitmaps.
165+
*
166+
* @param Bitmap $bitmap
167+
* @return Bitmap
168+
*/
169+
public function add(Bitmap $bitmap)
170+
{
171+
return new Bitmap($this->bitmap + $bitmap->getInt());
172+
}
135173
}

tests/BitmapTest.php

+93-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
class BitmapTest extends \PHPUnit_Framework_TestCase
66
{
7+
public function testIsAnyBitSetByMask()
8+
{
9+
$bitmap = new Bitmap(E_USER_ERROR | E_USER_WARNING | E_USER_DEPRECATED);
10+
$this->assertTrue($bitmap->isAnyMaskBitSet(E_USER_ERROR | E_ERROR));
11+
$this->assertFalse($bitmap->isAnyMaskBitSet(E_NOTICE | E_ERROR));
12+
}
13+
14+
public function testIsAllBitsSetByMask()
15+
{
16+
$bitmap = new Bitmap(E_USER_ERROR | E_USER_WARNING | E_USER_DEPRECATED);
17+
$this->assertTrue($bitmap->isAllMaskBitsSet(E_USER_ERROR | E_USER_WARNING));
18+
$this->assertFalse($bitmap->isAllMaskBitsSet(E_USER_ERROR | E_ERROR));
19+
}
20+
721
public function testIsBitSet()
822
{
923
// 5 => 101
@@ -15,68 +29,127 @@ public function testIsBitSet()
1529
public function testSetBit()
1630
{
1731
$bitmap = new Bitmap();
18-
19-
$this->assertEquals(8, $bitmap->setBit(3)->getInt());
32+
33+
// 8 => 1000
34+
$this->assertEquals(
35+
8,
36+
$bitmap->setBit(3)->getInt()
37+
);
2038
}
2139

2240
public function testSetBits()
2341
{
2442
$bitmap = new Bitmap();
25-
26-
$this->assertEquals(10, $bitmap->setBits(array(1, 3))->getInt());
43+
44+
// 10 => 1010
45+
$this->assertEquals(
46+
10,
47+
$bitmap->setBits(array(1, 3))->getInt()
48+
);
2749
}
2850

2951
public function testSetBitsByMask()
3052
{
3153
$bitmap = new Bitmap();
32-
33-
$this->assertEquals(10, $bitmap->setBitsByMask(10)->getInt());
54+
55+
// 10 => 1010
56+
$this->assertEquals(
57+
10,
58+
$bitmap->setBitsByMask(10)->getInt()
59+
);
3460
}
3561

3662
public function testUnsetBit()
3763
{
3864
$bitmap = new Bitmap(5);
39-
40-
$this->assertEquals(4, $bitmap->unsetBit(0)->getInt());
65+
66+
// 4 => 100
67+
$this->assertEquals(
68+
4,
69+
$bitmap->unsetBit(0)->getInt()
70+
);
4171
}
4272

4373
public function testUnsetBits()
4474
{
75+
// 5 => 101
4576
$bitmap = new Bitmap(5);
46-
47-
$this->assertEquals(4, $bitmap->unsetBits(array(0, 1))->getInt());
77+
78+
// 4 => 100
79+
$this->assertEquals(
80+
4,
81+
$bitmap->unsetBits(array(0, 1))->getInt()
82+
);
4883
}
4984

5085
public function testUnsetBitsByMask()
5186
{
87+
// 5 => 101
88+
// 4 => 100
5289
$bitmap = new Bitmap(5);
5390

54-
$this->assertEquals(1, $bitmap->unsetBitsByMask(4)->getInt());
91+
$this->assertEquals(
92+
1,
93+
$bitmap->unsetBitsByMask(4)->getInt()
94+
);
5595
}
5696

5797
public function testInvert()
5898
{
99+
// 5 => 101
59100
$bitmap = new Bitmap(5);
60101
$bitmap->invert();
61-
62-
$this->assertEquals(-6, $bitmap->getInt());
102+
103+
// -6 => 1111111111111111111111111111111111111111111111111111111111111010 (64 bit)
104+
$this->assertEquals(
105+
-6,
106+
$bitmap->getInt()
107+
);
63108
}
64109

65110
public function testGetInt()
66111
{
67112
$bitmap = new Bitmap();
68-
$bitmap->setBit(0);
69-
$bitmap->setBit(2);
113+
$bitmap->setBit(0); // 1
114+
$bitmap->setBit(2); // 101 => 5
70115

71-
$this->assertEquals(5, $bitmap->getInt());
116+
$this->assertEquals(
117+
5,
118+
$bitmap->getInt()
119+
);
72120
}
73121

74-
public function testBitmap()
122+
public function testGetBinary()
75123
{
76124
$bitmap = new Bitmap();
77-
$bitmap->setBit(0);
78-
$bitmap->setBit(2);
125+
$bitmap->setBit(0); // 1
126+
$bitmap->setBit(2); // 101
79127

80-
$this->assertEquals('101', $bitmap->getBinary());
128+
$this->assertEquals(
129+
'101',
130+
$bitmap->getBinary()
131+
);
132+
}
133+
134+
public function testEquals()
135+
{
136+
$bitmap42 = new Bitmap(42);
137+
138+
$this->assertTrue(
139+
$bitmap42->equals(new Bitmap(42))
140+
);
141+
142+
$this->assertFalse(
143+
$bitmap42->equals(new Bitmap(43))
144+
);
145+
}
146+
147+
public function testAdd()
148+
{
149+
$bitmap10 = new Bitmap(10);
150+
$bitmap42 = new Bitmap(42);
151+
$bitmap52 = $bitmap10->add($bitmap42);
152+
153+
$this->assertEquals(52, $bitmap52->getInt());
81154
}
82155
}

0 commit comments

Comments
 (0)