• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5
6// Test (64 bit) double
7const buffer = Buffer.allocUnsafe(8);
8
9buffer[0] = 0x55;
10buffer[1] = 0x55;
11buffer[2] = 0x55;
12buffer[3] = 0x55;
13buffer[4] = 0x55;
14buffer[5] = 0x55;
15buffer[6] = 0xd5;
16buffer[7] = 0x3f;
17assert.strictEqual(buffer.readDoubleBE(0), 1.1945305291680097e+103);
18assert.strictEqual(buffer.readDoubleLE(0), 0.3333333333333333);
19
20buffer[0] = 1;
21buffer[1] = 0;
22buffer[2] = 0;
23buffer[3] = 0;
24buffer[4] = 0;
25buffer[5] = 0;
26buffer[6] = 0xf0;
27buffer[7] = 0x3f;
28assert.strictEqual(buffer.readDoubleBE(0), 7.291122019655968e-304);
29assert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000002);
30
31buffer[0] = 2;
32assert.strictEqual(buffer.readDoubleBE(0), 4.778309726801735e-299);
33assert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000004);
34
35buffer[0] = 1;
36buffer[6] = 0;
37buffer[7] = 0;
38assert.strictEqual(buffer.readDoubleBE(0), 7.291122019556398e-304);
39assert.strictEqual(buffer.readDoubleLE(0), 5e-324);
40
41buffer[0] = 0xff;
42buffer[1] = 0xff;
43buffer[2] = 0xff;
44buffer[3] = 0xff;
45buffer[4] = 0xff;
46buffer[5] = 0xff;
47buffer[6] = 0x0f;
48buffer[7] = 0x00;
49assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
50assert.strictEqual(buffer.readDoubleLE(0), 2.225073858507201e-308);
51
52buffer[6] = 0xef;
53buffer[7] = 0x7f;
54assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
55assert.strictEqual(buffer.readDoubleLE(0), 1.7976931348623157e+308);
56
57buffer[0] = 0;
58buffer[1] = 0;
59buffer[2] = 0;
60buffer[3] = 0;
61buffer[4] = 0;
62buffer[5] = 0;
63buffer[6] = 0xf0;
64buffer[7] = 0x3f;
65assert.strictEqual(buffer.readDoubleBE(0), 3.03865e-319);
66assert.strictEqual(buffer.readDoubleLE(0), 1);
67
68buffer[6] = 0;
69buffer[7] = 0x40;
70assert.strictEqual(buffer.readDoubleBE(0), 3.16e-322);
71assert.strictEqual(buffer.readDoubleLE(0), 2);
72
73buffer[7] = 0xc0;
74assert.strictEqual(buffer.readDoubleBE(0), 9.5e-322);
75assert.strictEqual(buffer.readDoubleLE(0), -2);
76
77buffer[6] = 0x10;
78buffer[7] = 0;
79assert.strictEqual(buffer.readDoubleBE(0), 2.0237e-320);
80assert.strictEqual(buffer.readDoubleLE(0), 2.2250738585072014e-308);
81
82buffer[6] = 0;
83assert.strictEqual(buffer.readDoubleBE(0), 0);
84assert.strictEqual(buffer.readDoubleLE(0), 0);
85assert.ok(1 / buffer.readDoubleLE(0) >= 0);
86
87buffer[7] = 0x80;
88assert.strictEqual(buffer.readDoubleBE(0), 6.3e-322);
89assert.strictEqual(buffer.readDoubleLE(0), -0);
90assert.ok(1 / buffer.readDoubleLE(0) < 0);
91
92buffer[6] = 0xf0;
93buffer[7] = 0x7f;
94assert.strictEqual(buffer.readDoubleBE(0), 3.0418e-319);
95assert.strictEqual(buffer.readDoubleLE(0), Infinity);
96
97buffer[7] = 0xff;
98assert.strictEqual(buffer.readDoubleBE(0), 3.04814e-319);
99assert.strictEqual(buffer.readDoubleLE(0), -Infinity);
100
101['readDoubleLE', 'readDoubleBE'].forEach((fn) => {
102
103  // Verify that default offset works fine.
104  buffer[fn](undefined);
105  buffer[fn]();
106
107  ['', '0', null, {}, [], () => {}, true, false].forEach((off) => {
108    assert.throws(
109      () => buffer[fn](off),
110      { code: 'ERR_INVALID_ARG_TYPE' }
111    );
112  });
113
114  [Infinity, -1, 1].forEach((offset) => {
115    assert.throws(
116      () => buffer[fn](offset),
117      {
118        code: 'ERR_OUT_OF_RANGE',
119        name: 'RangeError',
120        message: 'The value of "offset" is out of range. ' +
121                 `It must be >= 0 and <= 0. Received ${offset}`
122      });
123  });
124
125  assert.throws(
126    () => Buffer.alloc(1)[fn](1),
127    {
128      code: 'ERR_BUFFER_OUT_OF_BOUNDS',
129      name: 'RangeError',
130      message: 'Attempt to access memory outside buffer bounds'
131    });
132
133  [NaN, 1.01].forEach((offset) => {
134    assert.throws(
135      () => buffer[fn](offset),
136      {
137        code: 'ERR_OUT_OF_RANGE',
138        name: 'RangeError',
139        message: 'The value of "offset" is out of range. ' +
140                 `It must be an integer. Received ${offset}`
141      });
142  });
143});
144