Add Tests for Utils.Modulo

This commit is contained in:
2023-04-26 19:29:12 +02:00
parent ee5bda2fe3
commit 6b6aba1073
2 changed files with 18 additions and 1 deletions

View File

@ -81,8 +81,11 @@ namespace Elwig.Helpers {
} }
public static int Modulo(string a, int b) { 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"); 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); return a.Select(ch => ch - '0').Aggregate((sum, n) => (sum * 10 + n) % b);
} }

View File

@ -44,5 +44,19 @@ namespace Tests {
Assert.AreEqual(817910, Utils.DecToDb(817.9099M, 3)); Assert.AreEqual(817910, Utils.DecToDb(817.9099M, 3));
Assert.AreEqual(-561894, Utils.DecToDb(-5618.944M, 2)); 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<ArgumentException>(() => Utils.Modulo("", 4));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("1ab", 5));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("123", 1));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("456", 0));
Assert.ThrowsException<ArgumentException>(() => Utils.Modulo("789", -1));
}
} }
} }