-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestBuffer.ts
43 lines (37 loc) · 1.24 KB
/
TestBuffer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { expect } from 'chai';
import { array, uint8Array } from 'fast-check';
import { over } from 'mocha-foam';
import { concat, encode } from '../../src/Buffer.js';
import { sum } from '../../src/Math.js';
describe('buffer utils', async () => {
describe('concat', async () => {
it('should append chunk buffers', async () => {
expect(concat([
Buffer.from('hello'),
Buffer.from('world'),
])).to.deep.equal(Buffer.from('helloworld'));
});
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);
});
});
});
describe('encode', async () => {
it('should encode chunk buffers', async () => {
expect(encode([
Buffer.from('hello world'),
], 'utf-8')).to.equal('hello world');
});
it('should encode no buffers', async () => {
expect(encode([], 'utf-8')).to.equal('');
});
it('should encode empty buffers', async () => {
expect(encode([
new Buffer(0),
], 'utf-8')).to.equal('');
});
});
});