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