diff --git a/__tests__/Functions__Test.re b/__tests__/Functions__Test.re index d5cc0d03..d32f44e8 100644 --- a/__tests__/Functions__Test.re +++ b/__tests__/Functions__Test.re @@ -69,4 +69,22 @@ describe("Functions", () => { test("max#3", () => { expect(Functions.max([|(-1.), (-2.), 0., (-2.2)|])) |> toEqual(0.) }); + test("random#1", () => { + expect(Functions.random(1, 5)) |> toBeLessThanOrEqual(5) + }); + test("random#2", () => { + expect(Functions.random(1, 5)) |> toBeGreaterThanOrEqual(1) + }); + test("up#1", () => { + expect(Functions.up(1, 5)) |> toEqual([|1, 2, 3, 4, 5|]) + }); + test("up#2", () => { + expect(Functions.up(-1, 5)) |> toEqual([|(-1), 0, 1, 2, 3, 4, 5|]) + }); + test("down#1", () => { + expect(Functions.down(5, 1)) |> toEqual([|5, 4, 3, 2, 1|]) + }); + test("down#2", () => { + expect(Functions.down(5, -1)) |> toEqual([|5, 4, 3, 2, 1, 0, (-1)|]) + }); }); diff --git a/src/utility/lib/Functions.re b/src/utility/lib/Functions.re index 4600f237..26a8bf20 100644 --- a/src/utility/lib/Functions.re +++ b/src/utility/lib/Functions.re @@ -13,8 +13,9 @@ let sum = Belt.Array.reduce(_, 0., (i, j) => i +. j); let mean = a => sum(a) /. (Array.length(a) |> float_of_int); let min = a => Belt.Array.reduce(a, a[0], (i, j) => i < j ? i : j); let max = a => Belt.Array.reduce(a, a[0], (i, j) => i > j ? i : j); -let up = (a, b) => Array.make(b - a, a) |> Array.mapi((i, c) => c + i); -let down = (a, b) => Array.make(a - b, a) |> Array.mapi((i, c) => c - i); +let up = (a, b) => Array.make(b - a + 1, a) |> Array.mapi((i, c) => c + i); +let down = (a, b) => + Array.make(a - b + 1, a) |> Array.mapi((i, c) => c - i); let range = (min: float, max: float, n: int): array(float) => { switch (n) { | 0 => [||]