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

55 lines
2.2 KiB
Plaintext
Raw Normal View History

2022-04-07 22:38:49 +00:00
open Jest
open Expect
2022-04-13 23:17:49 +00:00
let expectErrorToBeBounded = (received, expected, ~epsilon) => {
let distance = Js.Math.abs_float(received -. expected)
2022-04-13 23:58:36 +00:00
let expectedAbs = Js.Math.abs_float(expected)
let normalizingDenom = Js.Math.max_float(
expectedAbs,
epsilon < 1.0 ? epsilon ** 2.0 : epsilon ** -2.0,
)
let error = distance /. normalizingDenom
2022-04-13 23:18:08 +00:00
error->expect->toBeLessThan(epsilon)
2022-04-13 23:17:49 +00:00
}
2022-04-07 22:38:49 +00:00
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-04-07 22:38:49 +00:00
let {toFloat, toDist, toString, toError, fmap} = module(DistributionOperation.Output)
let fnImage = (theFn, inps) => Js.Array.map(theFn, inps)
let env: DistributionOperation.env = {
2022-04-13 04:55:41 +00:00
sampleCount: 10000,
xyPointLength: 1000,
2022-04-07 22:38:49 +00:00
}
let run = DistributionOperation.run(~env)
let outputMap = fmap(~env)
let unreachableInTestFileMessage = "Should be impossible to reach (This error is in test file)"
let toExtFloat: option<float> => float = E.O.toExt(unreachableInTestFileMessage)
2022-04-13 05:02:53 +00:00
let toExtDist: option<DistributionTypes.genericDist> => DistributionTypes.genericDist = E.O.toExt(
unreachableInTestFileMessage,
)
2022-04-07 22:38:49 +00:00
// let toExt: option<'a> => 'a = E.O.toExt(unreachableInTestFileMessage)
2022-04-13 05:02:53 +00:00
let unpackFloat = x => x->toFloat->toExtFloat
let unpackDist = y => y->toDist->toExtDist
2022-04-12 21:06:53 +00:00
let mkNormal = (mean, stdev) => DistributionTypes.Symbolic(#Normal({mean: mean, stdev: stdev}))
let mkBeta = (alpha, beta) => DistributionTypes.Symbolic(#Beta({alpha: alpha, beta: beta}))
let mkExponential = rate => DistributionTypes.Symbolic(#Exponential({rate: rate}))
2022-04-13 05:02:53 +00:00
let mkUniform = (low, high) => DistributionTypes.Symbolic(#Uniform({low: low, high: high}))
2022-04-12 21:06:53 +00:00
let mkCauchy = (local, scale) => DistributionTypes.Symbolic(#Cauchy({local: local, scale: scale}))
let mkLognormal = (mu, sigma) => DistributionTypes.Symbolic(#Lognormal({mu: mu, sigma: sigma}))
let normalMake = SymbolicDist.Normal.make
let betaMake = SymbolicDist.Beta.make
let exponentialMake = SymbolicDist.Exponential.make
let uniformMake = SymbolicDist.Uniform.make
let cauchyMake = SymbolicDist.Cauchy.make
let lognormalMake = SymbolicDist.Lognormal.make
2022-04-13 05:02:53 +00:00
let triangularMake = SymbolicDist.Triangular.make
let floatMake = SymbolicDist.Float.make