2022-09-01 10:36:27 +00:00
|
|
|
import { run, SqProject, SqValue, SqValueTag } from "../../src/js";
|
2022-08-29 03:46:52 +00:00
|
|
|
import { testRun } from "./TestHelpers";
|
2022-03-22 02:33:28 +00:00
|
|
|
|
2022-04-11 01:06:13 +00:00
|
|
|
function Ok<b>(x: b) {
|
2022-04-11 01:08:34 +00:00
|
|
|
return { tag: "Ok", value: x };
|
2022-04-11 01:06:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 02:12:20 +00:00
|
|
|
describe("Simple calculations and results", () => {
|
2022-04-08 23:48:53 +00:00
|
|
|
test("mean(normal(5,2))", () => {
|
2022-08-27 17:46:43 +00:00
|
|
|
const result = testRun("mean(normal(5,2))"); // FIXME
|
2022-09-01 10:36:27 +00:00
|
|
|
expect(result.toString()).toEqual("5");
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
test("10+10", () => {
|
2022-09-01 10:36:27 +00:00
|
|
|
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");
|
2022-04-08 23:48:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-01 10:36:27 +00:00
|
|
|
describe("Array", () => {
|
|
|
|
test("nested Array", () => {
|
|
|
|
expect(testRun("[[ 1 ]]").toString()).toEqual("[[1]]");
|
|
|
|
});
|
|
|
|
});
|
2022-04-28 20:16:31 +00:00
|
|
|
|
2022-09-01 10:36:27 +00:00
|
|
|
describe("Record", () => {
|
|
|
|
test("Return record", () => {
|
|
|
|
expect(testRun("{a:1}").toString()).toEqual("{a: 1}");
|
|
|
|
});
|
|
|
|
});
|
2022-04-28 20:16:31 +00:00
|
|
|
|
2022-09-01 10:36:27 +00:00
|
|
|
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("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;
|
2022-04-29 14:10:41 +00:00
|
|
|
|
2022-09-01 10:36:27 +00:00
|
|
|
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;
|
|
|
|
};
|
2022-04-29 18:46:44 +00:00
|
|
|
|
2022-09-01 10:36:27 +00:00
|
|
|
const dist = buildDist(dist1Samples);
|
|
|
|
const dist2 = buildDist([20, 22, 24, 29, 30, 35, 38, 44, 52]);
|
2022-04-09 01:31:08 +00:00
|
|
|
|
2022-09-01 10:36:27 +00:00
|
|
|
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())
|
|
|
|
// ).toEqual(Ok("Point Set Distribution"));
|
|
|
|
// });
|
|
|
|
// test("toSparkline", () => {
|
|
|
|
// expect(dist.toSparkline(20).value).toEqual("▁▁▃▇█▇▄▂▂▂▁▁▁▁▁▂▂▁▁▁");
|
|
|
|
// });
|
|
|
|
// test("algebraicAdd", () => {
|
|
|
|
// expect(
|
|
|
|
// resultMap(dist.algebraicAdd(dist2), (r: Distribution) =>
|
|
|
|
// r.toSparkline(20)
|
|
|
|
// ).value
|
|
|
|
// ).toEqual(Ok("▁▁▂▄▆████▇▆▄▄▃▃▃▂▁▁▁"));
|
|
|
|
// });
|
|
|
|
// test("pointwiseAdd", () => {
|
|
|
|
// expect(
|
|
|
|
// resultMap(dist.pointwiseAdd(dist2), (r: Distribution) =>
|
|
|
|
// r.toSparkline(20)
|
|
|
|
// ).value
|
|
|
|
// ).toEqual(Ok("▁▂██▃▃▃▃▄▅▄▃▃▂▂▂▁▁▁▁"));
|
|
|
|
// });
|
|
|
|
});
|