BitMatrixTest.php 3.3 KB

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