summaryrefslogtreecommitdiff
path: root/test/parallel/test-buffer-readfloat.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-01-30 17:34:25 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2018-03-02 19:29:46 +0000
commite8bb1f35df079cf0111fc18a8a24ec0a2d66eb3e (patch)
tree635c3c8e56a123fb45ab1f3136739aade631ba9c /test/parallel/test-buffer-readfloat.js
parenta6c490cc8e8c848d6c4d5b8739827198055fe40f (diff)
downloadnode-new-e8bb1f35df079cf0111fc18a8a24ec0a2d66eb3e.tar.gz
buffer: refactor all read/write functions
There are a lot of changes in this commit: 1) Remove the `noAssert` argument from all read and write functions. 2) Improve the performance of all read floating point functions significantly. This is done by switching to TypedArrays as the write floating point write functions. 3) No implicit type coercion for offset and byteLength anymore. 4) Adds a lot of tests. 5) Moves the read and write functions to the internal buffer file to split the files in smaller chunks. 6) Reworked a lot of existing tests. 7) Improve the performane of all all read write functions by using a faster input validation and by improving function logic. 8) Significantly improved the performance of all read int functions. This is done by using a implementation without a loop. 9) Improved error handling. 10) Rename test file to use the correct subsystem. PR-URL: https://github.com/nodejs/node/pull/18395 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'test/parallel/test-buffer-readfloat.js')
-rw-r--r--test/parallel/test-buffer-readfloat.js101
1 files changed, 101 insertions, 0 deletions
diff --git a/test/parallel/test-buffer-readfloat.js b/test/parallel/test-buffer-readfloat.js
new file mode 100644
index 0000000000..8e1e0ba5bb
--- /dev/null
+++ b/test/parallel/test-buffer-readfloat.js
@@ -0,0 +1,101 @@
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+// Test 32 bit float
+const buffer = Buffer.alloc(4);
+
+buffer[0] = 0;
+buffer[1] = 0;
+buffer[2] = 0x80;
+buffer[3] = 0x3f;
+assert.strictEqual(buffer.readFloatBE(0), 4.600602988224807e-41);
+assert.strictEqual(buffer.readFloatLE(0), 1);
+
+buffer[0] = 0;
+buffer[1] = 0;
+buffer[2] = 0;
+buffer[3] = 0xc0;
+assert.strictEqual(buffer.readFloatBE(0), 2.6904930515036488e-43);
+assert.strictEqual(buffer.readFloatLE(0), -2);
+
+buffer[0] = 0xff;
+buffer[1] = 0xff;
+buffer[2] = 0x7f;
+buffer[3] = 0x7f;
+assert.ok(Number.isNaN(buffer.readFloatBE(0)));
+assert.strictEqual(buffer.readFloatLE(0), 3.4028234663852886e+38);
+
+buffer[0] = 0xab;
+buffer[1] = 0xaa;
+buffer[2] = 0xaa;
+buffer[3] = 0x3e;
+assert.strictEqual(buffer.readFloatBE(0), -1.2126478207002966e-12);
+assert.strictEqual(buffer.readFloatLE(0), 0.3333333432674408);
+
+buffer[0] = 0;
+buffer[1] = 0;
+buffer[2] = 0;
+buffer[3] = 0;
+assert.strictEqual(buffer.readFloatBE(0), 0);
+assert.strictEqual(buffer.readFloatLE(0), 0);
+assert.ok(1 / buffer.readFloatLE(0) >= 0);
+
+buffer[3] = 0x80;
+assert.strictEqual(buffer.readFloatBE(0), 1.793662034335766e-43);
+assert.strictEqual(buffer.readFloatLE(0), -0);
+assert.ok(1 / buffer.readFloatLE(0) < 0);
+
+buffer[0] = 0;
+buffer[1] = 0;
+buffer[2] = 0x80;
+buffer[3] = 0x7f;
+assert.strictEqual(buffer.readFloatBE(0), 4.609571298396486e-41);
+assert.strictEqual(buffer.readFloatLE(0), Infinity);
+
+buffer[0] = 0;
+buffer[1] = 0;
+buffer[2] = 0x80;
+buffer[3] = 0xff;
+assert.strictEqual(buffer.readFloatBE(0), 4.627507918739843e-41);
+assert.strictEqual(buffer.readFloatLE(0), -Infinity);
+
+['readFloatLE', 'readFloatBE'].forEach((fn) => {
+ ['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
+ assert.throws(
+ () => buffer[fn](off),
+ { code: 'ERR_INVALID_ARG_TYPE' }
+ );
+ });
+
+ [Infinity, -1, 1].forEach((offset) => {
+ assert.throws(
+ () => buffer[fn](offset),
+ {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError [ERR_OUT_OF_RANGE]',
+ message: 'The value of "offset" is out of range. ' +
+ `It must be >= 0 and <= 0. Received ${offset}`
+ });
+ });
+
+ assert.throws(
+ () => Buffer.alloc(1)[fn](1),
+ {
+ code: 'ERR_BUFFER_OUT_OF_BOUNDS',
+ name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
+ message: 'Attempt to write outside buffer bounds'
+ });
+
+ [NaN, 1.01].forEach((offset) => {
+ assert.throws(
+ () => buffer[fn](offset),
+ {
+ code: 'ERR_OUT_OF_RANGE',
+ name: 'RangeError [ERR_OUT_OF_RANGE]',
+ message: 'The value of "offset" is out of range. ' +
+ `It must be an integer. Received ${offset}`
+ });
+ });
+});