mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
feat: Added mod method to MathUtils
This commit is contained in:
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
### Added
|
### Added
|
||||||
- Added a method for converting 24 hour to 12 hour format
|
- Added a method for converting 24 hour to 12 hour format
|
||||||
- Added a method for determining if a culture uses 24 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
|
## [9.8.0] - 2019-09-03
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ICD.Common.Properties;
|
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Tests
|
namespace ICD.Common.Utils.Tests
|
||||||
{
|
{
|
||||||
[TestFixture, UsedImplicitly]
|
[TestFixture]
|
||||||
public sealed class MathUtilsTest
|
public sealed class MathUtilsTest
|
||||||
{
|
{
|
||||||
[Test, UsedImplicitly]
|
[Test]
|
||||||
public void ClampTest()
|
public void ClampTest()
|
||||||
{
|
{
|
||||||
Assert.AreEqual(MathUtils.Clamp(-10, 0, 0), 0);
|
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);
|
Assert.AreEqual(MathUtils.Clamp(20, 10, 10), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test, UsedImplicitly]
|
[Test]
|
||||||
public void MapRangeTest()
|
public void MapRangeTest()
|
||||||
{
|
{
|
||||||
Assert.AreEqual(5, MathUtils.MapRange(-100, 100, 0, 10, 0));
|
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));
|
Assert.AreEqual(100, MathUtils.MapRange(0, 10, 0, 100, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test, UsedImplicitly]
|
[Test]
|
||||||
public void GetRangesTest()
|
public void GetRangesTest()
|
||||||
{
|
{
|
||||||
IEnumerable<int> values = new [] { 1, 3, 5, 6, 7, 8, 9, 10, 12 };
|
IEnumerable<int> 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]);
|
Assert.AreEqual(12, ranges[3][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test, UsedImplicitly]
|
[Test]
|
||||||
public void RoundToNearestTest()
|
public void RoundToNearestTest()
|
||||||
{
|
{
|
||||||
IEnumerable<int> values = new [] { 0, 15, 30, 45 };
|
IEnumerable<int> values = new [] { 0, 15, 30, 45 };
|
||||||
Assert.AreEqual(15, MathUtils.RoundToNearest(21, values));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static int To12Hour(int hour)
|
public static int To12Hour(int hour)
|
||||||
{
|
{
|
||||||
return ((hour + 11) % 12) + 1;
|
return MathUtils.Mod(hour + 11, 12) + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -209,5 +209,17 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
return nearest.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);
|
return nearest.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Calculates the modulus of the given number.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="number"></param>
|
||||||
|
/// <param name="mod"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static int Mod(int number, int mod)
|
||||||
|
{
|
||||||
|
int remainder = number % mod;
|
||||||
|
return remainder < 0 ? remainder + mod : remainder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user