2022-07-17 02:50:18 +00:00
|
|
|
open Jest
|
|
|
|
open Expect
|
2022-07-17 05:01:26 +00:00
|
|
|
open Reducer_TestHelpers
|
2022-07-17 02:50:18 +00:00
|
|
|
|
2022-07-25 14:34:33 +00:00
|
|
|
let expectEvalToBeOk = (code: string) =>
|
2022-09-19 22:19:55 +00:00
|
|
|
Reducer_Expression.BackCompatible.evaluateString(code)->E.R.isOk->expect->toBe(true)
|
2022-07-17 02:50:18 +00:00
|
|
|
|
|
|
|
let registry = FunctionRegistry_Library.registry
|
|
|
|
let examples = E.A.to_list(FunctionRegistry_Core.Registry.allExamples(registry))
|
|
|
|
|
2022-07-19 15:12:04 +00:00
|
|
|
describe("FunctionRegistry Library", () => {
|
|
|
|
describe("Regular tests", () => {
|
2022-10-06 13:49:33 +00:00
|
|
|
testEvalToBe("List.length([3,5,8])", "Ok(3)")
|
|
|
|
testEvalToBe("List.length([])", "Ok(0)")
|
2022-07-19 15:12:04 +00:00
|
|
|
testEvalToBe("List.make(3, 'HI')", "Ok(['HI','HI','HI'])")
|
2022-09-18 14:33:01 +00:00
|
|
|
testEvalToBe("make(3, 'HI')", "Error(make is not defined)")
|
2022-07-19 15:12:04 +00:00
|
|
|
testEvalToBe("List.upTo(1,3)", "Ok([1,2,3])")
|
|
|
|
testEvalToBe("List.first([3,5,8])", "Ok(3)")
|
|
|
|
testEvalToBe("List.last([3,5,8])", "Ok(8)")
|
|
|
|
testEvalToBe("List.reverse([3,5,8])", "Ok([8,5,3])")
|
2022-07-19 17:35:31 +00:00
|
|
|
testEvalToBe("double(x)=2*x; arr=[1,2,3]; List.map(arr, double)", "Ok([2,4,6])")
|
2022-07-21 15:31:46 +00:00
|
|
|
testEvalToBe("double(x)=2*x; arr=[1,2,3]; map(arr, double)", "Ok([2,4,6])")
|
2022-07-19 17:35:31 +00:00
|
|
|
testEvalToBe("myadd(acc,x)=acc+x; arr=[1,2,3]; List.reduce(arr, 0, myadd)", "Ok(6)")
|
|
|
|
testEvalToBe("change(acc,x)=acc*x+x; arr=[1,2,3]; List.reduce(arr, 0, change)", "Ok(15)")
|
|
|
|
testEvalToBe("change(acc,x)=acc*x+x; arr=[1,2,3]; List.reduceReverse(arr, 0, change)", "Ok(9)")
|
|
|
|
testEvalToBe("check(x)=(x==2);arr=[1,2,3]; List.filter(arr,check)", "Ok([2])")
|
|
|
|
testEvalToBe("arr=[1,2,3]; List.reverse(arr)", "Ok([3,2,1])")
|
2022-07-19 15:12:04 +00:00
|
|
|
testEvalToBe("Dist.normal(5,2)", "Ok(Normal(5,2))")
|
|
|
|
testEvalToBe("normal(5,2)", "Ok(Normal(5,2))")
|
|
|
|
testEvalToBe("normal({mean:5,stdev:2})", "Ok(Normal(5,2))")
|
|
|
|
testEvalToBe("-2 to 4", "Ok(Normal(1,1.8238704957353074))")
|
|
|
|
testEvalToBe("pointMass(5)", "Ok(PointMass(5))")
|
|
|
|
testEvalToBe("Number.floor(5.5)", "Ok(5)")
|
|
|
|
testEvalToBe("Number.ceil(5.5)", "Ok(6)")
|
|
|
|
testEvalToBe("floor(5.5)", "Ok(5)")
|
|
|
|
testEvalToBe("ceil(5.5)", "Ok(6)")
|
|
|
|
testEvalToBe("Number.abs(5.5)", "Ok(5.5)")
|
|
|
|
testEvalToBe("abs(5.5)", "Ok(5.5)")
|
|
|
|
testEvalToBe("Number.exp(10)", "Ok(22026.465794806718)")
|
|
|
|
testEvalToBe("Number.log10(10)", "Ok(1)")
|
|
|
|
testEvalToBe("Number.log2(10)", "Ok(3.321928094887362)")
|
|
|
|
testEvalToBe("Number.sum([2,5,3])", "Ok(10)")
|
|
|
|
testEvalToBe("sum([2,5,3])", "Ok(10)")
|
|
|
|
testEvalToBe("Number.product([2,5,3])", "Ok(30)")
|
|
|
|
testEvalToBe("Number.min([2,5,3])", "Ok(2)")
|
|
|
|
testEvalToBe("Number.max([2,5,3])", "Ok(5)")
|
|
|
|
testEvalToBe("Number.mean([0,5,10])", "Ok(5)")
|
|
|
|
testEvalToBe("Number.geomean([1,5,18])", "Ok(4.481404746557164)")
|
|
|
|
testEvalToBe("Number.stdev([0,5,10,15])", "Ok(5.5901699437494745)")
|
|
|
|
testEvalToBe("Number.variance([0,5,10,15])", "Ok(31.25)")
|
|
|
|
testEvalToBe("Number.sort([10,0,15,5])", "Ok([0,5,10,15])")
|
|
|
|
testEvalToBe("Number.cumsum([1,5,3])", "Ok([1,6,9])")
|
|
|
|
testEvalToBe("Number.cumprod([1,5,3])", "Ok([1,5,15])")
|
|
|
|
testEvalToBe("Number.diff([1,5,3])", "Ok([4,-2])")
|
|
|
|
testEvalToBe(
|
|
|
|
"Dist.logScore({estimate: normal(5,2), answer: normal(5.2,1), prior: normal(5.5,3)})",
|
|
|
|
"Ok(-0.33591375663884876)",
|
|
|
|
)
|
|
|
|
testEvalToBe(
|
|
|
|
"Dist.logScore({estimate: normal(5,2), answer: normal(5.2,1)})",
|
|
|
|
"Ok(0.32244107041564646)",
|
|
|
|
)
|
|
|
|
testEvalToBe("Dist.logScore({estimate: normal(5,2), answer: 4.5})", "Ok(1.6433360626394853)")
|
|
|
|
testEvalToBe("Dist.klDivergence(normal(5,2), normal(5,1.5))", "Ok(0.06874342818671068)")
|
2022-07-21 15:54:41 +00:00
|
|
|
testEvalToBe("SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)")
|
|
|
|
testEvalToBe("SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)")
|
2022-07-21 17:31:53 +00:00
|
|
|
testEvalToBe("SampleSet.fromFn({|| sample(normal(5,2))})", "Ok(Sample Set Distribution)")
|
2022-09-04 19:52:48 +00:00
|
|
|
testEvalToBe(
|
|
|
|
"SampleSet.min(SampleSet.fromDist(normal(50,2)), 2)",
|
|
|
|
"Ok(Sample Set Distribution)",
|
|
|
|
)
|
2022-09-01 05:14:41 +00:00
|
|
|
testEvalToBe("mean(SampleSet.min(SampleSet.fromDist(normal(50,2)), 2))", "Ok(2)")
|
2022-09-04 19:52:48 +00:00
|
|
|
testEvalToBe(
|
|
|
|
"SampleSet.max(SampleSet.fromDist(normal(50,2)), 10)",
|
|
|
|
"Ok(Sample Set Distribution)",
|
|
|
|
)
|
2022-07-21 15:31:46 +00:00
|
|
|
testEvalToBe(
|
2022-07-21 15:54:41 +00:00
|
|
|
"addOne(t)=t+1; SampleSet.toList(SampleSet.map(SampleSet.fromList([1,2,3,4,5,6]), addOne))",
|
2022-07-21 15:31:46 +00:00
|
|
|
"Ok([2,3,4,5,6,7])",
|
|
|
|
)
|
|
|
|
testEvalToBe(
|
2022-07-21 18:29:59 +00:00
|
|
|
"SampleSet.toList(SampleSet.mapN([SampleSet.fromList([1,2,3,4,5,6]), SampleSet.fromList([6, 5, 4, 3, 2, 1])], {|x| x[0] > x[1] ? x[0] : x[1]}))",
|
2022-07-21 15:31:46 +00:00
|
|
|
"Ok([6,5,4,4,5,6])",
|
|
|
|
)
|
2022-10-06 20:19:43 +00:00
|
|
|
testEvalToBe(
|
|
|
|
"SampleSet.fromList([1, 2, 3])",
|
|
|
|
"Error(Error: Too few samples when constructing sample set)",
|
|
|
|
)
|
2022-09-20 15:42:50 +00:00
|
|
|
|
|
|
|
testEvalToBe("Dict.merge({a: 1, b: 2}, {b: 3, c: 4, d: 5})", "Ok({a: 1,b: 3,c: 4,d: 5})")
|
2022-09-28 04:16:05 +00:00
|
|
|
testEvalToBe(
|
|
|
|
"Dict.mergeMany([{a: 1, b: 2}, {c: 3, d: 4}, {c: 5, e: 6}])",
|
|
|
|
"Ok({a: 1,b: 2,c: 5,d: 4,e: 6})",
|
|
|
|
)
|
2022-09-20 15:42:50 +00:00
|
|
|
testEvalToBe("Dict.keys({a: 1, b: 2})", "Ok(['a','b'])")
|
|
|
|
testEvalToBe("Dict.values({a: 1, b: 2})", "Ok([1,2])")
|
|
|
|
testEvalToBe("Dict.toList({a: 1, b: 2})", "Ok([['a',1],['b',2]])")
|
|
|
|
testEvalToBe("Dict.fromList([['a', 1], ['b', 2]])", "Ok({a: 1,b: 2})")
|
2022-07-17 02:50:18 +00:00
|
|
|
})
|
|
|
|
|
2022-07-19 15:12:04 +00:00
|
|
|
describe("Fn auto-testing", () => {
|
2022-10-12 16:11:28 +00:00
|
|
|
testAll(
|
|
|
|
"tests of validity",
|
|
|
|
examples,
|
|
|
|
r => {
|
|
|
|
expectEvalToBeOk(r)
|
|
|
|
},
|
|
|
|
)
|
2022-07-17 05:01:26 +00:00
|
|
|
|
2022-07-19 15:12:04 +00:00
|
|
|
testAll(
|
|
|
|
"tests of type",
|
|
|
|
E.A.to_list(
|
2022-10-12 16:11:28 +00:00
|
|
|
FunctionRegistry_Core.Registry.allExamplesWithFns(registry)->E.A2.filter(
|
|
|
|
((fn, _)) => E.O.isSome(fn.output),
|
2022-07-19 15:12:04 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
((fn, example)) => {
|
|
|
|
let responseType =
|
|
|
|
example
|
2022-08-19 18:56:45 +00:00
|
|
|
->Reducer_Expression.BackCompatible.evaluateString
|
2022-09-19 19:10:09 +00:00
|
|
|
->E.R2.fmap(Reducer_Value.valueToValueType)
|
2022-07-19 15:12:04 +00:00
|
|
|
let expectedOutputType = fn.output |> E.O.toExn("")
|
|
|
|
expect(responseType)->toEqual(Ok(expectedOutputType))
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2022-07-17 05:01:26 +00:00
|
|
|
})
|