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