import 'mocha' ; import * as assert from 'assert' ; import ByteMatrix from './../../../../core/qrcode/encoder/ByteMatrix' ; import MaskUtil from './../../../../core/qrcode/encoder/MaskUtil' ; author satorux google author mysen google describe ( "MaskUtilTestCase" , ( ) => { it ( "testApplyMaskPenaltyRule1" , ( ) => { let matrix = new ByteMatrix ( 4 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 2 , 0 , 0 ) ; matrix . setNumber ( 3 , 0 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule1 ( matrix ) , 0 ) ; matrix = new ByteMatrix ( 6 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 2 , 0 , 0 ) ; matrix . setNumber ( 3 , 0 , 0 ) ; matrix . setNumber ( 4 , 0 , 0 ) ; matrix . setNumber ( 5 , 0 , 1 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule1 ( matrix ) , 3 ) ; matrix . setNumber ( 5 , 0 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule1 ( matrix ) , 4 ) ; matrix = new ByteMatrix ( 1 , 6 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 0 , 1 , 0 ) ; matrix . setNumber ( 0 , 2 , 0 ) ; matrix . setNumber ( 0 , 3 , 0 ) ; matrix . setNumber ( 0 , 4 , 0 ) ; matrix . setNumber ( 0 , 5 , 1 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule1 ( matrix ) , 3 ) ; matrix . setNumber ( 0 , 5 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule1 ( matrix ) , 4 ) ; } ) ; it ( "testApplyMaskPenaltyRule2" , ( ) => { let matrix = new ByteMatrix ( 1 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule2 ( matrix ) , 0 ) ; matrix = new ByteMatrix ( 2 , 2 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 0 , 1 , 0 ) ; matrix . setNumber ( 1 , 1 , 1 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule2 ( matrix ) , 0 ) ; matrix = new ByteMatrix ( 2 , 2 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 0 , 1 , 0 ) ; matrix . setNumber ( 1 , 1 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule2 ( matrix ) , 3 ) ; matrix = new ByteMatrix ( 3 , 3 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 2 , 0 , 0 ) ; matrix . setNumber ( 0 , 1 , 0 ) ; matrix . setNumber ( 1 , 1 , 0 ) ; matrix . setNumber ( 2 , 1 , 0 ) ; matrix . setNumber ( 0 , 2 , 0 ) ; matrix . setNumber ( 1 , 2 , 0 ) ; matrix . setNumber ( 2 , 2 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule2 ( matrix ) , 3 * 4 ) ; } ) ; it ( "testApplyMaskPenaltyRule3" , ( ) => { let matrix = new ByteMatrix ( 11 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 2 , 0 , 0 ) ; matrix . setNumber ( 3 , 0 , 0 ) ; matrix . setNumber ( 4 , 0 , 1 ) ; matrix . setNumber ( 5 , 0 , 0 ) ; matrix . setNumber ( 6 , 0 , 1 ) ; matrix . setNumber ( 7 , 0 , 1 ) ; matrix . setNumber ( 8 , 0 , 1 ) ; matrix . setNumber ( 9 , 0 , 0 ) ; matrix . setNumber ( 10 , 0 , 1 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule3 ( matrix ) , 40 ) ; matrix = new ByteMatrix ( 11 , 1 ) ; matrix . setNumber ( 0 , 0 , 1 ) ; matrix . setNumber ( 1 , 0 , 0 ) ; matrix . setNumber ( 2 , 0 , 1 ) ; matrix . setNumber ( 3 , 0 , 1 ) ; matrix . setNumber ( 4 , 0 , 1 ) ; matrix . setNumber ( 5 , 0 , 0 ) ; matrix . setNumber ( 6 , 0 , 1 ) ; matrix . setNumber ( 7 , 0 , 0 ) ; matrix . setNumber ( 8 , 0 , 0 ) ; matrix . setNumber ( 9 , 0 , 0 ) ; matrix . setNumber ( 10 , 0 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule3 ( matrix ) , 40 ) ; matrix = new ByteMatrix ( 1 , 11 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 0 , 1 , 0 ) ; matrix . setNumber ( 0 , 2 , 0 ) ; matrix . setNumber ( 0 , 3 , 0 ) ; matrix . setNumber ( 0 , 4 , 1 ) ; matrix . setNumber ( 0 , 5 , 0 ) ; matrix . setNumber ( 0 , 6 , 1 ) ; matrix . setNumber ( 0 , 7 , 1 ) ; matrix . setNumber ( 0 , 8 , 1 ) ; matrix . setNumber ( 0 , 9 , 0 ) ; matrix . setNumber ( 0 , 10 , 1 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule3 ( matrix ) , 40 ) ; matrix = new ByteMatrix ( 1 , 11 ) ; matrix . setNumber ( 0 , 0 , 1 ) ; matrix . setNumber ( 0 , 1 , 0 ) ; matrix . setNumber ( 0 , 2 , 1 ) ; matrix . setNumber ( 0 , 3 , 1 ) ; matrix . setNumber ( 0 , 4 , 1 ) ; matrix . setNumber ( 0 , 5 , 0 ) ; matrix . setNumber ( 0 , 6 , 1 ) ; matrix . setNumber ( 0 , 7 , 0 ) ; matrix . setNumber ( 0 , 8 , 0 ) ; matrix . setNumber ( 0 , 9 , 0 ) ; matrix . setNumber ( 0 , 10 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule3 ( matrix ) , 40 ) ; } ) ; it ( "testApplyMaskPenaltyRule4" , ( ) => { let matrix = new ByteMatrix ( 1 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule4 ( matrix ) , 100 ) ; matrix = new ByteMatrix ( 2 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 0 , 0 , 1 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule4 ( matrix ) , 0 ) ; matrix = new ByteMatrix ( 6 , 1 ) ; matrix . setNumber ( 0 , 0 , 0 ) ; matrix . setNumber ( 1 , 0 , 1 ) ; matrix . setNumber ( 2 , 0 , 1 ) ; matrix . setNumber ( 3 , 0 , 1 ) ; matrix . setNumber ( 4 , 0 , 1 ) ; matrix . setNumber ( 5 , 0 , 0 ) ; assert . strictEqual ( MaskUtil . applyMaskPenaltyRule4 ( matrix ) , 30 ) ; } ) ; function TestGetDataMaskBitInternal ( maskPattern , expected ) { for ( let x = 0 ; x < 6 ; ++ x ) { for ( let y = 0 ; y < 6 ; ++ y ) { if ( ( expected [ y ] [ x ] === 1 ) !== MaskUtil . getDataMaskBit ( maskPattern , x , y ) ) { return false ; } } } return true ; } it ( "testGetDataMaskBit" , ( ) => { const mask0 = [ Int32Array . from ( [ 1 , 0 , 1 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 0 , 1 , 0 , 1 , 0 , 1 ] ) , Int32Array . from ( [ 1 , 0 , 1 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 0 , 1 , 0 , 1 , 0 , 1 ] ) , Int32Array . from ( [ 1 , 0 , 1 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 0 , 1 , 0 , 1 , 0 , 1 ] ) ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 0 , mask0 ) , true ) ; const mask1 = [ Int32Array . from ( [ 1 , 1 , 1 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 0 , 0 , 0 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 0 , 0 , 0 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 0 , 0 , 0 , 0 , 0 , 0 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 1 , mask1 ) , true ) ; const mask2 = [ Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 2 , mask2 ) , true ) ; const mask3 = [ Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 0 , 0 , 1 , 0 , 0 , 1 ] ) , Int32Array . from ( [ 0 , 1 , 0 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 0 , 0 , 1 , 0 , 0 , 1 ] ) , Int32Array . from ( [ 0 , 1 , 0 , 0 , 1 , 0 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 3 , mask3 ) , true ) ; const mask4 = [ Int32Array . from ( [ 1 , 1 , 1 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 0 , 0 , 0 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 0 , 0 , 0 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 0 , 0 , 0 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 4 , mask4 ) , true ) ; const mask5 = [ Int32Array . from ( [ 1 , 1 , 1 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 1 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 1 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 0 , 0 , 0 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 5 , mask5 ) , true ) ; const mask6 = [ Int32Array . from ( [ 1 , 1 , 1 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 1 , 1 , 0 , 1 , 1 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 1 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 1 , 0 , 1 , 1 , 0 , 1 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 0 , 1 , 1 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 6 , mask6 ) , true ) ; const mask7 = [ Int32Array . from ( [ 1 , 0 , 1 , 0 , 1 , 0 ] ) , Int32Array . from ( [ 0 , 0 , 0 , 1 , 1 , 1 ] ) , Int32Array . from ( [ 1 , 0 , 0 , 0 , 1 , 1 ] ) , Int32Array . from ( [ 0 , 1 , 0 , 1 , 0 , 1 ] ) , Int32Array . from ( [ 1 , 1 , 1 , 0 , 0 , 0 ] ) , Int32Array . from ( [ 0 , 1 , 1 , 1 , 0 , 0 ] ) , ] ; assert . strictEqual ( TestGetDataMaskBitInternal ( 7 , mask7 ) , true ) ; } ) ; } ) ;