squiggle/packages/squiggle-lang/__tests__/TS/TestHelpers.ts

84 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2022-04-20 03:42:24 +00:00
import {
run,
2022-04-29 14:10:41 +00:00
runPartial,
bindings,
2022-04-20 03:42:24 +00:00
squiggleExpression,
errorValueToString,
2022-04-29 20:29:42 +00:00
defaultImports,
defaultBindings,
jsImports,
2022-04-20 03:42:24 +00:00
} from "../../src/js/index";
2022-04-29 18:46:44 +00:00
export function testRun(
x: string,
2022-04-29 20:29:42 +00:00
bindings: bindings = defaultBindings,
imports: jsImports = defaultImports
2022-04-29 18:46:44 +00:00
): squiggleExpression {
let squiggleResult = run(
x,
bindings,
{
sampleCount: 1000,
xyPointLength: 100,
},
2022-04-29 20:29:42 +00:00
imports
2022-04-29 18:46:44 +00:00
);
2022-04-20 22:48:04 +00:00
if (squiggleResult.tag === "Ok") {
return squiggleResult.value;
} else {
throw new Error(
`Expected squiggle expression to evaluate but got error: ${errorValueToString(
squiggleResult.value
)}`
);
}
}
2022-04-29 18:46:44 +00:00
export function testRunPartial(
x: string,
2022-04-29 20:29:42 +00:00
bindings: bindings = defaultBindings,
imports: jsImports = defaultImports
2022-04-29 18:46:44 +00:00
): bindings {
let squiggleResult = runPartial(
x,
bindings,
{
sampleCount: 1000,
xyPointLength: 100,
},
2022-04-29 20:29:42 +00:00
imports
2022-04-29 18:46:44 +00:00
);
2022-04-29 14:10:41 +00:00
if (squiggleResult.tag === "Ok") {
return squiggleResult.value;
} else {
throw new Error(
`Expected squiggle expression to evaluate but got error: ${errorValueToString(
squiggleResult.value
)}`
);
}
}
2022-04-20 03:42:24 +00:00
export function failDefault() {
2022-04-20 04:50:46 +00:00
expect("be reached").toBe("codepath should never");
}
/**
* This appears also in `TestHelpers.res`. According to https://www.math.net/percent-error, it computes
* absolute error when numerical stability concerns make me not want to compute relative error.
* */
export function expectErrorToBeBounded(
received: number,
expected: number,
epsilon: number,
digits: number
) {
let distance = Math.abs(received - expected);
let expectedAbs = Math.abs(expected);
let normalizingDenom = Math.max(expectedAbs, 1);
let error = distance / normalizingDenom;
expect(Math.round(10 ** digits * error) / 10 ** digits).toBeLessThanOrEqual(
epsilon
);
2022-04-20 03:42:24 +00:00
}