Skip to content

Commit ff394a6

Browse files
committed
feat: Add RandomScheduling new algorithm with Junit tests
1 parent bcf4034 commit ff394a6

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Random;
7+
8+
/**
9+
* RandomScheduling is an algorithm that assigns tasks in a random order.
10+
* It doesn't consider priority, deadlines, or burst times, making it
11+
* inefficient but useful in scenarios where fairness or unpredictability
12+
* is required (e.g., load balancing in distributed systems).
13+
*
14+
* Use Case: Distributed systems where randomness helps avoid task starvation.
15+
*
16+
* @author Hardvan
17+
*/
18+
public final class RandomScheduling {
19+
20+
private final List<String> tasks;
21+
private final Random random;
22+
23+
/**
24+
* Constructs a new RandomScheduling instance.
25+
*
26+
* @param tasks A list of task names to be scheduled.
27+
* @param random A Random instance for generating random numbers.
28+
*/
29+
public RandomScheduling(List<String> tasks, Random random) {
30+
this.tasks = new ArrayList<>(tasks); // Store tasks locally
31+
this.random = random;
32+
}
33+
34+
/**
35+
* Schedules the tasks randomly and returns the randomized order.
36+
*
37+
* @return A list representing the tasks in their randomized execution order.
38+
*/
39+
public List<String> schedule() {
40+
List<String> shuffledTasks = new ArrayList<>(tasks);
41+
Collections.shuffle(shuffledTasks, random);
42+
return shuffledTasks;
43+
}
44+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.Mockito.*;
5+
6+
import java.util.List;
7+
import java.util.Random;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class RandomSchedulingTest {
12+
13+
private RandomScheduling randomScheduling;
14+
private Random mockRandom;
15+
16+
@BeforeEach
17+
public void setup() {
18+
mockRandom = mock(Random.class); // Mocking Random for predictable behavior
19+
}
20+
21+
@Test
22+
public void testRandomOrder1() {
23+
// Arrange
24+
List<String> tasks = List.of("Task1", "Task2", "Task3");
25+
// Mock the random sequence to control shuffling: swap 0 <-> 1, and 1 <-> 2.
26+
when(mockRandom.nextInt(anyInt())).thenReturn(1, 2, 0);
27+
randomScheduling = new RandomScheduling(tasks, mockRandom);
28+
29+
// Act
30+
List<String> result = randomScheduling.schedule();
31+
32+
// Assert
33+
assertEquals(List.of("Task1", "Task2", "Task3"), result);
34+
}
35+
36+
@Test
37+
public void testRandomOrder2() {
38+
// Arrange
39+
List<String> tasks = List.of("A", "B", "C", "D");
40+
// Mocking predictable swaps for the sequence: [C, B, D, A]
41+
when(mockRandom.nextInt(anyInt())).thenReturn(2, 1, 3, 0);
42+
randomScheduling = new RandomScheduling(tasks, mockRandom);
43+
44+
// Act
45+
List<String> result = randomScheduling.schedule();
46+
47+
// Assert
48+
assertEquals(List.of("A", "C", "B", "D"), result);
49+
}
50+
51+
@Test
52+
public void testSingleTask() {
53+
// Arrange
54+
List<String> tasks = List.of("SingleTask");
55+
when(mockRandom.nextInt(anyInt())).thenReturn(0); // No real shuffle
56+
randomScheduling = new RandomScheduling(tasks, mockRandom);
57+
58+
// Act
59+
List<String> result = randomScheduling.schedule();
60+
61+
// Assert
62+
assertEquals(List.of("SingleTask"), result);
63+
}
64+
65+
@Test
66+
public void testEmptyTaskList() {
67+
// Arrange
68+
List<String> tasks = List.of();
69+
randomScheduling = new RandomScheduling(tasks, mockRandom);
70+
71+
// Act
72+
List<String> result = randomScheduling.schedule();
73+
74+
// Assert
75+
assertEquals(List.of(), result); // Should return an empty list
76+
}
77+
78+
@Test
79+
public void testSameTasksMultipleTimes() {
80+
// Arrange
81+
List<String> tasks = List.of("X", "X", "Y", "Z");
82+
when(mockRandom.nextInt(anyInt())).thenReturn(3, 0, 1, 2);
83+
randomScheduling = new RandomScheduling(tasks, mockRandom);
84+
85+
// Act
86+
List<String> result = randomScheduling.schedule();
87+
88+
// Assert
89+
assertEquals(List.of("Y", "X", "X", "Z"), result);
90+
}
91+
}

0 commit comments

Comments
 (0)