2020-03-30 23:09:36 +00:00
|
|
|
import { expect } from 'chai';
|
2021-07-25 00:41:28 +00:00
|
|
|
import { array, uint8Array } from 'fast-check';
|
|
|
|
import { over } from 'mocha-foam';
|
2020-03-30 23:09:36 +00:00
|
|
|
|
2020-03-31 13:29:47 +00:00
|
|
|
import { concat, encode } from '../../src/Buffer';
|
2021-08-01 14:47:48 +00:00
|
|
|
import { sum } from '../../src/Math';
|
2020-03-30 23:09:36 +00:00
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
describe('buffer utils', async () => {
|
|
|
|
describe('concat', async () => {
|
|
|
|
it('should append chunk buffers', async () => {
|
2020-03-30 23:09:36 +00:00
|
|
|
expect(concat([
|
|
|
|
Buffer.from('hello'),
|
|
|
|
Buffer.from('world'),
|
|
|
|
])).to.deep.equal(Buffer.from('helloworld'));
|
|
|
|
});
|
2021-07-25 00:41:28 +00:00
|
|
|
|
|
|
|
over('many chunk buffers', array(uint8Array().map((it) => Buffer.from(it.buffer))), (it) => {
|
|
|
|
it('should be the sum of the chunk lengths', (chunks) => {
|
|
|
|
const result = concat(chunks);
|
|
|
|
const total = chunks.map((c) => c.length).reduce(sum, 0);
|
|
|
|
expect(result.length).to.equal(total);
|
|
|
|
});
|
|
|
|
});
|
2020-03-30 23:09:36 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
describe('encode', async () => {
|
|
|
|
it('should encode chunk buffers', async () => {
|
2020-03-30 23:09:36 +00:00
|
|
|
expect(encode([
|
|
|
|
Buffer.from('hello world'),
|
|
|
|
], 'utf-8')).to.equal('hello world');
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should encode no buffers', async () => {
|
2020-03-30 23:09:36 +00:00
|
|
|
expect(encode([], 'utf-8')).to.equal('');
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should encode empty buffers', async () => {
|
2020-03-30 23:09:36 +00:00
|
|
|
expect(encode([
|
|
|
|
new Buffer(0),
|
|
|
|
], 'utf-8')).to.equal('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|