Remove hardcoded enum strings

This commit is contained in:
Sam Nolan 2022-08-29 13:46:52 +10:00
parent 25565ce5c0
commit e493437135
4 changed files with 12 additions and 32 deletions

View File

@ -1,6 +1,6 @@
import { SqProject, SqValue } from "../../src/js"; import { SqProject, SqValue } from "../../src/js";
import { SqNumberValue } from "../../src/js/SqValue"; import { SqNumberValue } from "../../src/js/SqValue";
import { failDefault, testRun } from "./TestHelpers"; import { testRun } from "./TestHelpers";
function Ok<b>(x: b) { function Ok<b>(x: b) {
return { tag: "Ok", value: x }; return { tag: "Ok", value: x };
@ -9,28 +9,11 @@ function Ok<b>(x: b) {
describe("Simple calculations and results", () => { describe("Simple calculations and results", () => {
test("mean(normal(5,2))", () => { test("mean(normal(5,2))", () => {
const result = testRun("mean(normal(5,2))"); // FIXME const result = testRun("mean(normal(5,2))"); // FIXME
expect(result.tag).toEqual("Number"); expect(result.value).toEqual(5);
switch (result.tag) {
case "Number":
expect(result.value).toEqual(5);
break;
default:
fail();
}
// tag: "number",
// value: 5,
// });
}); });
test("10+10", () => { test("10+10", () => {
let result = testRun("10 + 10") as SqNumberValue; let result = testRun("10 + 10") as SqNumberValue;
expect(result.tag).toEqual("Number"); expect(result.value).toEqual(20);
switch (result.tag) {
case "Number":
expect(result.value).toEqual(20);
break;
default:
fail();
}
}); });
}); });
// describe("Log function", () => { // describe("Log function", () => {

View File

@ -1,5 +1,5 @@
// import { errorValueToString } from "../../src/js/index"; // import { errorValueToString } from "../../src/js/index";
import { testRun, expectErrorToBeBounded } from "./TestHelpers"; import { testRun, expectErrorToBeBounded, SqValueTag } from "./TestHelpers";
import * as fc from "fast-check"; import * as fc from "fast-check";
describe("Mean of mixture is weighted average of means", () => { describe("Mean of mixture is weighted average of means", () => {
@ -20,7 +20,7 @@ describe("Mean of mixture is weighted average of means", () => {
let lognormalWeight = y / weightDenom; let lognormalWeight = y / weightDenom;
let betaMean = 1 / (1 + b / a); let betaMean = 1 / (1 + b / a);
let lognormalMean = m + s ** 2 / 2; let lognormalMean = m + s ** 2 / 2;
if (res.tag === "Number") { if (res.tag === SqValueTag.Number) {
expectErrorToBeBounded( expectErrorToBeBounded(
res.value, res.value,
betaWeight * betaMean + lognormalWeight * lognormalMean, betaWeight * betaMean + lognormalWeight * lognormalMean,

View File

@ -1,4 +1,4 @@
import { expectErrorToBeBounded, failDefault, testRun } from "./TestHelpers"; import { expectErrorToBeBounded, testRun, SqValueTag } from "./TestHelpers";
import * as fc from "fast-check"; import * as fc from "fast-check";
// Beware: float64Array makes it appear in an infinite loop. // Beware: float64Array makes it appear in an infinite loop.
@ -19,7 +19,7 @@ let arrayGen = () =>
let makeSampleSet = (samples: number[]) => { let makeSampleSet = (samples: number[]) => {
let sampleList = samples.map((x) => x.toFixed(20)).join(","); let sampleList = samples.map((x) => x.toFixed(20)).join(",");
let result = testRun(`SampleSet.fromList([${sampleList}])`); let result = testRun(`SampleSet.fromList([${sampleList}])`);
if (result.tag === "Distribution") { if (result.tag === SqValueTag.Distribution) {
return result.value; return result.value;
} else { } else {
fail("Expected to be distribution"); fail("Expected to be distribution");
@ -104,7 +104,7 @@ describe("cumulative density function", () => {
} else if (typeof cdfValue == "number") { } else if (typeof cdfValue == "number") {
expect(Math.round(1e5 * cdfValue) / 1e5).toBeLessThanOrEqual(1); expect(Math.round(1e5 * cdfValue) / 1e5).toBeLessThanOrEqual(1);
} else { } else {
failDefault(); fail();
} }
}) })
); );
@ -166,7 +166,7 @@ describe("mean is mean", () => {
1 1
); );
} else { } else {
failDefault(); fail();
} }
} }
) )
@ -191,7 +191,7 @@ describe("mean is mean", () => {
1 1
); );
} else { } else {
failDefault(); fail();
} }
} }
) )

View File

@ -1,4 +1,5 @@
import { run, SqValue } from "../../src/js"; import { run, SqValueTag } from "../../src/js";
export { SqValueTag };
export function testRun(x: string) { export function testRun(x: string) {
const { result, bindings } = run(x); // FIXME - set environment const { result, bindings } = run(x); // FIXME - set environment
@ -20,10 +21,6 @@ export function testRun(x: string) {
} }
} }
export function failDefault() {
expect("be reached").toBe("codepath should never");
}
/** /**
* This appears also in `TestHelpers.res`. According to https://www.math.net/percent-error, it computes * 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. * absolute error when numerical stability concerns make me not want to compute relative error.