node/test/parallel/test-fixed-queue.js
Jason Zhang 981c701400
lib: ensure no holey array in fixed_queue
Co-authored-by: Jake Yuesong Li <jake.yuesong@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/54537
Fixes: https://github.com/nodejs/node/issues/54472
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
2024-09-02 01:42:44 +00:00

47 lines
1.1 KiB
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const FixedQueue = require('internal/fixed_queue');
{
const queue = new FixedQueue();
assert.strictEqual(queue.head, queue.tail);
assert(queue.isEmpty());
queue.push('a');
assert(!queue.isEmpty());
assert.strictEqual(queue.shift(), 'a');
assert.strictEqual(queue.shift(), null);
}
{
const queue = new FixedQueue();
for (let i = 0; i < 2047; i++)
queue.push('a');
assert(queue.head.isFull());
queue.push('a');
assert(!queue.head.isFull());
assert.notStrictEqual(queue.head, queue.tail);
for (let i = 0; i < 2047; i++)
assert.strictEqual(queue.shift(), 'a');
assert.strictEqual(queue.head, queue.tail);
assert(!queue.isEmpty());
assert.strictEqual(queue.shift(), 'a');
assert(queue.isEmpty());
}
{
// FixedQueue must not be holey array
// Refs: https://github.com/nodejs/node/issues/54472
const queue = new FixedQueue();
for (let i = 0; i < queue.head.list.length; i++) {
assert(i in queue.head.list);
}
for (let i = 0; i < queue.tail.list.length; i++) {
assert(i in queue.tail.list);
}
}