squiggle/packages/squiggle-lang/__tests__/E/splitContinuousAndDiscrete_test.res

55 lines
1.8 KiB
Plaintext
Raw Normal View History

2022-04-07 03:01:17 +00:00
open Jest
2022-04-07 22:38:49 +00:00
open TestHelpers
2022-04-07 03:01:17 +00:00
2022-04-28 11:36:53 +00:00
let prepareInputs = (ar, minWeight) =>
2022-04-28 16:49:33 +00:00
E.A.Floats.Sorted.splitContinuousAndDiscreteForMinWeight(ar, ~minDiscreteWeight=minWeight) |> (
2022-04-28 11:36:53 +00:00
((c, disc)) => (c, disc |> E.FloatFloatMap.toArray)
2022-04-07 03:01:17 +00:00
)
2022-04-28 11:36:53 +00:00
describe("Continuous and discrete splits", () => {
2022-04-07 03:01:17 +00:00
makeTest(
2022-04-28 11:36:53 +00:00
"is empty, with no common elements",
prepareInputs([1.33455, 1.432, 2.0], 2),
2022-04-28 11:36:53 +00:00
([1.33455, 1.432, 2.0], []),
)
makeTest(
2022-04-28 11:36:53 +00:00
"only stores 3.5 as discrete when minWeight is 3",
prepareInputs([1.33455, 1.432, 2.0, 2.0, 3.5, 3.5, 3.5], 3),
([1.33455, 1.432, 2.0, 2.0], [(3.5, 3.0)]),
)
makeTest(
2022-04-28 11:36:53 +00:00
"doesn't store 3.5 as discrete when minWeight is 5",
prepareInputs([1.33455, 1.432, 2.0, 2.0, 3.5, 3.5, 3.5], 5),
([1.33455, 1.432, 2.0, 2.0, 3.5, 3.5, 3.5], []),
2022-04-07 03:01:17 +00:00
)
makeTest(
"more general test",
prepareInputs([10., 10., 11., 11., 11., 12., 13., 13., 13., 13., 13., 14.], 3),
([10., 10., 12., 14.], [(11., 3.), (13., 5.)]),
)
2022-04-07 03:01:17 +00:00
let makeDuplicatedArray = count => {
let arr = Belt.Array.range(1, count) |> E.A.fmap(float_of_int)
let sorted = arr |> Belt.SortArray.stableSortBy(_, compare)
E.A.concatMany([sorted, sorted, sorted, sorted]) |> Belt.SortArray.stableSortBy(_, compare)
}
2022-04-28 16:49:33 +00:00
let (_, discrete1) = E.A.Floats.Sorted.splitContinuousAndDiscreteForMinWeight(
2022-04-07 03:01:17 +00:00
makeDuplicatedArray(10),
2022-04-28 11:56:23 +00:00
~minDiscreteWeight=2,
2022-04-07 03:01:17 +00:00
)
2022-04-08 00:13:08 +00:00
let toArr1 = discrete1 |> E.FloatFloatMap.toArray
makeTest("splitMedium at count=10", toArr1 |> Belt.Array.length, 10)
2022-04-07 03:01:17 +00:00
2022-04-28 16:49:33 +00:00
let (_c, discrete2) = E.A.Floats.Sorted.splitContinuousAndDiscreteForMinWeight(
2022-04-07 03:01:17 +00:00
makeDuplicatedArray(500),
2022-04-28 11:56:23 +00:00
~minDiscreteWeight=2,
2022-04-07 03:01:17 +00:00
)
2022-04-08 00:13:08 +00:00
let toArr2 = discrete2 |> E.FloatFloatMap.toArray
makeTest("splitMedium at count=500", toArr2 |> Belt.Array.length, 500)
2022-04-26 15:59:07 +00:00
// makeTest("foo", [] |> Belt.Array.length, 500)
2022-04-07 03:01:17 +00:00
})