@@ -9,11 +9,34 @@ describe("fixUint8Array", () => {
99 expect ( fixed . buffer ) . toBe ( input . buffer ) ; // no copy must be performed in this case
1010 } ) ;
1111
12+ it ( "works for Uint8Array<ArrayBuffer> where data is not using all of the buffer" , ( ) => {
13+ const buffer = new Uint8Array ( [ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a ] ) . buffer ;
14+ expect ( buffer . byteLength ) . toEqual ( 11 ) ;
15+ const original = new Uint8Array ( buffer , 1 , 5 ) ;
16+
17+ const fixed = fixUint8Array ( original ) ;
18+ expect ( fixed . length ) . toEqual ( 5 ) ;
19+ expect ( fixed ) . toEqual ( new Uint8Array ( [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ] ) ) ;
20+ expect ( fixed . buffer ) . toBe ( buffer ) ; // no copy must be performed in this case
21+ } ) ;
22+
1223 it ( "works for Uint8Array<SharedArrayBuffer>" , ( ) => {
1324 const sharedBuffer = new SharedArrayBuffer ( 8 ) ;
1425 const input : Uint8Array < SharedArrayBuffer > = new Uint8Array ( sharedBuffer ) ;
1526 const fixed = fixUint8Array ( input ) ;
1627 expect ( fixed ) . toEqual ( input ) ;
1728 expect ( fixed . buffer ) . toBeInstanceOf ( ArrayBuffer ) ;
1829 } ) ;
30+
31+ it ( "works for Uint8Array<SharedArrayBuffer> where data is not using all of the buffer" , ( ) => {
32+ const sharedBuffer = new SharedArrayBuffer ( 8 ) ;
33+ const input : Uint8Array < SharedArrayBuffer > = new Uint8Array ( sharedBuffer , 0 , 3 ) ;
34+ input [ 0 ] = 0xaa ;
35+ input [ 1 ] = 0xbb ;
36+ input [ 2 ] = 0xcc ;
37+ const fixed = fixUint8Array ( input ) ;
38+ expect ( fixed . buffer ) . toBeInstanceOf ( ArrayBuffer ) ;
39+ expect ( fixed . length ) . toEqual ( 3 ) ;
40+ expect ( fixed ) . toEqual ( new Uint8Array ( [ 0xaa , 0xbb , 0xcc ] ) ) ;
41+ } ) ;
1942} ) ;
0 commit comments