aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/appearance/test_shuffle.js
blob: 6dcb5288dfe999d3e49b489f23fe1434596447f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Run: node desktop/modules/appearance/test_shuffle.js
// Shuffle.js is a QML library, so its .pragma line is stripped before node
// evaluates it.

const fs = require("fs");
const assert = require("assert");

const src = fs.readFileSync(__dirname + "/Shuffle.js", "utf8").replace(/^\.pragma library$/m, "");
const S = new Function(src + "\nreturn { draw };")();

// Every round shows each path once, and no path twice running, across the
// refill included.
const list = ["a", "b", "c", "d"];
let bag = [], last = "";
for (let r = 0; r < 200; r++) {
    const round = [];
    for (let i = 0; i < list.length; i++) {
        const d = S.draw(list, bag, last);
        assert.notStrictEqual(d.pick, last, `round ${r}: ${d.pick} twice running`);
        round.push(d.pick);
        bag = d.bag;
        last = d.pick;
    }
    assert.deepStrictEqual(round.slice().sort(), list);
}

// A path removed from the list leaves the bag with it.
assert.deepStrictEqual(S.draw(["a", "b"], ["c", "b"], "a"), { pick: "b", bag: [] });
// One path is all there is, every time.
assert.strictEqual(S.draw(["a"], [], "a").pick, "a");
// Nothing listed, nothing picked.
assert.deepStrictEqual(S.draw([], [], "a"), { pick: "", bag: [] });

console.log("test_shuffle OK");