license import { ENV } from '@tensorflow/tfjs-core' ; import { FileChunkIterator } from './file_chunk_iterator' ; const range = ( start , end ) => { return Array . from ( { length : ( end - start ) } , ( v , k ) => k + start ) ; } ; const testData = ENV . get ( 'IS_BROWSER' ) ? new Blob ( [ new Uint8Array ( range ( 0 , 55 ) ) ] ) : new Uint8Array ( range ( 0 , 55 ) ) ; describe ( 'FileChunkIterator' , ( ) => { it ( 'Reads the entire file and then closes the stream' , async ( ) => { const readIterator = new FileChunkIterator ( testData , { chunkSize : 10 } ) ; const result = await readIterator . toArrayForTest ( ) ; expect ( result . length ) . toEqual ( 6 ) ; const totalBytes = result . map ( x => x . length ) . reduce ( ( a , b ) => a + b ) ; expect ( totalBytes ) . toEqual ( 55 ) ; } ) ; it ( 'Reads chunks in order' , async ( ) => { const readIterator = new FileChunkIterator ( testData , { chunkSize : 10 } ) ; const result = await readIterator . toArrayForTest ( ) ; expect ( result [ 0 ] [ 0 ] ) . toEqual ( 0 ) ; expect ( result [ 1 ] [ 0 ] ) . toEqual ( 10 ) ; expect ( result [ 2 ] [ 0 ] ) . toEqual ( 20 ) ; expect ( result [ 3 ] [ 0 ] ) . toEqual ( 30 ) ; expect ( result [ 4 ] [ 0 ] ) . toEqual ( 40 ) ; expect ( result [ 5 ] [ 0 ] ) . toEqual ( 50 ) ; } ) ; it ( 'Reads chunks of expected sizes' , async ( ) => { const readIterator = new FileChunkIterator ( testData , { chunkSize : 10 } ) ; const result = await readIterator . toArrayForTest ( ) ; expect ( result [ 0 ] . length ) . toEqual ( 10 ) ; expect ( result [ 1 ] . length ) . toEqual ( 10 ) ; expect ( result [ 2 ] . length ) . toEqual ( 10 ) ; expect ( result [ 3 ] . length ) . toEqual ( 10 ) ; expect ( result [ 4 ] . length ) . toEqual ( 10 ) ; expect ( result [ 5 ] . length ) . toEqual ( 5 ) ; } ) ; it ( 'Provides mistyped data throws error' , ( ) => { expect ( ( ) => { new FileChunkIterator ( null ) ; } ) . toThrowError ( 'FileChunkIterator only supports File, Blob and Uint8Array right ' + 'now.' ) ; } ) ; } ) ;