aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-12 20:57:09 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commit803d435d2b7aedc58410693425b74a4c88a4b1bd (patch)
tree5fed971ed5dced66b4e640034cc243caa984f0f4 /test
parent772a6a78feae898e279735795cfc347e19944e5b (diff)
downloadgleam_stdlib-803d435d2b7aedc58410693425b74a4c88a4b1bd.tar.gz
gleam_stdlib-803d435d2b7aedc58410693425b74a4c88a4b1bd.zip
add int tests, increase test tolerance
Diffstat (limited to 'test')
-rw-r--r--test/gleam/float_test.gleam25
-rw-r--r--test/gleam/int_test.gleam27
2 files changed, 40 insertions, 12 deletions
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index 8234045..e8d7c21 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -353,7 +353,7 @@ pub fn random_uniform_test() {
}
pub fn random_between_test() {
- let test_boundary = fn(_acc, _e) {
+ let test_boundaries = fn(_acc, _e) {
float.random_between(-10.0, 0.0)
|> fn(x) { x >=. -10.0 && x <. 0.0 }
|> should.be_true
@@ -380,9 +380,14 @@ pub fn random_between_test() {
}
list.range(0, 100)
|> iterator.from_list()
- |> iterator.fold(Nil, test_boundary)
-
- let run_mean_tests = fn(iterations: Int, min: Float, max: Float) {
+ |> iterator.fold(Nil, test_boundaries)
+
+ let run_mean_tests = fn(
+ iterations: Int,
+ min: Float,
+ max: Float,
+ tolerance: Float,
+ ) {
let expected_average = float.sum([min, max]) /. 2.0
list.range(0, iterations)
|> iterator.from_list()
@@ -392,12 +397,12 @@ pub fn random_between_test() {
)
|> fn(sum) { sum /. int.to_float(iterations) }
// |> function.tap(fn(sum) { should.equal(sum, expected_average) })
- |> float.loosely_compare(expected_average, 3.0)
+ |> float.loosely_compare(expected_average, tolerance)
|> should.equal(order.Eq)
}
- run_mean_tests(100, 0.0, 0.0)
- run_mean_tests(1000, 0.0, 100.0)
- run_mean_tests(1000, -100.0, 100.0)
- run_mean_tests(1000, -100.0, 0.0)
- run_mean_tests(1000, 0.0, -100.0)
+ run_mean_tests(100, 0.0, 0.0, 5.0)
+ run_mean_tests(1_000, 0.0, 100.0, 5.0)
+ run_mean_tests(1_000, -100.0, 100.0, 5.0)
+ run_mean_tests(1_000, -100.0, 0.0, 5.0)
+ run_mean_tests(1_000, 0.0, -100.0, 5.0)
}
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 82612f6..afd2ac6 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -3,6 +3,8 @@ import gleam/int
import gleam/order
import gleam/list
import gleam/iterator
+import gleam/float
+import gleam/function
pub fn absolute_value_test() {
123
@@ -320,7 +322,7 @@ pub fn undigits_test() {
}
pub fn random_between_test() {
- let one_random_between_test_set = fn(_acc, _e) {
+ let test_boundaries = fn(_acc, _e) {
int.random_between(0, 0)
|> should.equal(0)
int.random_between(-1, 0)
@@ -335,5 +337,26 @@ pub fn random_between_test() {
}
list.range(0, 100)
|> iterator.from_list
- |> iterator.fold(Nil, one_random_between_test_set)
+ |> iterator.fold(Nil, test_boundaries)
+
+ let run_mean_tests = fn(iterations: Int, min: Int, max: Int, tolerance: Int) {
+ let expected_average = int.sum([min, max]) / 2
+ list.range(0, iterations)
+ |> iterator.from_list
+ |> iterator.fold(
+ from: 0,
+ with: fn(acc, _element) { acc + int.random_between(min, max) },
+ )
+ |> fn(sum) { sum / iterations }
+ // |> function.tap(fn(sum) { should.equal(sum, expected_average) })
+ |> fn(sum) {
+ sum - tolerance <= expected_average || sum + tolerance >= expected_average
+ }
+ |> should.be_true
+ }
+ run_mean_tests(100, 0, 0, 5)
+ run_mean_tests(1_000, 0, 100, 5)
+ run_mean_tests(1_000, -100, 100, 5)
+ run_mean_tests(1_000, -100, 0, 5)
+ run_mean_tests(1_000, 0, -100, 5)
}