Skip to content

Commit 9485caa

Browse files
watildeaduh95
authored andcommitted
test_runner: remove unused shuffleArrayWithSeed
shuffleArrayWithSeed was introduced along with test order randomization but was never called: the feature picks a random pending subtest at dequeue time using createSeededGenerator directly. Remove the dead helper and its export, and add direct unit coverage for createSeededGenerator's deterministic contract. Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com> PR-URL: #63847 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 509cd1b commit 9485caa

2 files changed

Lines changed: 33 additions & 27 deletions

File tree

lib/internal/test_runner/utils.js

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
ArrayPrototypePop,
88
ArrayPrototypePush,
99
ArrayPrototypeReduce,
10-
ArrayPrototypeSlice,
1110
ArrayPrototypeSome,
1211
JSONParse,
1312
MathFloor,
@@ -166,31 +165,6 @@ function createSeededGenerator(seed) {
166165
};
167166
}
168167

169-
/**
170-
* Return a deterministically shuffled copy of an array.
171-
* @template T
172-
* @param {T[]} values
173-
* @param {number} seed
174-
* @returns {T[]}
175-
*/
176-
function shuffleArrayWithSeed(values, seed) {
177-
if (values.length < 2) {
178-
return values;
179-
}
180-
181-
const randomized = ArrayPrototypeSlice(values);
182-
const random = createSeededGenerator(seed);
183-
184-
for (let i = randomized.length - 1; i > 0; i--) {
185-
const j = MathFloor(random() * (i + 1));
186-
const tmp = randomized[i];
187-
randomized[i] = randomized[j];
188-
randomized[j] = tmp;
189-
}
190-
191-
return randomized;
192-
}
193-
194168
function tryBuiltinReporter(name) {
195169
const builtinPath = kBuiltinReporters.get(name);
196170

@@ -761,7 +735,6 @@ module.exports = {
761735
kDefaultPattern,
762736
kMaxRandomSeed,
763737
parseCommandLine,
764-
shuffleArrayWithSeed,
765738
reporterScope,
766739
shouldColorizeTestFiles,
767740
getCoverageReport,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('node:assert');
5+
const { createSeededGenerator, kMaxRandomSeed } = require('internal/test_runner/utils');
6+
7+
function sequence(seed, length = 10) {
8+
const random = createSeededGenerator(seed);
9+
const values = [];
10+
for (let i = 0; i < length; i++) {
11+
values.push(random());
12+
}
13+
return values;
14+
}
15+
16+
// The same seed must always produce the same sequence so that
17+
// --test-random-seed reproduces a randomized test order across runs.
18+
assert.deepStrictEqual(sequence(0), sequence(0));
19+
assert.deepStrictEqual(sequence(12345), sequence(12345));
20+
assert.deepStrictEqual(sequence(kMaxRandomSeed), sequence(kMaxRandomSeed));
21+
22+
// Different seeds must produce different sequences.
23+
assert.notDeepStrictEqual(sequence(11111), sequence(22222));
24+
assert.notDeepStrictEqual(sequence(0), sequence(1));
25+
26+
// Seeds are coerced to uint32, matching the range accepted by
27+
// --test-random-seed.
28+
assert.deepStrictEqual(sequence(1), sequence(1 + 2 ** 32));
29+
30+
// All generated values must fall in [0, 1).
31+
for (const value of sequence(98765, 1000)) {
32+
assert.ok(value >= 0 && value < 1, `value ${value} out of range`);
33+
}

0 commit comments

Comments
 (0)