|
| 1 | +import static org.junit.jupiter.api.Assertions.*; |
| 2 | +import org.junit.jupiter.api.BeforeEach; |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public class ProportionalShareSchedulingTest { |
| 8 | + |
| 9 | + private List<ProportionalShareScheduling.Process> processList; |
| 10 | + |
| 11 | + @BeforeEach |
| 12 | + public void setUp() { |
| 13 | + // Initialize the list of processes for testing |
| 14 | + processList = new ArrayList<>(); |
| 15 | + processList.add(new ProportionalShareScheduling.Process("P1", 5, 10, 0)); |
| 16 | + processList.add(new ProportionalShareScheduling.Process("P2", 3, 8, 1)); |
| 17 | + processList.add(new ProportionalShareScheduling.Process("P3", 2, 12, 2)); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testProportionalShareScheduling() { |
| 22 | + // Create an instance of ProportionalShareScheduling |
| 23 | + ProportionalShareScheduling scheduler = new ProportionalShareScheduling(processList); |
| 24 | + |
| 25 | + // Call the scheduling method and check the returned processes |
| 26 | + List<ProportionalShareScheduling.Process> executedProcesses = scheduler.scheduleProcesses(20); |
| 27 | + |
| 28 | + // Assert that all processes are executed |
| 29 | + assertEquals(3, executedProcesses.size(), "All processes should be executed"); |
| 30 | + |
| 31 | + // Verify each process received CPU time as expected |
| 32 | + ProportionalShareScheduling.Process p1 = executedProcesses.get(0); |
| 33 | + assertEquals("P1", p1.getProcessId(), "Process P1 should execute first"); |
| 34 | + assertEquals(10, p1.getTimeReceived(), "Process P1 should receive 10 units of time"); |
| 35 | + |
| 36 | + ProportionalShareScheduling.Process p2 = executedProcesses.get(1); |
| 37 | + assertEquals("P2", p2.getProcessId(), "Process P2 should execute next"); |
| 38 | + assertEquals(8, p2.getTimeReceived(), "Process P2 should receive 8 units of time"); |
| 39 | + |
| 40 | + ProportionalShareScheduling.Process p3 = executedProcesses.get(2); |
| 41 | + assertEquals("P3", p3.getProcessId(), "Process P3 should execute last"); |
| 42 | + assertEquals(12, p3.getTimeReceived(), "Process P3 should receive 12 units of time"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testStarvationPrevention() { |
| 47 | + // Create a process list with one high-weight and one low-weight process |
| 48 | + processList = new ArrayList<>(); |
| 49 | + processList.add(new ProportionalShareScheduling.Process("P1", 10, 10, 0)); // High-weight process |
| 50 | + processList.add(new ProportionalShareScheduling.Process("P2", 1, 5, 1)); // Low-weight process |
| 51 | + |
| 52 | + // Create an instance of ProportionalShareScheduling |
| 53 | + ProportionalShareScheduling scheduler = new ProportionalShareScheduling(processList); |
| 54 | + |
| 55 | + // Schedule processes |
| 56 | + List<ProportionalShareScheduling.Process> executedProcesses = scheduler.scheduleProcesses(30); |
| 57 | + |
| 58 | + // Assert that both processes have been executed and starvation has been prevented |
| 59 | + ProportionalShareScheduling.Process p1 = executedProcesses.get(0); |
| 60 | + ProportionalShareScheduling.Process p2 = executedProcesses.get(1); |
| 61 | + |
| 62 | + assertTrue(p2.getPriorityBoost() > 0, "Starvation prevention should increase priority boost for low-weight process"); |
| 63 | + assertEquals(5, p2.getTimeReceived(), "Low-weight process P2 should receive 5 units of time"); |
| 64 | + assertEquals(10, p1.getTimeReceived(), "High-weight process P1 should receive its full burst time"); |
| 65 | + } |
| 66 | +} |
0 commit comments