Repository: TheAlgorithms/Java
Pull request #5107 (2024-04-13 18:40:56)

Added tests for NumberOfDigits
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

I added some tests for the class computing number of digits.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5107?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 37.20%. Comparing base [(`f39bb8f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f39bb8f32f158c8b3f2e350ced006be6cd015c7a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`980836b`)](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5107?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5107      +/-   ##
============================================
+ Coverage     37.11%   37.20%   +0.08%     
- Complexity     2330     2339       +9     
============================================
  Files           517      517              
  Lines         15645    15637       -8     
  Branches       2984     2979       -5     
============================================
+ Hits           5807     5817      +10     
+ Misses         9549     9532      -17     
+ Partials        289      288       -1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5107?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/NumberOfDigits.java b/src/main/java/com/thealgorithms/maths/NumberOfDigits.java
index 665d8ef5..fc538196 100644
--- a/src/main/java/com/thealgorithms/maths/NumberOfDigits.java
+++ b/src/main/java/com/thealgorithms/maths/NumberOfDigits.java
@@ -3,35 +3,16 @@ package com.thealgorithms.maths;
 /**
  * Find the number of digits in a number.
  */
-public class NumberOfDigits {
-
-    public static void main(String[] args) {
-        int[] numbers = {
-            0,
-            12,
-            123,
-            1234,
-            -12345,
-            123456,
-            1234567,
-            12345678,
-            123456789,
-        };
-        for (int i = 0; i < numbers.length; ++i) {
-            assert numberOfDigits(numbers[i]) == i + 1;
-            assert numberOfDigitsFast(numbers[i]) == i + 1;
-            assert numberOfDigitsFaster(numbers[i]) == i + 1;
-            assert numberOfDigitsRecursion(numbers[i]) == i + 1;
-        }
+public final class NumberOfDigits {
+    private NumberOfDigits() {
     }
-
     /**
      * Find the number of digits in a number.
      *
      * @param number number to find
      * @return number of digits of given number
      */
-    private static int numberOfDigits(int number) {
+    public static int numberOfDigits(int number) {
         int digits = 0;
         do {
             digits++;
@@ -46,7 +27,7 @@ public class NumberOfDigits {
      * @param number number to find
      * @return number of digits of given number
      */
-    private static int numberOfDigitsFast(int number) {
+    public static int numberOfDigitsFast(int number) {
         return number == 0 ? 1 : (int) Math.floor(Math.log10(Math.abs(number)) + 1);
     }
 
@@ -56,7 +37,7 @@ public class NumberOfDigits {
      * @param number number to find
      * @return number of digits of given number
      */
-    private static int numberOfDigitsFaster(int number) {
+    public static int numberOfDigitsFaster(int number) {
         return number < 0 ? (-number + "").length() : (number + "").length();
     }
 
@@ -66,7 +47,7 @@ public class NumberOfDigits {
      * @param number number to find
      * @return number of digits of given number
      */
-    private static int numberOfDigitsRecursion(int number) {
+    public static int numberOfDigitsRecursion(int number) {
         return number / 10 == 0 ? 1 : 1 + numberOfDigitsRecursion(number / 10);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/NumberOfDigitsTest.java b/src/test/java/com/thealgorithms/maths/NumberOfDigitsTest.java
new file mode 100644
index 00000000..2e807db1
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/NumberOfDigitsTest.java
@@ -0,0 +1,40 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.function.IntFunction;
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class NumberOfDigitsTest {
+
+    @ParameterizedTest
+    @MethodSource("testCases")
+    void testNumberOfDigits(final int expected, final int number, final IntFunction<Integer> methodUnderTest) {
+        assertEquals(expected, methodUnderTest.apply(number));
+        assertEquals(expected, methodUnderTest.apply(-number));
+    }
+
+    private static Stream<Arguments> testCases() {
+        final Integer[][] inputs = new Integer[][] {
+            {3, 100},
+            {1, 0},
+            {2, 12},
+            {3, 123},
+            {4, 1234},
+            {5, 12345},
+            {6, 123456},
+            {7, 1234567},
+            {8, 12345678},
+            {9, 123456789},
+            {9, 987654321},
+        };
+
+        final IntFunction<Integer>[] methods = new IntFunction[] {NumberOfDigits::numberOfDigits, NumberOfDigits::numberOfDigitsFast, NumberOfDigits::numberOfDigitsFaster, NumberOfDigits::numberOfDigitsRecursion};
+
+        return Stream.of(inputs).flatMap(input -> Stream.of(methods).map(method -> Arguments.of(input[0], input[1], method)));
+    }
+}