@ -32,7 +32,9 @@ assert.strictEqual(0, d.length);
var ui32 = new Uint32Array ( 4 ) . fill ( 42 ) ;
var e = Buffer . from ( ui32 ) ;
assert . deepEqual ( ui32 , e ) ;
for ( const [ index , value ] of e . entries ( ) ) {
assert . strictEqual ( value , ui32 [ index ] ) ;
}
// First check Buffer#fill() works as expected.
@ -463,32 +465,32 @@ for (let i = 0; i < Buffer.byteLength(utf8String); i++) {
assert . equal ( d [ 0 ] , 23 ) ;
assert . equal ( d [ 1 ] , 42 ) ;
assert . equal ( d [ 2 ] , 255 ) ;
assert . deepEqual ( d , Buffer . from ( d ) ) ;
assert . deepStrict Equal ( d , Buffer . from ( d ) ) ;
}
{
const e = Buffer . from ( 'über' ) ;
console . error ( 'uber: \'%s\'' , e . toString ( ) ) ;
assert . deepEqual ( e , Buffer . from ( [ 195 , 188 , 98 , 101 , 114 ] ) ) ;
assert . deepStrict Equal ( e , Buffer . from ( [ 195 , 188 , 98 , 101 , 114 ] ) ) ;
}
{
const f = Buffer . from ( 'über' , 'ascii' ) ;
console . error ( 'f.length: %d (should be 4)' , f . length ) ;
assert . deepEqual ( f , Buffer . from ( [ 252 , 98 , 101 , 114 ] ) ) ;
assert . deepStrict Equal ( f , Buffer . from ( [ 252 , 98 , 101 , 114 ] ) ) ;
}
[ 'ucs2' , 'ucs-2' , 'utf16le' , 'utf-16le' ] . forEach ( function ( encoding ) {
{
const f = Buffer . from ( 'über' , encoding ) ;
console . error ( 'f.length: %d (should be 8)' , f . length ) ;
assert . deepEqual ( f , Buffer . from ( [ 252 , 0 , 98 , 0 , 101 , 0 , 114 , 0 ] ) ) ;
assert . deepStrict Equal ( f , Buffer . from ( [ 252 , 0 , 98 , 0 , 101 , 0 , 114 , 0 ] ) ) ;
}
{
const f = Buffer . from ( 'привет' , encoding ) ;
console . error ( 'f.length: %d (should be 12)' , f . length ) ;
assert . deepEqual ( f ,
assert . deepStrict Equal ( f ,
Buffer . from ( [ 63 , 4 , 64 , 4 , 56 , 4 , 50 , 4 , 53 , 4 , 66 , 4 ] ) ) ;
assert . equal ( f . toString ( encoding ) , 'привет' ) ;
}
@ -499,23 +501,23 @@ for (let i = 0; i < Buffer.byteLength(utf8String); i++) {
const size = f . write ( 'あいうえお' , encoding ) ;
console . error ( 'bytes written to buffer: %d (should be 4)' , size ) ;
assert . equal ( size , 4 ) ;
assert . deepEqual ( f , Buffer . from ( [ 0x42 , 0x30 , 0x44 , 0x30 , 0x00 ] ) ) ;
assert . deepStrict Equal ( f , Buffer . from ( [ 0x42 , 0x30 , 0x44 , 0x30 , 0x00 ] ) ) ;
}
} ) ;
{
const f = Buffer . from ( '\uD83D\uDC4D' , 'utf-16le' ) ; // THUMBS UP SIGN (U+1F44D)
assert . equal ( f . length , 4 ) ;
assert . deepEqual ( f , Buffer . from ( '3DD84DDC' , 'hex' ) ) ;
assert . deepStrict Equal ( f , Buffer . from ( '3DD84DDC' , 'hex' ) ) ;
}
var arrayIsh = { 0 : 0 , 1 : 1 , 2 : 2 , 3 : 3 , length : 4 } ;
var g = Buffer . from ( arrayIsh ) ;
assert . deepEqual ( g , Buffer . from ( [ 0 , 1 , 2 , 3 ] ) ) ;
assert . deepStrict Equal ( g , Buffer . from ( [ 0 , 1 , 2 , 3 ] ) ) ;
var strArrayIsh = { 0 : '0' , 1 : '1' , 2 : '2' , 3 : '3' , length : 4 } ;
g = Buffer . from ( strArrayIsh ) ;
assert . deepEqual ( g , Buffer . from ( [ 0 , 1 , 2 , 3 ] ) ) ;
assert . deepStrict Equal ( g , Buffer . from ( [ 0 , 1 , 2 , 3 ] ) ) ;
//
@ -526,10 +528,10 @@ assert.equal('TWFu', (Buffer.from('Man')).toString('base64'));
{
// test that regular and URL-safe base64 both work
const expected = [ 0xff , 0xff , 0xbe , 0xff , 0xef , 0xbf , 0xfb , 0xef , 0xff ] ;
assert . deepEqual ( Buffer . from ( '//++/++/++//' , 'base64' ) ,
Buffer . from ( expected ) ) ;
assert . deepEqual ( Buffer . from ( '__--_--_--__' , 'base64' ) ,
Buffer . from ( expected ) ) ;
assert . deepStrict Equal ( Buffer . from ( '//++/++/++//' , 'base64' ) ,
Buffer . from ( expected ) ) ;
assert . deepStrict Equal ( Buffer . from ( '__--_--_--__' , 'base64' ) ,
Buffer . from ( expected ) ) ;
}
{
@ -1021,7 +1023,7 @@ Buffer.from(Buffer.allocUnsafe(0), 0, 0);
assert . equal ( string , '{"type":"Buffer","data":[116,101,115,116]}' ) ;
assert . deepEqual ( buffer , JSON . parse ( string , function ( key , value ) {
assert . deepStrict Equal ( buffer , JSON . parse ( string , function ( key , value ) {
return value && value . type === 'Buffer'
? Buffer . from ( value . data )
: value ;
@ -1167,92 +1169,92 @@ assert.throws(function() {
( function ( ) {
var buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeUIntLE ( 0x123456 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x56 , 0x34 , 0x12 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x56 , 0x34 , 0x12 ] ) ;
assert . equal ( buf . readUIntLE ( 0 , 3 ) , 0x123456 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeUIntBE ( 0x123456 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 ] ) ;
assert . equal ( buf . readUIntBE ( 0 , 3 ) , 0x123456 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntLE ( 0x123456 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x56 , 0x34 , 0x12 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x56 , 0x34 , 0x12 ] ) ;
assert . equal ( buf . readIntLE ( 0 , 3 ) , 0x123456 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntBE ( 0x123456 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 ] ) ;
assert . equal ( buf . readIntBE ( 0 , 3 ) , 0x123456 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntLE ( - 0x123456 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0xaa , 0xcb , 0xed ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0xaa , 0xcb , 0xed ] ) ;
assert . equal ( buf . readIntLE ( 0 , 3 ) , - 0x123456 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntBE ( - 0x123456 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0xed , 0xcb , 0xaa ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0xed , 0xcb , 0xaa ] ) ;
assert . equal ( buf . readIntBE ( 0 , 3 ) , - 0x123456 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntLE ( - 0x123400 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x00 , 0xcc , 0xed ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x00 , 0xcc , 0xed ] ) ;
assert . equal ( buf . readIntLE ( 0 , 3 ) , - 0x123400 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntBE ( - 0x123400 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0xed , 0xcc , 0x00 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0xed , 0xcc , 0x00 ] ) ;
assert . equal ( buf . readIntBE ( 0 , 3 ) , - 0x123400 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntLE ( - 0x120000 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x00 , 0x00 , 0xee ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x00 , 0x00 , 0xee ] ) ;
assert . equal ( buf . readIntLE ( 0 , 3 ) , - 0x120000 ) ;
buf = Buffer . allocUnsafe ( 3 ) ;
buf . writeIntBE ( - 0x120000 , 0 , 3 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0xee , 0x00 , 0x00 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0xee , 0x00 , 0x00 ] ) ;
assert . equal ( buf . readIntBE ( 0 , 3 ) , - 0x120000 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeUIntLE ( 0x1234567890 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x90 , 0x78 , 0x56 , 0x34 , 0x12 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x90 , 0x78 , 0x56 , 0x34 , 0x12 ] ) ;
assert . equal ( buf . readUIntLE ( 0 , 5 ) , 0x1234567890 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeUIntBE ( 0x1234567890 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 ] ) ;
assert . equal ( buf . readUIntBE ( 0 , 5 ) , 0x1234567890 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeIntLE ( 0x1234567890 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x90 , 0x78 , 0x56 , 0x34 , 0x12 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x90 , 0x78 , 0x56 , 0x34 , 0x12 ] ) ;
assert . equal ( buf . readIntLE ( 0 , 5 ) , 0x1234567890 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeIntBE ( 0x1234567890 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x12 , 0x34 , 0x56 , 0x78 , 0x90 ] ) ;
assert . equal ( buf . readIntBE ( 0 , 5 ) , 0x1234567890 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeIntLE ( - 0x1234567890 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x70 , 0x87 , 0xa9 , 0xcb , 0xed ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x70 , 0x87 , 0xa9 , 0xcb , 0xed ] ) ;
assert . equal ( buf . readIntLE ( 0 , 5 ) , - 0x1234567890 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeIntBE ( - 0x1234567890 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0xed , 0xcb , 0xa9 , 0x87 , 0x70 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0xed , 0xcb , 0xa9 , 0x87 , 0x70 ] ) ;
assert . equal ( buf . readIntBE ( 0 , 5 ) , - 0x1234567890 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeIntLE ( - 0x0012000000 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0x00 , 0x00 , 0x00 , 0xee , 0xff ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0x00 , 0x00 , 0x00 , 0xee , 0xff ] ) ;
assert . equal ( buf . readIntLE ( 0 , 5 ) , - 0x0012000000 ) ;
buf = Buffer . allocUnsafe ( 5 ) ;
buf . writeIntBE ( - 0x0012000000 , 0 , 5 ) ;
assert . deepEqual ( buf . toJSON ( ) . data , [ 0xff , 0xee , 0x00 , 0x00 , 0x00 ] ) ;
assert . deepStrict Equal ( buf . toJSON ( ) . data , [ 0xff , 0xee , 0x00 , 0x00 , 0x00 ] ) ;
assert . equal ( buf . readIntBE ( 0 , 5 ) , - 0x0012000000 ) ;
} ) ( ) ;
@ -1292,7 +1294,7 @@ assert.throws(function() {
}
var utf16Buf = Buffer . from ( '0123456789' , 'utf16le' ) ;
assert . deepEqual ( utf16Buf . slice ( 0 , 6 ) , Buffer . from ( '012' , 'utf16le' ) ) ;
assert . deepStrict Equal ( utf16Buf . slice ( 0 , 6 ) , Buffer . from ( '012' , 'utf16le' ) ) ;
assert . equal ( buf . slice ( '0' , '1' ) , '0' ) ;
assert . equal ( buf . slice ( '-5' , '10' ) , '56789' ) ;