MatrixUtilTest.php 15 KB

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