You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.9 KiB
61 lines
1.9 KiB
import { describe, expect, it } from "vitest";
|
|
import { createRng, normalizeSeed } from "../../src/stages/games/chase/logic/rng";
|
|
|
|
describe("chase rng", () => {
|
|
it("generates the same sequence for the same seed", () => {
|
|
const seed = 12345;
|
|
const rngA = createRng(seed);
|
|
const rngB = createRng(seed);
|
|
|
|
const sequenceA = Array.from({ length: 5 }, () => rngA());
|
|
const sequenceB = Array.from({ length: 5 }, () => rngB());
|
|
|
|
expect(sequenceA).toEqual(sequenceB);
|
|
});
|
|
|
|
it("keeps createRng outputs in [0, 1)", () => {
|
|
const rng = createRng(12345);
|
|
const samples = Array.from({ length: 500 }, () => rng());
|
|
|
|
for (const value of samples) {
|
|
expect(value).toBeGreaterThanOrEqual(0);
|
|
expect(value).toBeLessThan(1);
|
|
}
|
|
});
|
|
|
|
it("matches a stable golden sequence for a fixed seed", () => {
|
|
const rng = createRng(12345);
|
|
const sequence = Array.from({ length: 5 }, () => rng());
|
|
|
|
expect(sequence).toEqual([
|
|
0.9797282677609473,
|
|
0.3067522644996643,
|
|
0.484205421525985,
|
|
0.817934412509203,
|
|
0.5094283693470061,
|
|
]);
|
|
});
|
|
|
|
it("produces different sequences for different seeds", () => {
|
|
const rngA = createRng(12345);
|
|
const rngB = createRng(12346);
|
|
|
|
const sequenceA = Array.from({ length: 5 }, () => rngA());
|
|
const sequenceB = Array.from({ length: 5 }, () => rngB());
|
|
|
|
expect(sequenceA).not.toEqual(sequenceB);
|
|
});
|
|
|
|
it("normalizes undefined, empty and non-numeric string to numbers", () => {
|
|
expect(typeof normalizeSeed(undefined)).toBe("number");
|
|
expect(typeof normalizeSeed("")).toBe("number");
|
|
expect(typeof normalizeSeed("abc")).toBe("number");
|
|
});
|
|
|
|
it("normalizes edge case seeds into uint32 semantics", () => {
|
|
expect(normalizeSeed(NaN)).toBe(0x9e3779b9);
|
|
expect(normalizeSeed(-1)).toBe(0xffffffff);
|
|
expect(normalizeSeed(1.9)).toBe(1);
|
|
expect(normalizeSeed(" ")).toBe(0x9e3779b9);
|
|
});
|
|
});
|
|
|