Repository: TheAlgorithms/Java
Pull request #5378 (2024-08-23 21:51:54)

test: `NQueensTest`
Adding 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/5378?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 46.41%. Comparing base [(`ce4eb55`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ce4eb55e0e7c5216e2cba53217627f6bbe8b5083?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`65913ea`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/65913eaa2536ebf01fd87bae5e9b51cc334a7ccf?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    #5378      +/-   ##
============================================
+ Coverage     46.26%   46.41%   +0.14%     
- Complexity     2866     2877      +11     
============================================
  Files           525      525              
  Lines         15410    15406       -4     
  Branches       2916     2916              
============================================
+ Hits           7130     7151      +21     
+ Misses         7981     7955      -26     
- Partials        299      300       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5378?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/backtracking/NQueens.java b/src/main/java/com/thealgorithms/backtracking/NQueens.java
index e21a8bb7..1a8e453e 100644
--- a/src/main/java/com/thealgorithms/backtracking/NQueens.java
+++ b/src/main/java/com/thealgorithms/backtracking/NQueens.java
@@ -36,13 +36,10 @@ public final class NQueens {
     private NQueens() {
     }
 
-    public static void main(String[] args) {
-        placeQueens(1);
-        placeQueens(2);
-        placeQueens(3);
-        placeQueens(4);
-        placeQueens(5);
-        placeQueens(6);
+    public static List<List<String>> getNQueensArrangements(int queens) {
+        List<List<String>> arrangements = new ArrayList<>();
+        getSolution(queens, arrangements, new int[queens], 0);
+        return arrangements;
     }
 
     public static void placeQueens(final int queens) {


Test patch:
diff --git a/src/test/java/com/thealgorithms/backtracking/NQueensTest.java b/src/test/java/com/thealgorithms/backtracking/NQueensTest.java
new file mode 100644
index 00000000..977e3dfa
--- /dev/null
+++ b/src/test/java/com/thealgorithms/backtracking/NQueensTest.java
@@ -0,0 +1,48 @@
+package com.thealgorithms.backtracking;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class NQueensTest {
+
+    @Test
+    public void testNQueens1() {
+        List<List<String>> expected = Arrays.asList(Arrays.asList("Q"));
+        assertEquals(expected, NQueens.getNQueensArrangements(1));
+    }
+
+    @Test
+    public void testNQueens2() {
+        List<List<String>> expected = new ArrayList<>(); // No solution exists
+        assertEquals(expected, NQueens.getNQueensArrangements(2));
+    }
+
+    @Test
+    public void testNQueens3() {
+        List<List<String>> expected = new ArrayList<>(); // No solution exists
+        assertEquals(expected, NQueens.getNQueensArrangements(3));
+    }
+
+    @Test
+    public void testNQueens4() {
+        List<List<String>> expected = Arrays.asList(Arrays.asList(".Q..", "...Q", "Q...", "..Q."), Arrays.asList("..Q.", "Q...", "...Q", ".Q.."));
+        assertEquals(expected, NQueens.getNQueensArrangements(4));
+    }
+
+    @Test
+    public void testNQueens5() {
+        // Only the number of solutions is tested for larger N due to the complexity of checking each board configuration
+        List<List<String>> result = NQueens.getNQueensArrangements(5);
+        assertEquals(10, result.size()); // 5x5 board has 10 solutions
+    }
+
+    @Test
+    public void testNQueens6() {
+        List<List<String>> result = NQueens.getNQueensArrangements(6);
+        assertEquals(4, result.size()); // 6x6 board has 4 solutions
+    }
+}