Fix more tests

This commit is contained in:
thepaperpilot 2023-03-21 00:15:28 -05:00
parent 8939ad133a
commit 5afb691b30
4 changed files with 78 additions and 119 deletions

View file

@ -477,9 +477,7 @@ function invertTetrate(
payload: FormulaSource payload: FormulaSource
) { ) {
if (hasVariable(base)) { if (hasVariable(base)) {
return base.invert( return base.invert(Decimal.ssqrt(value));
Decimal.slog(value, Decimal.minabs(1e308, unrefFormulaSource(height)).toNumber())
);
} }
// Other params can't be inverted ATM // Other params can't be inverted ATM
throw "Could not invert due to no input being a variable"; throw "Could not invert due to no input being a variable";

View file

@ -82,7 +82,7 @@ export interface CostRequirementOptions {
* When calculating multiple levels to be handled at once, whether it should consider resources used for each level as spent. Setting this to false causes calculations to be faster with larger numbers and supports more math functions. * When calculating multiple levels to be handled at once, whether it should consider resources used for each level as spent. Setting this to false causes calculations to be faster with larger numbers and supports more math functions.
* @see {Formula} * @see {Formula}
*/ */
spendResources?: Computable<boolean>; spendResources: Computable<boolean>;
/** /**
* Pass-through to {@link Requirement.pay}. May be required for maximizing support. * Pass-through to {@link Requirement.pay}. May be required for maximizing support.
* @see {@link cost} for restrictions on maximizing support. * @see {@link cost} for restrictions on maximizing support.
@ -148,8 +148,9 @@ export function createCostRequirement<T extends CostRequirementOptions>(
setDefault(req, "visibility", Visibility.Visible); setDefault(req, "visibility", Visibility.Visible);
processComputable(req as T, "cost"); processComputable(req as T, "cost");
processComputable(req as T, "requiresPay"); processComputable(req as T, "requiresPay");
processComputable(req as T, "spendResources");
setDefault(req, "requiresPay", true); setDefault(req, "requiresPay", true);
processComputable(req as T, "spendResources");
setDefault(req, "spendResources", false);
setDefault(req, "pay", function (amount?: DecimalSource) { setDefault(req, "pay", function (amount?: DecimalSource) {
const cost = const cost =
req.cost instanceof Formula req.cost instanceof Formula

View file

@ -40,7 +40,10 @@ const invertibleZeroParamFunctionNames = [
"tanh", "tanh",
"asinh", "asinh",
"acosh", "acosh",
"atanh" "atanh",
"slog",
"tetrate",
"iteratedexp"
] as const; ] as const;
const nonInvertibleZeroParamFunctionNames = [ const nonInvertibleZeroParamFunctionNames = [
"abs", "abs",
@ -122,7 +125,7 @@ const invertibleOneParamFunctionNames = [
"log", "log",
"pow", "pow",
"root", "root",
"slog" "layeradd"
] as const; ] as const;
const nonInvertibleOneParamFunctionNames = ["layeradd10"] as const; const nonInvertibleOneParamFunctionNames = ["layeradd10"] as const;
const integrableOneParamFunctionNames = ["add", "sub", "mul", "div", "log", "pow", "root"] as const; const integrableOneParamFunctionNames = ["add", "sub", "mul", "div", "log", "pow", "root"] as const;
@ -130,12 +133,8 @@ const nonIntegrableOneParamFunctionNames = [...nonInvertibleOneParamFunctionName
const invertibleIntegralOneParamFunctionNames = integrableOneParamFunctionNames; const invertibleIntegralOneParamFunctionNames = integrableOneParamFunctionNames;
const nonInvertibleIntegralOneParamFunctionNames = nonIntegrableOneParamFunctionNames; const nonInvertibleIntegralOneParamFunctionNames = nonIntegrableOneParamFunctionNames;
const invertibleTwoParamFunctionNames = ["tetrate", "layeradd", "iteratedexp"] as const;
const nonInvertibleTwoParamFunctionNames = ["iteratedlog", "pentate"] as const; const nonInvertibleTwoParamFunctionNames = ["iteratedlog", "pentate"] as const;
const nonIntegrableTwoParamFunctionNames = [ const nonIntegrableTwoParamFunctionNames = nonInvertibleTwoParamFunctionNames;
...invertibleTwoParamFunctionNames,
...nonInvertibleZeroParamFunctionNames
];
const nonInvertibleIntegralTwoParamFunctionNames = nonIntegrableTwoParamFunctionNames; const nonInvertibleIntegralTwoParamFunctionNames = nonIntegrableTwoParamFunctionNames;
describe("Formula Equality Checking", () => { describe("Formula Equality Checking", () => {
@ -201,16 +200,6 @@ describe("Formula Equality Checking", () => {
}); });
} }
); );
[...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);
});
}
);
}); });
}); });
@ -331,13 +320,7 @@ describe("Creating Formulas", () => {
); );
}); });
describe("2-param", () => { describe("2-param", () => {
( ([...nonInvertibleTwoParamFunctionNames, "clamp"] as const).forEach(names =>
[
...invertibleTwoParamFunctionNames,
...nonInvertibleTwoParamFunctionNames,
"clamp"
] as const
).forEach(names =>
describe(names, () => { describe(names, () => {
checkFormula(names, [0, 0, 0] as const); checkFormula(names, [0, 0, 0] as const);
testValues.forEach(i => testValues.forEach(i =>
@ -403,24 +386,6 @@ describe("Inverting", () => {
checkFormula(Formula[name](variable, variable), false)); checkFormula(Formula[name](variable, variable), false));
}); });
}); });
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));
});
});
}); });
describe("Non-invertible formulas marked as such", () => { describe("Non-invertible formulas marked as such", () => {
@ -479,30 +444,13 @@ describe("Inverting", () => {
const result = formula.evaluate(); const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2); expect(formula.invert(result)).compare_tolerance(2);
}); });
if (name !== "layeradd") {
test(`${name}(const, var).invert()`, () => { test(`${name}(const, var).invert()`, () => {
const formula = Formula[name](constant, variable); const formula = Formula[name](constant, variable);
const result = formula.evaluate(); const result = formula.evaluate();
expect(formula.invert(result)).compare_tolerance(2); 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);
});
}) })
); );
}); });
@ -530,7 +478,7 @@ describe("Inverting", () => {
test("Inverting with non-invertible sections", () => { test("Inverting with non-invertible sections", () => {
const formula = Formula.add(variable, constant.ceil()); const formula = Formula.add(variable, constant.ceil());
expect(formula.isInvertible()).toBe(true); expect(formula.isInvertible()).toBe(true);
expect(formula.invert(10)).compare_tolerance(7); expect(formula.invert(10)).compare_tolerance(0);
}); });
}); });
@ -562,8 +510,10 @@ describe("Integrating", () => {
describe(name, () => { describe(name, () => {
test(`${name}(var, const) is marked as integrable`, () => test(`${name}(var, const) is marked as integrable`, () =>
checkFormula(Formula[name](variable, constant))); checkFormula(Formula[name](variable, constant)));
if (name !== "log" && name !== "root") {
test(`${name}(const, var) is marked as integrable`, () => test(`${name}(const, var) is marked as integrable`, () =>
checkFormula(Formula[name](constant, variable))); checkFormula(Formula[name](constant, variable)));
}
test(`${name}(var, var) is marked as not integrable`, () => test(`${name}(var, var) is marked as not integrable`, () =>
expect(Formula[name](variable, variable).isIntegrable()).toBe(false)); expect(Formula[name](variable, variable).isIntegrable()).toBe(false));
}); });
@ -645,8 +595,10 @@ describe("Inverting integrals", () => {
describe(name, () => { describe(name, () => {
test(`${name}(var, const) is marked as having an invertible integral`, () => test(`${name}(var, const) is marked as having an invertible integral`, () =>
checkFormula(Formula[name](variable, constant))); checkFormula(Formula[name](variable, constant)));
if (name !== "log" && name !== "root") {
test(`${name}(const, var) is marked as having an invertible integral`, () => test(`${name}(const, var) is marked as having an invertible integral`, () =>
checkFormula(Formula[name](constant, variable))); checkFormula(Formula[name](constant, variable)));
}
test(`${name}(var, var) is marked as not having an invertible integral`, () => { test(`${name}(var, var) is marked as not having an invertible integral`, () => {
const formula = Formula[name](variable, variable); const formula = Formula[name](variable, variable);
expect(formula.isIntegralInvertible()).toBe(false); expect(formula.isIntegralInvertible()).toBe(false);
@ -871,6 +823,11 @@ describe("Conditionals", () => {
}); });
describe("Custom Formulas", () => { describe("Custom Formulas", () => {
let variable: GenericFormula;
beforeAll(() => {
variable = Formula.variable(1);
});
describe("Formula with evaluate", () => { describe("Formula with evaluate", () => {
test("Zero input evaluates correctly", () => test("Zero input evaluates correctly", () =>
expect(new Formula({ inputs: [], evaluate: () => 10 }).evaluate()).compare_tolerance( expect(new Formula({ inputs: [], evaluate: () => 10 }).evaluate()).compare_tolerance(
@ -887,28 +844,28 @@ describe("Custom Formulas", () => {
}); });
describe("Formula with invert", () => { describe("Formula with invert", () => {
test("Zero input inverts correctly", () => test("Zero input does not invert", () =>
expect( expect(() =>
new Formula({ new Formula({
inputs: [], inputs: [],
evaluate: () => 6, evaluate: () => 6,
invert: value => value, invert: value => value,
hasVariable: true hasVariable: true
}).invert(10) }).invert(10)
).compare_tolerance(10)); ).toThrow());
test("One input inverts correctly", () => test("One input inverts correctly", () =>
expect( expect(
new Formula({ new Formula({
inputs: [1], inputs: [variable],
evaluate: () => 10, evaluate: () => 10,
invert: (value, v1) => v1, invert: (value, v1) => v1.evaluate(),
hasVariable: true hasVariable: true
}).invert(10) }).invert(10)
).compare_tolerance(1)); ).compare_tolerance(1));
test("Two inputs inverts correctly", () => test("Two inputs inverts correctly", () =>
expect( expect(
new Formula({ new Formula({
inputs: [1, 2], inputs: [variable, 2],
evaluate: () => 10, evaluate: () => 10,
invert: (value, v1, v2) => v2, invert: (value, v1, v2) => v2,
hasVariable: true hasVariable: true
@ -918,7 +875,7 @@ describe("Custom Formulas", () => {
describe("Formula with integrate", () => { describe("Formula with integrate", () => {
test("Zero input integrates correctly", () => test("Zero input integrates correctly", () =>
expect( expect(() =>
new Formula({ new Formula({
inputs: [], inputs: [],
evaluate: () => 10, evaluate: () => 10,
@ -928,7 +885,7 @@ describe("Custom Formulas", () => {
test("One input integrates correctly", () => test("One input integrates correctly", () =>
expect( expect(
new Formula({ new Formula({
inputs: [1], inputs: [variable],
evaluate: () => 10, evaluate: () => 10,
integrate: (val, v1) => val ?? 20 integrate: (val, v1) => val ?? 20
}).evaluateIntegral() }).evaluateIntegral()
@ -936,7 +893,7 @@ describe("Custom Formulas", () => {
test("Two inputs integrates correctly", () => test("Two inputs integrates correctly", () =>
expect( expect(
new Formula({ new Formula({
inputs: [1, 2], inputs: [variable, 2],
evaluate: (v1, v2) => 10, evaluate: (v1, v2) => 10,
integrate: (v1, v2) => 3 integrate: (v1, v2) => 3
}).evaluateIntegral() }).evaluateIntegral()
@ -944,19 +901,19 @@ describe("Custom Formulas", () => {
}); });
describe("Formula with invertIntegral", () => { describe("Formula with invertIntegral", () => {
test("Zero input inverts integral correctly", () => test("Zero input does not invert integral", () =>
expect( expect(() =>
new Formula({ new Formula({
inputs: [], inputs: [],
evaluate: () => 10, evaluate: () => 10,
invertIntegral: () => 1, invertIntegral: () => 1,
hasVariable: true hasVariable: true
}).invertIntegral(8) }).invertIntegral(8)
).compare_tolerance(1)); ).toThrow());
test("One input inverts integral correctly", () => test("One input inverts integral correctly", () =>
expect( expect(
new Formula({ new Formula({
inputs: [1], inputs: [variable],
evaluate: () => 10, evaluate: () => 10,
invertIntegral: (val, v1) => 1, invertIntegral: (val, v1) => 1,
hasVariable: true hasVariable: true
@ -965,7 +922,7 @@ describe("Custom Formulas", () => {
test("Two inputs inverts integral correctly", () => test("Two inputs inverts integral correctly", () =>
expect( expect(
new Formula({ new Formula({
inputs: [1, 2], inputs: [variable, 2],
evaluate: (v1, v2) => 10, evaluate: (v1, v2) => 10,
invertIntegral: (v1, v2) => 1, invertIntegral: (v1, v2) => 1,
hasVariable: true hasVariable: true
@ -977,36 +934,36 @@ describe("Custom Formulas", () => {
describe("Buy Max", () => { describe("Buy Max", () => {
let resource: Resource; let resource: Resource;
beforeAll(() => { beforeAll(() => {
resource = createResource(ref(10)); resource = createResource(ref(1000));
}); });
describe("With spending", () => { describe("Without spending", () => {
test("Throws on formula with non-invertible integral", () => { test("Throws on formula with non-invertible integral", () => {
const maxAffordable = calculateMaxAffordable(Formula.neg(10), resource, false); const maxAffordable = calculateMaxAffordable(Formula.neg(10), resource, false);
expect(() => maxAffordable.value).toThrow(); expect(() => maxAffordable.value).toThrow();
}); });
// https://www.desmos.com/calculator/5vgletdc1p // https://www.desmos.com/calculator/5vgletdc1p
test("Calculates max affordable and cost correctly", () => { test("Calculates max affordable and cost correctly", () => {
const variable = Formula.variable(10); const variable = Formula.variable(0);
const formula = Formula.pow(1.05, variable); const formula = Formula.pow(1.05, variable).times(100);
const maxAffordable = calculateMaxAffordable(formula, resource, false); const maxAffordable = calculateMaxAffordable(formula, resource, false);
expect(maxAffordable.value).compare_tolerance(47); expect(maxAffordable.value).compare_tolerance(47);
expect(calculateCost(formula, maxAffordable.value, false)).compare_tolerance( expect(calculateCost(formula, maxAffordable.value, false)).compare_tolerance(
Decimal.pow(1.05, 47) Decimal.pow(1.05, 47).times(100)
); );
}); });
}); });
describe("Without spending", () => { describe("With spending", () => {
test("Throws on non-invertible formula", () => { test("Throws on non-invertible formula", () => {
const maxAffordable = calculateMaxAffordable(Formula.abs(10), resource); const maxAffordable = calculateMaxAffordable(Formula.abs(10), resource);
expect(() => maxAffordable.value).toThrow(); expect(() => maxAffordable.value).toThrow();
}); });
// https://www.desmos.com/calculator/5vgletdc1p // https://www.desmos.com/calculator/5vgletdc1p
test("Calculates max affordable and cost correctly", () => { test("Calculates max affordable and cost correctly", () => {
const variable = Formula.variable(10); const variable = Formula.variable(0);
const formula = Formula.pow(1.05, variable); const formula = Formula.pow(1.05, variable).times(100);
const maxAffordable = calculateMaxAffordable(formula, resource); const maxAffordable = calculateMaxAffordable(formula, resource);
expect(maxAffordable.value).compare_tolerance(7); expect(maxAffordable.value).compare_tolerance(7);
expect(calculateCost(formula, maxAffordable.value)).compare_tolerance(7.35); expect(calculateCost(formula, maxAffordable.value)).compare_tolerance(735);
}); });
}); });
}); });

View file

@ -2,6 +2,7 @@ import { Visibility } from "features/feature";
import { createResource, Resource } from "features/resources/resource"; import { createResource, Resource } from "features/resources/resource";
import Formula from "game/formulas"; import Formula from "game/formulas";
import { import {
CostRequirement,
createBooleanRequirement, createBooleanRequirement,
createCostRequirement, createCostRequirement,
createVisibilityRequirement, createVisibilityRequirement,
@ -17,19 +18,18 @@ import "../utils";
describe("Creating cost requirement", () => { describe("Creating cost requirement", () => {
describe("Minimal requirement", () => { describe("Minimal requirement", () => {
let resource: Resource; let resource: Resource;
let requirement: Requirement; let requirement: CostRequirement;
beforeAll(() => { beforeAll(() => {
resource = createResource(ref(10)); resource = createResource(ref(10));
requirement = createCostRequirement(() => ({ requirement = createCostRequirement(() => ({
resource, resource,
cost: 10 cost: 10,
spendResources: false
})); }));
}); });
// eslint-disable-next-line @typescript-eslint/no-explicit-any test("resource pass-through", () => expect(requirement.resource).toBe(resource));
test("resource pass-through", () => expect((requirement as any).resource).toBe(resource)); test("cost pass-through", () => expect(requirement.cost).toBe(10));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
test("cost pass-through", () => expect((requirement as any).cost).toBe(10));
test("partialDisplay exists", () => test("partialDisplay exists", () =>
expect(typeof requirement.partialDisplay).toBe("function")); expect(typeof requirement.partialDisplay).toBe("function"));
@ -41,23 +41,22 @@ describe("Creating cost requirement", () => {
}); });
test("is visible", () => expect(requirement.visibility).toBe(Visibility.Visible)); test("is visible", () => expect(requirement.visibility).toBe(Visibility.Visible));
test("requires pay", () => expect(requirement.requiresPay).toBe(true)); test("requires pay", () => expect(requirement.requiresPay).toBe(true));
// eslint-disable-next-line @typescript-eslint/no-explicit-any test("does not spend resources", () => expect(requirement.spendResources).toBe(false));
test("spends resources", () => expect((requirement as any).spendResources).toBe(true));
test("cannot maximize", () => expect(unref(requirement.canMaximize)).toBe(false)); test("cannot maximize", () => expect(unref(requirement.canMaximize)).toBe(false));
}); });
describe("Fully customized", () => { describe("Fully customized", () => {
let resource: Resource; let resource: Resource;
let requirement: Requirement; let requirement: CostRequirement;
beforeAll(() => { beforeAll(() => {
resource = createResource(ref(10)); resource = createResource(ref(10));
requirement = createCostRequirement(() => ({ requirement = createCostRequirement(() => ({
resource, resource,
cost: 10, cost: Formula.variable(resource).times(10),
visibility: Visibility.None, visibility: Visibility.None,
requiresPay: false, requiresPay: false,
maximize: true, maximize: true,
spendResources: false, spendResources: true,
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
pay() {} pay() {}
})); }));
@ -69,9 +68,7 @@ describe("Creating cost requirement", () => {
requirement.pay.length === 1); requirement.pay.length === 1);
test("is not visible", () => expect(requirement.visibility).toBe(Visibility.None)); test("is not visible", () => expect(requirement.visibility).toBe(Visibility.None));
test("does not require pay", () => expect(requirement.requiresPay).toBe(false)); test("does not require pay", () => expect(requirement.requiresPay).toBe(false));
test("does not spend resources", () => test("spends resources", () => expect(requirement.spendResources).toBe(true));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((requirement as any).spendResources).toBe(false));
test("can maximize", () => expect(unref(requirement.canMaximize)).toBe(true)); test("can maximize", () => expect(unref(requirement.canMaximize)).toBe(true));
}); });
@ -79,7 +76,8 @@ describe("Creating cost requirement", () => {
const resource = createResource(ref(10)); const resource = createResource(ref(10));
const requirement = createCostRequirement(() => ({ const requirement = createCostRequirement(() => ({
resource, resource,
cost: 10 cost: 10,
spendResources: false
})); }));
expect(unref(requirement.requirementMet)).toBe(true); expect(unref(requirement.requirementMet)).toBe(true);
}); });
@ -88,7 +86,8 @@ describe("Creating cost requirement", () => {
const resource = createResource(ref(10)); const resource = createResource(ref(10));
const requirement = createCostRequirement(() => ({ const requirement = createCostRequirement(() => ({
resource, resource,
cost: 100 cost: 100,
spendResources: false
})); }));
expect(unref(requirement.requirementMet)).toBe(false); expect(unref(requirement.requirementMet)).toBe(false);
}); });
@ -148,7 +147,8 @@ describe("Checking maximum levels of requirements met", () => {
createBooleanRequirement(true), createBooleanRequirement(true),
createCostRequirement(() => ({ createCostRequirement(() => ({
resource: createResource(ref(10)), resource: createResource(ref(10)),
cost: Formula.variable(0) cost: Formula.variable(0),
spendResources: false
})) }))
]; ];
expect(maxRequirementsMet(requirements)).compare_tolerance(0); expect(maxRequirementsMet(requirements)).compare_tolerance(0);
@ -159,7 +159,8 @@ describe("Checking maximum levels of requirements met", () => {
createBooleanRequirement(true), createBooleanRequirement(true),
createCostRequirement(() => ({ createCostRequirement(() => ({
resource: createResource(ref(10)), resource: createResource(ref(10)),
cost: Formula.variable(0) cost: Formula.variable(0),
spendResources: false
})) }))
]; ];
expect(maxRequirementsMet(requirements)).compare_tolerance(10); expect(maxRequirementsMet(requirements)).compare_tolerance(10);
@ -171,11 +172,13 @@ test("Paying requirements", () => {
const noPayment = createCostRequirement(() => ({ const noPayment = createCostRequirement(() => ({
resource, resource,
cost: 10, cost: 10,
requiresPay: false requiresPay: false,
spendResources: false
})); }));
const payment = createCostRequirement(() => ({ const payment = createCostRequirement(() => ({
resource, resource,
cost: 10 cost: 10,
spendResources: false
})); }));
payRequirements([noPayment, payment]); payRequirements([noPayment, payment]);
expect(resource.value).compare_tolerance(90); expect(resource.value).compare_tolerance(90);