aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNicklas Sindlev Andersen <nsa200293@live.dk>2022-06-07 19:55:57 +0200
committerGitHub <noreply@github.com>2022-06-07 18:55:57 +0100
commiteeceba29e9d27fe1820bf2d806d4699a37a0469c (patch)
treed14003ab111183644e00f92b48465a379f757a5e /test
parent32d63fb1a0d8d46d17ef7077a4eb1e795dd1cc62 (diff)
downloadgleam_stdlib-eeceba29e9d27fe1820bf2d806d4699a37a0469c.tar.gz
gleam_stdlib-eeceba29e9d27fe1820bf2d806d4699a37a0469c.zip
Fix int.power and float.power functions (#302)
Diffstat (limited to 'test')
-rw-r--r--test/gleam/float_test.gleam33
-rw-r--r--test/gleam/int_test.gleam30
2 files changed, 53 insertions, 10 deletions
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index 132cd69..e952908 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -245,19 +245,42 @@ pub fn absolute_value_test() {
pub fn power_test() {
float.power(2.0, 2.0)
- |> should.equal(4.0)
+ |> should.equal(Ok(4.0))
float.power(-5.0, 3.0)
- |> should.equal(-125.0)
+ |> should.equal(Ok(-125.0))
float.power(10.5, 0.0)
- |> should.equal(1.0)
+ |> should.equal(Ok(1.0))
float.power(16.0, 0.5)
- |> should.equal(4.0)
+ |> should.equal(Ok(4.0))
float.power(2.0, -1.0)
- |> should.equal(0.5)
+ |> should.equal(Ok(0.5))
+
+ float.power(2.0, -1.0)
+ |> should.equal(Ok(0.5))
+
+ // float.power(-1.0, 0.5) is equivalent to float.square_root(-1.0)
+ // and should return an error as an imaginary number would otherwise
+ // have to be returned
+ float.power(-1.0, 0.5)
+ |> should.equal(Error(Nil))
+
+ // Check another case with a negative base and fractional exponent
+ float.power(-1.5, 1.5)
+ |> should.equal(Error(Nil))
+
+ // float.power(0.0, -1.0) is equivalent to 1. /. 0 and is expected
+ // to be an error
+ float.power(0.0, -1.0)
+ |> should.equal(Error(Nil))
+
+ // Check that a negative base and exponent is fine as long as the
+ // exponent is not fractional
+ float.power(-2.0, -1.0)
+ |> should.equal(Ok(-0.5))
}
pub fn square_root_test() {
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 2209490..71c526b 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -257,19 +257,39 @@ pub fn is_odd_test() {
pub fn power_test() {
int.power(2, 2.0)
- |> should.equal(4.0)
+ |> should.equal(Ok(4.0))
int.power(-5, 3.0)
- |> should.equal(-125.0)
+ |> should.equal(Ok(-125.0))
int.power(10, 0.0)
- |> should.equal(1.0)
+ |> should.equal(Ok(1.0))
int.power(16, 0.5)
- |> should.equal(4.0)
+ |> should.equal(Ok(4.0))
int.power(2, -1.0)
- |> should.equal(0.5)
+ |> should.equal(Ok(0.5))
+
+ // int.power(-1, 0.5) is equivalent to int.square_root(-1) and should
+ // return an error as an imaginary number would otherwise have to be
+ // returned
+ int.power(-1, 0.5)
+ |> should.equal(Error(Nil))
+
+ // Check another case with a negative base and fractional exponent
+ int.power(-1, 1.5)
+ |> should.equal(Error(Nil))
+
+ // float.power(0, -1) is equivalent to 1 / 0 and is expected
+ // to be an error
+ int.power(0, -1.0)
+ |> should.equal(Error(Nil))
+
+ // Check that a negative base and exponent is fine as long as the
+ // exponent is not fractional
+ int.power(-2, -1.0)
+ |> should.equal(Ok(-0.5))
}
pub fn square_root_test() {