From 400cd752db3e04dcad8bf2a199a4d136ed85cedc Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 19 Jan 2021 14:30:01 +1100 Subject: Add int.clamp and float.clamp --- test/gleam/float_test.gleam | 11 +++++++++++ test/gleam/int_test.gleam | 15 +++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'test') diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 2b3334e..3e0e6f6 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -38,6 +38,17 @@ pub fn to_string_test() { |> should.equal("-8.1") } +pub fn clamp_test() { + float.clamp(1.4, min: 1.3, max: 1.5) + |> should.equal(1.4) + + float.clamp(1.2, min: 1.3, max: 1.5) + |> should.equal(1.3) + + float.clamp(1.6, min: 1.3, max: 1.5) + |> should.equal(1.5) +} + pub fn compare_test() { float.compare(0., 0.) |> should.equal(order.Eq) diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index 11906db..bec9624 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -12,6 +12,21 @@ pub fn absolute_value_test() { |> should.equal(123) } +pub fn clamp_test() { + int.clamp(40, min: 30, max: 50) + |> should.equal(40) + + int.clamp(20, min: 30, max: 50) + |> should.equal(30) + + int.clamp(60, min: 30, max: 50) + |> should.equal(50) + + // If the bounds are reversed we return the min + int.clamp(100, min: 50, max: 30) + |> should.equal(50) +} + pub fn to_string_test() { 123 |> int.to_string -- cgit v1.2.3