Debian Codegraph @

test/Common/BitMatrixTest.php

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