Skip to content

Commit eb5bfb5

Browse files
Added Junit Testing
1 parent 53b3699 commit eb5bfb5

File tree

2 files changed

+108
-28
lines changed

2 files changed

+108
-28
lines changed

src/main/java/com/thealgorithms/misc/ShuffleArray.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.thealgorithms.misc;
22
import java.util.Random;
3-
import java.util.Scanner;
43
/**
54
* The Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's
65
* elements, ensuring each permutation is equally likely.
@@ -13,7 +12,7 @@
1312
*
1413
* This class provides a static method to shuffle an array in place.
1514
*
16-
* @author Your Name (https://github.com/Chiefpatwal)
15+
* @author Rashi Dashore (https://github.com/rashi07dashore)
1716
*/
1817
public final class ShuffleArray {
1918
// Prevent instantiation
@@ -33,32 +32,6 @@ public static void shuffle(int[] arr) {
3332
arr[j] = temp;
3433
}
3534
}
36-
/**
37-
* This method takes user input to create an array.
38-
*
39-
* @return the array created from user input
40-
*/
41-
public static int[] getUserInputArray() {
42-
Scanner scanner = new Scanner(System.in);
43-
System.out.print("Enter the size of the array: ");
44-
int size = scanner.nextInt();
45-
46-
int[] arr = new int[size];
47-
System.out.println("Enter " + size + " elements:");
48-
for (int i = 0; i < size; i++) {
49-
arr[i] = scanner.nextInt();
50-
}
51-
return arr;
52-
}
53-
public static void main(String[] args) {
54-
int[] userArray = getUserInputArray();
55-
System.out.println("Original Array:");
56-
printArray(userArray);
57-
58-
shuffle(userArray);
59-
System.out.println("Shuffled Array:");
60-
printArray(userArray);
61-
}
6235
/**
6336
* This method prints the elements of the array.
6437
*
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
4+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Unit tests for the ShuffleArray class.
11+
*
12+
* @author Rashi Dashore (https://github.com/rashi07dashore)
13+
*/
14+
class ShuffleArrayTest {
15+
16+
/**
17+
* Test for basic shuffling.
18+
*/
19+
@Test
20+
void testShuffleBasic() {
21+
int[] arr = {1, 2, 3, 4, 5};
22+
int[] originalArr = arr.clone(); // Clone original array for comparison
23+
ShuffleArray.shuffle(arr);
24+
25+
// Check that the shuffled array is not the same as the original
26+
assertNotEquals(originalArr, arr);
27+
}
28+
29+
/**
30+
* Test for an edge case with a single element.
31+
*/
32+
@Test
33+
void testShuffleSingleElement() {
34+
int[] arr = {1};
35+
int[] originalArr = arr.clone();
36+
ShuffleArray.shuffle(arr);
37+
38+
// Check that the shuffled array is the same as the original
39+
assertArrayEquals(originalArr, arr);
40+
}
41+
42+
/**
43+
* Test for an edge case with two elements.
44+
*/
45+
@Test
46+
void testShuffleTwoElements() {
47+
int[] arr = {1, 2};
48+
int[] originalArr = arr.clone();
49+
ShuffleArray.shuffle(arr);
50+
51+
// Check that the shuffled array is not the same as the original
52+
assertNotEquals(originalArr, arr);
53+
// Check that the shuffled array still contains the same elements
54+
assertTrue(arr[0] == 1 || arr[0] == 2);
55+
assertTrue(arr[1] == 1 || arr[1] == 2);
56+
}
57+
58+
/**
59+
* Test for an empty array.
60+
*/
61+
@Test
62+
void testShuffleEmptyArray() {
63+
int[] arr = {};
64+
int[] originalArr = arr.clone();
65+
ShuffleArray.shuffle(arr);
66+
67+
// Check that the shuffled array is the same as the original (still empty)
68+
assertArrayEquals(originalArr, arr);
69+
}
70+
71+
/**
72+
* Test for large array.
73+
*/
74+
@Test
75+
void testShuffleLargeArray() {
76+
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
77+
int[] originalArr = arr.clone();
78+
ShuffleArray.shuffle(arr);
79+
80+
// Check that the shuffled array is not the same as the original
81+
assertNotEquals(originalArr, arr);
82+
}
83+
84+
/**
85+
* Test to ensure all elements are present after shuffle.
86+
*/
87+
@Test
88+
void testShuffleRetainsElements() {
89+
int[] arr = {1, 2, 3, 4, 5};
90+
ShuffleArray.shuffle(arr);
91+
92+
// Check that the shuffled array contains the same elements
93+
assertTrue(arr.length == 5);
94+
for (int i = 1; i <= 5; i++) {
95+
assertTrue(contains(arr, i));
96+
}
97+
}
98+
99+
private boolean contains(int[] arr, int value) {
100+
for (int num : arr) {
101+
if (num == value) {
102+
return true;
103+
}
104+
}
105+
return false;
106+
}
107+
}

0 commit comments

Comments
 (0)