Added tests for XYShape validator

This commit is contained in:
Ozzie Gooen 2022-04-28 14:52:44 -04:00
parent 1cca9bde38
commit 5dd0292b52
4 changed files with 59 additions and 46 deletions

View File

@ -19,6 +19,24 @@ let pointSetDist3: PointSetTypes.xyShape = {
}
describe("XYShapes", () => {
describe("Validator", () => {
makeTest("with no errors", XYShape.T.Validator.validate(pointSetDist1), None)
makeTest(
"when empty",
XYShape.T.Validator.validate({xs: [], ys: []})->E.O2.fmap(Errors.toString),
Some("XYShape validate Xs is empty"),
)
makeTest(
"when not sorted, different lengths, and not finite",
XYShape.T.Validator.validate({xs: [2.0, 1.0, infinity, 0.0], ys: [3.0, infinity]})->E.O2.fmap(
Errors.toString,
),
Some(
"Multiple Errors: [XYShape validate Xs is not sorted], [XYShape validate Xs and Ys have different lengths. Xs has length 4 and Ys has length 2], [XYShape validate Xs is not finite. Example value: Infinity], [XYShape validate Ys is not finite. Example value: Infinity]",
),
)
})
describe("logScorePoint", () => {
makeTest("When identical", XYShape.logScorePoint(30, pointSetDist1, pointSetDist1), Some(0.0))
makeTest(
@ -32,16 +50,6 @@ describe("XYShapes", () => {
Some(210.3721280423322),
)
})
// describe("transverse", () => {
// makeTest(
// "When very different",
// XYShape.Transversal._transverse(
// (aCurrent, aLast) => aCurrent +. aLast,
// [|1.0, 2.0, 3.0, 4.0|],
// ),
// [|1.0, 3.0, 6.0, 10.0|],
// )
// });
describe("integrateWithTriangles", () =>
makeTest(
"integrates correctly",

View File

@ -500,7 +500,11 @@ module A = {
let getBelowZero = (t: t) => Belt.Array.getBy(t, r => r < 0.0)
let isSorted = (t: t): bool =>
reduce(zip(t, tail(t)), true, (acc, (first, second)) => acc && first < second)
if Array.length(t) < 1 {
true
} else {
reduce(zip(t, tail(t)), true, (acc, (first, second)) => acc && first < second)
}
//Passing true for the exclusive parameter excludes both endpoints of the range.
//https://jstat.github.io/all.html
@ -618,7 +622,7 @@ module A = {
module A2 = {
let fmap = (a, b) => A.fmap(b, a)
let joinWith = (a, b) => A.joinWith(b, a)
let filter = (a,b) => A.filter(b, a)
let filter = (a, b) => A.filter(b, a)
}
module JsArray = {

View File

@ -20,10 +20,12 @@ let rec toString = (t: error) =>
| NotSorted({fnName, propertyName}) => `${fnName} ${propertyName} is not sorted`
| IsEmpty({fnName, propertyName}) => `${fnName} ${propertyName} is empty`
| NotFinite({fnName, propertyName}, exampleValue) =>
`${fnName} ${propertyName} is not finite. Example value: ${E.Float.toString(exampleValue)}`
`${fnName} ${propertyName} is not finite. Example value: ${E.Float.toString(exampleValue)}`
| DifferentLengths({fnName, p1Name, p2Name, p1Length, p2Length}) =>
`${fnName} ${p1Name} and ${p2Name} have different lengths. ${p1Name} has length ${E.I.toString(
p1Length,
)} and ${p2Name} has length ${E.I.toString(p2Length)}`
| Multiple(errors) => `Multiple Errors: ${E.A2.fmap(errors, toString) |> E.A.joinWith(", ")}`
| Multiple(errors) =>
`Multiple Errors: ${E.A2.fmap(errors, toString)->E.A2.fmap(r => `[${r}]`)
|> E.A.joinWith(", ")}`
}

View File

@ -60,41 +60,40 @@ module T = {
let fromZippedArray = (pairs: array<(float, float)>): t => pairs |> Belt.Array.unzip |> fromArray
let equallyDividedXs = (t: t, newLength) => E.A.Floats.range(minX(t), maxX(t), newLength)
let toJs = (t: t) => {"xs": t.xs, "ys": t.ys}
}
module Validator = {
type t = T.t
let fnName = "XYShape validate"
let property = (propertyName: string): Errors.property => {
fnName: fnName,
propertyName: propertyName,
}
let notSortedError = (p: string): Errors.error => NotSorted(property(p))
let notFiniteError = (p, exampleValue): Errors.error => NotFinite(property(p), exampleValue)
let isEmptyError = (propertyName): Errors.error => IsEmpty(property(propertyName))
let differentLengthsError = (t): Errors.error => DifferentLengths({
fnName: fnName,
p1Name: "Xs",
p2Name: "Ys",
p1Length: E.A.length(T.xs(t)),
p2Length: E.A.length(T.ys(t)),
})
module Validator = {
let fnName = "XYShape validate"
let property = (propertyName: string): Errors.property => {
fnName: fnName,
propertyName: propertyName,
}
let notSortedError = (p: string): Errors.error => NotSorted(property(p))
let notFiniteError = (p, exampleValue): Errors.error => NotFinite(property(p), exampleValue)
let isEmptyError = (propertyName): Errors.error => IsEmpty(property(propertyName))
let differentLengthsError = (t): Errors.error => DifferentLengths({
fnName: fnName,
p1Name: "Xs",
p2Name: "Ys",
p1Length: E.A.length(xs(t)),
p2Length: E.A.length(ys(t)),
})
let areXsSorted = (t: t) => E.A.Floats.isSorted(T.xs(t))
let areXsEmpty = (t: t) => E.A.length(t.xs) == 0
let getNonFiniteXs = (t: t) => t->T.xs->E.A.Floats.getNonFinite
let getNonFiniteYs = (t: t) => t->T.ys->E.A.Floats.getNonFinite
let areXsSorted = (t: t) => E.A.Floats.isSorted(xs(t))
let areXsEmpty = (t: t) => E.A.length(xs(t)) == 0
let getNonFiniteXs = (t: t) => t->xs->E.A.Floats.getNonFinite
let getNonFiniteYs = (t: t) => t->ys->E.A.Floats.getNonFinite
let validate = (t: t) => {
let xsNotSorted = areXsSorted(t) ? None : Some(notSortedError("Xs"))
let xsEmpty = areXsEmpty(t) ? None : Some(isEmptyError("Xs"))
let differentLengths =
E.A.length(T.xs(t)) !== E.A.length(T.ys(t)) ? Some(differentLengthsError(t)) : None
let xsNotFinite = getNonFiniteXs(t)->E.O2.fmap(notFiniteError("Xs"))
let ysNotFinite = getNonFiniteYs(t)->E.O2.fmap(notFiniteError("Ys"))
[xsNotSorted, xsEmpty, differentLengths, xsNotFinite, ysNotFinite]
->E.A.O.concatSomes
->Errors.mapErrorArrayToError
let validate = (t: t) => {
let xsNotSorted = areXsSorted(t) ? None : Some(notSortedError("Xs"))
let xsEmpty = areXsEmpty(t) ? Some(isEmptyError("Xs")) : None
let differentLengths =
E.A.length(xs(t)) !== E.A.length(ys(t)) ? Some(differentLengthsError(t)) : None
let xsNotFinite = getNonFiniteXs(t)->E.O2.fmap(notFiniteError("Xs"))
let ysNotFinite = getNonFiniteYs(t)->E.O2.fmap(notFiniteError("Ys"))
[xsNotSorted, xsEmpty, differentLengths, xsNotFinite, ysNotFinite]
->E.A.O.concatSomes
->Errors.mapErrorArrayToError
}
}
}