Example #1
0
exports.testWriteFloat = helper('writeFloatBE/writeFloatLE', function (assert) {
  var buffer = new Buffer(8);

  buffer.writeFloatBE(1, 0);
  buffer.writeFloatLE(1, 4);
  assert.equal(0x3f, 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(0x3f, buffer[7]);

  buffer.writeFloatBE(1 / 3, 0);
  buffer.writeFloatLE(1 / 3, 4);
  assert.equal(0x3e, buffer[0]);
  assert.equal(0xaa, buffer[1]);
  assert.equal(0xaa, buffer[2]);
  assert.equal(0xab, buffer[3]);
  assert.equal(0xab, buffer[4]);
  assert.equal(0xaa, buffer[5]);
  assert.equal(0xaa, buffer[6]);
  assert.equal(0x3e, buffer[7]);

  buffer.writeFloatBE(3.4028234663852886e+38, 0);
  buffer.writeFloatLE(3.4028234663852886e+38, 4);
  assert.equal(0x7f, buffer[0]);
  assert.equal(0x7f, buffer[1]);
  assert.equal(0xff, buffer[2]);
  assert.equal(0xff, buffer[3]);
  assert.equal(0xff, buffer[4]);
  assert.equal(0xff, buffer[5]);
  assert.equal(0x7f, buffer[6]);
  assert.equal(0x7f, buffer[7]);

  buffer.writeFloatLE(1.1754943508222875e-38, 0);
  buffer.writeFloatBE(1.1754943508222875e-38, 4);
  assert.equal(0x00, buffer[0]);
  assert.equal(0x00, buffer[1]);
  assert.equal(0x80, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x80, buffer[5]);
  assert.equal(0x00, buffer[6]);
  assert.equal(0x00, buffer[7]);

  buffer.writeFloatBE(0 * -1, 0);
  buffer.writeFloatLE(0 * -1, 4);
  assert.equal(0x80, buffer[0]);
  assert.equal(0x00, 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(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);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0xFF === buffer[0] || 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(0xFF, buffer[7]);
  assert.equal(-Infinity, buffer.readFloatBE(0));
  assert.equal(-Infinity, buffer.readFloatLE(4));

  buffer.writeFloatBE(NaN, 0);
  buffer.writeFloatLE(NaN, 4);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
  assert.equal(0xc0, buffer[1]);
  assert.equal(0x00, buffer[2]);
  assert.equal(0x00, buffer[3]);
  assert.equal(0x00, buffer[4]);
  assert.equal(0x00, buffer[5]);
  assert.equal(0xc0, buffer[6]);
  // Darwin ia32 does the other kind of NaN.
  // Compiler bug.  No one really cares.
  assert.ok(0x7F === buffer[7] || 0xFF === buffer[7]);
  assert.ok(isNaN(buffer.readFloatBE(0)));
  assert.ok(isNaN(buffer.readFloatLE(4)));
});