1'use strict'; 2 3// Tests to verify floats are correctly written 4 5require('../common'); 6const assert = require('assert'); 7 8const buffer = Buffer.allocUnsafe(8); 9 10buffer.writeFloatBE(1, 0); 11buffer.writeFloatLE(1, 4); 12assert.ok(buffer.equals( 13 new Uint8Array([ 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f ]))); 14 15buffer.writeFloatBE(1 / 3, 0); 16buffer.writeFloatLE(1 / 3, 4); 17assert.ok(buffer.equals( 18 new Uint8Array([ 0x3e, 0xaa, 0xaa, 0xab, 0xab, 0xaa, 0xaa, 0x3e ]))); 19 20buffer.writeFloatBE(3.4028234663852886e+38, 0); 21buffer.writeFloatLE(3.4028234663852886e+38, 4); 22assert.ok(buffer.equals( 23 new Uint8Array([ 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f ]))); 24 25buffer.writeFloatLE(1.1754943508222875e-38, 0); 26buffer.writeFloatBE(1.1754943508222875e-38, 4); 27assert.ok(buffer.equals( 28 new Uint8Array([ 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00 ]))); 29 30buffer.writeFloatBE(0 * -1, 0); 31buffer.writeFloatLE(0 * -1, 4); 32assert.ok(buffer.equals( 33 new Uint8Array([ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 ]))); 34 35buffer.writeFloatBE(Infinity, 0); 36buffer.writeFloatLE(Infinity, 4); 37assert.ok(buffer.equals( 38 new Uint8Array([ 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x7F ]))); 39 40assert.strictEqual(buffer.readFloatBE(0), Infinity); 41assert.strictEqual(buffer.readFloatLE(4), Infinity); 42 43buffer.writeFloatBE(-Infinity, 0); 44buffer.writeFloatLE(-Infinity, 4); 45assert.ok(buffer.equals( 46 new Uint8Array([ 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF ]))); 47 48assert.strictEqual(buffer.readFloatBE(0), -Infinity); 49assert.strictEqual(buffer.readFloatLE(4), -Infinity); 50 51buffer.writeFloatBE(NaN, 0); 52buffer.writeFloatLE(NaN, 4); 53 54// JS only knows a single NaN but there exist two platform specific 55// implementations. Therefore, allow both quiet and signalling NaNs. 56if (buffer[1] === 0xBF) { 57 assert.ok( 58 buffer.equals(new Uint8Array( 59 [ 0x7F, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0x7F ]))); 60} else { 61 assert.ok( 62 buffer.equals(new Uint8Array( 63 [ 0x7F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x7F ]))); 64} 65 66assert.ok(Number.isNaN(buffer.readFloatBE(0))); 67assert.ok(Number.isNaN(buffer.readFloatLE(4))); 68 69// OOB in writeFloat{LE,BE} should throw. 70{ 71 const small = Buffer.allocUnsafe(1); 72 73 ['writeFloatLE', 'writeFloatBE'].forEach((fn) => { 74 75 // Verify that default offset works fine. 76 buffer[fn](23, undefined); 77 buffer[fn](23); 78 79 assert.throws( 80 () => small[fn](11.11, 0), 81 { 82 code: 'ERR_BUFFER_OUT_OF_BOUNDS', 83 name: 'RangeError', 84 message: 'Attempt to access memory outside buffer bounds' 85 }); 86 87 ['', '0', null, {}, [], () => {}, true, false].forEach((off) => { 88 assert.throws( 89 () => small[fn](23, off), 90 { code: 'ERR_INVALID_ARG_TYPE' } 91 ); 92 }); 93 94 [Infinity, -1, 5].forEach((offset) => { 95 assert.throws( 96 () => buffer[fn](23, offset), 97 { 98 code: 'ERR_OUT_OF_RANGE', 99 name: 'RangeError', 100 message: 'The value of "offset" is out of range. ' + 101 `It must be >= 0 and <= 4. Received ${offset}` 102 } 103 ); 104 }); 105 106 [NaN, 1.01].forEach((offset) => { 107 assert.throws( 108 () => buffer[fn](42, offset), 109 { 110 code: 'ERR_OUT_OF_RANGE', 111 name: 'RangeError', 112 message: 'The value of "offset" is out of range. ' + 113 `It must be an integer. Received ${offset}` 114 }); 115 }); 116 }); 117} 118