BitArrayTest.php 4.9 KB

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