squiggle/packages/squiggle-lang/__tests__/XYShape_test.res

65 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2022-01-29 22:43:08 +00:00
open Jest
open Expect
let makeTest = (~only=false, str, item1, item2) =>
only
2022-04-12 23:59:40 +00:00
? Only.test(str, () => expect(item1)->toEqual(item2))
: test(str, () => expect(item1)->toEqual(item2))
2022-01-29 22:43:08 +00:00
2022-02-15 21:13:33 +00:00
let pointSetDist1: PointSetTypes.xyShape = {xs: [1., 4., 8.], ys: [0.2, 0.4, 0.8]}
2022-01-29 22:43:08 +00:00
2022-02-15 21:13:33 +00:00
let pointSetDist2: PointSetTypes.xyShape = {
2022-01-29 22:43:08 +00:00
xs: [1., 5., 10.],
ys: [0.2, 0.5, 0.8],
}
2022-02-15 21:13:33 +00:00
let pointSetDist3: PointSetTypes.xyShape = {
2022-01-29 22:43:08 +00:00
xs: [1., 20., 50.],
ys: [0.2, 0.5, 0.8],
}
2022-04-28 20:49:51 +00:00
let makeAndGetErrorString = (~xs, ~ys) =>
XYShape.T.make(~xs, ~ys)->E.R.getError->E.O2.fmap(XYShape.Error.toString)
2022-01-29 22:43:08 +00:00
describe("XYShapes", () => {
2022-04-28 18:52:44 +00:00
describe("Validator", () => {
makeTest(
2022-04-28 20:49:51 +00:00
"with no errors",
makeAndGetErrorString(~xs=[1.0, 4.0, 8.0], ~ys=[0.2, 0.4, 0.8]),
None,
2022-04-28 18:52:44 +00:00
)
2022-04-28 20:49:51 +00:00
makeTest("when empty", makeAndGetErrorString(~xs=[], ~ys=[]), Some("Xs is empty"))
2022-04-28 18:52:44 +00:00
makeTest(
"when not sorted, different lengths, and not finite",
2022-04-28 20:49:51 +00:00
makeAndGetErrorString(~xs=[2.0, 1.0, infinity, 0.0], ~ys=[3.0, Js.Float._NaN]),
2022-04-28 18:52:44 +00:00
Some(
2022-04-28 20:49:51 +00:00
"Multiple Errors: [Xs is not sorted], [Xs and Ys have different lengths. Xs has length 4 and Ys has length 2], [Xs is not finite. Example value: Infinity], [Ys is not finite. Example value: NaN]",
2022-04-28 18:52:44 +00:00
),
)
})
2022-01-29 22:43:08 +00:00
describe("logScorePoint", () => {
2022-02-15 21:13:33 +00:00
makeTest("When identical", XYShape.logScorePoint(30, pointSetDist1, pointSetDist1), Some(0.0))
2022-04-12 23:59:40 +00:00
makeTest(
"When similar",
XYShape.logScorePoint(30, pointSetDist1, pointSetDist2),
Some(1.658971191043856),
)
2022-01-29 22:43:08 +00:00
makeTest(
"When very different",
2022-02-15 21:13:33 +00:00
XYShape.logScorePoint(30, pointSetDist1, pointSetDist3),
2022-01-29 22:43:08 +00:00
Some(210.3721280423322),
)
})
describe("integrateWithTriangles", () =>
makeTest(
"integrates correctly",
2022-02-15 21:13:33 +00:00
XYShape.Range.integrateWithTriangles(pointSetDist1),
2022-01-29 22:43:08 +00:00
Some({
xs: [1., 4., 8.],
ys: [0.0, 0.9000000000000001, 3.3000000000000007],
}),
)
)
})