From 02422cad5813576a76c6d8e9131ad467ecd3cf13 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Wed, 22 May 2024 21:52:45 +0800 Subject: [PATCH 1/3] feat: add safe clamp. --- .../Helpers/MathHelpers.cs | 55 +++++++++++++++++++ .../Irihi.Avalonia.Shared.Public.projitems | 1 + 2 files changed, 56 insertions(+) create mode 100644 src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs diff --git a/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs b/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs new file mode 100644 index 0000000..e8ff561 --- /dev/null +++ b/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs @@ -0,0 +1,55 @@ +using Avalonia.Controls.Platform; +using Avalonia.Utilities; + +namespace Irihi.Avalonia.Shared.Helpers; + +public static class MathHelpers +{ + private static (double min, double max) GetMinMax(double a, double b) + { + return a >= b ? (b, a) : (a, b); + } + + private static (float min, float max) GetMinMax(float a, float b) + { + return a >= b ? (b, a) : (a, b); + } + + private static (decimal min, decimal max) GetMinMax(decimal a, decimal b) + { + return a >= b ? (b, a) : (a, b); + } + + private static (int min, int max) GetMinMax(int a, int b) + { + return a >= b ? (b, a) : (a, b); + } + + public static double SafeClamp(double value, double min, double max) + { + (min, max) = GetMinMax(min, max); + if (value < min) return min; + return value > max ? max : value; + } + + public static decimal SafeClamp(decimal value, decimal min, decimal max) + { + var minimum = Math.Min(min, max); + var maximum = Math.Max(min, max); + return Math.Min(Math.Max(value, minimum), maximum); + } + + public static int SafeClamp(int value, int min, int max) + { + var minimum = Math.Min(min, max); + var maximum = Math.Max(min, max); + return Math.Min(Math.Max(value, minimum), maximum); + } + + public static float SafeClamp(float value, float min, float max) + { + (min, max) = GetMinMax(min, max); + if (value < min) return min; + return value > max ? max : value; + } +} \ No newline at end of file diff --git a/src/Irihi.Avalonia.Shared.Public/Irihi.Avalonia.Shared.Public.projitems b/src/Irihi.Avalonia.Shared.Public/Irihi.Avalonia.Shared.Public.projitems index 8e787a0..e07f2d7 100644 --- a/src/Irihi.Avalonia.Shared.Public/Irihi.Avalonia.Shared.Public.projitems +++ b/src/Irihi.Avalonia.Shared.Public/Irihi.Avalonia.Shared.Public.projitems @@ -20,6 +20,7 @@ + From 413880303ec04f1fb725ead1319ad3429b1986e2 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Wed, 22 May 2024 21:57:57 +0800 Subject: [PATCH 2/3] feat: use cleaner syntax. --- .../Helpers/MathHelpers.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs b/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs index e8ff561..35bcc03 100644 --- a/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs +++ b/src/Irihi.Avalonia.Shared.Public/Helpers/MathHelpers.cs @@ -34,16 +34,16 @@ public static double SafeClamp(double value, double min, double max) public static decimal SafeClamp(decimal value, decimal min, decimal max) { - var minimum = Math.Min(min, max); - var maximum = Math.Max(min, max); - return Math.Min(Math.Max(value, minimum), maximum); + (min, max) = GetMinMax(min, max); + if (value < min) return min; + return value > max ? max : value; } public static int SafeClamp(int value, int min, int max) { - var minimum = Math.Min(min, max); - var maximum = Math.Max(min, max); - return Math.Min(Math.Max(value, minimum), maximum); + (min, max) = GetMinMax(min, max); + if (value < min) return min; + return value > max ? max : value; } public static float SafeClamp(float value, float min, float max) From 53c52b6414dd11e53cff250b950ae4d91c54b44d Mon Sep 17 00:00:00 2001 From: rabbitism Date: Wed, 22 May 2024 22:05:38 +0800 Subject: [PATCH 3/3] feat: add ut. --- .../Helpers/MathHelperTests.cs | 68 +++++++++++++++++++ ....Avalonia.Shared.UnitTest.Public.projitems | 1 + 2 files changed, 69 insertions(+) create mode 100644 test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/MathHelperTests.cs diff --git a/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/MathHelperTests.cs b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/MathHelperTests.cs new file mode 100644 index 0000000..5314d82 --- /dev/null +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/MathHelperTests.cs @@ -0,0 +1,68 @@ +using Irihi.Avalonia.Shared.Helpers; + +namespace Irihi.Avalonia.Shared.UnitTest.Helpers; + +public class MathHelperTests +{ + [Theory] + [InlineData(0, 1, 2, 1)] + [InlineData(1, 1, 2, 1)] + [InlineData(2, 1, 2, 2)] + [InlineData(3, 1, 2, 2)] + [InlineData(0, 2, 1, 1)] + [InlineData(1, 2, 1, 1)] + [InlineData(2, 2, 1, 2)] + [InlineData(3, 2, 1, 2)] + [InlineData(3, 1, 1, 1)] + public void SafeClamp_Decimal_Success(decimal value, decimal min, decimal max, decimal expected) + { + Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); + } + + [Theory] + [InlineData(0, 1, 2, 1)] + [InlineData(1, 1, 2, 1)] + [InlineData(2, 1, 2, 2)] + [InlineData(3, 1, 2, 2)] + [InlineData(0, 2, 1, 1)] + [InlineData(1, 2, 1, 1)] + [InlineData(2, 2, 1, 2)] + [InlineData(3, 2, 1, 2)] + [InlineData(3, 1, 1, 1)] + public void SafeClamp_Double_Success(double value, double min, double max, double expected) + { + Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); + } + + [Theory] + [InlineData(0, 1, 2, 1)] + [InlineData(1, 1, 2, 1)] + [InlineData(2, 1, 2, 2)] + [InlineData(3, 1, 2, 2)] + [InlineData(0, 2, 1, 1)] + [InlineData(1, 2, 1, 1)] + [InlineData(2, 2, 1, 2)] + [InlineData(3, 2, 1, 2)] + [InlineData(3, 1, 1, 1)] + public void SafeClamp_Float_Success(float value, float min, float max, float expected) + { + Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); + } + + [Theory] + [InlineData(0, 1, 2, 1)] + [InlineData(1, 1, 2, 1)] + [InlineData(2, 1, 2, 2)] + [InlineData(3, 1, 2, 2)] + [InlineData(0, 2, 1, 1)] + [InlineData(1, 2, 1, 1)] + [InlineData(2, 2, 1, 2)] + [InlineData(3, 2, 1, 2)] + [InlineData(3, 1, 1, 1)] + public void SafeClamp_Int_Success(int value, int min, int max, int expected) + { + Assert.Equal(expected, MathHelpers.SafeClamp(value, min, max)); + } + + +} \ No newline at end of file diff --git a/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems b/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems index dc98db3..82d34ed 100644 --- a/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems @@ -14,6 +14,7 @@ +