Example #1
0
 it('returns a JSON object of the provided buffer.', function () {
   var array = new Array(255);
   for (var i = 0; i < 255; i++) {
     array[i] = i;
   }
   var buffer = new Buffer(array);
   assert.deepEqual(buffer.toJSON(), array);
 });
Example #2
0
 it('constructs a new buffer with a size.', function () {
   var buffer = new Buffer(10);
   assert.deepEqual(buffer.toJSON(), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
 });
Example #3
0
 it('constructs a new buffer with a string with an encoding.', function () {
   var buffer1 = new Buffer('안녕, 세계!', 'UTF-8');
   assert.deepEqual(buffer1.toJSON(), [236, 149, 136, 235, 133, 149, 44, 32, 236, 132, 184, 234, 179, 132, 33]);
   var buffer2 = new Buffer('안녕, 세계!', 'EUC-KR');
   assert.deepEqual(buffer2.toJSON(), [190, 200, 179, 231, 44, 32, 188, 188, 176, 232, 33]);
 });
Example #4
0
 it('constructs a new buffer with a string.', function () {
   var buffer = new Buffer('Hello, world!');
   assert.deepEqual(buffer.toJSON(), [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]);
 });
Example #5
0
 it('constructs a new buffer with a byte array.', function () {
   var buffer = new Buffer([0x01, 0x30, 0x05, 0x70, 0x09, 0xa0, 0x0c, 0xe0, 0x0f]);
   assert.deepEqual(buffer.toJSON(), [1, 48, 5, 112, 9, 160, 12, 224, 15]);
 });
Example #6
0
(function() {
  var buf = new Buffer(3);
  buf.writeUIntLE(0x123456, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
  assert.equal(buf.readUIntLE(0, 3), 0x123456);

  buf = new Buffer(3);
  buf.writeUIntBE(0x123456, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
  assert.equal(buf.readUIntBE(0, 3), 0x123456);

  buf = new Buffer(3);
  buf.writeIntLE(0x123456, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
  assert.equal(buf.readIntLE(0, 3), 0x123456);

  buf = new Buffer(3);
  buf.writeIntBE(0x123456, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
  assert.equal(buf.readIntBE(0, 3), 0x123456);

  buf = new Buffer(3);
  buf.writeIntLE(-0x123456, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);
  assert.equal(buf.readIntLE(0, 3), -0x123456);

  buf = new Buffer(3);
  buf.writeIntBE(-0x123456, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);
  assert.equal(buf.readIntBE(0, 3), -0x123456);

  buf = new Buffer(3);
  buf.writeIntLE(-0x123400, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
  assert.equal(buf.readIntLE(0, 3), -0x123400);

  buf = new Buffer(3);
  buf.writeIntBE(-0x123400, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
  assert.equal(buf.readIntBE(0, 3), -0x123400);

  buf = new Buffer(3);
  buf.writeIntLE(-0x120000, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
  assert.equal(buf.readIntLE(0, 3), -0x120000);

  buf = new Buffer(3);
  buf.writeIntBE(-0x120000, 0, 3);
  assert.deepEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
  assert.equal(buf.readIntBE(0, 3), -0x120000);

  buf = new Buffer(5);
  buf.writeUIntLE(0x1234567890, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
  assert.equal(buf.readUIntLE(0, 5), 0x1234567890);

  buf = new Buffer(5);
  buf.writeUIntBE(0x1234567890, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
  assert.equal(buf.readUIntBE(0, 5), 0x1234567890);

  buf = new Buffer(5);
  buf.writeIntLE(0x1234567890, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
  assert.equal(buf.readIntLE(0, 5), 0x1234567890);

  buf = new Buffer(5);
  buf.writeIntBE(0x1234567890, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
  assert.equal(buf.readIntBE(0, 5), 0x1234567890);

  buf = new Buffer(5);
  buf.writeIntLE(-0x1234567890, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);
  assert.equal(buf.readIntLE(0, 5), -0x1234567890);

  buf = new Buffer(5);
  buf.writeIntBE(-0x1234567890, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
  assert.equal(buf.readIntBE(0, 5), -0x1234567890);

  buf = new Buffer(5);
  buf.writeIntLE(-0x0012000000, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
  assert.equal(buf.readIntLE(0, 5), -0x0012000000);

  buf = new Buffer(5);
  buf.writeIntBE(-0x0012000000, 0, 5);
  assert.deepEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
  assert.equal(buf.readIntBE(0, 5), -0x0012000000);
})();