123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- declare(strict_types = 1);
- namespace BaconQrCodeTest\Common;
- use BaconQrCode\Common\BitArray;
- use PHPUnit\Framework\TestCase;
- use PHPUnit\Runner\Version as PHPUnitVersion;
- final class BitArrayTest extends TestCase
- {
- private function getPhpUnitMajorVersion(): int
- {
- return (int) explode('.', PHPUnitVersion::id())[0];
- }
- public function testGetSet() : void
- {
- $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() : void
- {
- $array = new BitArray(32);
- for ($i = 0; $i < $array->getSize(); ++$i) {
- if ($this->getPhpUnitMajorVersion() === 7) {
- $this->assertEquals($i, 32, '', $array->getNextSet($i));
- } else {
- $this->assertEqualsWithDelta($i, 32, $array->getNextSet($i));
- }
- }
- $array = new BitArray(33);
- for ($i = 0; $i < $array->getSize(); ++$i) {
- if ($this->getPhpUnitMajorVersion() === 7) {
- $this->assertEquals($i, 33, '', $array->getNextSet($i));
- } else {
- $this->assertEqualsWithDelta($i, 33, $array->getNextSet($i));
- }
- }
- }
- public function testGetNextSet2() : void
- {
- $array = new BitArray(33);
- for ($i = 0; $i < $array->getSize(); ++$i) {
- if ($this->getPhpUnitMajorVersion() === 7) {
- $this->assertEquals($i, $i <= 31 ? 31 : 33, '', $array->getNextSet($i));
- } else {
- $this->assertEqualsWithDelta($i, $i <= 31 ? 31 : 33, $array->getNextSet($i));
- }
- }
- $array = new BitArray(33);
- for ($i = 0; $i < $array->getSize(); ++$i) {
- if ($this->getPhpUnitMajorVersion() === 7) {
- $this->assertEquals($i, 32, '', $array->getNextSet($i));
- } else {
- $this->assertEqualsWithDelta($i, 32, $array->getNextSet($i));
- }
- }
- }
- public function testGetNextSet3() : void
- {
- $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;
- }
- if ($this->getPhpUnitMajorVersion() === 7) {
- $this->assertEquals($i, $expected, '', $array->getNextSet($i));
- } else {
- $this->assertEqualsWithDelta($i, $expected, $array->getNextSet($i));
- }
- }
- }
- public function testGetNextSet4() : void
- {
- $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;
- }
- if ($this->getPhpUnitMajorVersion() === 7) {
- $this->assertEquals($i, $expected, '', $array->getNextSet($i));
- } else {
- $this->assertEqualsWithDelta($i, $expected, $array->getNextSet($i));
- }
- }
- }
- public function testGetNextSet5() : void
- {
- mt_srand(0xdeadbeef, MT_RAND_PHP);
- 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() : void
- {
- $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() : void
- {
- $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() : void
- {
- $array = new BitArray(64);
- $array->set(0);
- $array->set(63);
- $ints = $array->getBitArray();
- $this->assertSame(1, $ints[0]);
- $this->assertSame(0x80000000, $ints[1]);
- }
- public function testIsRange() : void
- {
- $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));
- }
- }
|