Debian Codegraph @

test/Encoder/EncoderTest.php

raw
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\Mode;
9 use BaconQrCode\Common\Version;
10 use BaconQrCode\Encoder\Encoder;
11 use BaconQrCode\Exception\WriterException;
12 use PHPUnit\Framework\TestCase;
13 use ReflectionClass;
14 use ReflectionMethod;
15 use SplFixedArray;
16
17 final class EncoderTest extends TestCase
18 {
19 /**
20 * @var ReflectionMethod[]
21 */
22 protected array $methods = [];
23
24 public function setUp() : void
25 {
26 // Hack to be able to test protected methods
27 $reflection = new ReflectionClass(Encoder::class);
28
29 foreach ($reflection->getMethods(ReflectionMethod::IS_STATIC) as $method) {
30 $this->methods[$method->getName()] = $method;
31 }
32 }
33
34 public function testGetAlphanumericCode() : void
35 {
36 // The first ten code points are numbers.
37 for ($i = 0; $i < 10; ++$i) {
38 $this->assertSame($i, $this->methods['getAlphanumericCode']->invoke(null, ord('0') + $i));
39 }
40
41 // The next 26 code points are capital alphabet letters.
42 for ($i = 10; $i < 36; ++$i) {
43 // The first ten code points are numbers
44 $this->assertSame($i, $this->methods['getAlphanumericCode']->invoke(null, ord('A') + $i - 10));
45 }
46
47 // Others are symbol letters.
48 $this->assertSame(36, $this->methods['getAlphanumericCode']->invoke(null, ord(' ')));
49 $this->assertSame(37, $this->methods['getAlphanumericCode']->invoke(null, ord('$')));
50 $this->assertSame(38, $this->methods['getAlphanumericCode']->invoke(null, ord('%')));
51 $this->assertSame(39, $this->methods['getAlphanumericCode']->invoke(null, ord('*')));
52 $this->assertSame(40, $this->methods['getAlphanumericCode']->invoke(null, ord('+')));
53 $this->assertSame(41, $this->methods['getAlphanumericCode']->invoke(null, ord('-')));
54 $this->assertSame(42, $this->methods['getAlphanumericCode']->invoke(null, ord('.')));
55 $this->assertSame(43, $this->methods['getAlphanumericCode']->invoke(null, ord('/')));
56 $this->assertSame(44, $this->methods['getAlphanumericCode']->invoke(null, ord(':')));
57
58 // Should return -1 for other letters.
59 $this->assertSame(-1, $this->methods['getAlphanumericCode']->invoke(null, ord('a')));
60 $this->assertSame(-1, $this->methods['getAlphanumericCode']->invoke(null, ord('#')));
61 $this->assertSame(-1, $this->methods['getAlphanumericCode']->invoke(null, ord("\0")));
62 }
63
64 public function testChooseMode() : void
65 {
66 // Empty string
67 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, ''));
68 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, '', 'SHIFT-JIS'));
69
70 // Numeric mode
71 $this->assertSame(Mode::NUMERIC(), $this->methods['chooseMode']->invoke(null, '0'));
72 $this->assertSame(Mode::NUMERIC(), $this->methods['chooseMode']->invoke(null, '0123456789'));
73
74 // Alphanumeric mode
75 $this->assertSame(Mode::ALPHANUMERIC(), $this->methods['chooseMode']->invoke(null, 'A'));
76 $this->assertSame(
77 Mode::ALPHANUMERIC(),
78 $this->methods['chooseMode']->invoke(null, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:')
79 );
80
81 // 8-bit byte mode
82 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, 'a'));
83 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, '#'));
84
85 // AIUE in Hiragana in SHIFT-JIS
86 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, "\x8\xa\x8\xa\x8\xa\x8\xa6"));
87
88 // Nihon in Kanji in SHIFT-JIS
89 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, "\x9\xf\x9\x7b"));
90
91 // Sou-Utso-Byou in Kanji in SHIFT-JIS
92 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, "\xe\x4\x9\x5\x9\x61"));
93
94 // SHIFT-JIS encoding, content only consists of double-byte kanji characters
95 $this->assertSame(Mode::KANJI(), $this->methods['chooseMode']->invoke(null, 'あいうえお', 'SHIFT-JIS'));
96
97 // SHIFT-JIS encoding, but content doesn't exclusively consist of kanji characters
98 $this->assertSame(Mode::BYTE(), $this->methods['chooseMode']->invoke(null, 'あいうえお123', 'SHIFT-JIS'));
99 }
100
101 public function testEncode() : void
102 {
103 $qrCode = Encoder::encode('ABCDEF', ErrorCorrectionLevel::H());
104 $expected = "<<\n"
105 . " mode: ALPHANUMERIC\n"
106 . " ecLevel: H\n"
107 . " version: 1\n"
108 . " maskPattern: 0\n"
109 . " matrix:\n"
110 . " 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1\n"
111 . " 1 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 0 0 0 0 1\n"
112 . " 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1\n"
113 . " 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1\n"
114 . " 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1\n"
115 . " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1\n"
116 . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n"
117 . " 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0\n"
118 . " 0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1\n"
119 . " 1 0 1 1 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0\n"
120 . " 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 0\n"
121 . " 1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 0\n"
122 . " 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 1 1 0\n"
123 . " 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 0 0 0\n"
124 . " 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 0 1\n"
125 . " 1 0 0 0 0 0 1 0 1 1 1 1 0 1 0 1 1 1 1 0 1\n"
126 . " 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 1 0 0 0 0 1\n"
127 . " 1 0 1 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0\n"
128 . " 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 1 1 1 0 1\n"
129 . " 1 0 0 0 0 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 1\n"
130 . " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1\n"
131 . ">>\n";
132
133 $this->assertSame($expected, (string) $qrCode);
134 }
135
136 public function testSimpleUtf8Eci() : void
137 {
138 $qrCode = Encoder::encode('hello', ErrorCorrectionLevel::H(), 'utf-8');
139 $expected = "<<\n"
140 . " mode: BYTE\n"
141 . " ecLevel: H\n"
142 . " version: 1\n"
143 . " maskPattern: 3\n"
144 . " matrix:\n"
145 . " 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n"
146 . " 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1\n"
147 . " 1 0 1 1 1 0 1 0 0 1 0 1 0 0 1 0 1 1 1 0 1\n"
148 . " 1 0 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1\n"
149 . " 1 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1\n"
150 . " 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1\n"
151 . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n"
152 . " 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0\n"
153 . " 0 0 1 1 0 0 1 1 1 1 0 0 0 1 1 0 1 0 0 0 0\n"
154 . " 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0 1 1 1 0\n"
155 . " 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1\n"
156 . " 1 1 0 0 1 0 0 1 1 0 0 1 1 1 1 0 1 0 1 1 0\n"
157 . " 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 1 0 0\n"
158 . " 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1\n"
159 . " 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 1 0 0\n"
160 . " 1 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 1 1 1 0 1\n"
161 . " 1 0 1 1 1 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0\n"
162 . " 1 0 1 1 1 0 1 0 1 1 1 0 1 0 0 0 1 1 0 0 0\n"
163 . " 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 0\n"
164 . " 1 0 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 1 0\n"
165 . " 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 1 1 0 0 0 0\n"
166 . ">>\n";
167
168 $this->assertSame($expected, (string) $qrCode);
169 }
170
171 public function testSimpleUtf8WithoutEci() : void
172 {
173 $qrCode = Encoder::encode('hello', ErrorCorrectionLevel::H(), 'utf-8', null, false);
174 $expected = "<<\n"
175 . " mode: BYTE\n"
176 . " ecLevel: H\n"
177 . " version: 1\n"
178 . " maskPattern: 6\n"
179 . " matrix:\n"
180 . " 1 1 1 1 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 1\n"
181 . " 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n"
182 . " 1 0 1 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1\n"
183 . " 1 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 1 1 1 0 1\n"
184 . " 1 0 1 1 1 0 1 0 0 0 1 1 1 0 1 0 1 1 1 0 1\n"
185 . " 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1\n"
186 . " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n"
187 . " 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0\n"
188 . " 0 0 0 1 1 0 1 1 0 1 1 1 0 0 0 0 0 1 1 0 0\n"
189 . " 1 0 1 0 0 1 0 1 0 1 1 0 0 0 1 1 1 1 1 0 0\n"
190 . " 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1\n"
191 . " 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 0 1 0 0\n"
192 . " 0 1 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 0 1 0\n"
193 . " 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 0\n"
194 . " 1 1 1 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0\n"
195 . " 1 0 0 0 0 0 1 0 0 1 1 1 0 1 0 0 0 1 1 1 1\n"
196 . " 1 0 1 1 1 0 1 0 1 0 1 0 1 0 0 0 1 1 0 1 1\n"
197 . " 1 0 1 1 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 0\n"
198 . " 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 1 1 1 1 1 1\n"
199 . " 1 0 0 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1\n"
200 . " 1 1 1 1 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0\n"
201 . ">>\n";
202
203 $this->assertSame($expected, (string) $qrCode);
204 }
205
206 public function testAppendModeInfo() : void
207 {
208 $bits = new BitArray();
209 $this->methods['appendModeInfo']->invoke(null, Mode::NUMERIC(), $bits);
210 $this->assertSame(' ...X', (string) $bits);
211 }
212
213 public function testAppendLengthInfo() : void
214 {
215 // 1 letter (1/1), 10 bits.
216 $bits = new BitArray();
217 $this->methods['appendLengthInfo']->invoke(
218 null,
219 1,
220 Version::getVersionForNumber(1),
221 Mode::NUMERIC(),
222 $bits
223 );
224 $this->assertSame(' ........ .X', (string) $bits);
225
226 // 2 letters (2/1), 11 bits.
227 $bits = new BitArray();
228 $this->methods['appendLengthInfo']->invoke(
229 null,
230 2,
231 Version::getVersionForNumber(10),
232 Mode::ALPHANUMERIC(),
233 $bits
234 );
235 $this->assertSame(' ........ .X.', (string) $bits);
236
237 // 255 letters (255/1), 16 bits.
238 $bits = new BitArray();
239 $this->methods['appendLengthInfo']->invoke(
240 null,
241 255,
242 Version::getVersionForNumber(27),
243 Mode::BYTE(),
244 $bits
245 );
246 $this->assertSame(' ........ XXXXXXXX', (string) $bits);
247
248 // 512 letters (1024/2), 12 bits.
249 $bits = new BitArray();
250 $this->methods['appendLengthInfo']->invoke(
251 null,
252 512,
253 Version::getVersionForNumber(40),
254 Mode::KANJI(),
255 $bits
256 );
257 $this->assertSame(' ..X..... ....', (string) $bits);
258 }
259
260 public function testAppendBytes() : void
261 {
262 // Should use appendNumericBytes.
263 // 1 = 01 = 0001 in 4 bits.
264 $bits = new BitArray();
265 $this->methods['appendBytes']->invoke(
266 null,
267 '1',
268 Mode::NUMERIC(),
269 $bits,
270 Encoder::DEFAULT_BYTE_MODE_ENCODING
271 );
272 $this->assertSame(' ...X', (string) $bits);
273
274 // Should use appendAlphaNumericBytes.
275 // A = 10 = 0xa = 001010 in 6 bits.
276 $bits = new BitArray();
277 $this->methods['appendBytes']->invoke(
278 null,
279 'A',
280 Mode::ALPHANUMERIC(),
281 $bits,
282 Encoder::DEFAULT_BYTE_MODE_ENCODING
283 );
284 $this->assertSame(' ..X.X.', (string) $bits);
285
286 // Should use append8BitBytes.
287 // 0x61, 0x62, 0x63
288 $bits = new BitArray();
289 $this->methods['appendBytes']->invoke(
290 null,
291 'abc',
292 Mode::BYTE(),
293 $bits,
294 Encoder::DEFAULT_BYTE_MODE_ENCODING
295 );
296 $this->assertSame(' .XX....X .XX...X. .XX...XX', (string) $bits);
297
298 // Should use appendKanjiBytes.
299 // 0x93, 0x5f :点
300 $bits = new BitArray();
301 $this->methods['appendBytes']->invoke(
302 null,
303 '点',
304 Mode::KANJI(),
305 $bits,
306 Encoder::DEFAULT_BYTE_MODE_ENCODING
307 );
308 $this->assertSame(' .XX.XX.. XXXXX', (string) $bits);
309
310 // Lower letters such as 'a' cannot be encoded in alphanumeric mode.
311 $this->expectException(WriterException::class);
312 $this->methods['appendBytes']->invoke(
313 null,
314 'a',
315 Mode::ALPHANUMERIC(),
316 $bits,
317 Encoder::DEFAULT_BYTE_MODE_ENCODING
318 );
319 }
320
321 public function testTerminateBits() : void
322 {
323 $bits = new BitArray();
324 $this->methods['terminateBits']->invoke(null, 0, $bits);
325 $this->assertSame('', (string) $bits);
326
327 $bits = new BitArray();
328 $this->methods['terminateBits']->invoke(null, 1, $bits);
329 $this->assertSame(' ........', (string) $bits);
330
331 $bits = new BitArray();
332 $bits->appendBits(0, 3);
333 $this->methods['terminateBits']->invoke(null, 1, $bits);
334 $this->assertSame(' ........', (string) $bits);
335
336 $bits = new BitArray();
337 $bits->appendBits(0, 5);
338 $this->methods['terminateBits']->invoke(null, 1, $bits);
339 $this->assertSame(' ........', (string) $bits);
340
341 $bits = new BitArray();
342 $bits->appendBits(0, 8);
343 $this->methods['terminateBits']->invoke(null, 1, $bits);
344 $this->assertSame(' ........', (string) $bits);
345
346 $bits = new BitArray();
347 $this->methods['terminateBits']->invoke(null, 2, $bits);
348 $this->assertSame(' ........ XXX.XX..', (string) $bits);
349
350 $bits = new BitArray();
351 $bits->appendBits(0, 1);
352 $this->methods['terminateBits']->invoke(null, 3, $bits);
353 $this->assertSame(' ........ XXX.XX.. ...X...X', (string) $bits);
354 }
355
356 public function testGetNumDataBytesAndNumEcBytesForBlockId() : void
357 {
358 // Version 1-H.
359 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
360 ->invoke(null, 26, 9, 1, 0);
361 $this->assertSame(9, $numDataBytes);
362 $this->assertSame(17, $numEcBytes);
363
364 // Version 3-H. 2 blocks.
365 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
366 ->invoke(null, 70, 26, 2, 0);
367 $this->assertSame(13, $numDataBytes);
368 $this->assertSame(22, $numEcBytes);
369 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
370 ->invoke(null, 70, 26, 2, 1);
371 $this->assertSame(13, $numDataBytes);
372 $this->assertSame(22, $numEcBytes);
373
374 // Version 7-H. (4 + 1) blocks.
375 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
376 ->invoke(null, 196, 66, 5, 0);
377 $this->assertSame(13, $numDataBytes);
378 $this->assertSame(26, $numEcBytes);
379 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
380 ->invoke(null, 196, 66, 5, 4);
381 $this->assertSame(14, $numDataBytes);
382 $this->assertSame(26, $numEcBytes);
383
384 // Version 40-H. (20 + 61) blocks.
385 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
386 ->invoke(null, 3706, 1276, 81, 0);
387 $this->assertSame(15, $numDataBytes);
388 $this->assertSame(30, $numEcBytes);
389 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
390 ->invoke(null, 3706, 1276, 81, 20);
391 $this->assertSame(16, $numDataBytes);
392 $this->assertSame(30, $numEcBytes);
393 list($numDataBytes, $numEcBytes) = $this->methods['getNumDataBytesAndNumEcBytesForBlockId']
394 ->invoke(null, 3706, 1276, 81, 80);
395 $this->assertSame(16, $numDataBytes);
396 $this->assertSame(30, $numEcBytes);
397 }
398
399 public function testInterleaveWithEcBytes() : void
400 {
401 $dataBytes = SplFixedArray::fromArray([32, 65, 205, 69, 41, 220, 46, 128, 236], false);
402 $in = new BitArray();
403
404 foreach ($dataBytes as $dataByte) {
405 $in->appendBits($dataByte, 8);
406 }
407
408 $outBits = $this->methods['interleaveWithEcBytes']->invoke(null, $in, 26, 9, 1);
409 $expected = SplFixedArray::fromArray([
410 // Data bytes.
411 32, 65, 205, 69, 41, 220, 46, 128, 236,
412 // Error correction bytes.
413 42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61,
414 ], false);
415
416 $out = $outBits->toBytes(0, count($expected));
417
418 $this->assertEquals($expected, $out);
419 }
420
421 public function testAppendNumericBytes() : void
422 {
423 // 1 = 01 = 0001 in 4 bits.
424 $bits = new BitArray();
425 $this->methods['appendNumericBytes']->invoke(null, '1', $bits);
426 $this->assertSame(' ...X', (string) $bits);
427
428 // 12 = 0xc = 0001100 in 7 bits.
429 $bits = new BitArray();
430 $this->methods['appendNumericBytes']->invoke(null, '12', $bits);
431 $this->assertSame(' ...XX..', (string) $bits);
432
433 // 123 = 0x7b = 0001111011 in 10 bits.
434 $bits = new BitArray();
435 $this->methods['appendNumericBytes']->invoke(null, '123', $bits);
436 $this->assertSame(' ...XXXX. XX', (string) $bits);
437
438 // 1234 = "123" + "4" = 0001111011 + 0100 in 14 bits.
439 $bits = new BitArray();
440 $this->methods['appendNumericBytes']->invoke(null, '1234', $bits);
441 $this->assertSame(' ...XXXX. XX.X..', (string) $bits);
442
443 // Empty
444 $bits = new BitArray();
445 $this->methods['appendNumericBytes']->invoke(null, '', $bits);
446 $this->assertSame('', (string) $bits);
447 }
448
449 public function testAppendAlphanumericBytes() : void
450 {
451 $bits = new BitArray();
452 $this->methods['appendAlphanumericBytes']->invoke(null, 'A', $bits);
453 $this->assertSame(' ..X.X.', (string) $bits);
454
455 $bits = new BitArray();
456 $this->methods['appendAlphanumericBytes']->invoke(null, 'AB', $bits);
457 $this->assertSame(' ..XXX..X X.X', (string) $bits);
458
459 $bits = new BitArray();
460 $this->methods['appendAlphanumericBytes']->invoke(null, 'ABC', $bits);
461 $this->assertSame(' ..XXX..X X.X..XX. .', (string) $bits);
462
463 // Empty
464 $bits = new BitArray();
465 $this->methods['appendAlphanumericBytes']->invoke(null, '', $bits);
466 $this->assertSame('', (string) $bits);
467
468 // Invalid data
469 $this->expectException(WriterException::class);
470 $bits = new BitArray();
471 $this->methods['appendAlphanumericBytes']->invoke(null, 'abc', $bits);
472 }
473
474 public function testAppend8BitBytes() : void
475 {
476 // 0x61, 0x62, 0x63
477 $bits = new BitArray();
478 $this->methods['append8BitBytes']->invoke(null, 'abc', $bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
479 $this->assertSame(' .XX....X .XX...X. .XX...XX', (string) $bits);
480
481 // Empty
482 $bits = new BitArray();
483 $this->methods['append8BitBytes']->invoke(null, '', $bits, Encoder::DEFAULT_BYTE_MODE_ENCODING);
484 $this->assertSame('', (string) $bits);
485 }
486
487 public function testAppendKanjiBytes() : void
488 {
489 // Numbers are from page 21 of JISX0510:2004 点 and 茗
490 $bits = new BitArray();
491 $this->methods['appendKanjiBytes']->invoke(null, '点', $bits);
492 $this->assertSame(' .XX.XX.. XXXXX', (string) $bits);
493
494 $this->methods['appendKanjiBytes']->invoke(null, '茗', $bits);
495 $this->assertSame(' .XX.XX.. XXXXXXX. X.X.X.X. X.', (string) $bits);
496 }
497
498 public function testGenerateEcBytes() : void
499 {
500 // Numbers are from http://www.swetake.com/qr/qr3.html and
501 // http://www.swetake.com/qr/qr9.html
502 $dataBytes = SplFixedArray::fromArray([32, 65, 205, 69, 41, 220, 46, 128, 236], false);
503 $ecBytes = $this->methods['generateEcBytes']->invoke(null, $dataBytes, 17);
504 $expected = SplFixedArray::fromArray(
505 [42, 159, 74, 221, 244, 169, 239, 150, 138, 70, 237, 85, 224, 96, 74, 219, 61],
506 false
507 );
508 $this->assertEquals($expected, $ecBytes);
509
510 $dataBytes = SplFixedArray::fromArray(
511 [67, 70, 22, 38, 54, 70, 86, 102, 118, 134, 150, 166, 182, 198, 214],
512 false
513 );
514 $ecBytes = $this->methods['generateEcBytes']->invoke(null, $dataBytes, 18);
515 $expected = SplFixedArray::fromArray(
516 [175, 80, 155, 64, 178, 45, 214, 233, 65, 209, 12, 155, 117, 31, 140, 214, 27, 187],
517 false
518 );
519 $this->assertEquals($expected, $ecBytes);
520
521 // High-order zero coefficient case.
522 $dataBytes = SplFixedArray::fromArray([32, 49, 205, 69, 42, 20, 0, 236, 17], false);
523 $ecBytes = $this->methods['generateEcBytes']->invoke(null, $dataBytes, 17);
524 $expected = SplFixedArray::fromArray(
525 [0, 3, 130, 179, 194, 0, 55, 211, 110, 79, 98, 72, 170, 96, 211, 137, 213],
526 false
527 );
528 $this->assertEquals($expected, $ecBytes);
529 }
530 }