diff --git a/CHANGELOG.md b/CHANGELOG.md index d7094d4..618f1de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - Added a method for converting 24 hour to 12 hour format - Added a method for determining if a culture uses 24 hour format + - Added math util method for mod ## [9.8.0] - 2019-09-03 ### Added diff --git a/ICD.Common.Utils.Tests/MathUtilsTest.cs b/ICD.Common.Utils.Tests/MathUtilsTest.cs index ebf3280..74129a1 100644 --- a/ICD.Common.Utils.Tests/MathUtilsTest.cs +++ b/ICD.Common.Utils.Tests/MathUtilsTest.cs @@ -1,14 +1,13 @@ using NUnit.Framework; using System.Collections.Generic; using System.Linq; -using ICD.Common.Properties; namespace ICD.Common.Utils.Tests { - [TestFixture, UsedImplicitly] + [TestFixture] public sealed class MathUtilsTest { - [Test, UsedImplicitly] + [Test] public void ClampTest() { Assert.AreEqual(MathUtils.Clamp(-10, 0, 0), 0); @@ -24,7 +23,7 @@ namespace ICD.Common.Utils.Tests Assert.AreEqual(MathUtils.Clamp(20, 10, 10), 10); } - [Test, UsedImplicitly] + [Test] public void MapRangeTest() { Assert.AreEqual(5, MathUtils.MapRange(-100, 100, 0, 10, 0)); @@ -40,7 +39,7 @@ namespace ICD.Common.Utils.Tests Assert.AreEqual(100, MathUtils.MapRange(0, 10, 0, 100, 10)); } - [Test, UsedImplicitly] + [Test] public void GetRangesTest() { IEnumerable values = new [] { 1, 3, 5, 6, 7, 8, 9, 10, 12 }; @@ -61,11 +60,21 @@ namespace ICD.Common.Utils.Tests Assert.AreEqual(12, ranges[3][1]); } - [Test, UsedImplicitly] + [Test] public void RoundToNearestTest() { IEnumerable values = new [] { 0, 15, 30, 45 }; Assert.AreEqual(15, MathUtils.RoundToNearest(21, values)); } + + [TestCase(10, 10, 0)] + [TestCase(-10, 10, 0)] + [TestCase(9, 3, 0)] + [TestCase(3, 2, 1)] + [TestCase(-3, 2, 1)] + public void ModTest(int value, int mod, int expected) + { + Assert.AreEqual(expected, MathUtils.Mod(value, mod)); + } } } diff --git a/ICD.Common.Utils/DateTimeUtils.cs b/ICD.Common.Utils/DateTimeUtils.cs index edcda83..a424ffd 100644 --- a/ICD.Common.Utils/DateTimeUtils.cs +++ b/ICD.Common.Utils/DateTimeUtils.cs @@ -9,7 +9,7 @@ /// public static int To12Hour(int hour) { - return ((hour + 11) % 12) + 1; + return MathUtils.Mod(hour + 11, 12) + 1; } } } diff --git a/ICD.Common.Utils/MathUtils.cs b/ICD.Common.Utils/MathUtils.cs index 832ccba..ee6ebe5 100644 --- a/ICD.Common.Utils/MathUtils.cs +++ b/ICD.Common.Utils/MathUtils.cs @@ -209,5 +209,17 @@ namespace ICD.Common.Utils return nearest.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y); } + + /// + /// Calculates the modulus of the given number. + /// + /// + /// + /// + public static int Mod(int number, int mod) + { + int remainder = number % mod; + return remainder < 0 ? remainder + mod : remainder; + } } }