Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class FlatQueue {
this.values = fixed ? /** @type {number[]} */ (/** @type {unknown} */ (new ValuesArray(capacity))) : [];

/** Maximum number of items the queue can hold; `Infinity` for regular-array queues, which grow on demand. */
this.capacity = capacity;
this.capacity = fixed ? this.ids.length : Infinity;

/** Number of items in the queue. */
this.length = 0;
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import assert from 'node:assert/strict';

import FlatQueue from './index.js';

test('does not push beyond the storage allocated for a fractional capacity', () => {
for (const capacity of [1, 1.5, 1.9]) {
const queue = new FlatQueue(capacity);
queue.push(42, 1);
assert.throws(() => queue.push(43, 0), RangeError);
assert.equal(queue.capacity, 1);
assert.equal(queue.length, 1);
assert.equal(queue.pop(), 42);
assert.equal(queue.pop(), undefined);
}
});

test('does not push when the allocated capacity is zero', () => {
for (const capacity of [0, 0.5]) {
const queue = new FlatQueue(capacity);
assert.throws(() => queue.push(42, 1), RangeError);
assert.equal(queue.capacity, 0);
assert.equal(queue.length, 0);
}
});

const data = [];
for (let i = 0; i < 100; i++) {
data.push(Math.floor(100 * Math.random()));
Expand Down