Repository: TheAlgorithms/Java
Pull request #5423 (2024-08-27 21:54:06)

test: `CountCharTest`
Added tests.

<!--
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
-->

<!-- 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/5423?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 49.36%. Comparing base [(`a9bc7c2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a9bc7c269d3146aa2d67d69f2922f1bb2b07b4c5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`a9f5546`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a9f5546819e030f000031126074adff5e2c52894?dropdown=coverage&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    #5423      +/-   ##
============================================
+ Coverage     49.34%   49.36%   +0.01%     
  Complexity     3067     3067              
============================================
  Files           524      524              
  Lines         15204    15206       +2     
  Branches       2890     2890              
============================================
+ Hits           7503     7506       +3     
+ Misses         7389     7388       -1     
  Partials        312      312              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5423?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/others/CountChar.java b/src/main/java/com/thealgorithms/others/CountChar.java
index ad858137..00cff686 100644
--- a/src/main/java/com/thealgorithms/others/CountChar.java
+++ b/src/main/java/com/thealgorithms/others/CountChar.java
@@ -5,13 +5,16 @@ public final class CountChar {
     }
 
     /**
-     * Count non space character in string
+     * Counts the number of non-whitespace characters in the given string.
      *
-     * @param str String to count the characters
-     * @return number of character in the specified string
+     * @param str the input string to count the characters in
+     * @return the number of non-whitespace characters in the specified string;
+     *         returns 0 if the input string is null
      */
-
     public static int countCharacters(String str) {
+        if (str == null) {
+            return 0;
+        }
         return str.replaceAll("\\s", "").length();
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/CountCharTest.java b/src/test/java/com/thealgorithms/others/CountCharTest.java
index 382ba424..2b87d380 100644
--- a/src/test/java/com/thealgorithms/others/CountCharTest.java
+++ b/src/test/java/com/thealgorithms/others/CountCharTest.java
@@ -2,15 +2,23 @@ package com.thealgorithms.others;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
+import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 class CountCharTest {
 
-    @Test
-    void testCountCharacters() {
-        String input = "12345";
-        int expectedValue = 5;
+    @ParameterizedTest(name = "\"{0}\" should have {1} non-whitespace characters")
+    @CsvSource({"'', 0", "'   ', 0", "'a', 1", "'abc', 3", "'a b c', 3", "'   a   b   c   ', 3", "'\tabc\n', 3", "'  a   b\tc  ', 3", "' 12345 ', 5", "'Hello, World!', 12"})
+    @DisplayName("Test countCharacters with various inputs")
+    void testCountCharacters(String input, int expected) {
+        assertEquals(expected, CountChar.countCharacters(input));
+    }
 
-        assertEquals(expectedValue, CountChar.countCharacters(input));
+    @Test
+    @DisplayName("Test countCharacters with null input")
+    void testCountCharactersNullInput() {
+        assertEquals(0, CountChar.countCharacters(null));
     }
 }