6e30ce44e0
The flange, built by hand in the app: ten variables (outer size, bore, thickness, hub wall, the hub's size worked out from bore and wall, hub height, how many bolt holes, their size, the bolt circle worked out from the others, the chamfer); a disc, a hub on its top face, a bore through both, a bolt hole placed by a variable, patterned by a variable and cut, a chamfer. Its volume is the formula's to the hundredth of a cubic millimetre, and stays so through every change of every variable.
What that found:
- A variable's name typed into a size on the line was made the number it came to, and the link never kept. And a size that followed a variable, opened and closed again, was cut loose from it. (The line is the only way to type a size since it became the only UI.) Now the formula is kept; a size that follows one says so ("od / 2 = 60 mm"), its box opens on the formula, closed unchanged it does nothing, and it is not dragged sideways (a finger sliding off it would cut it loose). The box has ƒx: one of the project's variables, or a new one from what is typed. What can't be read is said, with the name no variable has.
- The project's unit changed, every plain number in a formula was read in the new one: a flange 160 mm across was 160 inches. Nothing changes size now: each formula that would is given the unit its plain lengths were typed in ("160 mm", "od / 2 + 5 mm"); a count, a factor and an angle are left as they are. And the unit is the project's own before its formulas are read (it was set after, so the model was a step behind).
- A chain of variables stopped at eight deep: the ninth "couldn't be evaluated". Any depth now; one that uses itself, round any number of others, is refused. Each is worked out once (a variable used twice by each of thirty built on one another was a thousand million readings).
- More to write formulas with: floor, ceil, round (how many fit: floor(length / pitch)), min and max of several, asin, acos, atan.
- New Variable: a value refused closed the window with what was typed gone - it stays to put right. A name that differs from another only in its capitals is refused. A name no variable has is named. An angle (30 deg) is taken, as it was in the list. One made again by the same name says it changed. Deleting: capitals count (W gone is not w gone); another variable that still uses it is named; a point placed by it keeps its place.
- A variable with no place of its own had no tag in the view: a point placed by one, the count of a ring of holes cut into the body, one that only other variables are made from. All ten of the flange's have one.
Not done, found on the way: a pattern started from a hole's face copies the whole body (holes by the count of a variable are a pin, patterned, then cut - as here); a sketch pattern's count is a plain number.
In the app: the ten variables made in the window one after another; names refused (sin, max, m, 2x), a value that uses itself, an unknown name, a formula cut short, a number too big; the flange built step by step with each size typed as its variable; nine changes of variables in the list of them all, each volume the formula's; 0, 1 and 2.6 holes, a bore bigger than the flange, a thickness below zero, each put back; undo and redo; inches, centimetres and back; a size changed from its tag in the view; a pattern's count edited to n + 2; the typing box at phone width.
Tests: variablesflange 22 (the hand-built project: its variables changed alone, together, to formulas, to awkward values and back, and through three units); unitchange 22; units 55 (27 new); vartags 20; the rest as before.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
91 lines
4.1 KiB
TypeScript
91 lines
4.1 KiB
TypeScript
// Run: npx tsx tests/units.test.ts - what a size field accepts.
|
|
import { evaluate, setUnits, setVariables } from "../src/units";
|
|
|
|
let failed = 0;
|
|
const near = (a: number | null, b: number) => a !== null && Math.abs(a - b) < 1e-9;
|
|
function check(text: string, kind: "length" | "number" | "angle", want: number | null): void {
|
|
const got = evaluate(text, kind);
|
|
const ok = want === null ? got === null : near(got, want);
|
|
if (!ok) failed++;
|
|
console.log(ok ? "ok " : "FAIL", `${kind} "${text}" = ${got} (want ${want})`);
|
|
}
|
|
|
|
setVariables([{ name: "af", expr: "36 mm" }, { name: "tilt", expr: "30 deg" }]);
|
|
check("36/sqrt(3)", "length", 36 / Math.sqrt(3)); // a hex's corner radius from its size across the flats
|
|
check("af/sqrt(3)", "length", 36 / Math.sqrt(3));
|
|
check("2^3", "number", 8);
|
|
check("2^3^2", "number", 512); // right to left
|
|
check("3*2^2", "length", 12);
|
|
check("10*sin(30)", "length", 5); // degrees
|
|
check("cos(60)", "number", 0.5);
|
|
check("abs(-4)", "length", 4);
|
|
check("2*pi", "number", 2 * Math.PI);
|
|
check("1in + 1", "length", 26.4);
|
|
check("sqrt 3", "length", null); // needs its brackets
|
|
check("sqrt(3", "length", null);
|
|
check("nope(3)", "length", null);
|
|
check("5,5", "length", 5.5); // a decimal comma
|
|
check("1,000", "length", null); // one, or a thousand? not guessed
|
|
check("+5", "length", 5);
|
|
check("5.", "length", 5); // a dot typed, nothing after it yet
|
|
check("2^1000", "length", null); // no size is that big
|
|
check("tilt", "angle", 30); // a variable that is an angle, in an angle box
|
|
check("30°", "angle", 30);
|
|
check("30 deg", "length", null);
|
|
|
|
// More functions: whole numbers, the angle of a slope, the smaller and the larger of several.
|
|
check("floor(7.9)", "number", 7);
|
|
check("ceil(7.1)", "number", 8);
|
|
check("round(7.5)", "number", 8);
|
|
check("floor(120 / 25)", "number", 4); // how many 25s fit in 120
|
|
check("floor(0.3 * 10)", "number", 3); // 2.9999999999999996 is 3
|
|
check("atan(1)", "angle", 45);
|
|
check("asin(0.5)", "angle", 30);
|
|
check("acos(0.5)", "angle", 60);
|
|
check("min(3, 8, 5)", "length", 3);
|
|
check("max(3; 8; 5)", "length", 8);
|
|
check("max(1,5)", "length", 5); // a comma parts the values here, it is no decimal comma
|
|
check("min(af, 20) / 2", "length", 10);
|
|
check("min(3)", "length", 3);
|
|
check("floor(3, 4)", "length", null); // one value only
|
|
|
|
// A long chain of variables, each built on the one before: it stopped at eight deep.
|
|
setVariables(Array.from({ length: 40 }, (_, i) => ({ name: `v${i}`, expr: i ? `v${i - 1} + 1` : "1" })));
|
|
check("v39", "length", 40);
|
|
check("v39 * 2 - v0", "length", 79);
|
|
// Each used twice by the next, twenty deep: a million readings if each is read again every time.
|
|
setVariables(Array.from({ length: 30 }, (_, i) => ({ name: `d${i}`, expr: i ? `d${i - 1} / 2 + d${i - 1} / 2` : "8" })));
|
|
const t0 = performance.now();
|
|
check("d29", "length", 8);
|
|
const took = performance.now() - t0;
|
|
if (took > 200) failed++;
|
|
console.log(took > 200 ? "FAIL" : "ok ", `thirty deep, each used twice: ${took.toFixed(1)} ms`);
|
|
// A circle of them is refused, not followed for ever.
|
|
setVariables([{ name: "a", expr: "b + 1" }, { name: "b", expr: "c * 2" }, { name: "c", expr: "a" }, { name: "ok", expr: "5" }]);
|
|
check("a", "length", null);
|
|
check("ok + 1", "length", 6);
|
|
setVariables([{ name: "s", expr: "s" }]);
|
|
check("s", "length", null);
|
|
// What it knows of one set of variables is not kept for the next.
|
|
setVariables([{ name: "w", expr: "10" }]);
|
|
check("w * 2", "length", 20);
|
|
setVariables([{ name: "w", expr: "15" }]);
|
|
check("w * 2", "length", 30);
|
|
// Units: a variable with its unit means the same whatever unit is in use; one without is in that unit.
|
|
setUnits("in");
|
|
setVariables([{ name: "wall", expr: "2 mm" }, { name: "n", expr: "4" }, { name: "len", expr: "3" }]);
|
|
check("wall", "length", 2);
|
|
check("wall * 2", "length", 4);
|
|
check("2 * wall", "length", 4);
|
|
check("len", "length", 76.2);
|
|
check("len / 2", "length", 38.1);
|
|
check("len * n", "length", 304.8);
|
|
check("n", "number", 4);
|
|
check("n + 1", "number", 5);
|
|
check("sqrt(16)", "length", 4 * 25.4);
|
|
check("max(1, 2)", "length", 2 * 25.4);
|
|
setUnits("mm");
|
|
check("len", "length", 3); // read again for the unit now in use
|
|
|
|
process.exit(failed ? 1 : 0);
|