BitMatrixTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCodeTest\Common;
  4. use BaconQrCode\Common\BitArray;
  5. use BaconQrCode\Common\BitMatrix;
  6. use PHPUnit\Framework\TestCase;
  7. class BitMatrixTest extends TestCase
  8. {
  9. public function testGetSet() : void
  10. {
  11. $matrix = new BitMatrix(33);
  12. $this->assertEquals(33, $matrix->getHeight());
  13. for ($y = 0; $y < 33; ++$y) {
  14. for ($x = 0; $x < 33; ++$x) {
  15. if ($y * $x % 3 === 0) {
  16. $matrix->set($x, $y);
  17. }
  18. }
  19. }
  20. for ($y = 0; $y < 33; $y++) {
  21. for ($x = 0; $x < 33; ++$x) {
  22. $this->assertSame(0 === $x * $y % 3, $matrix->get($x, $y));
  23. }
  24. }
  25. }
  26. public function testSetRegion() : void
  27. {
  28. $matrix = new BitMatrix(5);
  29. $matrix->setRegion(1, 1, 3, 3);
  30. for ($y = 0; $y < 5; ++$y) {
  31. for ($x = 0; $x < 5; ++$x) {
  32. $this->assertSame($y >= 1 && $y <= 3 && $x >= 1 && $x <= 3, $matrix->get($x, $y));
  33. }
  34. }
  35. }
  36. public function testRectangularMatrix() : void
  37. {
  38. $matrix = new BitMatrix(75, 20);
  39. $this->assertSame(75, $matrix->getWidth());
  40. $this->assertSame(20, $matrix->getHeight());
  41. $matrix->set(10, 0);
  42. $matrix->set(11, 1);
  43. $matrix->set(50, 2);
  44. $matrix->set(51, 3);
  45. $matrix->flip(74, 4);
  46. $matrix->flip(0, 5);
  47. $this->assertTrue($matrix->get(10, 0));
  48. $this->assertTrue($matrix->get(11, 1));
  49. $this->assertTrue($matrix->get(50, 2));
  50. $this->assertTrue($matrix->get(51, 3));
  51. $this->assertTrue($matrix->get(74, 4));
  52. $this->assertTrue($matrix->get(0, 5));
  53. $matrix->flip(50, 2);
  54. $matrix->flip(51, 3);
  55. $this->assertFalse($matrix->get(50, 2));
  56. $this->assertFalse($matrix->get(51, 3));
  57. }
  58. public function testRectangularSetRegion() : void
  59. {
  60. $matrix = new BitMatrix(320, 240);
  61. $this->assertSame(320, $matrix->getWidth());
  62. $this->assertSame(240, $matrix->getHeight());
  63. $matrix->setRegion(105, 22, 80, 12);
  64. for ($y = 0; $y < 240; ++$y) {
  65. for ($x = 0; $x < 320; ++$x) {
  66. $this->assertEquals($y >= 22 && $y < 34 && $x >= 105 && $x < 185, $matrix->get($x, $y));
  67. }
  68. }
  69. }
  70. public function testGetRow() : void
  71. {
  72. $matrix = new BitMatrix(102, 5);
  73. for ($x = 0; $x < 102; ++$x) {
  74. if (0 === ($x & 3)) {
  75. $matrix->set($x, 2);
  76. }
  77. }
  78. $array1 = $matrix->getRow(2, null);
  79. $this->assertSame(102, $array1->getSize());
  80. $array2 = new BitArray(60);
  81. $array2 = $matrix->getRow(2, $array2);
  82. $this->assertSame(102, $array2->getSize());
  83. $array3 = new BitArray(200);
  84. $array3 = $matrix->getRow(2, $array3);
  85. $this->assertSame(200, $array3->getSize());
  86. for ($x = 0; $x < 102; ++$x) {
  87. $on = (0 === ($x & 3));
  88. $this->assertSame($on, $array1->get($x));
  89. $this->assertSame($on, $array2->get($x));
  90. $this->assertSame($on, $array3->get($x));
  91. }
  92. }
  93. }