|
@ -4,6 +4,32 @@ var bufferutils = require('../src/bufferutils') |
|
|
var fixtures = require('./fixtures/buffer.json') |
|
|
var fixtures = require('./fixtures/buffer.json') |
|
|
|
|
|
|
|
|
describe('bufferutils', function() { |
|
|
describe('bufferutils', function() { |
|
|
|
|
|
describe('pushDataSize', function() { |
|
|
|
|
|
it('matches test vectors', function() { |
|
|
|
|
|
fixtures.valid.forEach(function(f) { |
|
|
|
|
|
if (!f.hexPD) return |
|
|
|
|
|
|
|
|
|
|
|
var size = bufferutils.pushDataSize(f.dec) |
|
|
|
|
|
|
|
|
|
|
|
assert.equal(size, f.hexPD.length / 2) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('readPushDataInt', function() { |
|
|
|
|
|
it('matches test vectors', function() { |
|
|
|
|
|
fixtures.valid.forEach(function(f) { |
|
|
|
|
|
if (!f.hexPD) return |
|
|
|
|
|
|
|
|
|
|
|
var buffer = new Buffer(f.hexPD, 'hex') |
|
|
|
|
|
var d = bufferutils.readPushDataInt(buffer, 0) |
|
|
|
|
|
|
|
|
|
|
|
assert.equal(d.number, f.dec) |
|
|
|
|
|
assert.equal(d.size, buffer.length) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
describe('readUInt64LE', function() { |
|
|
describe('readUInt64LE', function() { |
|
|
it('matches test vectors', function() { |
|
|
it('matches test vectors', function() { |
|
|
fixtures.valid.forEach(function(f) { |
|
|
fixtures.valid.forEach(function(f) { |
|
@ -30,14 +56,38 @@ describe('bufferutils', function() { |
|
|
describe('varIntSize', function() { |
|
|
describe('varIntSize', function() { |
|
|
it('matches test vectors', function() { |
|
|
it('matches test vectors', function() { |
|
|
fixtures.valid.forEach(function(f) { |
|
|
fixtures.valid.forEach(function(f) { |
|
|
var number = parseInt(f.dec) |
|
|
var size = bufferutils.varIntSize(f.dec) |
|
|
var size = bufferutils.varIntSize(number) |
|
|
|
|
|
|
|
|
|
|
|
assert.equal(size, f.hexVI.length / 2) |
|
|
assert.equal(size, f.hexVI.length / 2) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('writePushDataInt', function() { |
|
|
|
|
|
it('matches test vectors', function() { |
|
|
|
|
|
fixtures.valid.forEach(function(f, i) { |
|
|
|
|
|
if (!f.hexPD) return |
|
|
|
|
|
|
|
|
|
|
|
var buffer = new Buffer(5) |
|
|
|
|
|
buffer.fill(0) |
|
|
|
|
|
|
|
|
|
|
|
var n = bufferutils.writePushDataInt(buffer, f.dec, 0) |
|
|
|
|
|
assert.equal(buffer.slice(0, n).toString('hex'), f.hexPD) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) { |
|
|
|
|
|
it('throws on ' + f.description, function() { |
|
|
|
|
|
var buffer = new Buffer(5) |
|
|
|
|
|
buffer.fill(0) |
|
|
|
|
|
|
|
|
|
|
|
assert.throws(function() { |
|
|
|
|
|
bufferutils.writePushDataInt(buffer, f.dec, 0) |
|
|
|
|
|
}, /value must be < 2\^53/) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
describe('writeUInt64LE', function() { |
|
|
describe('writeUInt64LE', function() { |
|
|
it('matches test vectors', function() { |
|
|
it('matches test vectors', function() { |
|
|
fixtures.valid.forEach(function(f) { |
|
|
fixtures.valid.forEach(function(f) { |
|
@ -51,12 +101,12 @@ describe('bufferutils', function() { |
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) { |
|
|
fixtures.invalid.forEach(function(f) { |
|
|
it('throws on ' + f.description, function() { |
|
|
it('throws on ' + f.description, function() { |
|
|
assert.throws(function() { |
|
|
var buffer = new Buffer(8) |
|
|
var buffer = new Buffer(8) |
|
|
buffer.fill(0) |
|
|
buffer.fill(0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.throws(function() { |
|
|
bufferutils.writeUInt64LE(buffer, f.dec, 0) |
|
|
bufferutils.writeUInt64LE(buffer, f.dec, 0) |
|
|
}) |
|
|
}, /value must be < 2\^53/) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
@ -74,12 +124,12 @@ describe('bufferutils', function() { |
|
|
|
|
|
|
|
|
fixtures.invalid.forEach(function(f) { |
|
|
fixtures.invalid.forEach(function(f) { |
|
|
it('throws on ' + f.description, function() { |
|
|
it('throws on ' + f.description, function() { |
|
|
assert.throws(function() { |
|
|
var buffer = new Buffer(9) |
|
|
var buffer = new Buffer(9) |
|
|
buffer.fill(0) |
|
|
buffer.fill(0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert.throws(function() { |
|
|
bufferutils.writeVarInt(buffer, f.dec, 0) |
|
|
bufferutils.writeVarInt(buffer, f.dec, 0) |
|
|
}) |
|
|
}, /value must be < 2\^53/) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|