-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: add {read|write}Big[U]Int64{BE|LE} methods
PR-URL: #19691 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
Showing
5 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
const buf = Buffer.allocUnsafe(8); | ||
|
||
['LE', 'BE'].forEach(function(endianness) { | ||
// Should allow simple BigInts to be written and read | ||
let val = 123456789n; | ||
buf['writeBigInt64' + endianness](val, 0); | ||
let rtn = buf['readBigInt64' + endianness](0); | ||
assert.strictEqual(val, rtn); | ||
|
||
// Should allow INT64_MAX to be written and read | ||
val = 0x7fffffffffffffffn; | ||
buf['writeBigInt64' + endianness](val, 0); | ||
rtn = buf['readBigInt64' + endianness](0); | ||
assert.strictEqual(val, rtn); | ||
|
||
// Should read and write a negative signed 64-bit integer | ||
val = -123456789n; | ||
buf['writeBigInt64' + endianness](val, 0); | ||
assert.strictEqual(val, buf['readBigInt64' + endianness](0)); | ||
|
||
// Should read and write an unsigned 64-bit integer | ||
val = 123456789n; | ||
buf['writeBigUInt64' + endianness](val, 0); | ||
assert.strictEqual(val, buf['readBigUInt64' + endianness](0)); | ||
|
||
// Should throw a RangeError upon INT64_MAX+1 being written | ||
assert.throws(function() { | ||
const val = 0x8000000000000000n; | ||
buf['writeBigInt64' + endianness](val, 0); | ||
}, RangeError); | ||
|
||
// Should throw a RangeError upon UINT64_MAX+1 being written | ||
assert.throws(function() { | ||
const val = 0x10000000000000000n; | ||
buf['writeBigUInt64' + endianness](val, 0); | ||
}, RangeError); | ||
|
||
// Should throw a TypeError upon invalid input | ||
assert.throws(function() { | ||
buf['writeBigInt64' + endianness]('bad', 0); | ||
}, TypeError); | ||
|
||
// Should throw a TypeError upon invalid input | ||
assert.throws(function() { | ||
buf['writeBigUInt64' + endianness]('bad', 0); | ||
}, TypeError); | ||
}); |