import { strict as assert } from "assert" ; import { SparseArray2D } from "../sparsearray2d" ; import { fill , check , extract } from "./utils" ; function expectEqual < T > ( actual , expected , rowStart , colStart , rowCount , colCount ) { assert . deepEqual ( extract ( actual , rowStart , colStart , rowCount , colCount ) , extract ( expected , rowStart , colStart , rowCount , colCount ) ) ; } describe ( "SparseArray2D" , ( ) => { it ( "read/write top-left 256x256" , ( ) => { const a = new SparseArray2D ( ) ; fill ( a , 0 , 0 , 256 , 256 ) ; check ( a , 0 , 0 , 256 , 256 ) ; } ) ; it ( "read/write bottom-right 256x256" , ( ) => { const a = new SparseArray2D ( ) ; fill ( a , 0 , 0 , 256 , 256 ) ; check ( a , 0 , 0 , 256 , 256 ) ; fill ( a , 0xffffff00 , 0xffffff00 , 256 , 256 ) ; check ( a , 0xffffff00 , 0xffffff00 , 256 , 256 ) ; } ) ; describe ( "clear row/cols" , ( ) => { function makeCases ( clearRows ) { function makeCase ( rowStart , colStart , rowCount , colCount , clearStart , clearCount ) { if ( clearRows ) { return { rowStart , colStart , rowCount , colCount , rowClearStart : clearStart , rowClearCount : clearCount , colClearStart : 0 , colClearCount : 0 } ; } else { return { rowStart , colStart , rowCount , colCount , rowClearStart : 0 , rowClearCount : 0 , colClearStart : clearStart , colClearCount : clearCount } ; } } const cases = [ makeCase ( 0 , 0 , 1 , 1 , 0 , 1 ) , makeCase ( 0 , 0 , 256 , 256 , 127 , 2 ) , ] ; for ( let i = 0 ; i < 16 ; i ++ ) { cases . push ( makeCase ( 0 , 0 , 16 , 16 , i , 1 ) ) ; } { const start = 0xfffffff0 ; const count = 16 ; for ( let i = start ; i < start + 16 ; i ++ ) { cases . push ( makeCase ( start , start , count , count , i , 1 ) ) ; } } return cases ; } function testClear ( { rowStart , colStart , rowCount , colCount , rowClearStart , rowClearCount , colClearStart , colClearCount } ) { const fillRange = `(${ rowStart },${ colStart })-(${ rowStart + rowCount },${ rowStart + colCount })` ; const rowClearRange = rowClearCount > 0 ? `clearRows [${ rowClearStart }..${ rowClearStart + rowClearCount }]` : "" ; const colClearRange = colClearCount > 0 ? `clearCols [${ colClearStart }..${ colClearStart + colClearCount }]` : "" ; it ( `${ rowClearRange }${ colClearRange } (filled: ${ fillRange })` , ( ) => { const actual = new SparseArray2D ( ) ; fill ( actual , rowStart , colStart , rowCount , colCount ) ; actual . clearRows ( rowClearStart , rowClearCount ) ; actual . clearCols ( colClearStart , colClearCount ) ; const expected = new SparseArray2D ( ) ; fill ( expected , rowStart , colStart , rowCount , colCount ) ; for ( let row = rowClearStart ; row < rowClearStart + rowClearCount ; row ++ ) { for ( let col = colStart ; col < colStart + colCount ; col ++ ) { expected . setCell ( row , col , undefined ) ; } } for ( let row = rowStart ; row < rowStart + rowCount ; row ++ ) { for ( let col = colClearStart ; col < colClearStart + colClearCount ; col ++ ) { expected . setCell ( row , col , undefined ) ; } } expectEqual ( actual , expected , rowStart , colStart , rowCount , colCount ) ; } ) ; } describe ( "clear rows" , ( ) => { const cases = makeCases ( true ) ; for ( const testCase of cases ) { testClear ( testCase ) ; } } ) ; describe ( "clear cols" , ( ) => { const cases = makeCases ( false ) ; for ( const testCase of cases ) { testClear ( testCase ) ; } } ) ; } ) ; interface IClearTestConfig { readonly rowStart : number ; readonly colStart : number ; readonly rowCount : number ; readonly colCount : number ; readonly rowClearStart : number ; readonly rowClearCount : number ; readonly colClearStart : number ; readonly colClearCount : number ; } } ) ;