|
| 1 | +package com.thealgorithms.scheduling; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class NonPreemptivePrioritySchedulingTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + public void testCalculateAverageWaitingTime() { |
| 11 | + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority |
| 12 | + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; |
| 13 | + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); |
| 14 | + |
| 15 | + double expectedAvgWaitingTime = (0 + 5 + 15) / 3.0; // Waiting times: 0 for P2, 5 for P1, 15 for P3 |
| 16 | + double actualAvgWaitingTime = NonPreemptivePriorityScheduling.calculateAverageWaitingTime(processes, executionOrder); |
| 17 | + |
| 18 | + assertEquals(expectedAvgWaitingTime, actualAvgWaitingTime, 0.01, "Average waiting time should be calculated correctly."); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testCalculateAverageTurnaroundTime() { |
| 23 | + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority |
| 24 | + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; |
| 25 | + |
| 26 | + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); |
| 27 | + |
| 28 | + double expectedAvgTurnaroundTime = (5 + 15 + 23) / 3.0; // Turnaround times: 5 for P2, 15 for P1, 23 for P3 |
| 29 | + double actualAvgTurnaroundTime = NonPreemptivePriorityScheduling.calculateAverageTurnaroundTime(processes, executionOrder); |
| 30 | + |
| 31 | + assertEquals(expectedAvgTurnaroundTime, actualAvgTurnaroundTime, 0.01, "Average turnaround time should be calculated correctly."); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testStartTimeIsCorrect() { |
| 36 | + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 10, 2), // id, arrivalTime, burstTime, priority |
| 37 | + new NonPreemptivePriorityScheduling.Process(2, 0, 5, 1), new NonPreemptivePriorityScheduling.Process(3, 0, 8, 3)}; |
| 38 | + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); |
| 39 | + |
| 40 | + // Check that the start time for each process is correctly set |
| 41 | + assertEquals(0, executionOrder[0].startTime, "First process (P2) should start at time 0."); // Process 2 has the highest priority |
| 42 | + assertEquals(5, executionOrder[1].startTime, "Second process (P1) should start at time 5."); |
| 43 | + assertEquals(15, executionOrder[2].startTime, "Third process (P3) should start at time 15."); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testWithDelayedArrivalTimes() { |
| 48 | + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 4, 1), // id, arrivalTime, burstTime, priority |
| 49 | + new NonPreemptivePriorityScheduling.Process(2, 2, 3, 2), new NonPreemptivePriorityScheduling.Process(3, 4, 2, 3)}; |
| 50 | + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); |
| 51 | + |
| 52 | + // Test the start times considering delayed arrivals |
| 53 | + assertEquals(0, executionOrder[0].startTime, "First process (P1) should start at time 0."); |
| 54 | + assertEquals(4, executionOrder[1].startTime, "Second process (P2) should start at time 4."); // After P1 finishes |
| 55 | + assertEquals(7, executionOrder[2].startTime, "Third process (P3) should start at time 7."); // After P2 finishes |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testWithGapsInArrivals() { |
| 60 | + NonPreemptivePriorityScheduling.Process[] processes = {new NonPreemptivePriorityScheduling.Process(1, 0, 6, 2), // id, arrivalTime, burstTime, priority |
| 61 | + new NonPreemptivePriorityScheduling.Process(2, 8, 4, 1), new NonPreemptivePriorityScheduling.Process(3, 12, 5, 3)}; |
| 62 | + |
| 63 | + NonPreemptivePriorityScheduling.Process[] executionOrder = NonPreemptivePriorityScheduling.scheduleProcesses(processes); |
| 64 | + |
| 65 | + // Test the start times for processes with gaps in arrival times |
| 66 | + assertEquals(0, executionOrder[0].startTime, "First process (P1) should start at time 0."); |
| 67 | + assertEquals(8, executionOrder[1].startTime, "Second process (P2) should start at time 8."); // After P1 finishes, arrives at 8 |
| 68 | + assertEquals(12, executionOrder[2].startTime, "Third process (P3) should start at time 12."); // After P2 finishes, arrives at 12 |
| 69 | + } |
| 70 | +} |
0 commit comments