Skip to content

Commit 9949e1a

Browse files
committed
test: add test cases for resize factor.
1 parent 1cb189c commit 9949e1a

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

test/dynamicBuffer.spec.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describe('Initialization tests', () => {
3030
it('Test initializing with a size large than buffer.constants.MAX_LENGTH', () => {
3131
assert.throws(() => new DynamicBuffer({ size: constants.MAX_LENGTH + 1 }));
3232
});
33+
34+
it("Test initializing with invalid factor", () => {
35+
assert.throws(() => {
36+
new DynamicBuffer({
37+
factor: -1,
38+
});
39+
});
40+
});
3341
});
3442

3543
describe('Resize tests', () => {
@@ -46,10 +54,12 @@ describe('Resize tests', () => {
4654
const buffer = new DynamicBuffer({
4755
size: 2,
4856
});
57+
const str = 'Hello world';
4958

50-
buffer.append('Hello world');
59+
buffer.append(str);
5160

52-
assert.equal(buffer.toString(), 'Hello world');
61+
assert.equal(buffer.toString(), str);
62+
assert.equal(Reflect.get(buffer, 'size'), str.length);
5363
});
5464

5565
it('Test resize again', () => {
@@ -60,4 +70,27 @@ describe('Resize tests', () => {
6070

6171
assert.equal(buffer.toString(), 'Hello world!\nThis is DynamicBuffer.');
6272
});
73+
74+
it('Test resize with default factor', () => {
75+
const buffer = new DynamicBuffer({
76+
size: 10,
77+
});
78+
const str = 'Hello world';
79+
80+
buffer.append(str);
81+
82+
assert.equal(Reflect.get(buffer, 'size'), Math.ceil(10 * (1 + Reflect.get(buffer, 'DefaultResizeFactor'))));
83+
});
84+
85+
it('Test resize with custom factor', () => {
86+
const buffer = new DynamicBuffer({
87+
size: 10,
88+
factor: 2, // (1+2)x
89+
});
90+
const str = 'Hello world';
91+
92+
buffer.append(str);
93+
94+
assert.equal(Reflect.get(buffer, 'size'), Math.ceil(10 * (1 + 2)));
95+
});
6396
});

0 commit comments

Comments
 (0)