1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* BaconQrCode
*
* @link http://github.com/Bacon/BaconQrCode For the canonical source repository
* @copyright 2013 Ben 'DASPRiD' Scholzen
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace BaconQrCode\Common;
use PHPUnit_Framework_TestCase as TestCase;
class BitArrayTest extends TestCase
{
public function testGetSet()
{
$array = new BitArray(33);
for ($i = 0; $i < 33; $i++) {
$this->assertFalse($array->get($i));
$array->set($i);
$this->assertTrue($array->get($i));
}
}
public function testGetNextSet1()
{
$array = new BitArray(32);
for ($i = 0; $i < $array->getSize(); $i++) {
$this->assertEquals($i, 32, '', $array->getNextSet($i));
}
$array = new BitArray(33);
for ($i = 0; $i < $array->getSize(); $i++) {
$this->assertEquals($i, 33, '', $array->getNextSet($i));
}
}
public function testGetNextSet2()
{
$array = new BitArray(33);
for ($i = 0; $i < $array->getSize(); $i++) {
$this->assertEquals($i, $i <= 31 ? 31 : 33, '', $array->getNextSet($i));
}
$array = new BitArray(33);
for ($i = 0; $i < $array->getSize(); $i++) {
$this->assertEquals($i, 32, '', $array->getNextSet($i));
}
}
public function testGetNextSet3()
{
$array = new BitArray(63);
$array->set(31);
$array->set(32);
for ($i = 0; $i < $array->getSize(); $i++) {
if ($i <= 31) {
$expected = 31;
} elseif ($i <= 32) {
$expected = 32;
} else {
$expected = 63;
}
$this->assertEquals($i, $expected, '', $array->getNextSet($i));
}
}
public function testGetNextSet4()
{
$array = new BitArray(63);
$array->set(33);
$array->set(40);
for ($i = 0; $i < $array->getSize(); $i++) {
if ($i <= 33) {
$expected = 33;
} elseif ($i <= 40) {
$expected = 40;
} else {
$expected = 63;
}
$this->assertEquals($i, $expected, '', $array->getNextSet($i));
}
}
public function testGetNextSet5()
{
if (defined('MT_RAND_PHP')) {
mt_srand(0xdeadbeef, MT_RAND_PHP);
} else {
mt_srand(0xdeadbeef);
}
for ($i = 0; $i < 10; $i++) {
$array = new BitArray(mt_rand(1, 100));
$numSet = mt_rand(0, 19);
for ($j = 0; $j < $numSet; $j++) {
$array->set(mt_rand(0, $array->getSize() - 1));
}
$numQueries = mt_rand(0, 19);
for ($j = 0; $j < $numQueries; $j++) {
$query = mt_rand(0, $array->getSize() - 1);
$expected = $query;
while ($expected < $array->getSize() && !$array->get($expected)) {
$expected++;
}
$actual = $array->getNextSet($query);
if ($actual !== $expected) {
$array->getNextSet($query);
}
$this->assertEquals($expected, $actual);
}
}
}
public function testSetBulk()
{
$array = new BitArray(64);
$array->setBulk(32, 0xFFFF0000);
for ($i = 0; $i < 48; $i++) {
$this->assertFalse($array->get($i));
}
for ($i = 48; $i < 64; $i++) {
$this->assertTrue($array->get($i));
}
}
public function testClear()
{
$array = new BitArray(32);
for ($i = 0; $i < 32; $i++) {
$array->set($i);
}
$array->clear();
for ($i = 0; $i < 32; $i++) {
$this->assertFalse($array->get($i));
}
}
public function testGetArray()
{
$array = new BitArray(64);
$array->set(0);
$array->set(63);
$ints = $array->getBitArray();
$this->assertEquals(1, $ints[0]);
$this->assertEquals(0x80000000, $ints[1]);
}
public function testIsRange()
{
$array = new BitArray(64);
$this->assertTrue($array->isRange(0, 64, false));
$this->assertFalse($array->isRange(0, 64, true));
$array->set(32);
$this->assertTrue($array->isRange(32, 33, true));
$array->set(31);
$this->assertTrue($array->isRange(31, 33, true));
$array->set(34);
$this->assertFalse($array->isRange(31, 35, true));
for ($i = 0; $i < 31; $i++) {
$array->set($i);
}
$this->assertTrue($array->isRange(0, 33, true));
for ($i = 33; $i < 64; $i++) {
$array->set($i);
}
$this->assertTrue($array->isRange(0, 64, true));
$this->assertFalse($array->isRange(0, 64, false));
}
}