Repository: TheAlgorithms/Java
Pull request #3779 (2022-11-28 06:03:22)

Create BucketSortTest
<!-- 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.

#3594 


Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/BucketSort.java b/src/main/java/com/thealgorithms/sorts/BucketSort.java
index e4239b5f..f2288479 100644
--- a/src/main/java/com/thealgorithms/sorts/BucketSort.java
+++ b/src/main/java/com/thealgorithms/sorts/BucketSort.java
@@ -32,7 +32,7 @@ public class BucketSort {
      *
      * @param arr the array contains elements
      */
-    private static void bucketSort(int[] arr) {
+    public static int[] bucketSort(int[] arr) {
         /* get max value of arr */
         int max = max(arr);
 
@@ -67,6 +67,8 @@ public class BucketSort {
                 arr[index++] = value;
             }
         }
+        
+        return arr;
     }
 
     /**


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/BucketSortTest.java b/src/test/java/com/thealgorithms/sorts/BucketSortTest.java
new file mode 100644
index 00000000..1f66be5b
--- /dev/null
+++ b/src/test/java/com/thealgorithms/sorts/BucketSortTest.java
@@ -0,0 +1,48 @@
+package com.thealgorithms.sorts;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class BucketSortTest {
+
+    @Test
+    public void bucketSortSingleIntegerArray() {
+        int[] inputArray = { 4 };
+        int[] outputArray = BucketSort.bucketSort(inputArray);
+        int[] expectedOutput = { 4 };
+        assertArrayEquals(outputArray, expectedOutput);
+    }
+
+    @Test
+    public void bucketSortNonDuplicateIntegerArray() {
+        int[] inputArray = { 6, 1, 99, 27, 15, 23, 36 };
+        int[] outputArray = BucketSort.bucketSort(inputArray);
+        int[] expectedOutput = {1, 6, 15, 23, 27, 36, 99};
+        assertArrayEquals(outputArray, expectedOutput);
+    }
+
+    @Test
+    public void bucketSortDuplicateIntegerArray() {
+        int[] inputArray = { 6, 1, 27, 15, 23, 27, 36, 23 };
+        int[] outputArray = BucketSort.bucketSort(inputArray);
+        int[] expectedOutput = {1, 6, 15, 23, 23, 27, 27, 36};
+        assertArrayEquals(outputArray, expectedOutput);
+    }
+
+    @Test
+    public void bucketSortNonDuplicateIntegerArrayWithNegativeNum() {
+        int[] inputArray = {  6, -1, 99, 27, -15, 23, -36  };
+        int[] outputArray = BucketSort.bucketSort(inputArray);
+        int[] expectedOutput = { -36, -15, -1, 6, 23, 27, 99};
+        assertArrayEquals(outputArray, expectedOutput);
+    }
+
+    @Test
+    public void bucketSortDuplicateIntegerArrayWithNegativeNum() {
+        int[] inputArray = { 6, -1, 27, -15, 23, 27, -36, 23  };
+        int[] outputArray = BucketSort.bucketSort(inputArray);
+        int[] expectedOutput = { -36, -15, -1, 6, 23, 23, 27, 27};
+        assertArrayEquals(outputArray, expectedOutput);
+    }
+}
\ No newline at end of file