fix more tests for new TS classes

This commit is contained in:
Vyacheslav Matyukhin 2022-09-01 14:36:27 +04:00
parent eda1a45fda
commit 48fb634140
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
6 changed files with 130 additions and 160 deletions

View File

@ -1,5 +1,4 @@
import { SqProject, SqValue } from "../../src/js";
import { SqNumberValue } from "../../src/js/SqValue";
import { run, SqProject, SqValue, SqValueTag } from "../../src/js";
import { testRun } from "./TestHelpers";
function Ok<b>(x: b) {
@ -9,137 +8,101 @@ function Ok<b>(x: b) {
describe("Simple calculations and results", () => {
test("mean(normal(5,2))", () => {
const result = testRun("mean(normal(5,2))"); // FIXME
expect(result.value).toEqual(5);
expect(result.toString()).toEqual("5");
});
test("10+10", () => {
let result = testRun("10 + 10") as SqNumberValue;
expect(result.value).toEqual(20);
let result = testRun("10 + 10");
expect(result.toString()).toEqual("20");
});
});
describe("Log function", () => {
test("log(1) = 0", () => {
let foo = testRun("log(1)");
expect(foo.toString()).toEqual("0");
});
});
// describe("Log function", () => {
// test("log(1) = 0", () => {
// let foo = testRun("log(1)");
// expect(foo).toEqual({ tag: "number", value: 0 });
// });
// });
// describe("Array", () => {
// test("nested Array", () => {
// expect(testRun("[[1]]")).toEqual({
// tag: "array",
// value: [
// {
// tag: "array",
// value: [
// {
// tag: "number",
// value: 1,
// },
// ],
// },
// ],
// });
// });
// });
describe("Array", () => {
test("nested Array", () => {
expect(testRun("[[ 1 ]]").toString()).toEqual("[[1]]");
});
});
// describe("Record", () => {
// test("Return record", () => {
// expect(testRun("{a: 1}")).toEqual({
// tag: "record",
// value: {
// a: {
// tag: "number",
// value: 1,
// },
// },
// });
// });
// });
describe("Record", () => {
test("Return record", () => {
expect(testRun("{a:1}").toString()).toEqual("{a: 1}");
});
});
// describe("Partials", () => {
// test("Can pass variables between partials and cells", () => {
// const project = Project.create();
// project.setSource("p1", "x = 5");
// project.setSource("p2", "y = x + 2");
// project.setSource("main", "y + 3");
// project.run("main");
// const result = project.getResult("main");
// expect(result.tag).toEqual("Ok");
// expect(result.value).toBeInstanceOf(SquiggleValue);
// expect(result.value).toHaveProperty("tag", "number");
// failDefault(); // FIXME
// // let bindings = testRunPartial(`x = 5`);
// // let bindings2 = testRunPartial(`y = x + 2`, bindings);
// // expect(testRun(`y + 3`, bindings2)).toEqual({
// // tag: "number",
// // value: 10,
// // });
// });
// test("Can merge bindings from three partials", () => {
// let bindings1 = testRunPartial(`x = 1`);
// let bindings2 = testRunPartial(`y = 2`);
// let bindings3 = testRunPartial(`z = 3`);
// expect(
// testRun(`x + y + z`, mergeBindings([bindings1, bindings2, bindings3]))
// ).toEqual({
// tag: "number",
// value: 6,
// });
// });
// });
describe("Continues", () => {
test("Bindings from continues are accessible", () => {
const project = SqProject.create();
project.setSource("p1", "x = 5");
project.setSource("p2", "y = x + 2");
project.setSource("main", "y + 3");
project.setContinues("main", ["p2"]);
project.setContinues("p2", ["p1"]);
project.run("main");
const result = project.getResult("main");
expect(result.tag).toEqual("Ok");
expect(result.value.toString()).toEqual("10");
});
test("Can merge bindings from three partials", () => {
const project = SqProject.create();
project.setSource("p1", "x = 1");
project.setSource("p2", "y = 2");
project.setSource("p3", "z = 3");
project.setSource("main", "x + y + z");
project.setContinues("main", ["p1", "p2", "p3"]);
project.run("main");
const result = project.getResult("main");
expect(result.tag).toEqual("Ok");
expect(result.value.toString()).toEqual("6");
});
});
// describe("JS Imports", () => {
// test("Can pass parameters into partials and cells", () => {
// let bindings = testRunPartial(`y = $x + 2`, defaultBindings, { x: 1 });
// let bindings2 = testRunPartial(`z = y + $a`, bindings, { a: 3 });
// expect(testRun(`z`, bindings2)).toEqual({
// tag: "number",
// value: 6,
// });
// });
// test("Complicated deep parameters", () => {
// expect(
// testRun(`$x.y[0][0].w + $x.z + $u.v`, defaultBindings, {
// x: { y: [[{ w: 1 }]], z: 2 },
// u: { v: 3 },
// })
// ).toEqual({
// tag: "number",
// value: 6,
// });
// });
// });
describe("Distribution", () => {
//It's important that sampleCount is less than 9. If it's more, than that will create randomness
//Also, note, the value should be created using makeSampleSetDist() later on.
let env = { sampleCount: 8, xyPointLength: 100 };
let dist1Samples = [3, 4, 5, 6, 6, 7, 10, 15, 30];
let dist1SampleCount = dist1Samples.length;
// describe("Distribution", () => {
// //It's important that sampleCount is less than 9. If it's more, than that will create randomness
// //Also, note, the value should be created using makeSampleSetDist() later on.
// let env = { sampleCount: 8, xyPointLength: 100 };
// let dist1Samples = [3, 4, 5, 6, 6, 7, 10, 15, 30];
// let dist1SampleCount = dist1Samples.length;
// let dist = new Distribution(
// { tag: "SampleSet", value: [3, 4, 5, 6, 6, 7, 10, 15, 30] },
// env
// );
// let dist2 = new Distribution(
// { tag: "SampleSet", value: [20, 22, 24, 29, 30, 35, 38, 44, 52] },
// env
// );
const buildDist = (samples: number[]) => {
const src = `SampleSet.fromList([${samples.join(",")}])`;
const { result } = run(src, {
environment: env,
});
if (result.tag !== "Ok") {
throw new Error(
`Failed to build SampleSet: from ${src}: ${result.value}`
);
}
const dist = result.value;
if (dist.tag !== SqValueTag.Distribution) {
throw new Error("Expected Distribution");
}
return dist.value;
};
// test("mean", () => {
// expect(dist.mean().value).toBeCloseTo(9.5555555);
// });
// test("pdf", () => {
// expect(dist.pdf(5.0).value).toBeCloseTo(0.10499097598222966, 1);
// });
// test("cdf", () => {
// expect(dist.cdf(5.0).value).toBeCloseTo(
// dist1Samples.filter((x) => x <= 5).length / dist1SampleCount,
// 1
// );
// });
// test("inv", () => {
// expect(dist.inv(0.5).value).toBeCloseTo(6);
// });
const dist = buildDist(dist1Samples);
const dist2 = buildDist([20, 22, 24, 29, 30, 35, 38, 44, 52]);
test("mean", () => {
expect(dist.mean(env).value).toBeCloseTo(9.5555555);
});
test("pdf", () => {
expect(dist.pdf(env, 5.0).value).toBeCloseTo(0.10499097598222966, 1);
});
test("cdf", () => {
expect(dist.cdf(env, 5.0).value).toBeCloseTo(
dist1Samples.filter((x) => x <= 5).length / dist1SampleCount,
1
);
});
test("inv", () => {
expect(dist.inv(env, 0.5).value).toBeCloseTo(6);
});
// test("toPointSet", () => {
// expect(
// resultMap(dist.toPointSet(), (r: Distribution) => r.toString())
@ -162,4 +125,4 @@ describe("Simple calculations and results", () => {
// ).value
// ).toEqual(Ok("▁▂██▃▃▃▃▄▅▄▃▃▂▂▂▁▁▁▁"));
// });
// });
});

View File

@ -1,4 +1,3 @@
// import { errorValueToString } from "../../src/js/index";
import * as fc from "fast-check";
import { testRun } from "./TestHelpers";

View File

@ -36,9 +36,9 @@ describe("Squiggle's parser is whitespace insensitive", () => {
whitespaceGen(),
whitespaceGen(),
(a, b, c, d, e, f, g, h) => {
expect(testRun(squiggleString(a, b, c, d, e, f, g, h))).toEqual(
squiggleOutput
);
expect(
testRun(squiggleString(a, b, c, d, e, f, g, h))
).toEqualSqValue(squiggleOutput);
}
)
);

View File

@ -1,4 +1,3 @@
// import { errorValueToString } from "../../src/js/index";
import { testRun, expectErrorToBeBounded, SqValueTag } from "./TestHelpers";
import * as fc from "fast-check";

View File

@ -1,16 +1,21 @@
import { run, SqValueTag } from "../../src/js";
export { SqValueTag };
expect.extend({
toEqualSqValue(x, y) {
// hack via https://github.com/facebook/jest/issues/10329#issuecomment-820656061
const { getMatchers } = require("expect/build/jestMatchersObject");
return getMatchers().toEqual.call(this, x.toString(), y.toString());
},
});
export function testRun(x: string) {
const { result, bindings } = run(x); // FIXME - set environment
// x,
// bindings,
// {
// sampleCount: 1000,
// xyPointLength: 100,
// },
// imports
// );
const { result, bindings } = run(x, {
environment: {
sampleCount: 1000,
xyPointLength: 100,
},
});
if (result.tag === "Ok") {
return result.value;

View File

@ -32,6 +32,10 @@ export abstract class SqAbstractValue {
}
return value;
};
toString() {
return RSValue.toString(this._value);
}
}
export class SqArrayValue extends SqAbstractValue {