import 'mocha' ; import * as assert from 'assert' ; import Encoder from './../../../../core/qrcode/encoder/Encoder' ; import EncodeHintType from './../../../../core/EncodeHintType' ; import CharacterSetECI from './../../../../core/common/CharacterSetECI' ; import BitArray from './../../../../core/common/BitArray' ; import ErrorCorrectionLevel from './../../../../core/qrcode/decoder/ErrorCorrectionLevel' ; import Mode from './../../../../core/qrcode/decoder/Mode' ; import Version from './../../../../core/qrcode/decoder/Version' ; import QRCode from './../../../../core/qrcode/encoder/QRCode' ; import StringBuilder from './../../../../core/util/StringBuilder' ; import StringEncoding from './../../../../core/util/StringEncoding' ; import Exception from './../../../../core/Exception' ; import { TextDecoder , TextEncoder } from 'text-encoding' ; author satorux google author mysen google describe ( "EncoderTestCase" , ( ) => { it ( "testGetAlphanumericCode" , ( ) => { for ( let i = 0 ; i < 10 ; ++ i ) { assert . strictEqual ( Encoder . getAlphanumericCode ( '0' . charCodeAt ( 0 ) + i ) , i ) ; } for ( let i = 10 ; i < 36 ; ++ i ) { assert . strictEqual ( Encoder . getAlphanumericCode ( 'A' . charCodeAt ( 0 ) + i - 10 ) , i ) ; } assert . strictEqual ( Encoder . getAlphanumericCode ( ' ' . charCodeAt ( 0 ) ) , 36 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '$' . charCodeAt ( 0 ) ) , 37 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '%' . charCodeAt ( 0 ) ) , 38 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '*' . charCodeAt ( 0 ) ) , 39 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '+' . charCodeAt ( 0 ) ) , 40 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '-' . charCodeAt ( 0 ) ) , 41 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '.' . charCodeAt ( 0 ) ) , 42 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '/' . charCodeAt ( 0 ) ) , 43 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( ':' . charCodeAt ( 0 ) ) , 44 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( 'a' . charCodeAt ( 0 ) ) , - 1 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '#' . charCodeAt ( 0 ) ) , - 1 ) ; assert . strictEqual ( Encoder . getAlphanumericCode ( '\0' . charCodeAt ( 0 ) ) , - 1 ) ; } ) ; it ( "testChooseMode" , ( ) => { assert . strictEqual ( Mode . NUMERIC . equals ( Encoder . chooseMode ( "0" ) ) , true ) ; assert . strictEqual ( Mode . NUMERIC . equals ( Encoder . chooseMode ( "0123456789" ) ) , true ) ; assert . strictEqual ( Mode . ALPHANUMERIC . equals ( Encoder . chooseMode ( "A" ) ) , true ) ; assert . strictEqual ( Mode . ALPHANUMERIC . equals ( Encoder . chooseMode ( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:" ) ) , true ) ; assert . strictEqual ( Mode . BYTE . equals ( Encoder . chooseMode ( "a" ) ) , true ) ; assert . strictEqual ( Mode . BYTE . equals ( Encoder . chooseMode ( "#" ) ) , true ) ; assert . strictEqual ( Mode . BYTE . equals ( Encoder . chooseMode ( "" ) ) , true ) ; assert . strictEqual ( Mode . BYTE . equals ( Encoder . chooseMode ( shiftJISString ( Uint8Array . from ( [ 0x8 , 0xa , 0x8 , 0xa , 0x8 , 0xa , 0x8 , 0xa6 ] ) ) ) ) , true ) ; assert . strictEqual ( Mode . BYTE . equals ( Encoder . chooseMode ( shiftJISString ( Uint8Array . from ( [ 0x9 , 0xf , 0x9 , 0x7b ] ) ) ) ) , true ) ; assert . strictEqual ( Mode . BYTE . equals ( Encoder . chooseMode ( shiftJISString ( Uint8Array . from ( [ 0xe , 0x4 , 0x9 , 0x5 , 0x9 , 0x61 ] ) ) ) ) , true ) ; } ) ; it ( "testEncode" , ( ) => { const qrCode = Encoder . encode ( "ABCDEF" , ErrorCorrectionLevel . H ) ; const expected = "<<\n" + " mode: ALPHANUMERIC\n" + " ecLevel: H\n" + " version: 1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strictEqual ( qrCode . toString ( ) , expected ) ; } ) ; it ( "testEncodeWithVersion" , ( ) => { const hints = new Map < EncodeHintType , any > ( ) ; hints . set ( EncodeHintType . QR_VERSION , 7 ) ; const qrCode = Encoder . encode ( "ABCDEF" , ErrorCorrectionLevel . H , hints ) ; assert . strictEqual ( qrCode . toString ( ) . indexOf ( " version: 7\n" ) !== - 1 , true ) ; } ) ; it ( "testEncodeWithVersionTooSmall" , ( ) => { assert . throws ( ( ) => { const hints = new Map < EncodeHintType , any > ( ) ; hints . set ( EncodeHintType . QR_VERSION , 3 ) ; Encoder . encode ( "THISMESSAGEISTOOLONGFORAQRCODEVERSION3" , ErrorCorrectionLevel . H , hints ) ; } , function ( ex ) { if ( Exception . isOfType ( ex , Exception . WriterException ) ) { return true ; } } , 'unexpected exception thrown' ) ; } ) ; it ( "testSimpleUTF8ECI" , ( ) => { const hints = new Map < EncodeHintType , any > ( ) ; hints . set ( EncodeHintType . CHARACTER_SET , "UTF8" ) ; const qrCode = Encoder . encode ( "hello" , ErrorCorrectionLevel . H , hints ) ; const expected = "<<\n" + " mode: BYTE\n" + " ecLevel: H\n" + " version: 1\n" + " maskPattern: 6\n" + " matrix:\n" + " 1 1 1 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 1\n" + " 1 0 0 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 0 0 1\n" + " 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 1 0 1\n" + " 1 0 1 1 1 0 1 0 1 0 0 0 1 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 0 0 0 0 1 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 1 1 1 1 0 0 0 0 0 0 0 0\n" + " 0 0 0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0\n" + " 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1\n" + " 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0 1 0 1 0 1 1\n" + " 0 0 0 0 1 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 0\n" + " 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 1 1 1\n" + " 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1\n" + " 1 1 1 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0\n" + " 1 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 1 0 0\n" + " 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0\n" + " 1 0 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 0 1 1 0\n" + " 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 0 1 0 1 1\n" + " 1 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0\n" + " 1 1 1 1 1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 0 0\n" + ">>\n" ; assert . strictEqual ( qrCode . toString ( ) , expected ) ; } ) ; it ( "testEncodeKanjiMode" , ( ) => { const hints = new Map < EncodeHintType , any > ( ) ; hints . set ( EncodeHintType . CHARACTER_SET , CharacterSetECI . SJIS . getName ( ) ) ; const qrCode = Encoder . encode ( "\u65e5\u672c" , ErrorCorrectionLevel . M , hints ) ; const expected = "<<\n" + " mode: KANJI\n" + " ecLevel: M\n" + " version: 1\n" + " maskPattern: 0\n" + " matrix:\n" + " 1 1 1 1 1 1 1 0 0 1 0 1 0 0 1 1 1 1 1 1 1\n" + " 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 0 0 1\n" + " 1 0 1 1 1 0 1 0 0 1 1 1 1 0 1 0 1 1 1 0 1\n" + " 1 0 1 1 1 0 1 0 0 0 0 0 1 0 1 0 1 1 1 0 1\n" + " 1 0 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1\n" + " 1 0 0 0 0 0 1 0 0 1 1 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 1 0 0 0 0 0 0 0 0 0 0\n" + " 1 0 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0 1 0\n" + " 1 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 1 0 0 0\n" + " 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0\n" + " 1 1 1 0 0 1 0 1 0 0 0 1 1 1 0 1 1 0 1 0 0\n" + " 0 1 1 0 0 1 1 0 1 1 0 1 0 1 1 1 0 1 0 0 1\n" + " 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1\n" + " 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 1\n" + " 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1\n" + " 1 0 1 1 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1\n" + " 1 0 1 1 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0\n" + " 1 0 1 1 1 0 1 0 1 0 1 1 0 1 1 1 0 0 1 0 1\n" + " 1 0 0 0 0 0 1 0 0 0 0 1 1 1 0 1 1 1 0 1 0\n" + " 1 1 1 1 1 1 1 0 1 1 0 1 0 1 1 1 0 0 1 0 0\n" + ">>\n" ; assert . strictEqual ( qrCode . toString ( ) , expected ) ; } ) ; it ( "testEncodeShiftjisNumeric" , ( ) => { const hints = new Map < EncodeHintType , any > ( ) ; hints . set ( EncodeHintType . CHARACTER_SET , CharacterSetECI . SJIS . getName ( ) ) ; const qrCode = Encoder . encode ( "0123" , ErrorCorrectionLevel . M , hints ) ; const expected = "<<\n" + " mode: NUMERIC\n" + " ecLevel: M\n" + " version: 1\n" + " maskPattern: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. toString ( ) , expected ) ; } ) ; it ( "testAppendModeInfo" , ( ) => { const bits = new BitArray ( ) ; Encoder . appendModeInfo ( Mode . NUMERIC , bits ) ; assert . strictEqual ( bits . toString ( ) , " ...X" ) ; } ) ; it ( "testAppendLengthInfo" , ( ) => { let bits = new BitArray ( ) ; Encoder . appendLengthInfo ( 1 , Version . getVersionForNumber ( 1 ) , Mode . NUMERIC , bits ) ; assert . strictEqual ( bits . toString ( ) , " ........ .X" ) ; bits = new BitArray ( ) ; Encoder . appendLengthInfo ( 2 , Version . getVersionForNumber ( 10 ) , Mode . ALPHANUMERIC , bits ) ; assert . strictEqual ( bits . toString ( ) , " ........ .X." ) ; bits = new BitArray ( ) ; Encoder . appendLengthInfo ( 255 , Version . getVersionForNumber ( 27 ) , Mode . BYTE , bits ) ; assert . strictEqual ( bits . toString ( ) , " ........ XXXXXXXX" ) ; bits = new BitArray ( ) ; Encoder . appendLengthInfo ( 512 , Version . getVersionForNumber ( 40 ) , Mode . KANJI , bits ) ; assert . strictEqual ( bits . toString ( ) , " ..X..... ...." ) ; } ) ; it ( "testAppendBytes" , ( ) => { let bits = new BitArray ( ) ; Encoder . appendBytes ( "1" , Mode . NUMERIC , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; assert . strictEqual ( bits . toString ( ) , " ...X" ) ; bits = new BitArray ( ) ; Encoder . appendBytes ( "A" , Mode . ALPHANUMERIC , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; assert . strictEqual ( bits . toString ( ) , " ..X.X." ) ; try { Encoder . appendBytes ( "a" , Mode . ALPHANUMERIC , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; } catch ( we ) { if ( Exception . isOfType ( we , Exception . WriterException ) ) { } else { throw we ; } } bits = new BitArray ( ) ; Encoder . appendBytes ( "abc" , Mode . BYTE , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; assert . strictEqual ( bits . toString ( ) , " .XX....X .XX...X. .XX...XX" ) ; bits = new BitArray ( ) ; Encoder . appendBytes ( shiftJISString ( Uint8Array . from ( [ 0x93 , 0x5f ] ) ) , Mode . KANJI , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; assert . strictEqual ( bits . toString ( ) , " .XX.XX.. XXXXX" ) ; } ) ; it ( "testTerminateBits" , ( ) => { let v = new BitArray ( ) ; Encoder . terminateBits ( 0 , v ) ; assert . strictEqual ( v . toString ( ) , "" ) ; v = new BitArray ( ) ; Encoder . terminateBits ( 1 , v ) ; assert . strictEqual ( v . toString ( ) , " ........" ) ; v = new BitArray ( ) ; v . appendBits ( 0 , 3 ) ; Encoder . terminateBits ( 1 , v ) ; assert . strictEqual ( v . toString ( ) , " ........" ) ; v = new BitArray ( ) ; v . appendBits ( 0 , 5 ) ; Encoder . terminateBits ( 1 , v ) ; assert . strictEqual ( v . toString ( ) , " ........" ) ; v = new BitArray ( ) ; v . appendBits ( 0 , 8 ) ; Encoder . terminateBits ( 1 , v ) ; assert . strictEqual ( v . toString ( ) , " ........" ) ; v = new BitArray ( ) ; Encoder . terminateBits ( 2 , v ) ; assert . strictEqual ( v . toString ( ) , " ........ XXX.XX.." ) ; v = new BitArray ( ) ; v . appendBits ( 0 , 1 ) ; Encoder . terminateBits ( 3 , v ) ; assert . strictEqual ( v . toString ( ) , " ........ XXX.XX.. ...X...X" ) ; } ) ; it ( "testGetNumDataBytesAndNumECBytesForBlockID" , ( ) => { const numDataBytes = new Int32Array ( 1 ) ; const numEcBytes = new Int32Array ( 1 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 26 , 9 , 1 , 0 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 9 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 17 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 70 , 26 , 2 , 0 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 13 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 22 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 70 , 26 , 2 , 1 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 13 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 22 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 196 , 66 , 5 , 0 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 13 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 26 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 196 , 66 , 5 , 4 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 14 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 26 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 3706 , 1276 , 81 , 0 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 15 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 30 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 3706 , 1276 , 81 , 20 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 16 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 30 ) ; Encoder . getNumDataBytesAndNumECBytesForBlockID ( 3706 , 1276 , 81 , 80 , numDataBytes , numEcBytes ) ; assert . strictEqual ( numDataBytes [ 0 ] , 16 ) ; assert . strictEqual ( numEcBytes [ 0 ] , 30 ) ; } ) ; it ( "testInterleaveWithECBytes" , ( ) => { let dataBytes = Uint8Array . from ( [ 32 , 65 , 205 , 69 , 41 , 220 , 46 , 128 , 236 ] ) ; let input = new BitArray ( ) ; for ( let i = 0 , length = dataBytes . length ; i !== length ; i ++ ) { const dataByte = dataBytes [ i ] ; input . appendBits ( dataByte , 8 ) ; } let out = Encoder . interleaveWithECBytes ( input , 26 , 9 , 1 ) ; let expected = Uint8Array . 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 , ] ) ; assert . strictEqual ( out . getSizeInBytes ( ) , expected . length ) ; let outArray = new Uint8Array ( expected . length ) ; out . toBytes ( 0 , outArray , 0 , expected . length ) ; for ( let x = 0 ; x < expected . length ; x ++ ) { assert . strictEqual ( outArray [ x ] , expected [ x ] ) ; } dataBytes = Uint8Array . from ( [ 67 , 70 , 22 , 38 , 54 , 70 , 86 , 102 , 118 , 134 , 150 , 166 , 182 , 198 , 214 , 230 , 247 , 7 , 23 , 39 , 55 , 71 , 87 , 103 , 119 , 135 , 151 , 166 , 22 , 38 , 54 , 70 , 86 , 102 , 118 , 134 , 150 , 166 , 182 , 198 , 214 , 230 , 247 , 7 , 23 , 39 , 55 , 71 , 87 , 103 , 119 , 135 , 151 , 160 , 236 , 17 , 236 , 17 , 236 , 17 , 236 , 17 ] ) ; input = new BitArray ( ) ; for ( let i = 0 , length = dataBytes . length ; i !== length ; i ++ ) { const dataByte = dataBytes [ i ] ; input . appendBits ( dataByte , 8 ) ; } out = Encoder . interleaveWithECBytes ( input , 134 , 62 , 4 ) ; expected = Uint8Array . from ( [ 67 , 230 , 54 , 55 , 70 , 247 , 70 , 71 , 22 , 7 , 86 , 87 , 38 , 23 , 102 , 103 , 54 , 39 , 118 , 119 , 70 , 55 , 134 , 135 , 86 , 71 , 150 , 151 , 102 , 87 , 166 , 160 , 118 , 103 , 182 , 236 , 134 , 119 , 198 , 17 , 150 , 135 , 214 , 236 , 166 , 151 , 230 , 17 , 182 , 166 , 247 , 236 , 198 , 22 , 7 , 17 , 214 , 38 , 23 , 236 , 39 , 17 , 175 , 155 , 245 , 236 , 80 , 146 , 56 , 74 , 155 , 165 , 133 , 142 , 64 , 183 , 132 , 13 , 178 , 54 , 132 , 108 , 45 , 113 , 53 , 50 , 214 , 98 , 193 , 152 , 233 , 147 , 50 , 71 , 65 , 190 , 82 , 51 , 209 , 199 , 171 , 54 , 12 , 112 , 57 , 113 , 155 , 117 , 211 , 164 , 117 , 30 , 158 , 225 , 31 , 190 , 242 , 38 , 140 , 61 , 179 , 154 , 214 , 138 , 147 , 87 , 27 , 96 , 77 , 47 , 187 , 49 , 156 , 214 , ] ) ; assert . strictEqual ( out . getSizeInBytes ( ) , expected . length ) ; outArray = new Uint8Array ( expected . length ) ; out . toBytes ( 0 , outArray , 0 , expected . length ) ; for ( let x = 0 ; x < expected . length ; x ++ ) { assert . strictEqual ( outArray [ x ] , expected [ x ] ) ; } } ) ; it ( "testAppendNumericBytes" , ( ) => { let bits = new BitArray ( ) ; Encoder . appendNumericBytes ( "1" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ...X" ) ; bits = new BitArray ( ) ; Encoder . appendNumericBytes ( "12" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ...XX.." ) ; bits = new BitArray ( ) ; Encoder . appendNumericBytes ( "123" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ...XXXX. XX" ) ; bits = new BitArray ( ) ; Encoder . appendNumericBytes ( "1234" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ...XXXX. XX.X.." ) ; bits = new BitArray ( ) ; Encoder . appendNumericBytes ( "" , bits ) ; assert . strictEqual ( bits . toString ( ) , "" ) ; } ) ; it ( "testAppendAlphanumericBytes" , ( ) => { let bits = new BitArray ( ) ; Encoder . appendAlphanumericBytes ( "A" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ..X.X." ) ; bits = new BitArray ( ) ; Encoder . appendAlphanumericBytes ( "AB" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ..XXX..X X.X" ) ; bits = new BitArray ( ) ; Encoder . appendAlphanumericBytes ( "ABC" , bits ) ; assert . strictEqual ( bits . toString ( ) , " ..XXX..X X.X..XX. ." ) ; bits = new BitArray ( ) ; Encoder . appendAlphanumericBytes ( "" , bits ) ; assert . strictEqual ( bits . toString ( ) , "" ) ; try { Encoder . appendAlphanumericBytes ( "abc" , new BitArray ( ) ) ; } catch ( we ) { } } ) ; it ( "testAppend8BitBytes" , ( ) => { let bits = new BitArray ( ) ; Encoder . append8BitBytes ( "abc" , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; assert . strictEqual ( bits . toString ( ) , " .XX....X .XX...X. .XX...XX" ) ; bits = new BitArray ( ) ; Encoder . append8BitBytes ( "" , bits , Encoder . DEFAULT_BYTE_MODE_ENCODING ) ; assert . strictEqual ( bits . toString ( ) , "" ) ; } ) ; it ( "testAppendKanjiBytes" , ( ) => { const bits = new BitArray ( ) ; Encoder . appendKanjiBytes ( shiftJISString ( Uint8Array . from ( [ 0x93 , 0x5f ] ) ) , bits ) ; assert . strictEqual ( bits . toString ( ) , " .XX.XX.. XXXXX" ) ; Encoder . appendKanjiBytes ( shiftJISString ( Uint8Array . from ( [ 0xe4 , 0xaa ] ) ) , bits ) ; assert . strictEqual ( bits . toString ( ) , " .XX.XX.. XXXXXXX. X.X.X.X. X." ) ; } ) ; it ( "testGenerateECBytes" , ( ) => { let dataBytes = Uint8Array . from ( [ 32 , 65 , 205 , 69 , 41 , 220 , 46 , 128 , 236 ] ) ; let ecBytes = Encoder . generateECBytes ( dataBytes , 17 ) ; let expected = Int32Array . from ( [ 42 , 159 , 74 , 221 , 244 , 169 , 239 , 150 , 138 , 70 , 237 , 85 , 224 , 96 , 74 , 219 , 61 ] ) ; assert . strictEqual ( ecBytes . length , expected . length ) ; for ( let x = 0 ; x < expected . length ; x ++ ) { assert . strictEqual ( ecBytes [ x ] & 0xFF , expected [ x ] ) ; } dataBytes = Uint8Array . from ( [ 67 , 70 , 22 , 38 , 54 , 70 , 86 , 102 , 118 , 134 , 150 , 166 , 182 , 198 , 214 ] ) ; ecBytes = Encoder . generateECBytes ( dataBytes , 18 ) ; expected = Int32Array . from ( [ 175 , 80 , 155 , 64 , 178 , 45 , 214 , 233 , 65 , 209 , 12 , 155 , 117 , 31 , 140 , 214 , 27 , 187 ] ) ; assert . strictEqual ( ecBytes . length , expected . length ) ; for ( let x = 0 ; x < expected . length ; x ++ ) { assert . strictEqual ( ecBytes [ x ] & 0xFF , expected [ x ] ) ; } dataBytes = Uint8Array . from ( [ 32 , 49 , 205 , 69 , 42 , 20 , 0 , 236 , 17 ] ) ; ecBytes = Encoder . generateECBytes ( dataBytes , 17 ) ; expected = Int32Array . from ( [ 0 , 3 , 130 , 179 , 194 , 0 , 55 , 211 , 110 , 79 , 98 , 72 , 170 , 96 , 211 , 137 , 213 ] ) ; assert . strictEqual ( ecBytes . length , expected . length ) ; for ( let x = 0 ; x < expected . length ; x ++ ) { assert . strictEqual ( ecBytes [ x ] & 0xFF , expected [ x ] ) ; } } ) ; it ( "testBugInBitVectorNumBytes" , ( ) => { const builder = new StringBuilder ( ) ; for ( let x = 0 ; x < 3518 ; x ++ ) { builder . append ( '0' ) ; } Encoder . encode ( builder . toString ( ) , ErrorCorrectionLevel . L ) ; } ) ; function shiftJISString ( bytes ) { try { return StringEncoding . decode ( bytes , CharacterSetECI . SJIS . getName ( ) ) ; } catch ( uee ) { throw new Exception ( Exception . WriterException , uee . toString ( ) ) ; } } } ) ;