From 6b9425fe3783193cf95cb04cbcbcbb97f5a77d31 Mon Sep 17 00:00:00 2001 From: koichik Date: Sun, 2 Sep 2012 00:14:18 +0900 Subject: [PATCH] buffer: added support for writing NaN and Infinity to writeDoubleBE(), writeDoubleLE(), writeFloatBE() and writeFloatLE(). Fixes #3934. --- lib/buffer.js | 10 ++++-- test/simple/test-writedouble.js | 63 +++++++++++++++++++++++++++++++++ test/simple/test-writefloat.js | 39 ++++++++++++++++++++ 3 files changed, 109 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 53f484c56d..cf458b0b2a 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1002,10 +1002,14 @@ function verifsint(value, max, min) { assert.ok(Math.floor(value) === value, 'value has a fractional component'); } -function verifIEEE754(value, max, min) { +function verifyIEEE754(value, max, min) { assert.ok(typeof (value) == 'number', 'cannot write a non-number as a number'); + if (isNaN(value) || value === Infinity || value === -Infinity) { + return; + } + assert.ok(value <= max, 'value larger than maximum allowed value'); assert.ok(value >= min, 'value smaller than minimum allowed value'); @@ -1112,7 +1116,7 @@ function writeFloat(buffer, value, offset, isBigEndian, noAssert) { assert.ok(offset + 3 < buffer.length, 'Trying to write beyond buffer length'); - verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38); + verifyIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38); } require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian, @@ -1141,7 +1145,7 @@ function writeDouble(buffer, value, offset, isBigEndian, noAssert) { assert.ok(offset + 7 < buffer.length, 'Trying to write beyond buffer length'); - verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308); + verifyIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308); } require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian, diff --git a/test/simple/test-writedouble.js b/test/simple/test-writedouble.js index 829ba29a73..58c88272c7 100644 --- a/test/simple/test-writedouble.js +++ b/test/simple/test-writedouble.js @@ -123,6 +123,69 @@ function test(clazz) { ASSERT.equal(0x00, buffer[13]); ASSERT.equal(0x00, buffer[14]); ASSERT.equal(0x80, buffer[15]); + + buffer.writeDoubleBE(Infinity, 0); + buffer.writeDoubleLE(Infinity, 8); + ASSERT.equal(0x7F, buffer[0]); + ASSERT.equal(0xF0, buffer[1]); + ASSERT.equal(0x00, buffer[2]); + ASSERT.equal(0x00, buffer[3]); + ASSERT.equal(0x00, buffer[4]); + ASSERT.equal(0x00, buffer[5]); + ASSERT.equal(0x00, buffer[6]); + ASSERT.equal(0x00, buffer[7]); + ASSERT.equal(0x00, buffer[8]); + ASSERT.equal(0x00, buffer[9]); + ASSERT.equal(0x00, buffer[10]); + ASSERT.equal(0x00, buffer[11]); + ASSERT.equal(0x00, buffer[12]); + ASSERT.equal(0x00, buffer[13]); + ASSERT.equal(0xF0, buffer[14]); + ASSERT.equal(0x7F, buffer[15]); + ASSERT.equal(Infinity, buffer.readDoubleBE(0)); + ASSERT.equal(Infinity, buffer.readDoubleLE(8)); + + buffer.writeDoubleBE(-Infinity, 0); + buffer.writeDoubleLE(-Infinity, 8); + ASSERT.equal(0xFF, buffer[0]); + ASSERT.equal(0xF0, buffer[1]); + ASSERT.equal(0x00, buffer[2]); + ASSERT.equal(0x00, buffer[3]); + ASSERT.equal(0x00, buffer[4]); + ASSERT.equal(0x00, buffer[5]); + ASSERT.equal(0x00, buffer[6]); + ASSERT.equal(0x00, buffer[7]); + ASSERT.equal(0x00, buffer[8]); + ASSERT.equal(0x00, buffer[9]); + ASSERT.equal(0x00, buffer[10]); + ASSERT.equal(0x00, buffer[11]); + ASSERT.equal(0x00, buffer[12]); + ASSERT.equal(0x00, buffer[13]); + ASSERT.equal(0xF0, buffer[14]); + ASSERT.equal(0xFF, buffer[15]); + ASSERT.equal(-Infinity, buffer.readDoubleBE(0)); + ASSERT.equal(-Infinity, buffer.readDoubleLE(8)); + + buffer.writeDoubleBE(NaN, 0); + buffer.writeDoubleLE(NaN, 8); + ASSERT.equal(0x7F, buffer[0]); + ASSERT.equal(0xF0, buffer[1]); + ASSERT.equal(0x00, buffer[2]); + ASSERT.equal(0x00, buffer[3]); + ASSERT.equal(0x00, buffer[4]); + ASSERT.equal(0x00, buffer[5]); + ASSERT.equal(0x00, buffer[6]); + ASSERT.equal(0x01, buffer[7]); + ASSERT.equal(0x01, buffer[8]); + ASSERT.equal(0x00, buffer[9]); + ASSERT.equal(0x00, buffer[10]); + ASSERT.equal(0x00, buffer[11]); + ASSERT.equal(0x00, buffer[12]); + ASSERT.equal(0x00, buffer[13]); + ASSERT.equal(0xF0, buffer[14]); + ASSERT.equal(0x7F, buffer[15]); + ASSERT.ok(isNaN(buffer.readDoubleBE(0))); + ASSERT.ok(isNaN(buffer.readDoubleLE(8))); } diff --git a/test/simple/test-writefloat.js b/test/simple/test-writefloat.js index d10cb7547b..b160d1290d 100644 --- a/test/simple/test-writefloat.js +++ b/test/simple/test-writefloat.js @@ -83,6 +83,45 @@ function test(clazz) { ASSERT.equal(0x00, buffer[5]); ASSERT.equal(0x00, buffer[6]); ASSERT.equal(0x80, buffer[7]); + + buffer.writeFloatBE(Infinity, 0); + buffer.writeFloatLE(Infinity, 4); + ASSERT.equal(0x7F, buffer[0]); + ASSERT.equal(0x80, buffer[1]); + ASSERT.equal(0x00, buffer[2]); + ASSERT.equal(0x00, buffer[3]); + ASSERT.equal(0x00, buffer[4]); + ASSERT.equal(0x00, buffer[5]); + ASSERT.equal(0x80, buffer[6]); + ASSERT.equal(0x7F, buffer[7]); + ASSERT.equal(Infinity, buffer.readFloatBE(0)); + ASSERT.equal(Infinity, buffer.readFloatLE(4)); + + buffer.writeFloatBE(-Infinity, 0); + buffer.writeFloatLE(-Infinity, 4); + ASSERT.equal(0xFF, buffer[0]); + ASSERT.equal(0x80, buffer[1]); + ASSERT.equal(0x00, buffer[2]); + ASSERT.equal(0x00, buffer[3]); + ASSERT.equal(0x00, buffer[4]); + ASSERT.equal(0x00, buffer[5]); + ASSERT.equal(0x80, buffer[6]); + ASSERT.equal(0xFF, buffer[7]); + ASSERT.equal(-Infinity, buffer.readFloatBE(0)); + ASSERT.equal(-Infinity, buffer.readFloatLE(4)); + + buffer.writeFloatBE(NaN, 0); + buffer.writeFloatLE(NaN, 4); + ASSERT.equal(0x7F, buffer[0]); + ASSERT.equal(0x80, buffer[1]); + ASSERT.equal(0x00, buffer[2]); + ASSERT.equal(0x01, buffer[3]); + ASSERT.equal(0x01, buffer[4]); + ASSERT.equal(0x00, buffer[5]); + ASSERT.equal(0x80, buffer[6]); + ASSERT.equal(0x7F, buffer[7]); + ASSERT.ok(isNaN(buffer.readFloatBE(0))); + ASSERT.ok(isNaN(buffer.readFloatLE(4))); }