Repository: TheAlgorithms/Java
Pull request #5391 (2024-08-25 19:30:52)

refactor: `BalancedBrackets`
Some improvements.
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/5391?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 47.44%. Comparing base [(`101cb95`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/101cb950ae9988d957e7a4f4244d65a46d8fab98?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`48a06e0`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/48a06e0459e623fdb719aa3c72c592116f0595bc?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    #5391      +/-   ##
============================================
+ Coverage     47.31%   47.44%   +0.12%     
- Complexity     2918     2929      +11     
============================================
  Files           524      524              
  Lines         15324    15320       -4     
  Branches       2907     2906       -1     
============================================
+ Hits           7251     7269      +18     
+ Misses         7772     7751      -21     
+ Partials        301      300       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5391?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).

siriak: Could you make the tests parameterized? To avoid having 1 test that checks everything with separate assertions

Patch:
diff --git a/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java b/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java
index a8f5254b..d3077ff5 100644
--- a/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java
+++ b/src/main/java/com/thealgorithms/stacks/BalancedBrackets.java
@@ -59,26 +59,22 @@ final class BalancedBrackets {
             switch (bracket) {
             case '(':
             case '[':
+            case '<':
             case '{':
                 bracketsStack.push(bracket);
                 break;
             case ')':
             case ']':
+            case '>':
             case '}':
                 if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
                     return false;
                 }
                 break;
             default:
-                /* other character is invalid */
                 return false;
             }
         }
         return bracketsStack.isEmpty();
     }
-
-    public static void main(String[] args) {
-        assert isBalanced("[()]{}{[()()]()}");
-        assert !isBalanced("[(])");
-    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/stacks/BalancedBracketsTest.java b/src/test/java/com/thealgorithms/stacks/BalancedBracketsTest.java
new file mode 100644
index 00000000..33ef18af
--- /dev/null
+++ b/src/test/java/com/thealgorithms/stacks/BalancedBracketsTest.java
@@ -0,0 +1,36 @@
+package com.thealgorithms.stacks;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+class BalancedBracketsTest {
+
+    @ParameterizedTest
+    @CsvSource({"(, )", "[, ]", "{, }", "<, >"})
+    void testIsPairedTrue(char opening, char closing) {
+        assertTrue(BalancedBrackets.isPaired(opening, closing));
+    }
+
+    @ParameterizedTest
+    @CsvSource({"(, ]", "[, )", "{, >", "<, )", "a, b", "!, @"})
+    void testIsPairedFalse(char opening, char closing) {
+        assertFalse(BalancedBrackets.isPaired(opening, closing));
+    }
+
+    @ParameterizedTest
+    @CsvSource({"'[()]{}{[()()]()}', true", "'()', true", "'[]', true", "'{}', true", "'<>', true", "'[{<>}]', true", "'', true", "'[(])', false", "'([)]', false", "'{[<]>}', false", "'[', false", "')', false", "'[{', false", "']', false", "'[a+b]', false", "'a+b', false"})
+    void testIsBalanced(String input, boolean expected) {
+        assertEquals(expected, BalancedBrackets.isBalanced(input));
+    }
+
+    @Test
+    void testIsBalancedNull() {
+        assertThrows(IllegalArgumentException.class, () -> BalancedBrackets.isBalanced(null));
+    }
+}