mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Refactor ProcessDetails and PreemptivePriorityScheduling (#5448)
* Refactor ProcessDetails and PreemptivePriorityScheduling for consistency * fix formatting * fix formatting * Improve test readability and maintainability
This commit is contained in:
@ -2,32 +2,29 @@ package com.thealgorithms.scheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import com.thealgorithms.devutils.entities.ProcessDetails;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
/**
|
||||
* Test Cases of Preemptive Priority Scheduling Algorithm
|
||||
*
|
||||
* @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
|
||||
*/
|
||||
class PreemptivePrioritySchedulingTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideProcessesAndExpectedSchedules")
|
||||
void testPreemptivePriorityScheduling(List<ProcessDetails> processes, List<String> expectedSchedule) {
|
||||
PreemptivePriorityScheduling scheduler = new PreemptivePriorityScheduling(processes);
|
||||
scheduler.scheduleProcesses();
|
||||
assertEquals(expectedSchedule, scheduler.ganttChart);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPreemptivePriorityScheduling() {
|
||||
// Arrange
|
||||
List<Process> processes = new ArrayList<>();
|
||||
processes.add(new Process("P1", 0, 5, 10));
|
||||
processes.add(new Process("P2", 1, 4, 20));
|
||||
processes.add(new Process("P3", 2, 2, 30));
|
||||
processes.add(new Process("P4", 4, 1, 40));
|
||||
|
||||
List<String> expectedGanttChart = Arrays.asList("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1");
|
||||
|
||||
// Act
|
||||
List<String> actualGanttChart = PreemptivePriorityScheduling.preemptivePriorityScheduling(processes);
|
||||
|
||||
// Assert
|
||||
assertEquals(expectedGanttChart, actualGanttChart);
|
||||
static Stream<Arguments> provideProcessesAndExpectedSchedules() {
|
||||
return Stream.of(Arguments.of(List.of(new ProcessDetails("P1", 0, 5, 2), new ProcessDetails("P2", 1, 4, 4), new ProcessDetails("P3", 2, 2, 6), new ProcessDetails("P4", 4, 1, 8)), List.of("P1", "P2", "P3", "P3", "P4", "P2", "P2", "P2", "P1", "P1", "P1", "P1")),
|
||||
Arguments.of(List.of(new ProcessDetails("P1", 2, 5, 3), new ProcessDetails("P2", 5, 3, 5), new ProcessDetails("P3", 7, 1, 9)), List.of("Idle", "Idle", "P1", "P1", "P1", "P2", "P2", "P3", "P2", "P1", "P1")));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user