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

147 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-04-29 13:50:57 +00:00
import { Distribution, resultMap } from "../../src/js/index";
2022-04-29 14:10:41 +00:00
import { testRun, testRunPartial } from "./TestHelpers";
2022-03-22 02:33:28 +00:00
2022-04-11 01:06:13 +00:00
function Ok<b>(x: b) {
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", () => {
test("mean(normal(5,2))", () => {
expect(testRun("mean(normal(5,2))")).toEqual({
2022-04-11 03:16:31 +00:00
tag: "number",
value: 5,
});
});
test("10+10", () => {
let foo = testRun("10 + 10");
2022-04-11 03:16:31 +00:00
expect(foo).toEqual({ tag: "number", value: 20 });
});
});
2022-03-23 02:12:20 +00:00
describe("Log function", () => {
test("log(1) = 0", () => {
let foo = testRun("log(1)");
2022-04-11 03:16:31 +00:00
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("Record", () => {
test("Return record", () => {
expect(testRun("{a: 1}")).toEqual({
tag: "record",
value: {
a: {
tag: "number",
value: 1,
},
},
});
});
});
2022-04-29 14:10:41 +00:00
describe("Partials", () => {
test("Can pass variables between partials and cells", () => {
let bindings = testRunPartial(`x = 5`);
let bindings2 = testRunPartial(`y = x + 2`, bindings);
expect(testRun(`y + 3`, bindings2)).toEqual({
tag: "number",
value: 10,
});
});
});
2022-04-29 18:46:44 +00:00
describe("Parameters", () => {
test("Can pass parameters into partials and cells", () => {
let bindings = testRunPartial(`y = $x + 2`, {}, { x: 1 }); // y = 3
let bindings2 = testRunPartial(`z = $x + y * $a`, bindings, { a: 3 }); // z = 1 + 3 * 3 = 10
expect(testRun(`z + $x + $a + y`, bindings2)).toEqual({
tag: "number",
value: 17,
});
});
test("Complicated deep parameters", () => {
expect(
testRun(
`$x.y[0][0].w + $x.z + $u.v`,
{},
{ x: { y: [[{ w: 1 }]], z: 2 }, u: { v: 3 } }
)
).toEqual({
tag: "number",
value: 6,
});
});
});
2022-04-11 01:06:13 +00:00
describe("Distribution", () => {
//It's important that sampleCount is less than 9. If it's more, than that will create randomness
2022-04-10 01:56:05 +00:00
//Also, note, the value should be created using makeSampleSetDist() later on.
let env = { sampleCount: 8, xyPointLength: 100 };
2022-04-26 16:22:31 +00:00
let dist1Samples = [3, 4, 5, 6, 6, 7, 10, 15, 30];
let dist1SampleCount = dist1Samples.length;
2022-04-11 01:06:13 +00:00
let dist = new Distribution(
{ tag: "SampleSet", value: [3, 4, 5, 6, 6, 7, 10, 15, 30] },
env
);
2022-04-11 01:06:13 +00:00
let dist2 = new Distribution(
{ tag: "SampleSet", value: [20, 22, 24, 29, 30, 35, 38, 44, 52] },
env
);
test("mean", () => {
2022-04-27 16:54:42 +00:00
expect(dist.mean().value).toBeCloseTo(9.5555555);
});
test("pdf", () => {
2022-04-27 16:54:42 +00:00
expect(dist.pdf(5.0).value).toBeCloseTo(0.10499097598222966, 1);
});
test("cdf", () => {
2022-04-26 16:22:31 +00:00
expect(dist.cdf(5.0).value).toBeCloseTo(
2022-04-27 16:54:42 +00:00
dist1Samples.filter((x) => x <= 5).length / dist1SampleCount,
1
2022-04-26 16:22:31 +00:00
);
});
test("inv", () => {
2022-04-26 16:22:31 +00:00
expect(dist.inv(0.5).value).toBeCloseTo(6);
});
test("toPointSet", () => {
expect(
resultMap(dist.toPointSet(), (r: Distribution) => r.toString())
2022-04-11 01:06:13 +00:00
).toEqual(Ok("Point Set Distribution"));
});
test("toSparkline", () => {
2022-04-26 15:59:07 +00:00
expect(dist.toSparkline(20).value).toEqual("▁▁▃▇█▇▄▂▂▂▁▁▁▁▁▂▂▁▁▁");
});
test("algebraicAdd", () => {
expect(
resultMap(dist.algebraicAdd(dist2), (r: Distribution) =>
r.toSparkline(20)
).value
2022-04-11 01:06:13 +00:00
).toEqual(Ok("▁▁▂▄▆████▇▆▄▄▃▃▃▂▁▁▁"));
});
test("pointwiseAdd", () => {
expect(
resultMap(dist.pointwiseAdd(dist2), (r: Distribution) =>
r.toSparkline(20)
).value
2022-04-27 16:54:42 +00:00
).toEqual(Ok("▁▂██▃▃▃▃▄▅▄▃▃▂▂▂▁▁▁▁"));
});
2022-02-18 01:50:32 +00:00
});