BitArrayTest.php 5.9 KB

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