|
|
@ -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'); |
|
|
|
}); |
|
|
|
|
|
|
|