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