MatrixUtilTest.php
336 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | |
| 4 | namespace BaconQrCodeTest\Encoder; |
| 5 | |
| 6 | use BaconQrCode\Common\BitArray; |
| 7 | use BaconQrCode\Common\ErrorCorrectionLevel; |
| 8 | use BaconQrCode\Common\Version; |
| 9 | use BaconQrCode\Encoder\ByteMatrix; |
| 10 | use BaconQrCode\Encoder\MatrixUtil; |
| 11 | use PHPUnit\Framework\TestCase; |
| 12 | use ReflectionClass; |
| 13 | use ReflectionMethod; |
| 14 | |
| 15 | class MatrixUtilTest extends TestCase |
| 16 | { |
| 17 | /** |
| 18 | * @var ReflectionMethod[] |
| 19 | */ |
| 20 | protected $methods = []; |
| 21 | |
| 22 | public function setUp() : void |
| 23 | { |
| 24 | // Hack to be able to test protected methods |
| 25 | $reflection = new ReflectionClass(MatrixUtil::class); |
| 26 | |
| 27 | foreach ($reflection->getMethods(ReflectionMethod::IS_STATIC) as $method) { |
| 28 | $method->setAccessible(true); |
| 29 | $this->methods[$method->getName()] = $method; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public function testToString() : void |
| 34 | { |
| 35 | $matrix = new ByteMatrix(3, 3); |
| 36 | $matrix->set(0, 0, 0); |
| 37 | $matrix->set(1, 0, 1); |
| 38 | $matrix->set(2, 0, 0); |
| 39 | $matrix->set(0, 1, 1); |
| 40 | $matrix->set(1, 1, 0); |
| 41 | $matrix->set(2, 1, 1); |
| 42 | $matrix->set(0, 2, -1); |
| 43 | $matrix->set(1, 2, -1); |
| 44 | $matrix->set(2, 2, -1); |
| 45 | |
| 46 | $expected = " 0 1 0\n 1 0 1\n \n"; |
| 47 | $this->assertSame($expected, (string) $matrix); |
| 48 | } |
| 49 | |
| 50 | public function testClearMatrix() : void |
| 51 | { |
| 52 | $matrix = new ByteMatrix(2, 2); |
| 53 | MatrixUtil::clearMatrix($matrix); |
| 54 | |
| 55 | $this->assertSame(-1, $matrix->get(0, 0)); |
| 56 | $this->assertSame(-1, $matrix->get(1, 0)); |
| 57 | $this->assertSame(-1, $matrix->get(0, 1)); |
| 58 | $this->assertSame(-1, $matrix->get(1, 1)); |
| 59 | } |
| 60 | |
| 61 | public function testEmbedBasicPatterns1() : void |
| 62 | { |
| 63 | $matrix = new ByteMatrix(21, 21); |
| 64 | MatrixUtil::clearMatrix($matrix); |
| 65 | $this->methods['embedBasicPatterns']->invoke( |
| 66 | null, |
| 67 | Version::getVersionForNumber(1), |
| 68 | $matrix |
| 69 | ); |
| 70 | $expected = " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" |
| 71 | . " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" |
| 72 | . " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" |
| 73 | . " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" |
| 74 | . " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" |
| 75 | . " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" |
| 76 | . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" |
| 77 | . " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 78 | . " 1 \n" |
| 79 | . " 0 \n" |
| 80 | . " 1 \n" |
| 81 | . " 0 \n" |
| 82 | . " 1 \n" |
| 83 | . " 0 0 0 0 0 0 0 0 1 \n" |
| 84 | . " 1 1 1 1 1 1 1 0 \n" |
| 85 | . " 1 0 0 0 0 0 1 0 \n" |
| 86 | . " 1 0 1 1 1 0 1 0 \n" |
| 87 | . " 1 0 1 1 1 0 1 0 \n" |
| 88 | . " 1 0 1 1 1 0 1 0 \n" |
| 89 | . " 1 0 0 0 0 0 1 0 \n" |
| 90 | . " 1 1 1 1 1 1 1 0 \n"; |
| 91 | |
| 92 | $this->assertSame($expected, (string) $matrix); |
| 93 | } |
| 94 | |
| 95 | public function testEmbedBasicPatterns2() : void |
| 96 | { |
| 97 | $matrix = new ByteMatrix(25, 25); |
| 98 | MatrixUtil::clearMatrix($matrix); |
| 99 | $this->methods['embedBasicPatterns']->invoke( |
| 100 | null, |
| 101 | Version::getVersionForNumber(2), |
| 102 | $matrix |
| 103 | ); |
| 104 | $expected = " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" |
| 105 | . " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" |
| 106 | . " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" |
| 107 | . " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" |
| 108 | . " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" |
| 109 | . " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" |
| 110 | . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" |
| 111 | . " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 112 | . " 1 \n" |
| 113 | . " 0 \n" |
| 114 | . " 1 \n" |
| 115 | . " 0 \n" |
| 116 | . " 1 \n" |
| 117 | . " 0 \n" |
| 118 | . " 1 \n" |
| 119 | . " 0 \n" |
| 120 | . " 1 1 1 1 1 1 \n" |
| 121 | . " 0 0 0 0 0 0 0 0 1 1 0 0 0 1 \n" |
| 122 | . " 1 1 1 1 1 1 1 0 1 0 1 0 1 \n" |
| 123 | . " 1 0 0 0 0 0 1 0 1 0 0 0 1 \n" |
| 124 | . " 1 0 1 1 1 0 1 0 1 1 1 1 1 \n" |
| 125 | . " 1 0 1 1 1 0 1 0 \n" |
| 126 | . " 1 0 1 1 1 0 1 0 \n" |
| 127 | . " 1 0 0 0 0 0 1 0 \n" |
| 128 | . " 1 1 1 1 1 1 1 0 \n"; |
| 129 | |
| 130 | $this->assertSame($expected, (string) $matrix); |
| 131 | } |
| 132 | |
| 133 | public function testEmbedTypeInfo() : void |
| 134 | { |
| 135 | $matrix = new ByteMatrix(21, 21); |
| 136 | MatrixUtil::clearMatrix($matrix); |
| 137 | $this->methods['embedTypeInfo']->invoke( |
| 138 | null, |
| 139 | ErrorCorrectionLevel::M(), |
| 140 | 5, |
| 141 | $matrix |
| 142 | ); |
| 143 | $expected = " 0 \n" |
| 144 | . " 1 \n" |
| 145 | . " 1 \n" |
| 146 | . " 1 \n" |
| 147 | . " 0 \n" |
| 148 | . " 0 \n" |
| 149 | . " \n" |
| 150 | . " 1 \n" |
| 151 | . " 1 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0\n" |
| 152 | . " \n" |
| 153 | . " \n" |
| 154 | . " \n" |
| 155 | . " \n" |
| 156 | . " \n" |
| 157 | . " 0 \n" |
| 158 | . " 0 \n" |
| 159 | . " 0 \n" |
| 160 | . " 0 \n" |
| 161 | . " 0 \n" |
| 162 | . " 0 \n" |
| 163 | . " 1 \n"; |
| 164 | |
| 165 | $this->assertSame($expected, (string) $matrix); |
| 166 | } |
| 167 | |
| 168 | public function testEmbedVersionInfo() : void |
| 169 | { |
| 170 | $matrix = new ByteMatrix(21, 21); |
| 171 | MatrixUtil::clearMatrix($matrix); |
| 172 | $this->methods['maybeEmbedVersionInfo']->invoke( |
| 173 | null, |
| 174 | Version::getVersionForNumber(7), |
| 175 | $matrix |
| 176 | ); |
| 177 | $expected = " 0 0 1 \n" |
| 178 | . " 0 1 0 \n" |
| 179 | . " 0 1 0 \n" |
| 180 | . " 0 1 1 \n" |
| 181 | . " 1 1 1 \n" |
| 182 | . " 0 0 0 \n" |
| 183 | . " \n" |
| 184 | . " \n" |
| 185 | . " \n" |
| 186 | . " \n" |
| 187 | . " 0 0 0 0 1 0 \n" |
| 188 | . " 0 1 1 1 1 0 \n" |
| 189 | . " 1 0 0 1 1 0 \n" |
| 190 | . " \n" |
| 191 | . " \n" |
| 192 | . " \n" |
| 193 | . " \n" |
| 194 | . " \n" |
| 195 | . " \n" |
| 196 | . " \n" |
| 197 | . " \n"; |
| 198 | |
| 199 | $this->assertSame($expected, (string) $matrix); |
| 200 | } |
| 201 | |
| 202 | public function testEmbedDataBits() : void |
| 203 | { |
| 204 | $matrix = new ByteMatrix(21, 21); |
| 205 | MatrixUtil::clearMatrix($matrix); |
| 206 | $this->methods['embedBasicPatterns']->invoke( |
| 207 | null, |
| 208 | Version::getVersionForNumber(1), |
| 209 | $matrix |
| 210 | ); |
| 211 | |
| 212 | $bits = new BitArray(); |
| 213 | $this->methods['embedDataBits']->invoke( |
| 214 | null, |
| 215 | $bits, |
| 216 | -1, |
| 217 | $matrix |
| 218 | ); |
| 219 | |
| 220 | $expected = " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n" |
| 221 | . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" |
| 222 | . " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" |
| 223 | . " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" |
| 224 | . " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n" |
| 225 | . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" |
| 226 | . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" |
| 227 | . " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 228 | . " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 229 | . " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 230 | . " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 231 | . " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 232 | . " 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 233 | . " 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 234 | . " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 235 | . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 236 | . " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 237 | . " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 238 | . " 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 239 | . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" |
| 240 | . " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"; |
| 241 | |
| 242 | $this->assertSame($expected, (string) $matrix); |
| 243 | } |
| 244 | |
| 245 | public function testBuildMatrix() : void |
| 246 | { |
| 247 | $bytes = [ |
| 248 | 32, 65, 205, 69, 41, 220, 46, 128, 236, 42, 159, 74, 221, 244, 169, |
| 249 | 239, 150, 138, 70, 237, 85, 224, 96, 74, 219 , 61 |
| 250 | ]; |
| 251 | $bits = new BitArray(); |
| 252 | |
| 253 | foreach ($bytes as $byte) { |
| 254 | $bits->appendBits($byte, 8); |
| 255 | } |
| 256 | |
| 257 | $matrix = new ByteMatrix(21, 21); |
| 258 | MatrixUtil::buildMatrix( |
| 259 | $bits, |
| 260 | ErrorCorrectionLevel::H(), |
| 261 | Version::getVersionForNumber(1), |
| 262 | 3, |
| 263 | $matrix |
| 264 | ); |
| 265 | |
| 266 | $expected = " 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n" |
| 267 | . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n" |
| 268 | . " 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\n" |
| 269 | . " 1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\n" |
| 270 | . " 1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\n" |
| 271 | . " 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\n" |
| 272 | . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" |
| 273 | . " 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\n" |
| 274 | . " 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\n" |
| 275 | . " 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\n" |
| 276 | . " 1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\n" |
| 277 | . " 1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\n" |
| 278 | . " 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\n" |
| 279 | . " 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\n" |
| 280 | . " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n" |
| 281 | . " 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\n" |
| 282 | . " 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\n" |
| 283 | . " 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\n" |
| 284 | . " 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" |
| 285 | . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" |
| 286 | . " 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n"; |
| 287 | |
| 288 | $this->assertSame($expected, (string) $matrix); |
| 289 | } |
| 290 | |
| 291 | public function testFindMsbSet() : void |
| 292 | { |
| 293 | $this->assertSame(0, $this->methods['findMsbSet']->invoke(null, 0)); |
| 294 | $this->assertSame(1, $this->methods['findMsbSet']->invoke(null, 1)); |
| 295 | $this->assertSame(8, $this->methods['findMsbSet']->invoke(null, 0x80)); |
| 296 | $this->assertSame(32, $this->methods['findMsbSet']->invoke(null, 0x80000000)); |
| 297 | } |
| 298 | |
| 299 | public function testCalculateBchCode() : void |
| 300 | { |
| 301 | // Encoding of type information. |
| 302 | // From Appendix C in JISX0510:2004 (p 65) |
| 303 | $this->assertSame(0xdc, $this->methods['calculateBchCode']->invoke(null, 5, 0x537)); |
| 304 | // From http://www.swetake.com/qr/qr6.html |
| 305 | $this->assertSame(0x1c2, $this->methods['calculateBchCode']->invoke(null, 0x13, 0x537)); |
| 306 | // From http://www.swetake.com/qr/qr11.html |
| 307 | $this->assertSame(0x214, $this->methods['calculateBchCode']->invoke(null, 0x1b, 0x537)); |
| 308 | |
| 309 | // Encoding of version information. |
| 310 | // From Appendix D in JISX0510:2004 (p 68) |
| 311 | $this->assertSame(0xc94, $this->methods['calculateBchCode']->invoke(null, 7, 0x1f25)); |
| 312 | $this->assertSame(0x5bc, $this->methods['calculateBchCode']->invoke(null, 8, 0x1f25)); |
| 313 | $this->assertSame(0xa99, $this->methods['calculateBchCode']->invoke(null, 9, 0x1f25)); |
| 314 | $this->assertSame(0x4d3, $this->methods['calculateBchCode']->invoke(null, 10, 0x1f25)); |
| 315 | $this->assertSame(0x9a6, $this->methods['calculateBchCode']->invoke(null, 20, 0x1f25)); |
| 316 | $this->assertSame(0xd75, $this->methods['calculateBchCode']->invoke(null, 30, 0x1f25)); |
| 317 | $this->assertSame(0xc69, $this->methods['calculateBchCode']->invoke(null, 40, 0x1f25)); |
| 318 | } |
| 319 | |
| 320 | public function testMakeVersionInfoBits() : void |
| 321 | { |
| 322 | // From Appendix D in JISX0510:2004 (p 68) |
| 323 | $bits = new BitArray(); |
| 324 | $this->methods['makeVersionInfoBits']->invoke(null, Version::getVersionForNumber(7), $bits); |
| 325 | $this->assertSame(' ...XXXXX ..X..X.X ..', (string) $bits); |
| 326 | } |
| 327 | |
| 328 | public function testMakeTypeInfoBits() : void |
| 329 | { |
| 330 | // From Appendix D in JISX0510:2004 (p 68) |
| 331 | $bits = new BitArray(); |
| 332 | $this->methods['makeTypeInfoBits']->invoke(null, ErrorCorrectionLevel::M(), 5, $bits); |
| 333 | $this->assertSame(' X......X X..XXX.', (string) $bits); |
| 334 | } |
| 335 | } |
| 336 |