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