Browse Source

BufferReader.prototype.set

patch-2
Ryan X. Charles 10 years ago
parent
commit
3475ee973c
  1. 16
      lib/bufferreader.js
  2. 52
      test/test.bufferreader.js
  3. 2
      test/test.bufferwriter.js

16
lib/bufferreader.js

@ -1,10 +1,16 @@
var BN = require('./bn');
var BufferReader = function BufferReader(buf, pos) {
var BufferReader = function BufferReader(obj) {
if (!(this instanceof BufferReader))
return new BufferReader(buf);
this.buf = buf;
this.pos = pos || 0;
return new BufferReader(obj);
if (obj)
this.set(obj);
};
BufferReader.prototype.set = function(obj) {
this.buf = obj.buf || this.buf || undefined;
this.pos = obj.pos || this.pos || 0;
return this;
};
BufferReader.prototype.eof = function eof() {
@ -56,7 +62,7 @@ BufferReader.prototype.readUInt64BEBN = function() {
BufferReader.prototype.readUInt64LEBN = function() {
var buf = this.buf.slice(this.pos, this.pos + 8);
var reversebuf = BufferReader(buf).reverse().read();
var reversebuf = BufferReader({buf: buf}).reverse().read();
var bn = BN().fromBuffer(reversebuf);
this.pos = this.pos + 8;
return bn;

52
test/test.bufferreader.js

@ -9,10 +9,18 @@ describe('BufferReader', function() {
should.exist(br);
});
describe('#set', function() {
it('should set pos', function() {
should.exist(BufferReader().set({pos: 1}).pos);
});
});
describe('#eof', function() {
it('should return true for a blank br', function() {
var br = new BufferReader(new Buffer([]));
var br = new BufferReader({buf: new Buffer([])});
br.eof().should.equal(true);
});
@ -22,7 +30,7 @@ describe('BufferReader', function() {
it('should return the same buffer', function() {
var buf = new Buffer([0]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.read().toString('hex').should.equal(buf.toString('hex'));
});
@ -33,7 +41,7 @@ describe('BufferReader', function() {
it('should return 1', function() {
var buf = new Buffer(1);
buf.writeUInt8(1, 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt8().should.equal(1);
});
@ -44,7 +52,7 @@ describe('BufferReader', function() {
it('should return 1', function() {
var buf = new Buffer(2);
buf.writeUInt16BE(1, 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt16BE().should.equal(1);
});
@ -55,7 +63,7 @@ describe('BufferReader', function() {
it('should return 1', function() {
var buf = new Buffer(2);
buf.writeUInt16LE(1, 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt16LE().should.equal(1);
});
@ -66,7 +74,7 @@ describe('BufferReader', function() {
it('should return 1', function() {
var buf = new Buffer(4);
buf.writeUInt32BE(1, 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt32BE().should.equal(1);
});
@ -77,7 +85,7 @@ describe('BufferReader', function() {
it('should return 1', function() {
var buf = new Buffer(4);
buf.writeUInt32LE(1, 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt32LE().should.equal(1);
});
@ -89,14 +97,14 @@ describe('BufferReader', function() {
var buf = new Buffer(8);
buf.fill(0);
buf.writeUInt32BE(1, 4);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt64BEBN().toNumber().should.equal(1);
});
it('should return 2^64', function() {
var buf = new Buffer(8);
buf.fill(0xff);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt64BEBN().toNumber().should.equal(Math.pow(2, 64));
});
@ -108,7 +116,7 @@ describe('BufferReader', function() {
var buf = new Buffer(8);
buf.fill(0);
buf.writeUInt32LE(1, 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt64LEBN().toNumber().should.equal(1);
});
@ -116,21 +124,21 @@ describe('BufferReader', function() {
var buf = new Buffer(8);
buf.fill(0);
buf.writeUInt32LE(Math.pow(2, 30), 0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt64LEBN().toNumber().should.equal(Math.pow(2, 30));
});
it('should return 0', function() {
var buf = new Buffer(8);
buf.fill(0);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt64LEBN().toNumber().should.equal(0);
});
it('should return 2^64', function() {
var buf = new Buffer(8);
buf.fill(0xff);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readUInt64LEBN().toNumber().should.equal(Math.pow(2, 64));
});
@ -140,26 +148,26 @@ describe('BufferReader', function() {
it('should read a 1 byte varint', function() {
var buf = new Buffer([50]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarInt().should.equal(50);
});
it('should read a 3 byte varint', function() {
var buf = new Buffer([253, 253, 0]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarInt().should.equal(253);
});
it('should read a 5 byte varint', function() {
var buf = new Buffer([254, 0, 0, 0, 0]);
buf.writeUInt32LE(50000, 1);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarInt().should.equal(50000);
});
it('should throw an error on a 9 byte varint', function() {
var buf = Buffer.concat([new Buffer([255]), new Buffer('ffffffffffffffff', 'hex')]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
(function() {
br.readVarInt();
}).should.throw('number too large to retain precision - use readVarIntBN');
@ -171,26 +179,26 @@ describe('BufferReader', function() {
it('should read a 1 byte varint', function() {
var buf = new Buffer([50]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarIntBN().toNumber().should.equal(50);
});
it('should read a 3 byte varint', function() {
var buf = new Buffer([253, 253, 0]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarIntBN().toNumber().should.equal(253);
});
it('should read a 5 byte varint', function() {
var buf = new Buffer([254, 0, 0, 0, 0]);
buf.writeUInt32LE(50000, 1);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarIntBN().toNumber().should.equal(50000);
});
it('should read a 9 byte varint', function() {
var buf = Buffer.concat([new Buffer([255]), new Buffer('ffffffffffffffff', 'hex')]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.readVarIntBN().toNumber().should.equal(Math.pow(2, 64));
});
@ -200,7 +208,7 @@ describe('BufferReader', function() {
it('should reverse this [0, 1]', function() {
var buf = new Buffer([0, 1]);
var br = new BufferReader(buf);
var br = new BufferReader({buf: buf});
br.reverse().read().toString('hex').should.equal('0100');
});

2
test/test.bufferwriter.js

@ -126,7 +126,7 @@ describe('BufferWriter', function() {
var n = Math.pow(2, 53);
n.should.equal(n + 1); //javascript number precision limit
bw.writeVarInt(n);
var br = new BufferReader(bw.concat());
var br = new BufferReader({buf: bw.concat()});
br.readVarIntBN().toNumber().should.equal(n);
});

Loading…
Cancel
Save