|
| 1 | +package com.thealgorithms.scheduling; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +public class HighestResponseRatioNextSchedulingTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + public void testCalculateTurnAroundTime() { |
| 11 | + String[] processNames = {"A", "B", "C"}; |
| 12 | + int[] arrivalTimes = {0, 2, 4}; |
| 13 | + int[] burstTimes = {3, 1, 2}; |
| 14 | + int noOfProcesses = 3; |
| 15 | + |
| 16 | + int[] expectedTurnAroundTimes = {3, 2, 2}; |
| 17 | + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); |
| 18 | + |
| 19 | + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times do not match"); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testCalculateWaitingTime() { |
| 24 | + int[] turnAroundTimes = {3, 1, 5}; |
| 25 | + int[] burstTimes = {3, 1, 2}; |
| 26 | + |
| 27 | + int[] expectedWaitingTimes = {0, 0, 3}; |
| 28 | + int[] actualWaitingTimes = HighestResponseRatioNextScheduling.calculateWaitingTime(turnAroundTimes, burstTimes); |
| 29 | + |
| 30 | + assertArrayEquals(expectedWaitingTimes, actualWaitingTimes, "Waiting Times do not match"); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCompleteSchedulingScenario() { |
| 35 | + String[] processNames = {"A", "B", "C"}; |
| 36 | + int[] arrivalTimes = {0, 1, 2}; |
| 37 | + int[] burstTimes = {5, 2, 1}; |
| 38 | + |
| 39 | + int[] expectedTurnAroundTimes = {5, 7, 4}; |
| 40 | + int[] turnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, processNames.length); |
| 41 | + assertArrayEquals(expectedTurnAroundTimes, turnAroundTimes, "Turn Around Times do not match"); |
| 42 | + |
| 43 | + int[] expectedWaitingTimes = {0, 5, 3}; |
| 44 | + int[] waitingTimes = HighestResponseRatioNextScheduling.calculateWaitingTime(turnAroundTimes, burstTimes); |
| 45 | + assertArrayEquals(expectedWaitingTimes, waitingTimes, "Waiting Times do not match"); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testZeroProcesses() { |
| 50 | + String[] processNames = {}; |
| 51 | + int[] arrivalTimes = {}; |
| 52 | + int[] burstTimes = {}; |
| 53 | + int noOfProcesses = 0; |
| 54 | + |
| 55 | + int[] expectedTurnAroundTimes = {}; |
| 56 | + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); |
| 57 | + |
| 58 | + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for zero processes should be an empty array"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testAllProcessesArriveAtSameTime() { |
| 63 | + String[] processNames = {"A", "B", "C", "D"}; |
| 64 | + int[] arrivalTimes = {0, 0, 0, 0}; |
| 65 | + int[] burstTimes = {4, 3, 1, 2}; |
| 66 | + int noOfProcesses = 4; |
| 67 | + |
| 68 | + int[] expectedTurnAroundTimes = {4, 10, 5, 7}; |
| 69 | + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); |
| 70 | + |
| 71 | + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes arriving at the same time do not match"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testProcessesWithZeroBurstTime() { |
| 76 | + String[] processNames = {"A", "B", "C"}; |
| 77 | + int[] arrivalTimes = {0, 1, 2}; |
| 78 | + int[] burstTimes = {3, 0, 2}; |
| 79 | + int noOfProcesses = 3; |
| 80 | + |
| 81 | + int[] expectedTurnAroundTimes = {3, 2, 3}; |
| 82 | + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); |
| 83 | + |
| 84 | + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with zero burst time do not match"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testProcessesWithLargeGapsBetweenArrivals() { |
| 89 | + String[] processNames = {"A", "B", "C"}; |
| 90 | + int[] arrivalTimes = {0, 100, 200}; |
| 91 | + int[] burstTimes = {10, 10, 10}; |
| 92 | + int noOfProcesses = 3; |
| 93 | + |
| 94 | + int[] expectedTurnAroundTimes = {10, 10, 10}; |
| 95 | + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); |
| 96 | + |
| 97 | + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with large gaps between arrivals do not match"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testProcessesWithVeryLargeBurstTimes() { |
| 102 | + String[] processNames = {"A", "B"}; |
| 103 | + int[] arrivalTimes = {0, 1}; |
| 104 | + int[] burstTimes = {Integer.MAX_VALUE / 2, Integer.MAX_VALUE / 2}; |
| 105 | + int noOfProcesses = 2; |
| 106 | + |
| 107 | + int[] expectedTurnAroundTimes = {Integer.MAX_VALUE / 2, Integer.MAX_VALUE - 2}; |
| 108 | + int[] actualTurnAroundTimes = HighestResponseRatioNextScheduling.calculateTurnAroundTime(processNames, arrivalTimes, burstTimes, noOfProcesses); |
| 109 | + |
| 110 | + assertArrayEquals(expectedTurnAroundTimes, actualTurnAroundTimes, "Turn Around Times for processes with very large burst times do not match"); |
| 111 | + } |
| 112 | +} |
0 commit comments