gleam-community-maths/test/gleam_community/arithmetics_test.gleam

204 lines
4 KiB
Gleam
Raw Permalink Normal View History

2024-12-07 22:46:49 +00:00
import gleam/float
import gleam_community/maths
import gleeunit/should
pub fn int_gcd_test() {
maths.gcd(1, 1)
|> should.equal(1)
maths.gcd(100, 10)
|> should.equal(10)
maths.gcd(10, 100)
|> should.equal(10)
maths.gcd(100, -10)
|> should.equal(10)
maths.gcd(-36, -17)
|> should.equal(1)
maths.gcd(-30, -42)
|> should.equal(6)
}
2024-12-08 19:40:33 +00:00
pub fn euclidean_modulo_test() {
2024-12-07 22:46:49 +00:00
// Base Case: Positive x, Positive y
// Note that the truncated, floored, and euclidean
// definitions should agree for this base case
2024-12-08 19:40:33 +00:00
maths.euclidean_modulo(15, 4)
2024-12-07 22:46:49 +00:00
|> should.equal(3)
// Case: Positive x, Negative y
2024-12-08 19:40:33 +00:00
maths.euclidean_modulo(15, -4)
2024-12-07 22:46:49 +00:00
|> should.equal(3)
// Case: Negative x, Positive y
2024-12-08 19:40:33 +00:00
maths.euclidean_modulo(-15, 4)
2024-12-07 22:46:49 +00:00
|> should.equal(1)
// Case: Negative x, Negative y
2024-12-08 19:40:33 +00:00
maths.euclidean_modulo(-15, -4)
2024-12-07 22:46:49 +00:00
|> should.equal(1)
// Case: Positive x, Zero y
2024-12-08 19:40:33 +00:00
maths.euclidean_modulo(5, 0)
2024-12-07 22:46:49 +00:00
|> should.equal(0)
// Case: Zero x, Negative y
2024-12-08 19:40:33 +00:00
maths.euclidean_modulo(0, 5)
2024-12-07 22:46:49 +00:00
|> should.equal(0)
}
2024-12-08 19:40:33 +00:00
pub fn lcm_test() {
2024-12-07 22:46:49 +00:00
maths.lcm(1, 1)
|> should.equal(1)
maths.lcm(100, 10)
|> should.equal(100)
maths.lcm(10, 100)
|> should.equal(100)
maths.lcm(100, -10)
|> should.equal(100)
maths.lcm(-36, -17)
|> should.equal(612)
maths.lcm(-30, -42)
|> should.equal(210)
}
2024-12-08 19:40:33 +00:00
pub fn proper_divisors_test() {
2024-12-07 22:46:49 +00:00
maths.proper_divisors(2)
|> should.equal([1])
maths.proper_divisors(6)
|> should.equal([1, 2, 3])
maths.proper_divisors(13)
|> should.equal([1])
maths.proper_divisors(18)
|> should.equal([1, 2, 3, 6, 9])
}
2024-12-08 19:40:33 +00:00
pub fn divisors_test() {
2024-12-07 22:46:49 +00:00
maths.divisors(2)
|> should.equal([1, 2])
maths.divisors(6)
|> should.equal([1, 2, 3, 6])
maths.divisors(13)
|> should.equal([1, 13])
maths.divisors(18)
|> should.equal([1, 2, 3, 6, 9, 18])
}
2024-12-08 19:40:33 +00:00
pub fn list_cumulative_sum_test() {
2024-12-07 22:46:49 +00:00
// An empty lists returns an empty list
[]
2024-12-08 19:40:33 +00:00
|> maths.cumulative_sum()
2024-12-07 22:46:49 +00:00
|> should.equal([])
// Valid input returns a result
[1.0, 2.0, 3.0]
2024-12-08 19:40:33 +00:00
|> maths.cumulative_sum()
2024-12-07 22:46:49 +00:00
|> should.equal([1.0, 3.0, 6.0])
[-2.0, 4.0, 6.0]
2024-12-08 19:40:33 +00:00
|> maths.cumulative_sum()
2024-12-07 22:46:49 +00:00
|> should.equal([-2.0, 2.0, 8.0])
}
pub fn int_list_cumulative_sum_test() {
// An empty lists returns an empty list
[]
|> maths.int_cumulative_sum()
|> should.equal([])
// Valid input returns a result
[1, 2, 3]
|> maths.int_cumulative_sum()
|> should.equal([1, 3, 6])
[-2, 4, 6]
|> maths.int_cumulative_sum()
|> should.equal([-2, 2, 8])
}
2024-12-08 19:40:33 +00:00
pub fn list_cumulative_product_test() {
2024-12-07 22:46:49 +00:00
// An empty lists returns an empty list
[]
2024-12-08 19:40:33 +00:00
|> maths.cumulative_product()
2024-12-07 22:46:49 +00:00
|> should.equal([])
// Valid input returns a result
[1.0, 2.0, 3.0]
2024-12-08 19:40:33 +00:00
|> maths.cumulative_product()
2024-12-07 22:46:49 +00:00
|> should.equal([1.0, 2.0, 6.0])
[-2.0, 4.0, 6.0]
2024-12-08 19:40:33 +00:00
|> maths.cumulative_product()
2024-12-07 22:46:49 +00:00
|> should.equal([-2.0, -8.0, -48.0])
}
pub fn int_list_cumulative_product_test() {
// An empty lists returns an empty list
[]
|> maths.int_cumulative_product()
|> should.equal([])
// Valid input returns a result
[1, 2, 3]
|> maths.int_cumulative_product()
|> should.equal([1, 2, 6])
[-2, 4, 6]
|> maths.int_cumulative_product()
|> should.equal([-2, -8, -48])
}
2024-12-08 19:40:33 +00:00
pub fn weighted_product_test() {
2024-12-07 22:46:49 +00:00
[]
2024-12-08 19:40:33 +00:00
|> maths.weighted_product()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(1.0))
[#(1.0, 0.0), #(2.0, 0.0), #(3.0, 0.0)]
2024-12-08 19:40:33 +00:00
|> maths.weighted_product()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(1.0))
[#(1.0, 1.0), #(2.0, 1.0), #(3.0, 1.0)]
2024-12-08 19:40:33 +00:00
|> maths.weighted_product()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(6.0))
let assert Ok(tolerance) = float.power(10.0, -6.0)
let assert Ok(result) =
[#(9.0, 0.5), #(10.0, 0.5), #(10.0, 0.5)]
2024-12-08 19:40:33 +00:00
|> maths.weighted_product()
2024-12-07 22:46:49 +00:00
result
|> maths.is_close(30.0, 0.0, tolerance)
|> should.be_true()
}
2024-12-08 19:40:33 +00:00
pub fn weighted_sum_test() {
2024-12-07 22:46:49 +00:00
[]
2024-12-08 19:40:33 +00:00
|> maths.weighted_sum()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(0.0))
[#(1.0, 0.0), #(2.0, 0.0), #(3.0, 0.0)]
2024-12-08 19:40:33 +00:00
|> maths.weighted_sum()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(0.0))
[#(1.0, 1.0), #(2.0, 1.0), #(3.0, 1.0)]
2024-12-08 19:40:33 +00:00
|> maths.weighted_sum()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(6.0))
[#(9.0, 0.5), #(10.0, 0.5), #(10.0, 0.5)]
2024-12-08 19:40:33 +00:00
|> maths.weighted_sum()
2024-12-07 22:46:49 +00:00
|> should.equal(Ok(14.5))
}