import * as assert from 'assert' ; import { BitArray } from '@zxing/library' ; import { QRCodeByteMatrix } from '@zxing/library' ; import { QRCodeMatrixUtil } from '@zxing/library' ; import { QRCodeDecoderErrorCorrectionLevel } from '@zxing/library' ; import { QRCodeVersion } from '@zxing/library' ; author satorux google author mysen google describe ( 'QRCodeMatrixUtil' , ( ) => { it ( 'testToString' , ( ) => { const array = new QRCodeByteMatrix ( 3 , 3 ) ; array . setNumber ( 0 , 0 , 0 ) ; array . setNumber ( 1 , 0 , 1 ) ; array . setNumber ( 2 , 0 , 0 ) ; array . setNumber ( 0 , 1 , 1 ) ; array . setNumber ( 1 , 1 , 0 ) ; array . setNumber ( 2 , 1 , 1 ) ; array . setNumber ( 0 , 2 , - 1 ) ; array . setNumber ( 1 , 2 , - 1 ) ; array . setNumber ( 2 , 2 , - 1 ) ; const expected = ' 0 1 0\n' + ' 1 0 1\n' + '      \n' ; assert . strictEqual ( array . toString ( ) , expected ) ; } ) ; it ( 'testClearMatrix' , ( ) => { const matrix = new QRCodeByteMatrix ( 2 , 2 ) ; QRCodeMatrixUtil . clearMatrix ( matrix ) ; assert . strictEqual ( matrix . get ( 0 , 0 ) , 255 ) ; assert . strictEqual ( matrix . get ( 1 , 0 ) , 255 ) ; assert . strictEqual ( matrix . get ( 0 , 1 ) , 255 ) ; assert . strictEqual ( matrix . get ( 1 , 1 ) , 255 ) ; } ) ; it ( 'testEmbedBasicPatterns1' , ( ) => { const matrix = new QRCodeByteMatrix ( 21 , 21 ) ; QRCodeMatrixUtil . clearMatrix ( matrix ) ; QRCodeMatrixUtil . embedBasicPatterns ( QRCodeVersion . getVersionForNumber ( 1 ) , matrix ) ; const expected = ' 1 1 1 1 1 1 1 0           0 1 1 1 1 1 1 1\n' + ' 1 0 0 0 0 0 1 0           0 1 0 0 0 0 0 1\n' + ' 1 0 1 1 1 0 1 0           0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0           0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0           0 1 0 1 1 1 0 1\n' + ' 1 0 0 0 0 0 1 0           0 1 0 0 0 0 0 1\n' + ' 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n' + ' 0 0 0 0 0 0 0 0           0 0 0 0 0 0 0 0\n' + '             1                            \n' + '             0                            \n' + '             1                            \n' + '             0                            \n' + '             1                            \n' + ' 0 0 0 0 0 0 0 0 1                        \n' + ' 1 1 1 1 1 1 1 0                          \n' + ' 1 0 0 0 0 0 1 0                          \n' + ' 1 0 1 1 1 0 1 0                          \n' + ' 1 0 1 1 1 0 1 0                          \n' + ' 1 0 1 1 1 0 1 0                          \n' + ' 1 0 0 0 0 0 1 0                          \n' + ' 1 1 1 1 1 1 1 0                          \n' ; assert . strictEqual ( matrix . toString ( ) , expected ) ; } ) ; it ( 'testEmbedBasicPatterns2' , ( ) => { const matrix = new QRCodeByteMatrix ( 25 , 25 ) ; QRCodeMatrixUtil . clearMatrix ( matrix ) ; QRCodeMatrixUtil . embedBasicPatterns ( QRCodeVersion . getVersionForNumber ( 2 ) , matrix ) ; const expected = ' 1 1 1 1 1 1 1 0                   0 1 1 1 1 1 1 1\n' + ' 1 0 0 0 0 0 1 0                   0 1 0 0 0 0 0 1\n' + ' 1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0                   0 1 0 1 1 1 0 1\n' + ' 1 0 0 0 0 0 1 0                   0 1 0 0 0 0 0 1\n' + ' 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' + ' 0 0 0 0 0 0 0 0                   0 0 0 0 0 0 0 0\n' + '             1                                    \n' + '             0                                    \n' + '             1                                    \n' + '             0                                    \n' + '             1                                    \n' + '             0                                    \n' + '             1                                    \n' + '             0                                    \n' + '             1                   1 1 1 1 1        \n' + ' 0 0 0 0 0 0 0 0 1               1 0 0 0 1        \n' + ' 1 1 1 1 1 1 1 0                 1 0 1 0 1        \n' + ' 1 0 0 0 0 0 1 0                 1 0 0 0 1        \n' + ' 1 0 1 1 1 0 1 0                 1 1 1 1 1        \n' + ' 1 0 1 1 1 0 1 0                                  \n' + ' 1 0 1 1 1 0 1 0                                  \n' + ' 1 0 0 0 0 0 1 0                                  \n' + ' 1 1 1 1 1 1 1 0                                  \n' ; assert . strictEqual ( matrix . toString ( ) , expected ) ; } ) ; it ( 'testEmbedTypeInfo' , ( ) => { const matrix = new QRCodeByteMatrix ( 21 , 21 ) ; QRCodeMatrixUtil . clearMatrix ( matrix ) ; QRCodeMatrixUtil . embedTypeInfo ( QRCodeDecoderErrorCorrectionLevel . M , 5 , matrix ) ; const expected = '                 0                        \n' + '                 1                        \n' + '                 1                        \n' + '                 1                        \n' + '                 0                        \n' + '                 0                        \n' + '                                          \n' + '                 1                        \n' + ' 1 0 0 0 0 0   0 1         1 1 0 0 1 1 1 0\n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                 0                        \n' + '                 0                        \n' + '                 0                        \n' + '                 0                        \n' + '                 0                        \n' + '                 0                        \n' + '                 1                        \n' ; assert . strictEqual ( matrix . toString ( ) , expected ) ; } ) ; it ( 'testEmbedVersionInfo' , ( ) => { const matrix = new QRCodeByteMatrix ( 21 , 21 ) ; QRCodeMatrixUtil . clearMatrix ( matrix ) ; QRCodeMatrixUtil . maybeEmbedVersionInfo ( QRCodeVersion . getVersionForNumber ( 7 ) , matrix ) ; const expected = '                     0 0 1                \n' + '                     0 1 0                \n' + '                     0 1 0                \n' + '                     0 1 1                \n' + '                     1 1 1                \n' + '                     0 0 0                \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + ' 0 0 0 0 1 0                              \n' + ' 0 1 1 1 1 0                              \n' + ' 1 0 0 1 1 0                              \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' + '                                          \n' ; assert . strictEqual ( matrix . toString ( ) , expected ) ; } ) ; it ( 'testEmbedDataBits' , ( ) => { const matrix = new QRCodeByteMatrix ( 21 , 21 ) ; QRCodeMatrixUtil . clearMatrix ( matrix ) ; QRCodeMatrixUtil . embedBasicPatterns ( QRCodeVersion . getVersionForNumber ( 1 ) , matrix ) ; const bits = new BitArray ( ) ; QRCodeMatrixUtil . embedDataBits ( bits , 255 , matrix ) ; const expected = ' 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1\n' + ' 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n' + ' 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 1\n' + ' 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n' + ' 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n' + ' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' + ' 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' ; assert . strictEqual ( matrix . toString ( ) , expected ) ; } ) ; it ( 'testBuildMatrix' , ( ) => { const bytes = Uint16Array . from ( [ 32 , 65 , 205 , 69 , 41 , 220 , 46 , 128 , 236 , 42 , 159 , 74 , 221 , 244 , 169 , 239 , 150 , 138 , 70 , 237 , 85 , 224 , 96 , 74 , 219 , 61 ] ) ; const bits = new BitArray ( ) ; for ( let i = 0 , length = bytes . length ; i !== length ; i ++ ) { const c = bytes [ i ] ; bits . appendBits ( c , 8 ) ; } const matrix = new QRCodeByteMatrix ( 21 , 21 ) ; QRCodeMatrixUtil . buildMatrix ( bits , QRCodeDecoderErrorCorrectionLevel . H , QRCodeVersion . getVersionForNumber ( 1 ) , 3 , matrix ) ; const expected = ' 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1\n' + ' 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1\n' + ' 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0 0 1 1 0 0 0 1 0 1 1 1 0 1\n' + ' 1 0 1 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 1\n' + ' 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1\n' + ' 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n' + ' 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0\n' + ' 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0\n' + ' 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0\n' + ' 1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0\n' + ' 1 0 1 0 1 1 0 1 1 1 0 0 1 1 1 0 0 1 0 1 0\n' + ' 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1\n' + ' 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1\n' + ' 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n' + ' 1 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 0 0\n' + ' 1 0 1 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1\n' + ' 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 1 1 1 0\n' + ' 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n' + ' 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n' + ' 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n' ; assert . strictEqual ( matrix . toString ( ) , expected ) ; } ) ; it ( 'testFindMSBSet' , ( ) => { assert . strictEqual ( QRCodeMatrixUtil . findMSBSet ( 0 ) , 0 ) ; assert . strictEqual ( QRCodeMatrixUtil . findMSBSet ( 1 ) , 1 ) ; assert . strictEqual ( QRCodeMatrixUtil . findMSBSet ( 0x80 ) , 8 ) ; assert . strictEqual ( QRCodeMatrixUtil . findMSBSet ( 0x80000000 ) , 32 ) ; } ) ; it ( 'testCalculateBCHCode' , ( ) => { assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 5 , 0x537 ) , 0xdc ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 0x13 , 0x537 ) , 0x1c2 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 0x1b , 0x537 ) , 0x214 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 7 , 0x1f25 ) , 0xc94 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 8 , 0x1f25 ) , 0x5bc ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 9 , 0x1f25 ) , 0xa99 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 10 , 0x1f25 ) , 0x4d3 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 20 , 0x1f25 ) , 0x9a6 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 30 , 0x1f25 ) , 0xd75 ) ; assert . strictEqual ( QRCodeMatrixUtil . calculateBCHCode ( 40 , 0x1f25 ) , 0xc69 ) ; } ) ; it ( 'testMakeVersionInfoBits' , ( ) => { const bits = new BitArray ( ) ; QRCodeMatrixUtil . makeVersionInfoBits ( QRCodeVersion . getVersionForNumber ( 7 ) , bits ) ; assert . strictEqual ( bits . toString ( ) , ' ...XXXXX ..X..X.X ..' ) ; } ) ; it ( 'testMakeTypeInfoInfoBits' , ( ) => { const bits = new BitArray ( ) ; QRCodeMatrixUtil . makeTypeInfoBits ( QRCodeDecoderErrorCorrectionLevel . M , 5 , bits ) ; assert . strictEqual ( bits . toString ( ) , ' X......X X..XXX.' ) ; } ) ; } ) ;