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
27 changes: 0 additions & 27 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeReduce,
ArrayPrototypeSlice,
ArrayPrototypeSome,
JSONParse,
MathFloor,
Expand Down Expand Up @@ -166,31 +165,6 @@ function createSeededGenerator(seed) {
};
}

/**
* Return a deterministically shuffled copy of an array.
* @template T
* @param {T[]} values
* @param {number} seed
* @returns {T[]}
*/
function shuffleArrayWithSeed(values, seed) {
if (values.length < 2) {
return values;
}

const randomized = ArrayPrototypeSlice(values);
const random = createSeededGenerator(seed);

for (let i = randomized.length - 1; i > 0; i--) {
const j = MathFloor(random() * (i + 1));
const tmp = randomized[i];
randomized[i] = randomized[j];
randomized[j] = tmp;
}

return randomized;
}

function tryBuiltinReporter(name) {
const builtinPath = kBuiltinReporters.get(name);

Expand Down Expand Up @@ -761,7 +735,6 @@ module.exports = {
kDefaultPattern,
kMaxRandomSeed,
parseCommandLine,
shuffleArrayWithSeed,
reporterScope,
shouldColorizeTestFiles,
getCoverageReport,
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-runner-seeded-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('node:assert');
const { createSeededGenerator, kMaxRandomSeed } = require('internal/test_runner/utils');

function sequence(seed, length = 10) {
const random = createSeededGenerator(seed);
const values = [];
for (let i = 0; i < length; i++) {
values.push(random());
}
return values;
}

// The same seed must always produce the same sequence so that
// --test-random-seed reproduces a randomized test order across runs.
assert.deepStrictEqual(sequence(0), sequence(0));
assert.deepStrictEqual(sequence(12345), sequence(12345));
assert.deepStrictEqual(sequence(kMaxRandomSeed), sequence(kMaxRandomSeed));

// Different seeds must produce different sequences.
assert.notDeepStrictEqual(sequence(11111), sequence(22222));
assert.notDeepStrictEqual(sequence(0), sequence(1));

// Seeds are coerced to uint32, matching the range accepted by
// --test-random-seed.
assert.deepStrictEqual(sequence(1), sequence(1 + 2 ** 32));

// All generated values must fall in [0, 1).
for (const value of sequence(98765, 1000)) {
assert.ok(value >= 0 && value < 1, `value ${value} out of range`);
}
Loading