SPB Git

Repository Public

rng-format.test.ts 5.6 KB 124 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : tests/unit/rng-format.test.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { describe, expect, it } from "vitest";44import { createRng, formatDate, formatLivingArea, formatMoney, formatPercent, formatSignedMoney, hashSeed, ordinal } from "../../packages/engine/src/index.ts";45import { CHI2_CRIT_001, chiSquareUniform } from "../helpers/stats.ts";4647describe("Générateur pseudo-aléatoire", () => {48  it("produit la même suite pour la même graine et une suite différente pour une autre", () => {49    const a = createRng("graine");50    const b = createRng("graine");51    const c = createRng("graine2");52    const seqA = Array.from({ length: 50 }, () => a.next());53    expect(Array.from({ length: 50 }, () => b.next())).toEqual(seqA);54    expect(Array.from({ length: 50 }, () => c.next())).not.toEqual(seqA);55  });56  it("reste dans [0, 1) et respecte les bornes de int() et range()", () => {57    const rng = createRng(42);58    for (let i = 0; i < 20_000; i++) {59      const x = rng.next();60      expect(x >= 0 && x < 1).toBe(true);61      const k = rng.int(3, 7);62      expect(Number.isInteger(k) && k >= 3 && k <= 7).toBe(true);63      const y = rng.range(0.15, 0.35);64      expect(y >= 0.15 && y < 0.35).toBe(true);65    }66  });67  it("tire int(0, 3) uniformément (χ², 3 ddl, α = 0,001)", () => {68    const rng = createRng("uniforme");69    const counts = [0, 0, 0, 0];70    for (let i = 0; i < 40_000; i++) counts[rng.int(0, 3)]! += 1;71    expect(chiSquareUniform(counts)).toBeLessThan(CHI2_CRIT_001[3]!);72  });73  it("mélange sans perdre ni dupliquer d'élément, sans modifier l'entrée", () => {74    const items = Array.from({ length: 100 }, (_, i) => i);75    const shuffled = createRng("melange").shuffle(items);76    expect(items).toEqual(Array.from({ length: 100 }, (_, i) => i));77    expect([...shuffled].sort((x, y) => x - y)).toEqual(items);78    expect(shuffled).not.toEqual(items);79  });80  it("mélange quatre éléments uniformément sur les 24 permutations (χ², 23 ddl, α = 0,001)", () => {81    const counts = new Map<string, number>();82    for (let i = 0; i < 24_000; i++) {83      const key = createRng(`perm:${i}`).shuffle(["a", "b", "c", "d"]).join("");84      counts.set(key, (counts.get(key) ?? 0) + 1);85    }86    expect(counts.size).toBe(24);87    expect(chiSquareUniform([...counts.values()])).toBeLessThan(CHI2_CRIT_001[23]!);88  });89  it("dérive des sous-générateurs indépendants de la consommation du parent", () => {90    const parent = createRng("parent");91    const before = parent.fork("enfant").next();92    parent.next();93    parent.next();94    expect(parent.fork("enfant").next()).toBe(before);95    expect(parent.fork("autre").next()).not.toBe(before);96  });97  it("refuse de tirer dans une liste vide et hache de façon stable", () => {98    expect(() => createRng("x").pick([])).toThrow(/liste vide/);99    expect(hashSeed("abc")).toBe(hashSeed("abc"));100    expect(hashSeed("abc")).not.toBe(hashSeed("abd"));101    expect(Number.isInteger(hashSeed("abc")) && hashSeed("abc") >= 0 && hashSeed("abc") <= 0xffffffff).toBe(true);102  });103});104105describe("Formatage en français canadien", () => {106  const plain = (s: string): string => s.replace(/[\s  ]/g, " ");107  it("formate les montants sans décimales, avec le symbole après le nombre", () => {108    expect(plain(formatMoney(487_650))).toBe("487 650 $");109    expect(plain(formatMoney(1_250_000.4))).toBe("1 250 000 $");110  });111  it("formate les écarts signés avec un vrai signe moins", () => {112    expect(plain(formatSignedMoney(25_000))).toBe("+25 000 $");113    expect(plain(formatSignedMoney(-25_000))).toBe("−25 000 $");114    expect(plain(formatSignedMoney(0))).toBe("0 $");115  });116  it("formate pourcentages, superficies, dates et rangs", () => {117    expect(plain(formatPercent(4.25))).toBe("4,3 %");118    expect(plain(formatLivingArea(1_076))).toBe("1 076 pi² (100 m²)");119    expect(formatDate("2026-11-01")).toBe("1 novembre 2026");120    expect(formatDate("2026-01-01")).toBe("1 janvier 2026");121    expect([ordinal(1), ordinal(2), ordinal(22)]).toEqual(["1er", "2e", "22e"]);122  });123});124
Files main
Menu

Theme