diff --git a/Elwig/Helpers/Utils.cs b/Elwig/Helpers/Utils.cs index 6ff4b59..3ae33f0 100644 --- a/Elwig/Helpers/Utils.cs +++ b/Elwig/Helpers/Utils.cs @@ -81,8 +81,11 @@ namespace Elwig.Helpers { } public static int Modulo(string a, int b) { - if (!a.All(char.IsDigit)) + if (a.Length == 0 || !a.All(char.IsDigit)) { throw new ArgumentException("First argument has to be a decimal string"); + } else if (b < 2) { + throw new ArgumentException("Second argument has to be greater than 1"); + } return a.Select(ch => ch - '0').Aggregate((sum, n) => (sum * 10 + n) % b); } diff --git a/Tests/HelpersUtilsTest.cs b/Tests/HelpersUtilsTest.cs index 80465e0..33689f0 100644 --- a/Tests/HelpersUtilsTest.cs +++ b/Tests/HelpersUtilsTest.cs @@ -44,5 +44,19 @@ namespace Tests { Assert.AreEqual(817910, Utils.DecToDb(817.9099M, 3)); Assert.AreEqual(-561894, Utils.DecToDb(-5618.944M, 2)); } + + [TestMethod] + public void Test_Modulo() { + Assert.AreEqual(1, Utils.Modulo("1", 2)); + Assert.AreEqual(1, Utils.Modulo("12", 11)); + Assert.AreEqual(1, Utils.Modulo("65", 16)); + Assert.AreEqual(4, Utils.Modulo("91746381048364", 10)); + Assert.AreEqual(1, Utils.Modulo("210501700012345678131468", 97)); + Assert.ThrowsException(() => Utils.Modulo("", 4)); + Assert.ThrowsException(() => Utils.Modulo("1ab", 5)); + Assert.ThrowsException(() => Utils.Modulo("123", 1)); + Assert.ThrowsException(() => Utils.Modulo("456", 0)); + Assert.ThrowsException(() => Utils.Modulo("789", -1)); + } } }