Repository: TheAlgorithms/Java
Pull request #5389 (2024-08-25 19:15:51)

refactor: `ArrayLeftRotationTest`
Class cleanup.
Readability improvements.
Added test.

<!--
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/5389?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.32%. 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 [(`91d0576`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/91d05769a82b32841324c97c840fd024522ddbd5?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    #5389   +/-   ##
=========================================
  Coverage     47.31%   47.32%           
- Complexity     2918     2920    +2     
=========================================
  Files           524      524           
  Lines         15324    15327    +3     
  Branches       2907     2909    +2     
=========================================
+ Hits           7251     7253    +2     
  Misses         7772     7772           
- Partials        301      302    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5389?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/ArrayLeftRotation.java b/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
index f43841f1..b54cbec0 100644
--- a/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
+++ b/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java
@@ -1,34 +1,44 @@
 package com.thealgorithms.others;
 
-/*
- * A left rotation operation on an array
- * shifts each of the array's elements
- * given integer n unit to the left.
+/**
+ * Provides a method to perform a left rotation on an array.
+ * A left rotation operation shifts each element of the array
+ * by a specified number of positions to the left.
  *
  * @author sangin-lee
  */
-
 public final class ArrayLeftRotation {
     private ArrayLeftRotation() {
     }
 
-    /*
-     * Returns the result of left rotation of given array arr and integer n
-     *
-     * @param arr : int[] given array
-     *
-     * @param n : int given integer
+    /**
+     * Performs a left rotation on the given array by the specified number of positions.
      *
-     * @return : int[] result of left rotation
+     * @param arr the array to be rotated
+     * @param n the number of positions to rotate the array to the left
+     * @return a new array containing the elements of the input array rotated to the left
      */
     public static int[] rotateLeft(int[] arr, int n) {
         int size = arr.length;
-        int[] dst = new int[size];
+
+        // Handle cases where array is empty or rotation count is zero
+        if (size == 0 || n <= 0) {
+            return arr.clone();
+        }
+
+        // Normalize the number of rotations
         n = n % size;
+        if (n == 0) {
+            return arr.clone();
+        }
+
+        int[] rotated = new int[size];
+
+        // Perform rotation
         for (int i = 0; i < size; i++) {
-            dst[i] = arr[n];
-            n = (n + 1) % size;
+            rotated[i] = arr[(i + n) % size];
         }
-        return dst;
+
+        return rotated;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
index 355f107d..b31b7d82 100644
--- a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
+++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
@@ -44,4 +44,11 @@ class ArrayLeftRotationTest {
         int[] result = ArrayLeftRotation.rotateLeft(arr, n);
         assertArrayEquals(expected, result);
     }
+
+    @Test
+    void testForEmptyArray() {
+        int[] arr = {};
+        int[] result = ArrayLeftRotation.rotateLeft(arr, 3);
+        assertArrayEquals(arr, result);
+    }
 }