import { ChecksumException } from '@zxing/library' ; import Random from '../../../util/Random' ; import { PDF417DecoderErrorCorrection } from '@zxing/library' ; import * as assert from 'assert' ; import AbstractErrorCorrectionSpec from './AbstractErrorCorrection.spec' ; author Sean Owen
  describe ( 'ErrorCorrectionTestCase' , ( ) => { it ( 'testNoError' , ( ) => { const received = Int32Array . from ( PDF417_TEST_WITH_EC ) ; checkDecode ( received ) ; } ) ; it ( 'testOneError' , ( ) => { const random = AbstractErrorCorrectionSpec . getRandom ( ) ; for ( let i = 0 ; i < PDF417_TEST_WITH_EC . length ; i ++ ) { const received = Int32Array . from ( PDF417_TEST_WITH_EC ) ; received [ i ] = random . nextInt ( 256 ) ; checkDecode ( received ) ; } } ) ; it ( 'testMaxErrors' , ( ) => { const random = AbstractErrorCorrectionSpec . getRandom ( ) ; for ( let testIterations = 0 ; testIterations < 100 ; testIterations ++ ) { const received = Int32Array . from ( PDF417_TEST_WITH_EC ) ; AbstractErrorCorrectionSpec . corrupt ( received , MAX_ERRORS , random ) ; checkDecode ( received ) ; } } ) ; it ( 'testTooManyErrors' , ( ) => { const received = Int32Array . from ( PDF417_TEST_WITH_EC ) ; const random = AbstractErrorCorrectionSpec . getRandom ( ) ; AbstractErrorCorrectionSpec . corrupt ( received , MAX_ERRORS + 1 , random ) ; try { checkDecode ( received ) ; assert . fail ( 'Should not have decoded' ) ; } catch ( ce ) { if ( ce instanceof ChecksumException ) { return ; } throw ce ; } } ) ; it ( 'testMaxErasures' , ( ) => { return ; const random = AbstractErrorCorrectionSpec . getRandom ( ) ; for ( const test of PDF417_TEST ) { const received = Int32Array . from ( PDF417_TEST_WITH_EC ) ; const erasures = AbstractErrorCorrectionSpec . erase ( received , MAX_ERASURES , random ) ; checkDecode ( received , erasures ) ; } } ) ; it ( 'testTooManyErasures' , ( ) => { const random = AbstractErrorCorrectionSpec . getRandom ( ) ; const received = Int32Array . from ( PDF417_TEST_WITH_EC ) ; const erasures = AbstractErrorCorrectionSpec . erase ( received , MAX_ERASURES + 1 , random ) ; try { checkDecode ( received , erasures ) ; assert . fail ( 'Should not have decoded' ) ; } catch ( ce ) { if ( ce instanceof ChecksumException ) { return ; } throw ce ; } } ) ; } ) ; const PDF417_TEST = Int32Array . from ( [ 48 , 901 , 56 , 141 , 627 , 856 , 330 , 69 , 244 , 900 , 852 , 169 , 843 , 895 , 852 , 895 , 913 , 154 , 845 , 778 , 387 , 89 , 869 , 901 , 219 , 474 , 543 , 650 , 169 , 201 , 9 , 160 , 35 , 70 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 ] ) ; const PDF417_TEST_WITH_EC = Int32Array . from ( [ 48 , 901 , 56 , 141 , 627 , 856 , 330 , 69 , 244 , 900 , 852 , 169 , 843 , 895 , 852 , 895 , 913 , 154 , 845 , 778 , 387 , 89 , 869 , 901 , 219 , 474 , 543 , 650 , 169 , 201 , 9 , 160 , 35 , 70 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 900 , 769 , 843 , 591 , 910 , 605 , 206 , 706 , 917 , 371 , 469 , 79 , 718 , 47 , 777 , 249 , 262 , 193 , 620 , 597 , 477 , 450 , 806 , 908 , 309 , 153 , 871 , 686 , 838 , 185 , 674 , 68 , 679 , 691 , 794 , 497 , 479 , 234 , 250 , 496 , 43 , 347 , 582 , 882 , 536 , 322 , 317 , 273 , 194 , 917 , 237 , 420 , 859 , 340 , 115 , 222 , 808 , 866 , 836 , 417 , 121 , 833 , 459 , 64 , 159 ] ) ; const ECC_BYTES = PDF417_TEST_WITH_EC . length - PDF417_TEST . length ; const ERROR_LIMIT = ECC_BYTES ; const MAX_ERRORS = ERROR_LIMIT / 2 ; const MAX_ERASURES = ERROR_LIMIT ; const ec = new PDF417DecoderErrorCorrection ( ) ; throws function checkDecode ( received , erasures ? ) { ec . decode ( received , ECC_BYTES , erasures || Int32Array . from ( [ 0 ] ) ) ; for ( let i = 0 ; i < PDF417_TEST . length ; i ++ ) { assert . strictEqual ( received [ i ] , PDF417_TEST [ i ] ) ; } }