aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEthan Thoma <ethanthoma@gmail.com>2024-12-21 14:09:12 -0800
committerGitHub <noreply@github.com>2024-12-21 22:09:12 +0000
commit9d76bea763732dee0358feaeae46840cb75093be (patch)
tree767277049d9bedd56652ebf378cd35db3e98c4a3 /test
parent9dee307c7f1fa0641975c98a81da0b6f763c24b5 (diff)
downloadgleam_stdlib-9d76bea763732dee0358feaeae46840cb75093be.tar.gz
gleam_stdlib-9d76bea763732dee0358feaeae46840cb75093be.zip
Add `sample` function to List module, add `log` and `exp` functions to Float module (#772)
Diffstat (limited to 'test')
-rw-r--r--test/gleam/float_test.gleam78
-rw-r--r--test/gleam/list_test.gleam49
2 files changed, 127 insertions, 0 deletions
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index 7124d80..1c8f96b 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -528,3 +528,81 @@ pub fn subtract_test() {
|> float.subtract(2.0, _)
|> should.equal(-1.0)
}
+
+pub fn logarithm_test() {
+ float.logarithm(1.0)
+ |> result.unwrap(or: 1.0)
+ |> float.loosely_equals(with: 0.0, tolerating: 0.001)
+ |> should.be_true
+
+ float.logarithm(2.718281828459045)
+ |> result.unwrap(or: 0.0)
+ |> float.loosely_equals(with: 1.0, tolerating: 0.001)
+ |> should.be_true
+
+ float.logarithm(10.0)
+ |> result.unwrap(or: 0.0)
+ |> float.loosely_equals(with: 2.302585092994046, tolerating: 0.001)
+ |> should.be_true
+
+ float.logarithm(100.0)
+ |> result.unwrap(or: 0.0)
+ |> float.loosely_equals(with: 4.605170185988092, tolerating: 0.001)
+ |> should.be_true
+
+ float.logarithm(0.5)
+ |> result.unwrap(or: 0.0)
+ |> float.loosely_equals(with: -0.6931471805599453, tolerating: 0.001)
+ |> should.be_true
+
+ float.logarithm(0.1)
+ |> result.unwrap(or: 0.0)
+ |> float.loosely_equals(with: -2.3025850929940455, tolerating: 0.001)
+ |> should.be_true
+
+ float.logarithm(0.0)
+ |> should.equal(Error(Nil))
+
+ float.logarithm(-1.0)
+ |> should.equal(Error(Nil))
+
+ float.logarithm(-100.0)
+ |> should.equal(Error(Nil))
+
+ float.logarithm(-0.1)
+ |> should.equal(Error(Nil))
+}
+
+pub fn exponential_test() {
+ float.exponential(0.0)
+ |> float.loosely_equals(with: 1.0, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(1.0)
+ |> float.loosely_equals(with: 2.718281828459045, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(2.0)
+ |> float.loosely_equals(with: 7.38905609893065, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(-1.0)
+ |> float.loosely_equals(with: 0.36787944117144233, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(5.0)
+ |> float.loosely_equals(with: 148.4131591025766, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(-5.0)
+ |> float.loosely_equals(with: 0.006737946999085467, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(0.000001)
+ |> float.loosely_equals(with: 1.0000010000005, tolerating: 0.001)
+ |> should.be_true
+
+ float.exponential(-100.0)
+ |> float.loosely_equals(with: 3.720075976020836e-44, tolerating: 0.001)
+ |> should.be_true
+}
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index 2a67f94..6228396 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -1299,3 +1299,52 @@ pub fn max_test() {
|> list.max(string.compare)
|> should.equal(Ok("c"))
}
+
+pub fn sample_test() {
+ []
+ |> list.sample(3)
+ |> should.equal([])
+
+ [1, 2, 3]
+ |> list.sample(0)
+ |> should.equal([])
+
+ [1, 2, 3]
+ |> list.sample(-1)
+ |> should.equal([])
+
+ [1, 2]
+ |> list.sample(5)
+ |> list.sort(int.compare)
+ |> should.equal([1, 2])
+
+ [1]
+ |> list.sample(1)
+ |> should.equal([1])
+
+ let input = list.range(1, 100)
+ let sample = list.sample(input, 10)
+ list.length(sample)
+ |> should.equal(10)
+
+ let repeated = [1, 1, 1, 1, 1]
+ let sample = list.sample(repeated, 3)
+ sample
+ |> list.all(fn(x) { x == 1 })
+ |> should.be_true()
+
+ let input = list.range(1, 1000)
+ let sample = list.sample(input, 100)
+ sample
+ |> list.sort(int.compare)
+ |> list.all(fn(x) { x >= 1 && x <= 1000 })
+ |> should.be_true()
+
+ list.length(sample)
+ |> should.equal(100)
+
+ let min = list.fold(sample, 1000, int.min)
+ let max = list.fold(sample, 1, int.max)
+ should.be_true(min >= 1)
+ should.be_true(max <= 1000)
+} \ No newline at end of file