-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathRandomSchedulingTest.java
93 lines (73 loc) · 2.73 KB
/
RandomSchedulingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.thealgorithms.scheduling;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Random;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class RandomSchedulingTest {
private RandomScheduling randomScheduling;
private Random mockRandom;
@BeforeEach
public void setup() {
mockRandom = mock(Random.class); // Mocking Random for predictable behavior
}
@Test
public void testRandomOrder1() {
// Arrange
List<String> tasks = List.of("Task1", "Task2", "Task3");
// Mock the random sequence to control shuffling: swap 0 <-> 1, and 1 <-> 2.
when(mockRandom.nextInt(anyInt())).thenReturn(1, 2, 0);
randomScheduling = new RandomScheduling(tasks, mockRandom);
// Act
List<String> result = randomScheduling.schedule();
// Assert
assertEquals(List.of("Task1", "Task2", "Task3"), result);
}
@Test
public void testRandomOrder2() {
// Arrange
List<String> tasks = List.of("A", "B", "C", "D");
// Mocking predictable swaps for the sequence: [C, B, D, A]
when(mockRandom.nextInt(anyInt())).thenReturn(2, 1, 3, 0);
randomScheduling = new RandomScheduling(tasks, mockRandom);
// Act
List<String> result = randomScheduling.schedule();
// Assert
assertEquals(List.of("A", "C", "B", "D"), result);
}
@Test
public void testSingleTask() {
// Arrange
List<String> tasks = List.of("SingleTask");
when(mockRandom.nextInt(anyInt())).thenReturn(0); // No real shuffle
randomScheduling = new RandomScheduling(tasks, mockRandom);
// Act
List<String> result = randomScheduling.schedule();
// Assert
assertEquals(List.of("SingleTask"), result);
}
@Test
public void testEmptyTaskList() {
// Arrange
List<String> tasks = List.of();
randomScheduling = new RandomScheduling(tasks, mockRandom);
// Act
List<String> result = randomScheduling.schedule();
// Assert
assertEquals(List.of(), result); // Should return an empty list
}
@Test
public void testSameTasksMultipleTimes() {
// Arrange
List<String> tasks = List.of("X", "X", "Y", "Z");
when(mockRandom.nextInt(anyInt())).thenReturn(3, 0, 1, 2);
randomScheduling = new RandomScheduling(tasks, mockRandom);
// Act
List<String> result = randomScheduling.schedule();
// Assert
assertEquals(List.of("Y", "X", "X", "Z"), result);
}
}