4
4
5
5
class BitmapTest extends \PHPUnit_Framework_TestCase
6
6
{
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
+
7
21
public function testIsBitSet ()
8
22
{
9
23
// 5 => 101
@@ -15,68 +29,127 @@ public function testIsBitSet()
15
29
public function testSetBit ()
16
30
{
17
31
$ 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
+ );
20
38
}
21
39
22
40
public function testSetBits ()
23
41
{
24
42
$ 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
+ );
27
49
}
28
50
29
51
public function testSetBitsByMask ()
30
52
{
31
53
$ 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
+ );
34
60
}
35
61
36
62
public function testUnsetBit ()
37
63
{
38
64
$ 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
+ );
41
71
}
42
72
43
73
public function testUnsetBits ()
44
74
{
75
+ // 5 => 101
45
76
$ 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
+ );
48
83
}
49
84
50
85
public function testUnsetBitsByMask ()
51
86
{
87
+ // 5 => 101
88
+ // 4 => 100
52
89
$ bitmap = new Bitmap (5 );
53
90
54
- $ this ->assertEquals (1 , $ bitmap ->unsetBitsByMask (4 )->getInt ());
91
+ $ this ->assertEquals (
92
+ 1 ,
93
+ $ bitmap ->unsetBitsByMask (4 )->getInt ()
94
+ );
55
95
}
56
96
57
97
public function testInvert ()
58
98
{
99
+ // 5 => 101
59
100
$ bitmap = new Bitmap (5 );
60
101
$ bitmap ->invert ();
61
-
62
- $ this ->assertEquals (-6 , $ bitmap ->getInt ());
102
+
103
+ // -6 => 1111111111111111111111111111111111111111111111111111111111111010 (64 bit)
104
+ $ this ->assertEquals (
105
+ -6 ,
106
+ $ bitmap ->getInt ()
107
+ );
63
108
}
64
109
65
110
public function testGetInt ()
66
111
{
67
112
$ bitmap = new Bitmap ();
68
- $ bitmap ->setBit (0 );
69
- $ bitmap ->setBit (2 );
113
+ $ bitmap ->setBit (0 ); // 1
114
+ $ bitmap ->setBit (2 ); // 101 => 5
70
115
71
- $ this ->assertEquals (5 , $ bitmap ->getInt ());
116
+ $ this ->assertEquals (
117
+ 5 ,
118
+ $ bitmap ->getInt ()
119
+ );
72
120
}
73
121
74
- public function testBitmap ()
122
+ public function testGetBinary ()
75
123
{
76
124
$ bitmap = new Bitmap ();
77
- $ bitmap ->setBit (0 );
78
- $ bitmap ->setBit (2 );
125
+ $ bitmap ->setBit (0 ); // 1
126
+ $ bitmap ->setBit (2 ); // 101
79
127
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 ());
81
154
}
82
155
}
0 commit comments