import { AztecCode } from '@zxing/library' ; import { assertEquals , assertTrue , assertFalse , assertArrayEquals } from '../../util/AssertUtils' ; import { BitMatrix } from '@zxing/library' ; import { BarcodeFormat , DecoderResult , EncodeHintType , BitArray , StringUtils , } from '@zxing/library' ; import { AztecEncoder } from '@zxing/library' ; import { ZXingStandardCharsets } from '@zxing/library' ; import { ZXingStringBuilder } from '@zxing/library' ; import { fail } from 'assert' ; import { AztecDetectorResult } from '@zxing/library' ; import { AztecDecoder } from '@zxing/library' ; import Random from '../../../core/util/Random' ; import { AztecHighLevelEncoder } from '@zxing/library' ; import { AztecCodeWriter } from '@zxing/library' ; import { ResultPoint } from '@zxing/library' ; import { ZXingStringEncoding } from '@zxing/library' ; import { ZXingCharset } from '@zxing/library' ; import '@zxing/text-encoding/cjs/encoding-indexes' ; import { TextEncoder , TextDecoder } from '@zxing/text-encoding' ; ZXingStringEncoding . customEncoder = ( b , e ) => new TextEncoder ( e , { NONSTANDARD_allowLegacyEncoding : true } ) . encode ( b ) ; ZXingStringEncoding . customDecoder = ( s , e ) => new TextDecoder ( e ) . decode ( s ) ; author Rustam Abdullaev
 *  author Frank Yellin
  describe ( 'EncoderTest' , ( ) => { const DOTX = new RegExp ( '[^.X]' , 'g' ) ; const SPACES = new RegExp ( '\\s+' , 'g' ) ; const NO_POINTS = [ ] ; it ( 'testEncode1' , ( ) => { testEncode ( 'This is an example Aztec symbol for Wikipedia.' , true , 3 , 'X     X X       X     X X     X     X         \n' + 'X         X     X X     X   X X   X X       X \n' + 'X X   X X X X X   X X X                 X     \n' + 'X X                 X X   X       X X X X X X \n' + '    X X X   X   X     X X X X         X X     \n' + '  X X X   X X X X   X     X   X     X X   X   \n' + '        X X X X X     X X X X   X   X     X   \n' + 'X       X   X X X X X X X X X X X     X   X X \n' + 'X   X     X X X               X X X X   X X   \n' + 'X     X X   X X   X X X X X   X X   X   X X X \n' + 'X   X         X   X       X   X X X X       X \n' + 'X       X     X   X   X   X   X   X X   X     \n' + '      X   X X X   X       X   X     X X X     \n' + '    X X X X X X   X X X X X   X X X X X X   X \n' + '  X X   X   X X               X X X   X X X X \n' + '  X   X       X X X X X X X X X X X X   X X   \n' + '  X X   X       X X X   X X X       X X       \n' + '  X               X   X X     X     X X X     \n' + '  X   X X X   X X   X   X X X X   X   X X X X \n' + '    X   X   X X X   X   X   X X X X     X     \n' + '        X               X                 X   \n' + '        X X     X   X X   X   X   X       X X \n' + '  X   X   X X       X   X         X X X     X \n' ) ; } ) ; it ( 'testEncode2' , ( ) => { testEncode ( 'Aztec Code is a public domain 2D matrix barcode symbology' + ' of nominally square symbols built on a square grid with a ' + 'distinctive square bullseye pattern at their center.' , false , 6 , '        X X     X X     X     X     X   X X X         X   X         X   X X       \n' + '  X       X X     X   X X   X X       X             X     X   X X   X           X \n' + '  X   X X X     X   X   X X     X X X   X   X X               X X       X X     X \n' + 'X X X             X   X         X         X     X     X   X     X X       X   X   \n' + 'X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X \n' + '    X X   X   X   X X X               X       X       X X     X X   X X       X   \n' + 'X X     X       X       X X X X   X   X X       X   X X   X       X X   X X   X   \n' + '  X       X   X     X X   X   X X   X X   X X X X X X   X X           X   X   X X \n' + 'X X   X X   X   X X X X   X X X X X X X X   X   X       X X   X X X X   X X X     \n' + '  X       X   X     X       X X     X X   X   X   X     X X   X X X   X     X X X \n' + '  X   X X X   X X       X X X         X X           X   X   X   X X X   X X     X \n' + '    X     X   X X     X X X X     X   X     X X X X   X X   X X   X X X     X   X \n' + 'X X X   X             X         X X X X X   X   X X   X   X   X X   X   X   X   X \n' + '          X       X X X   X X     X   X           X   X X X X   X X               \n' + '  X     X X   X   X       X X X X X X X X X X X X X X X   X   X X   X   X X X     \n' + '    X X                 X   X                       X X   X       X         X X X \n' + '        X   X X   X X X X X X   X X X X X X X X X   X     X X           X X X X   \n' + '          X X X   X     X   X   X               X   X X     X X X   X X           \n' + 'X X     X     X   X   X   X X   X   X X X X X   X   X X X X X X X       X   X X X \n' + 'X X X X       X       X   X X   X   X       X   X   X     X X X     X X       X X \n' + 'X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X \n' + '    X     X       X         X   X   X       X   X   X     X   X X                 \n' + '        X X     X X X X X   X   X   X X X X X   X   X X X     X X X X   X         \n' + 'X     X   X   X         X   X   X               X   X X   X X   X X X     X   X   \n' + '  X   X X X   X   X X   X X X   X X X X X X X X X   X X         X X     X X X X   \n' + '    X X   X   X   X X X     X                       X X X   X X   X   X     X     \n' + '    X X X X   X         X   X X X X X X X X X X X X X X   X       X X   X X   X X \n' + '            X   X   X X       X X X X X     X X X       X       X X X         X   \n' + 'X       X         X   X X X X   X     X X     X X     X X           X   X       X \n' + 'X     X       X X X X X     X   X X X X   X X X     X       X X X X   X   X X   X \n' + '  X X X X X               X     X X X   X       X X   X X   X X X X     X X       \n' + 'X             X         X   X X   X X     X     X     X   X   X X X X             \n' + '    X   X X       X     X       X   X X X X X X   X X   X X X X X X X X X   X   X \n' + '    X         X X   X       X     X   X   X       X     X X X     X       X X X X \n' + 'X     X X     X X X X X X             X X X   X               X   X     X     X X \n' + 'X   X X     X               X X X X X     X X     X X X X X X X X     X   X   X X \n' + 'X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X   X \n' + 'X           X     X X X X     X     X         X         X   X       X X   X X X   \n' + 'X   X   X X   X X X   X         X X     X X X X     X X   X   X     X   X       X \n' + '      X     X     X     X X     X   X X   X X   X         X X       X       X   X \n' + 'X       X           X   X   X     X X   X               X     X     X X X         \n' ) ; } ) ; it ( 'testAztecWriter' , ( ) => { testWriter ( '\u20AC 1 sample data.' , 'windows-1252' , 25 , true , 2 ) ; testWriter ( '\u20AC 1 sample data.' , 'ISO-8859-15' , 25 , true , 2 ) ; testWriter ( '\u20AC 1 sample data.' , 'UTF-8' , 25 , true , 2 ) ; testWriter ( '\u20AC 1 sample data.' , 'UTF-8' , 100 , true , 3 ) ; testWriter ( '\u20AC 1 sample data.' , 'UTF-8' , 300 , true , 4 ) ; testWriter ( '\u20AC 1 sample data.' , 'UTF-8' , 500 , false , 5 ) ; const data = 'In ut magna vel mauris malesuada' ; const writer = new AztecCodeWriter ( ) ; const matrix = writer . encode ( data , BarcodeFormat . AZTEC , 0 , 0 ) ; const aztec = AztecEncoder . encode ( StringUtils . getBytes ( data , ZXingStandardCharsets . ISO_8859_1 ) , AztecEncoder . DEFAULT_EC_PERCENT , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; const expectedMatrix = aztec . getMatrix ( ) ; assertEquals ( matrix . getHeight ( ) , expectedMatrix . getHeight ( ) ) ; assertEquals ( matrix . getRowSize ( ) , expectedMatrix . getRowSize ( ) ) ; assertEquals ( matrix . getWidth ( ) , expectedMatrix . getWidth ( ) ) ; assertArrayEquals ( matrix [ 'bits' ] , expectedMatrix [ 'bits' ] ) ; } ) ; it ( 'testEncodeDecode1' , ( ) => { testEncodeDecode ( 'Abc123!' , true , 1 ) ; } ) ; it ( 'testEncodeDecode2' , ( ) => { testEncodeDecode ( 'Lorem ipsum. http://test/' , true , 2 ) ; } ) ; it ( 'testEncodeDecode3' , ( ) => { testEncodeDecode ( 'AAAANAAAANAAAANAAAANAAAANAAAANAAAANAAAANAAAANAAAAN' , true , 3 ) ; } ) ; it ( 'testEncodeDecode4' , ( ) => { testEncodeDecode ( 'http://test/~!@#*^%&)__ ;:\'"[]{}\\|-+-=`1029384' , true , 4 ) ; } ) ; it ( 'testEncodeDecode5' , ( ) => { testEncodeDecode ( 'http://test/~!@#*^%&)__ ;:\'"[]{}\\|-+-=`1029384756<>/?abc' + 'Four score and seven our forefathers brought forth' , false , 5 ) ; } ) ; it ( 'testEncodeDecode10' , ( ) => { testEncodeDecode ( 'In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam' + ' cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoreet rutrum' + ' est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo et congue' + ' auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. Nulla' + ' ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, id' + ' elementum sapien dolor et diam.' , false , 10 ) ; } ) ; it ( 'testEncodeDecode23' , ( ) => { testEncodeDecode ( 'In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam' + ' cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoreet rutrum' + ' est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo et congue' + ' auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. Nulla' + ' ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, id' + ' elementum sapien dolor et diam. Donec ac nunc sodales elit placerat eleifend.' + ' Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra fringilla, risus' + ' justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo diam, lobortis eu' + ' tristique ac, p.In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus' + ' quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec' + ' laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasellus gravida,' + ' justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec' + ' lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar' + ' nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit placerat' + ' eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra' + ' fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo' + ' diam, lobortis eu tristique ac, p. In ut magna vel mauris malesuada dictum. Nulla' + ' ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum' + ' sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet.' + ' Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget hendrerit' + ' felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo' + ' erat pulvinar nisi, id elementum sapien dolor et diam.' , false , 23 ) ; } ) ; it ( 'testEncodeDecode31' , ( ) => { testEncodeDecode ( 'In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus quis diam' + ' cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec laoreet rutrum' + ' est, nec convallis mauris condimentum sit amet. Phasellus gravida, justo et congue' + ' auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec lorem. Nulla' + ' ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar nisi, id' + ' elementum sapien dolor et diam. Donec ac nunc sodales elit placerat eleifend.' + ' Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra fringilla, risus' + ' justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo diam, lobortis eu' + ' tristique ac, p.In ut magna vel mauris malesuada dictum. Nulla ullamcorper metus' + ' quis diam cursus facilisis. Sed mollis quam id justo rutrum sagittis. Donec' + ' laoreet rutrum est, nec convallis mauris condimentum sit amet. Phasellus gravida,' + ' justo et congue auctor, nisi ipsum viverra erat, eget hendrerit felis turpis nec' + ' lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo erat pulvinar' + ' nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit placerat' + ' eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at pharetra' + ' fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est. Ut justo' + ' diam, lobortis eu tristique ac, p. In ut magna vel mauris malesuada dictum. Nulla' + ' ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum' + ' sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet.' + ' Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget hendrerit' + ' felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet laoreet, justo' + ' erat pulvinar nisi, id elementum sapien dolor et diam. Donec ac nunc sodales elit' + ' placerat eleifend. Sed ornare luctus ornare. Vestibulum vehicula, massa at' + ' pharetra fringilla, risus justo faucibus erat, nec porttitor nibh tellus sed est.' + ' Ut justo diam, lobortis eu tristique ac, p.In ut magna vel mauris malesuada' + ' dictum. Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id' + ' justo rutrum sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum' + ' sit amet. Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat,' + ' eget hendrerit felis turpis nec lorem. Nulla ultrices, elit pellentesque aliquet' + ' laoreet, justo erat pulvinar nisi, id elementum sapien dolor et diam. Donec ac' + ' nunc sodales elit placerat eleifend. Sed ornare luctus ornare. Vestibulum vehicula,' + ' massa at pharetra fringilla, risus justo faucibus erat, nec porttitor nibh tellus' + ' sed est. Ut justo diam, lobortis eu tris. In ut magna vel mauris malesuada dictum.' + ' Nulla ullamcorper metus quis diam cursus facilisis. Sed mollis quam id justo rutrum' + ' sagittis. Donec laoreet rutrum est, nec convallis mauris condimentum sit amet.' + ' Phasellus gravida, justo et congue auctor, nisi ipsum viverra erat, eget' + ' hendrerit felis turpis nec lorem.' , false , 31 ) ; } ) ; it ( 'testGenerateModeMessage' , ( ) => { testModeMessage ( true , 2 , 29 , '.X .XXX.. ...X XX.. ..X .XX. .XX.X' ) ; testModeMessage ( true , 4 , 64 , 'XX XXXXXX .X.. ...X ..XX .X.. XX..' ) ; testModeMessage ( false , 21 , 660 , 'X.X.. .X.X..X..XX .XXX ..X.. .XXX. .X... ..XXX' ) ; testModeMessage ( false , 32 , 4096 , 'XXXXX XXXXXXXXXXX X.X. ..... XXX.X ..X.. X.XXX' ) ; } ) ; it ( 'testStuffBits' , ( ) => { testStuffBits ( 5 , '.X.X. X.X.X .X.X.' , '.X.X. X.X.X .X.X.' ) ; testStuffBits ( 5 , '.X.X. ..... .X.X' , '.X.X. ....X ..X.X' ) ; testStuffBits ( 3 , 'XX. ... ... ..X XXX .X. ..' , 'XX. ..X ..X ..X ..X .XX XX. .X. ..X' ) ; testStuffBits ( 6 , '.X.X.. ...... ..X.XX' , '.X.X.. .....X. ..X.XX XXXX.' ) ; testStuffBits ( 6 , '.X.X.. ...... ...... ..X.X.' , '.X.X.. .....X .....X ....X. X.XXXX' ) ; testStuffBits ( 6 , '.X.X.. XXXXXX ...... ..X.XX' , '.X.X.. XXXXX. X..... ...X.X XXXXX.' ) ; testStuffBits ( 6 , '...... ..XXXX X..XX. .X.... .X.X.X .....X .X.... ...X.X .....X ....XX ..X... ....X. X..XXX X.XX.X' , '.....X ...XXX XX..XX ..X... ..X.X. X..... X.X... ....X. X..... X....X X..X.. .....X X.X..X XXX.XX .XXXXX' ) ; } ) ; it ( 'testHighLevelEncode' , ( ) => { testHighLevelEncodeString ( 'A. b.' , '...X. ..... ...XX XXX.. ...XX XXXX. XX.X' ) ; testHighLevelEncodeString ( 'Lorem ipsum.' , '.XX.X XXX.. X.... X..XX ..XX. .XXX. ....X .X.X. X...X X.X.. X.XX. .XXX. XXXX. XX.X' ) ; testHighLevelEncodeString ( 'Lo. Test 123.' , '.XX.X XXX.. X.... ..... ...XX XXX.. X.X.X ..XX. X.X.. X.X.X  XXXX. ...X ..XX .X.. .X.X XX.X' ) ; testHighLevelEncodeString ( 'Lo...x' , '.XX.X XXX.. X.... XXXX. XX.X XX.X XX.X XXX. XXX.. XX..X' ) ; testHighLevelEncodeString ( '. x://abc/.' , '..... ...XX XXX.. XX..X ..... X.X.X ..... X.X.. ..... X.X.. ...X. ...XX ..X.. ..... X.X.. XXXX. XX.X' ) ; testHighLevelEncodeString ( 'ABCdEFG' , '...X. ...XX ..X.. XXXXX ....X .XX..X.. ..XX. ..XXX .X...' ) ; testHighLevelEncodeString ( '09  UAG    ^160MEUCIQC0sYS/HpKxnBELR1uB85R20OoqqwFGa0q2uEi' + 'Ygh6utAIgLl1aBVM4EOTQtMQQYH9M2Z3Dp4qnA/fwWuQ+M8L3V8U=' , 823 ) ; } ) ; it ( 'testHighLevelEncodeBinary' , ( ) => { testHighLevelEncodeString ( 'N\0N' , '.XXXX XXXXX ....X ........ .XXXX' ) ; testHighLevelEncodeString ( 'N\0n' , '.XXXX XXXXX ...X. ........ .XX.XXX.' ) ; testHighLevelEncodeString ( 'N\0\u0080 A' , '.XXXX XXXXX ...X. ........ X....... ....X ...X.' ) ; testHighLevelEncodeString ( '\0a\u00FF\u0080 A' , 'XXXXX ..X.. ........ .XX....X XXXXXXXX X....... ....X ...X.' ) ; testHighLevelEncodeString ( '1234\0' , 'XXXX. ..XX .X.. .X.X .XX. XXX. XXXXX ....X ........' ) ; let sb = new ZXingStringBuilder ( ) ; for ( let i = 0 ; i <= 3000 ; i ++ ) { sb . append ( 128 + ( i % 30 ) ) ; } for ( let i of [ 1 , 2 , 3 , 10 , 29 , 30 , 31 , 32 , 33 , 60 , 61 , 62 , 63 , 64 , 2076 , 2077 , 2078 , 2079 , 2080 , 2100 , ] ) { const expectedLength = 8 * i + ( i <= 31 ? 10 : i <= 62 ? 20 : i <= 2078 ? 21 : 31 ) ; testHighLevelEncodeString ( sb . substring ( 0 , i ) , expectedLength ) ; if ( i != 1 && i != 32 && i != 2079 ) { testHighLevelEncodeString ( 'a' + sb . substring ( 0 , i - 1 ) , expectedLength ) ; testHighLevelEncodeString ( sb . substring ( 0 , i - 1 ) + 'a' , expectedLength ) ; } testHighLevelEncodeString ( 'a' + sb . substring ( 0 , i ) + 'b' , expectedLength + 15 ) ; } sb = new ZXingStringBuilder ( ) ; for ( let i = 0 ; i < 32 ; i ++ ) { sb . append ( '§' ) ; } sb . setCharAt ( 1 , 'A' ) ; testHighLevelEncodeString ( sb . toString ( ) , 5 + 20 + 31 * 8 ) ; sb = new ZXingStringBuilder ( ) ; for ( let i = 0 ; i < 31 ; i ++ ) { sb . append ( '§' ) ; } sb . setCharAt ( 1 , 'A' ) ; testHighLevelEncodeString ( sb . toString ( ) , 10 + 31 * 8 ) ; sb = new ZXingStringBuilder ( ) ; for ( let i = 0 ; i < 34 ; i ++ ) { sb . append ( '§' ) ; } sb . setCharAt ( 1 , 'A' ) ; testHighLevelEncodeString ( sb . toString ( ) , 20 + 34 * 8 ) ; sb = new ZXingStringBuilder ( ) ; for ( let i = 0 ; i < 64 ; i ++ ) { sb . append ( '§' ) ; } sb . setCharAt ( 30 , 'A' ) ; testHighLevelEncodeString ( sb . toString ( ) , 21 + 64 * 8 ) ; } ) ; it ( 'testHighLevelEncodePairs' , ( ) => { testHighLevelEncodeString ( 'ABC. DEF\r\n' , '...X. ...XX ..X.. ..... ...XX ..X.X ..XX. ..XXX ..... ...X.' ) ; testHighLevelEncodeString ( 'A. : , \r\n' , '...X. XXX.X XXXX. ...XX ..X.X  ..X.. ...X.' ) ; testHighLevelEncodeString ( 'A. 1234' , '...X. XXXX. XX.X ...X ..XX .X.. .X.X .X X.' ) ; testHighLevelEncodeString ( 'A\x80. \x80' , '...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X.......' ) ; } ) ; it ( 'testUserSpecifiedLayers' , ( ) => { const alphabet = StringUtils . getBytes ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' , ZXingStandardCharsets . ISO_8859_1 ) ; let aztec = AztecEncoder . encode ( alphabet , 25 , - 2 ) ; assertEquals ( 2 , aztec . getLayers ( ) ) ; assertTrue ( aztec . isCompact ( ) ) ; aztec = AztecEncoder . encode ( alphabet , 25 , 32 ) ; assertEquals ( 32 , aztec . getLayers ( ) ) ; assertFalse ( aztec . isCompact ( ) ) ; try { AztecEncoder . encode ( alphabet , 25 , 33 ) ; fail ( 'Encode should have failed.  No such thing as 33 layers' ) ; } catch ( expected ) { } try { AztecEncoder . encode ( alphabet , 25 , - 1 ) ; fail ( 'Encode should have failed.  Text can\'t fit in 1-layer compact' ) ; } catch ( expected ) { } } ) ; it ( 'testBorderCompact4Case' , ( ) => { const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ; const alphabet4 = alphabet + alphabet + alphabet + alphabet ; const data = StringUtils . getBytes ( alphabet4 , ZXingStandardCharsets . ISO_8859_1 ) ; try { AztecEncoder . encode ( data , 0 , - 4 ) ; fail ( 'Encode should have failed.  Text can\'t fit in 1-layer compact' ) ; } catch ( expected ) { } let aztecCode = AztecEncoder . encode ( data , 0 , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; assertFalse ( aztecCode . isCompact ( ) ) ; assertEquals ( 4 , aztecCode . getLayers ( ) ) ; aztecCode = AztecEncoder . encode ( StringUtils . getBytes ( alphabet4 . substring ( 0 , 100 ) , ZXingStandardCharsets . ISO_8859_1 ) , 10 , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; assertTrue ( aztecCode . isCompact ( ) ) ; assertEquals ( 4 , aztecCode . getLayers ( ) ) ; } ) ; function testEncode ( data , compact , layers , expected ) { const aztec = AztecEncoder . encode ( StringUtils . getBytes ( data , ZXingStandardCharsets . ISO_8859_1 ) , 33 , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; assertEquals ( compact , aztec . isCompact ( ) ) ; assertEquals ( layers , aztec . getLayers ( ) ) ; const matrix = aztec . getMatrix ( ) ; assertEquals ( expected , matrix . toString ( ) ) ; } function testEncodeDecode ( data , compact , layers ) { const aztec = AztecEncoder . encode ( StringUtils . getBytes ( data , ZXingStandardCharsets . ISO_8859_1 ) , 25 , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; assertEquals ( compact , aztec . isCompact ( ) ) ; assertEquals ( layers , aztec . getLayers ( ) ) ; const matrix = aztec . getMatrix ( ) ; let r = new AztecDetectorResult ( matrix , NO_POINTS , aztec . isCompact ( ) , aztec . getCodeWords ( ) , aztec . getLayers ( ) ) ; let res = new AztecDecoder ( ) . decode ( r ) ; assertEquals ( data , res . getText ( ) ) ; const random = getPseudoRandom ( ) ; matrix . flip ( random . nextInt ( matrix . getWidth ( ) ) , random . nextInt ( 2 ) ) ; matrix . flip ( random . nextInt ( matrix . getWidth ( ) ) , matrix . getHeight ( ) - 2 + random . nextInt ( 2 ) ) ; matrix . flip ( random . nextInt ( 2 ) , random . nextInt ( matrix . getHeight ( ) ) ) ; matrix . flip ( matrix . getWidth ( ) - 2 + random . nextInt ( 2 ) , random . nextInt ( matrix . getHeight ( ) ) ) ; r = new AztecDetectorResult ( matrix , NO_POINTS , aztec . isCompact ( ) , aztec . getCodeWords ( ) , aztec . getLayers ( ) ) ; res = new AztecDecoder ( ) . decode ( r ) ; assertEquals ( data , res . getText ( ) ) ; } function testWriter ( data , charset , eccPercent , compact , layers ) { const expectedData = ZXingStringEncoding . decode ( StringUtils . getBytes ( data , ZXingCharset . forName ( charset ) ) , ZXingStandardCharsets . ISO_8859_1 ) ; const hints = new Map < EncodeHintType , any > ( ) ; hints . set ( EncodeHintType . CHARACTER_SET , charset ) ; hints . set ( EncodeHintType . ERROR_CORRECTION , eccPercent ) ; const writer = new AztecCodeWriter ( ) ; const matrix = writer . encodeWithHints ( data , BarcodeFormat . AZTEC , 0 , 0 , hints ) ; const aztec = AztecEncoder . encode ( StringUtils . getBytes ( data , ZXingCharset . forName ( charset ) ) , eccPercent , AztecEncoder . DEFAULT_AZTEC_LAYERS ) ; assertEquals ( compact , aztec . isCompact ( ) ) ; assertEquals ( layers , aztec . getLayers ( ) ) ; const matrix2 = aztec . getMatrix ( ) ; assertEquals ( matrix . toString ( ) , matrix2 . toString ( ) ) ; let r = new AztecDetectorResult ( matrix , NO_POINTS , aztec . isCompact ( ) , aztec . getCodeWords ( ) , aztec . getLayers ( ) ) ; let res = new AztecDecoder ( ) . decode ( r ) ; assertEquals ( expectedData , res . getText ( ) ) ; const ecWords = ( aztec . getCodeWords ( ) * eccPercent ) / 100 / 2 ; const random = getPseudoRandom ( ) ; for ( let i = 0 ; i < ecWords ; i ++ ) { const x = random . nextBoolean ( ) ? random . nextInt ( aztec . getLayers ( ) * 2 ) : matrix . getWidth ( ) - 1 - random . nextInt ( aztec . getLayers ( ) * 2 ) ; const y = random . nextBoolean ( ) ? random . nextInt ( aztec . getLayers ( ) * 2 ) : matrix . getHeight ( ) - 1 - random . nextInt ( aztec . getLayers ( ) * 2 ) ; matrix . flip ( x , y ) ; } r = new AztecDetectorResult ( matrix , NO_POINTS , aztec . isCompact ( ) , aztec . getCodeWords ( ) , aztec . getLayers ( ) ) ; res = new AztecDecoder ( ) . decode ( r ) ; assertEquals ( expectedData , res . getText ( ) ) ; } function getPseudoRandom ( ) { return new Random ( 0xdeadbeef ) ; } function testModeMessage ( compact , layers , words , expected ) { const inArr = AztecEncoder . generateModeMessage ( compact , layers , words ) ; assertEquals ( stripSpace ( expected ) , stripSpace ( inArr . toString ( ) ) ) ; } function testStuffBits ( wordSize , bits , expected ) { const inArr = toBitArray ( bits ) ; const stuffed = AztecEncoder . stuffBits ( inArr , wordSize ) ; assertEquals ( stripSpace ( expected ) , stripSpace ( stuffed . toString ( ) ) ) ; } function toBitArray ( bits ) { const inArr = new BitArray ( ) ; const str = bits . replace ( DOTX , '' ) . split ( '' ) ; for ( let aStr of str ) { inArr . appendBit ( aStr === 'X' ) ; } return inArr ; } function toBooleanArray ( bitArray ) { const result = new Array < boolean > ( bitArray . getSize ( ) ) ; for ( let i = 0 ; i < result . length ; i ++ ) { result [ i ] = bitArray . get ( i ) ; } return result ; } function testHighLevelEncodeString ( s , expectedBits ) { const bits = new AztecHighLevelEncoder ( StringUtils . getBytes ( s , ZXingStandardCharsets . ISO_8859_1 ) ) . encode ( ) ; if ( typeof expectedBits === 'number' ) { const receivedBitCount = stripSpace ( bits . toString ( ) ) . length ; assertEquals ( receivedBitCount , expectedBits ) ; assertEquals ( AztecDecoder . highLevelDecode ( toBooleanArray ( bits ) ) , s ) ; } else { const receivedBits = stripSpace ( bits . toString ( ) ) ; assertEquals ( receivedBits , stripSpace ( expectedBits ) ) ; assertEquals ( AztecDecoder . highLevelDecode ( toBooleanArray ( bits ) ) , s ) ; } } function stripSpace ( s ) { return s . replace ( SPACES , '' ) ; } } ) ;