profectus-template/tests/game/formulas.test.ts

478 lines
18 KiB
TypeScript
Raw Normal View History

import Formula, { GenericFormula, InvertibleFormula, unrefFormulaSource } from "game/formulas";
2023-01-08 16:15:46 +00:00
import Decimal, { DecimalSource, format } from "util/bignum";
import { beforeAll, describe, expect, test } from "vitest";
2023-01-04 02:56:35 +00:00
import { ref } from "vue";
type FormulaFunctions = keyof GenericFormula & keyof typeof Formula & keyof typeof Decimal;
2023-01-04 02:56:35 +00:00
2023-01-08 16:15:46 +00:00
expect.extend({
compare_tolerance(received, expected) {
const { isNot } = this;
let pass = false;
if (!Decimal.isFinite(expected)) {
pass = !Decimal.isFinite(received);
} else if (Decimal.isNaN(expected)) {
pass = Decimal.isNaN(received);
} else {
pass = Decimal.eq_tolerance(received, expected);
}
2023-01-08 16:15:46 +00:00
return {
// do not alter your "pass" based on isNot. Vitest does it for you
pass,
2023-01-08 16:15:46 +00:00
message: () =>
`Expected ${received} to${
(isNot as boolean) ? " not" : ""
} be close to ${expected}`,
expected: format(expected),
actual: format(received)
};
}
});
interface CustomMatchers<R = unknown> {
compare_tolerance(expected: DecimalSource): R;
}
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Vi {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Assertion extends CustomMatchers {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
2023-01-04 02:56:35 +00:00
}
function testConstant(
desc: string,
formulaFunc: () => InvertibleFormula,
expectedValue: DecimalSource = 10
) {
describe(desc, () => {
let formula: GenericFormula;
2023-01-04 02:56:35 +00:00
beforeAll(() => {
formula = formulaFunc();
});
test("evaluates correctly", () =>
2023-01-08 16:15:46 +00:00
expect(formula.evaluate()).compare_tolerance(expectedValue));
test("invert is pass-through", () => expect(formula.invert(25)).compare_tolerance(25));
test("is not marked as having a variable", () => expect(formula.hasVariable()).toBe(false));
2023-01-04 02:56:35 +00:00
});
}
function testFormula<T extends FormulaFunctions>(
2023-01-08 16:15:46 +00:00
functionName: T,
args: Readonly<Parameters<typeof Formula[T]>>,
2023-01-04 02:56:35 +00:00
invertible = true
) {
let formula: GenericFormula;
2023-01-04 02:56:35 +00:00
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
formula = Formula[functionName](...args);
2023-01-04 02:56:35 +00:00
});
test("Formula is not marked as having a variable", () =>
expect(formula.hasVariable()).toBe(false));
test(`Formula is${invertible ? "" : " not"} invertible`, () =>
expect(formula.isInvertible()).toBe(invertible));
if (invertible) {
test(`Formula throws if inverting without any variables`, () =>
expect(() => formula.invert(10)).toThrow());
}
}
2023-01-04 02:56:35 +00:00
// Utility function that will test all the different
// It's a lot of tests, but I'd rather be exhaustive
function testFormulaCall<T extends FormulaFunctions>(
functionName: T,
args: Readonly<Parameters<typeof Formula[T]>>
) {
2023-01-08 16:15:46 +00:00
let testName = functionName + "(";
for (let i = 0; i < args.length; i++) {
if (i !== 0) {
testName += ", ";
2023-01-04 02:56:35 +00:00
}
testName += args[i];
2023-01-08 16:15:46 +00:00
}
testName += ") evaluates correctly";
test(testName, () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const formula = Formula[functionName](...args);
2023-01-08 16:15:46 +00:00
try {
const expectedEvaluation = Decimal[functionName](
2023-01-04 02:56:35 +00:00
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...args.map(i => unrefFormulaSource(i))
);
if (expectedEvaluation != null) {
expect(formula.evaluate()).compare_tolerance(expectedEvaluation);
2023-01-08 16:15:46 +00:00
}
} catch {
// If this is an invalid Decimal operation, then ignore this test case
2023-01-08 16:15:46 +00:00
}
2023-01-04 02:56:35 +00:00
});
}
function testAliases<T extends FormulaFunctions>(
aliases: T[],
args: Parameters<typeof Formula[T]>
2023-01-08 16:15:46 +00:00
) {
describe(aliases[0], () => {
let formula: GenericFormula;
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
formula = Formula[aliases[0]](...args);
});
aliases.slice(1).forEach(alias => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
test(alias, () => expect(Formula[alias](...args).equals(formula)).toBe(true));
});
});
2023-01-08 16:15:46 +00:00
}
const testValues = ["-1e400", 0, 0.25] as const;
2023-01-04 02:56:35 +00:00
const invertibleZeroParamFunctionNames = [
2023-01-08 16:15:46 +00:00
"neg",
"recip",
"log10",
"log2",
"ln",
"pow10",
"exp",
"sqr",
"sqrt",
"cube",
"cbrt",
"lambertw",
"ssqrt",
"sin",
"cos",
"tan",
"asin",
"acos",
"atan",
"sinh",
"cosh",
"tanh",
"asinh",
"acosh",
"atanh"
2023-01-04 02:56:35 +00:00
] as const;
const nonInvertibleZeroParamFunctionNames = [
2023-01-08 16:15:46 +00:00
"abs",
"sign",
"round",
"floor",
"ceil",
"trunc",
"pLog10",
"absLog10",
"factorial",
"gamma",
"lngamma"
2023-01-04 02:56:35 +00:00
] as const;
const invertibleOneParamFunctionNames = [
2023-01-08 16:15:46 +00:00
"add",
"sub",
"mul",
"div",
"log",
"pow",
"root",
"slog"
2023-01-04 02:56:35 +00:00
] as const;
const nonInvertibleOneParamFunctionNames = [
2023-01-08 16:15:46 +00:00
"max",
"min",
"maxabs",
"minabs",
"clampMin",
"clampMax",
"layeradd10"
2023-01-04 02:56:35 +00:00
] as const;
2023-01-08 16:15:46 +00:00
const invertibleTwoParamFunctionNames = ["tetrate", "layeradd", "iteratedexp"] as const;
2023-01-04 02:56:35 +00:00
2023-01-08 16:15:46 +00:00
const nonInvertibleTwoParamFunctionNames = ["clamp", "iteratedlog", "pentate"] as const;
2023-01-04 02:56:35 +00:00
describe("Formula Equality Checking", () => {
describe("Equality Checks", () => {
test("Equals", () => Formula.add(1, 1).equals(Formula.add(1, 1)));
test("Not Equals due to inputs", () => Formula.add(1, 1).equals(Formula.add(1, 0)));
test("Not Equals due to functions", () => Formula.add(1, 1).equals(Formula.sub(1, 1)));
test("Not Equals due to hasVariable", () =>
Formula.constant(1).equals(Formula.variable(1)));
});
describe("Formula aliases", () => {
testAliases(["neg", "negate", "negated"], [1]);
testAliases(["recip", "reciprocal", "reciprocate"], [1]);
testAliases(["sign", "sgn"], [1]);
testAliases(["add", "plus"], [1, 1]);
testAliases(["sub", "subtract", "minus"], [1, 1]);
testAliases(["mul", "multiply", "times"], [1, 1]);
testAliases(["div", "divide"], [1, 1]);
testAliases(["log", "logarithm"], [1, 1]);
});
describe("Instance vs Static methods", () => {
let formula: GenericFormula;
beforeAll(() => {
formula = Formula.constant(10);
});
[...invertibleZeroParamFunctionNames, ...nonInvertibleZeroParamFunctionNames].forEach(
name => {
test(name, () => {
const instanceFormula = formula[name]();
const staticFormula = Formula[name](formula);
expect(instanceFormula.equals(staticFormula)).toBe(true);
});
}
);
[...invertibleOneParamFunctionNames, ...nonInvertibleOneParamFunctionNames].forEach(
name => {
test(name, () => {
const instanceFormula = formula[name](10);
const staticFormula = Formula[name](formula, 10);
expect(instanceFormula.equals(staticFormula)).toBe(true);
});
}
);
[...invertibleTwoParamFunctionNames, ...nonInvertibleTwoParamFunctionNames].forEach(
name => {
test(name, () => {
const instanceFormula = formula[name](1, 1);
const staticFormula = Formula[name](formula, 1, 1);
expect(instanceFormula.equals(staticFormula)).toBe(true);
});
}
);
2023-01-04 02:56:35 +00:00
});
});
2023-01-04 02:56:35 +00:00
describe("Creating Formulas", () => {
2023-01-04 02:56:35 +00:00
describe("Constants", () => {
testConstant("number", () => Formula.constant(10));
testConstant("string", () => Formula.constant("10"));
testConstant("formula", () => Formula.constant(Formula.constant(10)));
testConstant("decimal", () => Formula.constant(new Decimal("1e400")), "1e400");
testConstant("ref", () => Formula.constant(ref(10)));
});
describe("Invertible 0-param", () => {
invertibleZeroParamFunctionNames.forEach(names =>
describe(names, () => {
testFormula(names, [0] as const);
testValues.forEach(i => testFormulaCall(names, [i] as const));
})
);
2023-01-04 02:56:35 +00:00
});
describe("Non-Invertible 0-param", () => {
nonInvertibleZeroParamFunctionNames.forEach(names =>
describe(names, () => {
testFormula(names, [0] as const, false);
testValues.forEach(i => testFormulaCall(names, [i] as const));
})
);
2023-01-04 02:56:35 +00:00
});
describe("Invertible 1-param", () => {
invertibleOneParamFunctionNames.forEach(names =>
describe(names, () => {
testFormula(names, [0, 0] as const);
testValues.forEach(i =>
testValues.forEach(j => testFormulaCall(names, [i, j] as const))
);
})
);
2023-01-04 02:56:35 +00:00
});
describe("Non-Invertible 1-param", () => {
nonInvertibleOneParamFunctionNames.forEach(names =>
describe(names, () => {
testFormula(names, [0, 0] as const, false);
testValues.forEach(i =>
testValues.forEach(j => testFormulaCall(names, [i, j] as const))
);
})
);
2023-01-04 02:56:35 +00:00
});
describe("Invertible 2-param", () => {
invertibleTwoParamFunctionNames.forEach(names =>
describe(names, () => {
testFormula(names, [0, 0, 0] as const);
testValues.forEach(i =>
testValues.forEach(j =>
testValues.forEach(k => testFormulaCall(names, [i, j, k] as const))
)
);
})
);
2023-01-04 02:56:35 +00:00
});
describe("Non-Invertible 2-param", () => {
nonInvertibleTwoParamFunctionNames.forEach(names =>
describe(names, () => {
testFormula(names, [0, 0, 0] as const, false);
testValues.forEach(i =>
testValues.forEach(j =>
testValues.forEach(k => testFormulaCall(names, [i, j, k] as const))
)
);
})
);
2023-01-04 02:56:35 +00:00
});
2023-01-08 16:15:46 +00:00
});
2023-01-04 02:56:35 +00:00
2023-01-08 16:15:46 +00:00
describe("Variables", () => {
let variable: GenericFormula;
let constant: GenericFormula;
2023-01-08 16:15:46 +00:00
beforeAll(() => {
variable = Formula.variable(10);
constant = Formula.constant(10);
});
2023-01-04 02:56:35 +00:00
test("Created variable is marked as a variable", () =>
expect(variable.hasVariable()).toBe(true));
2023-01-08 16:15:46 +00:00
test("Evaluate() returns variable's value", () =>
expect(variable.evaluate()).compare_tolerance(10));
test("Invert() is pass-through", () => expect(variable.invert(100)).compare_tolerance(100));
2023-01-04 02:56:35 +00:00
2023-01-08 16:15:46 +00:00
test("Nested variable is marked as having a variable", () =>
expect(variable.add(10).div(3).pow(2).hasVariable()).toBe(true));
2023-01-08 16:15:46 +00:00
test("Nested non-variable is marked as not having a variable", () =>
expect(constant.add(10).div(3).pow(2).hasVariable()).toBe(false));
2023-01-04 02:56:35 +00:00
2023-01-08 16:15:46 +00:00
describe("Invertible Formulas correctly calculate when they contain a variable", () => {
function checkFormula(formula: GenericFormula, expectedBool = true) {
expect(formula.isInvertible()).toBe(expectedBool);
expect(formula.hasVariable()).toBe(expectedBool);
2023-01-08 16:15:46 +00:00
}
invertibleZeroParamFunctionNames.forEach(name => {
describe(name, () => {
test(`${name}(var) is marked as invertible and having a variable`, () =>
checkFormula(Formula[name](variable)));
2023-01-04 02:56:35 +00:00
});
2023-01-08 16:15:46 +00:00
});
invertibleOneParamFunctionNames.forEach(name => {
describe(name, () => {
test(`${name}(var, const) is marked as invertible and having a variable`, () =>
checkFormula(Formula[name](variable, constant)));
test(`${name}(const, var) is marked as invertible and having a variable`, () =>
checkFormula(Formula[name](constant, variable)));
test(`${name}(var, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, variable), false));
2023-01-04 02:56:35 +00:00
});
2023-01-08 16:15:46 +00:00
});
invertibleTwoParamFunctionNames.forEach(name => {
describe(name, () => {
test(`${name}(var, const, const) is marked as invertible and having a variable`, () =>
checkFormula(Formula[name](variable, constant, constant)));
test(`${name}(const, var, const) is marked as invertible and having a variable`, () =>
checkFormula(Formula[name](constant, variable, constant)));
test(`${name}(const, const, var) is marked as invertible and having a variable`, () =>
checkFormula(Formula[name](constant, constant, variable)));
test(`${name}(var, var, const) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, variable, constant), false));
test(`${name}(var, const, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, constant, variable), false));
test(`${name}(const, var, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](constant, variable, variable), false));
test(`${name}(var, var, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, variable, variable), false));
2023-01-04 02:56:35 +00:00
});
});
2023-01-08 16:15:46 +00:00
});
2023-01-04 02:56:35 +00:00
2023-01-08 16:15:46 +00:00
describe("Non-Invertible Formulas never marked as having a variable", () => {
function checkFormula(formula: GenericFormula) {
expect(formula.isInvertible()).toBe(false);
expect(formula.hasVariable()).toBe(false);
2023-01-08 16:15:46 +00:00
}
nonInvertibleZeroParamFunctionNames.forEach(name => {
describe(name, () => {
test(`${name}(var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable)));
2023-01-04 02:56:35 +00:00
});
2023-01-08 16:15:46 +00:00
});
nonInvertibleOneParamFunctionNames.forEach(name => {
describe(name, () => {
test(`${name}(var, const) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, constant)));
test(`${name}(const, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](constant, variable)));
test(`${name}(var, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, variable)));
2023-01-04 02:56:35 +00:00
});
2023-01-08 16:15:46 +00:00
});
nonInvertibleTwoParamFunctionNames.forEach(name => {
describe(name, () => {
test(`${name}(var, const, const) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, constant, constant)));
test(`${name}(const, var, const) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](constant, variable, constant)));
test(`${name}(const, const, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](constant, constant, variable)));
test(`${name}(var, var, const) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, variable, constant)));
test(`${name}(var, const, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, constant, variable)));
test(`${name}(const, var, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](constant, variable, variable)));
test(`${name}(var, var, var) is marked as not invertible and not having a variable`, () =>
checkFormula(Formula[name](variable, variable, variable)));
2023-01-04 02:56:35 +00:00
});
});
2023-01-08 16:15:46 +00:00
});
2023-01-06 06:38:11 +00:00
2023-01-08 16:15:46 +00:00
describe("Inverting calculates the value of the variable", () => {
let variable: GenericFormula;
let constant: GenericFormula;
2023-01-08 16:15:46 +00:00
beforeAll(() => {
variable = Formula.variable(2);
constant = Formula.constant(3);
2023-01-06 06:38:11 +00:00
});
2023-01-08 16:15:46 +00:00
invertibleOneParamFunctionNames.forEach(name =>
describe(name, () => {
test(`${name}(var, const).invert()`, () => {
const formula = Formula[name](variable, constant);
const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2);
});
test(`${name}(const, var).invert()`, () => {
const formula = Formula[name](constant, variable);
const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2);
});
})
);
invertibleTwoParamFunctionNames.forEach(name =>
describe(name, () => {
test(`${name}(var, const, const).invert()`, () => {
const formula = Formula[name](variable, constant, constant);
const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2);
});
test(`${name}(const, var, const).invert()`, () => {
const formula = Formula[name](constant, variable, constant);
const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2);
});
test(`${name}(const, const, var).invert()`, () => {
const formula = Formula[name](constant, constant, variable);
const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2);
});
})
);
2023-01-04 02:56:35 +00:00
});
});