From 6b6aba1073b0350bd237b3390f2300b9849e3fda Mon Sep 17 00:00:00 2001
From: Lorenz Stechauner <lorenz.stechauner@necronda.net>
Date: Wed, 26 Apr 2023 19:29:12 +0200
Subject: [PATCH] Add Tests for Utils.Modulo

---
 Elwig/Helpers/Utils.cs    |  5 ++++-
 Tests/HelpersUtilsTest.cs | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

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<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));
+        }
     }
 }