mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-21 18:02:45 -05:00
107 lines
2.6 KiB
TypeScript
107 lines
2.6 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
||
import {
|
||
averageArray,
|
||
cutToNDecimalPlaces,
|
||
niceStep,
|
||
roundToNDecimalPlaces,
|
||
safeNumberParse,
|
||
winPercentage,
|
||
} from "./number";
|
||
|
||
describe("roundToNDecimalPlaces()", () => {
|
||
test.each([
|
||
[1.234, 1.23],
|
||
[1.235, 1.24],
|
||
[1.2, 1.2],
|
||
[1, 1],
|
||
])("rounds %d to %d with the default 2 decimal places", (input, expected) => {
|
||
expect(roundToNDecimalPlaces(input)).toBe(expected);
|
||
});
|
||
|
||
test.each([
|
||
[1.6, 0, 2],
|
||
[1.4, 0, 1],
|
||
[2.5, 0, 3],
|
||
[1.23456, 3, 1.235],
|
||
[1.23444, 3, 1.234],
|
||
[-1.2345, 2, -1.23],
|
||
[-1.2355, 2, -1.24],
|
||
[0, 2, 0],
|
||
[0, 0, 0],
|
||
[123456.789, 1, 123456.8],
|
||
[123456.789, 0, 123457],
|
||
])("rounds %d to %d decimal places as %d", (input, decimals, expected) => {
|
||
expect(roundToNDecimalPlaces(input, decimals)).toBe(expected);
|
||
});
|
||
});
|
||
|
||
describe("cutToNDecimalPlaces()", () => {
|
||
test.each([
|
||
[3.9999, 2, 3.99],
|
||
[3.12, 1, 3.1],
|
||
[100, 2, 100],
|
||
[3.0001, 2, 3],
|
||
[0.29, 2, 0.29],
|
||
// values whose binary representation is just below the decimal shown
|
||
[2.32, 2, 2.32],
|
||
[-0.29, 2, -0.29],
|
||
])("cuts %d to %d decimal places as %d", (input, decimals, expected) => {
|
||
expect(cutToNDecimalPlaces(input, decimals)).toBe(expected);
|
||
});
|
||
});
|
||
|
||
describe("averageArray()", () => {
|
||
test.each([
|
||
[[2, 4, 6, 8], 5],
|
||
[[-2, -4, -6, -8], -5],
|
||
[[10, -10, 20, -20], 0],
|
||
[[42], 42],
|
||
[[], 0],
|
||
])("averages %j to %d", (input, expected) => {
|
||
expect(averageArray(input)).toBe(expected);
|
||
});
|
||
});
|
||
|
||
describe("safeNumberParse()", () => {
|
||
test.each([
|
||
["42", 42],
|
||
["3.14", 3.14],
|
||
[" 7 ", 7],
|
||
["-123", -123],
|
||
["0", 0],
|
||
["abc", null],
|
||
["", null],
|
||
[" ", null],
|
||
[null, null],
|
||
])("parses %j as %j", (input, expected) => {
|
||
expect(safeNumberParse(input)).toBe(expected);
|
||
});
|
||
});
|
||
|
||
describe("winPercentage()", () => {
|
||
test.each([
|
||
[3, 1, 75],
|
||
[1, 1, 50],
|
||
[0, 4, 0],
|
||
[4, 0, 100],
|
||
])("%d wins and %d losses is %d%%", (wins, losses, expected) => {
|
||
expect(winPercentage(wins, losses)).toBe(expected);
|
||
});
|
||
|
||
test("returns null when no games were played", () => {
|
||
expect(winPercentage(0, 0)).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe("niceStep()", () => {
|
||
test.each([
|
||
{ why: "exact power of ten", roughStep: 10, expected: 10 },
|
||
{ why: "rounds up to 2×", roughStep: 13, expected: 20 },
|
||
{ why: "rounds up to 5×", roughStep: 42, expected: 50 },
|
||
{ why: "rounds up to the next power of ten", roughStep: 70, expected: 100 },
|
||
{ why: "fractional step", roughStep: 0.3, expected: 0.5 },
|
||
])("$why: $roughStep → $expected", ({ roughStep, expected }) => {
|
||
expect(niceStep(roughStep)).toBeCloseTo(expected);
|
||
});
|
||
});
|