Adds tests

This commit is contained in:
Roman Galochkin 2020-02-25 12:45:41 +03:00
parent 188266546d
commit 946e2dad7e
5 changed files with 72 additions and 6 deletions

View File

@ -100,4 +100,47 @@ describe("CDF", () => {
expect(Dist.findX(4.)) |> toEqual(1.) expect(Dist.findX(4.)) |> toEqual(1.)
}); });
}); });
describe("convertWithAlternativeXs", () => {
open Functions;
let xs = up(1, 9);
let ys = up(20, 28);
module Dist =
CDF.Make({
let shape = CDF.order({xs, ys});
});
let xs2 = up(3, 7);
module Dist2 =
CDF.Make({
let shape = Dist.convertWithAlternativeXs(xs2);
});
test("#1", () => {
expect(Dist2.xs) |> toEqual([|3., 4., 5., 6., 7.|])
});
test("#2", () => {
expect(Dist2.ys) |> toEqual([|22., 23., 24., 25., 26.|])
});
});
describe("convertToNewLength", () => {
open Functions;
let xs = up(1, 9);
let ys = up(50, 58);
module Dist =
CDF.Make({
let shape = CDF.order({xs, ys});
});
module Dist2 =
CDF.Make({
let shape = Dist.convertToNewLength(3);
});
test("#1", () => {
expect(Dist2.xs) |> toEqual([|1., 5., 9.|])
});
test("#2", () => {
expect(Dist2.ys) |> toEqual([|50., 54., 58.|])
});
});
}); });

View File

@ -76,15 +76,17 @@ describe("Functions", () => {
expect(Functions.random(1, 5)) |> toBeGreaterThanOrEqual(1) expect(Functions.random(1, 5)) |> toBeGreaterThanOrEqual(1)
}); });
test("up#1", () => { test("up#1", () => {
expect(Functions.up(1, 5)) |> toEqual([|1, 2, 3, 4, 5|]) expect(Functions.up(1, 5)) |> toEqual([|1., 2., 3., 4., 5.|])
}); });
test("up#2", () => { test("up#2", () => {
expect(Functions.up(-1, 5)) |> toEqual([|(-1), 0, 1, 2, 3, 4, 5|]) expect(Functions.up(-1, 5))
|> toEqual([|(-1.), 0., 1., 2., 3., 4., 5.|])
}); });
test("down#1", () => { test("down#1", () => {
expect(Functions.down(5, 1)) |> toEqual([|5, 4, 3, 2, 1|]) expect(Functions.down(5, 1)) |> toEqual([|5., 4., 3., 2., 1.|])
}); });
test("down#2", () => { test("down#2", () => {
expect(Functions.down(5, -1)) |> toEqual([|5, 4, 3, 2, 1, 0, (-1)|]) expect(Functions.down(5, -1))
|> toEqual([|5., 4., 3., 2., 1., 0., (-1.)|])
}); });
}); });

View File

@ -78,5 +78,17 @@ module Make = (Config: Config) => {
}; };
}; };
}; };
let convertWithAlternativeXs = (newXs: array(float)): DistTypes.xyShape => {
let newYs = Belt.Array.map(newXs, findY);
{xs: newXs, ys: newYs};
};
let convertToNewLength = (newLength: int): DistTypes.xyShape => {
Functions.(
range(min(xs), max(xs), newLength) |> convertWithAlternativeXs
);
};
let sampleSingle = (): float => Js.Math.random() |> findY;
let sample = (size: int): array(float) =>
Belt.Array.makeBy(size, i => sampleSingle());
1; 1;
}; };

View File

@ -13,9 +13,14 @@ let sum = Belt.Array.reduce(_, 0., (i, j) => i +. j);
let mean = a => sum(a) /. (Array.length(a) |> float_of_int); 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 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 max = a => Belt.Array.reduce(a, a[0], (i, j) => i > j ? i : j);
let up = (a, b) => Array.make(b - a + 1, a) |> Array.mapi((i, c) => c + i); let up = (a, b) =>
Array.make(b - a + 1, a)
|> Array.mapi((i, c) => c + i)
|> Belt.Array.map(_, float_of_int);
let down = (a, b) => let down = (a, b) =>
Array.make(a - b + 1, a) |> Array.mapi((i, c) => c - i); Array.make(a - b + 1, a)
|> Array.mapi((i, c) => c - i)
|> Belt.Array.map(_, float_of_int);
let range = (min: float, max: float, n: int): array(float) => { let range = (min: float, max: float, n: int): array(float) => {
switch (n) { switch (n) {
| 0 => [||] | 0 => [||]

View File

@ -136,6 +136,7 @@ class ContinuousDistribution {
} }
/** /**
* @Done
* @param {number[]} xs * @param {number[]} xs
* @return {ContinuousDistribution} * @return {ContinuousDistribution}
*/ */
@ -145,6 +146,7 @@ class ContinuousDistribution {
} }
/** /**
* Done
* @param {number} newLength * @param {number} newLength
* @return {ContinuousDistribution} * @return {ContinuousDistribution}
*/ */
@ -154,6 +156,7 @@ class ContinuousDistribution {
} }
/** /**
* @Done
* @return {number} * @return {number}
*/ */
sampleSingle() { sampleSingle() {
@ -162,6 +165,7 @@ class ContinuousDistribution {
} }
/** /**
* @Done
* Poduce n samples, using ``sampleSingle`` for each. * Poduce n samples, using ``sampleSingle`` for each.
* @param size * @param size
* @return {number[]} * @return {number[]}