2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { describe, it } from 'mocha';
|
|
|
|
import * as bufferutils from '../src/bufferutils';
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
import * as fixtures from './fixtures/bufferutils.json';
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('bufferutils', () => {
|
|
|
|
describe('readUInt64LE', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('decodes ' + f.hex, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const buffer = Buffer.from(f.hex, 'hex');
|
2019-09-12 10:35:08 +02:00
|
|
|
const num = bufferutils.readUInt64LE(buffer, 0);
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-09-12 10:35:08 +02:00
|
|
|
assert.strictEqual(num, f.dec);
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
2014-05-31 05:18:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.readUInt64LE.forEach(f => {
|
|
|
|
it('throws on ' + f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const buffer = Buffer.from(f.hex, 'hex');
|
2014-05-31 05:18:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
bufferutils.readUInt64LE(buffer, 0);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('writeUInt64LE', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('encodes ' + f.dec, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const buffer = Buffer.alloc(8, 0);
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
bufferutils.writeUInt64LE(buffer, f.dec, 0);
|
|
|
|
assert.strictEqual(buffer.toString('hex'), f.hex);
|
|
|
|
});
|
|
|
|
});
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.readUInt64LE.forEach(f => {
|
|
|
|
it('throws on ' + f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const buffer = Buffer.alloc(8, 0);
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
bufferutils.writeUInt64LE(buffer, f.dec, 0);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|