bitcoinjs-lib/test/bufferutils.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-07-23 09:45:01 +02:00
const { describe, it } = require('mocha')
2018-06-25 08:25:12 +02:00
const assert = require('assert')
const bufferutils = require('../src/bufferutils')
2014-05-13 08:35:07 +02:00
2018-06-25 08:25:12 +02:00
const fixtures = require('./fixtures/bufferutils.json')
2014-05-13 08:35:07 +02:00
describe('bufferutils', () => {
describe('readUInt64LE', () => {
fixtures.valid.forEach(f => {
it('decodes ' + f.hex, () => {
const buffer = Buffer.from(f.hex, 'hex')
const number = bufferutils.readUInt64LE(buffer, 0)
2014-05-13 08:35:07 +02:00
2015-05-07 03:29:20 +02:00
assert.strictEqual(number, f.dec)
2014-05-13 08:35:07 +02:00
})
})
fixtures.invalid.readUInt64LE.forEach(f => {
it('throws on ' + f.description, () => {
const buffer = Buffer.from(f.hex, 'hex')
assert.throws(() => {
bufferutils.readUInt64LE(buffer, 0)
}, new RegExp(f.exception))
})
})
2014-05-13 08:35:07 +02:00
})
describe('writeUInt64LE', () => {
fixtures.valid.forEach(f => {
it('encodes ' + f.dec, () => {
const buffer = Buffer.alloc(8, 0)
2014-05-13 08:35:07 +02:00
bufferutils.writeUInt64LE(buffer, f.dec, 0)
2018-04-13 17:07:10 +02:00
assert.strictEqual(buffer.toString('hex'), f.hex)
2014-05-13 08:35:07 +02:00
})
})
fixtures.invalid.readUInt64LE.forEach(f => {
it('throws on ' + f.description, () => {
const buffer = Buffer.alloc(8, 0)
2014-05-13 08:35:07 +02:00
assert.throws(() => {
2014-05-13 08:35:07 +02:00
bufferutils.writeUInt64LE(buffer, f.dec, 0)
}, new RegExp(f.exception))
2014-05-13 08:35:07 +02:00
})
})
})
})