mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add disk scheduling algorithms (#5748)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.thealgorithms.scheduling.diskscheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CircularLookSchedulingTest {
|
||||
|
||||
@Test
|
||||
public void testCircularLookSchedulingMovingUp() {
|
||||
CircularLookScheduling scheduling = new CircularLookScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expected = Arrays.asList(55, 58, 90, 150, 160, 18, 39);
|
||||
|
||||
List<Integer> result = scheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircularLookSchedulingMovingDown() {
|
||||
CircularLookScheduling scheduling = new CircularLookScheduling(50, false, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expected = Arrays.asList(39, 18, 160, 150, 90, 58, 55);
|
||||
|
||||
List<Integer> result = scheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircularLookSchedulingEmptyRequests() {
|
||||
CircularLookScheduling scheduling = new CircularLookScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList();
|
||||
List<Integer> expected = Arrays.asList();
|
||||
|
||||
List<Integer> result = scheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircularLookSchedulingPrintStatus() {
|
||||
CircularLookScheduling scheduling = new CircularLookScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> result = scheduling.execute(requests);
|
||||
|
||||
// Print the final status
|
||||
System.out.println("Final CircularLookScheduling Position: " + scheduling.getCurrentPosition());
|
||||
System.out.println("CircularLookScheduling Moving Up: " + scheduling.isMovingUp());
|
||||
|
||||
// Print the order of request processing
|
||||
System.out.println("Request Order: " + result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.thealgorithms.scheduling.diskscheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CircularScanSchedulingTest {
|
||||
|
||||
@Test
|
||||
public void testCircularScanSchedulingMovingUp() {
|
||||
CircularScanScheduling circularScan = new CircularScanScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expectedOrder = Arrays.asList(55, 58, 90, 150, 160, 18, 39);
|
||||
|
||||
List<Integer> result = circularScan.execute(requests);
|
||||
assertEquals(expectedOrder, result);
|
||||
|
||||
System.out.println("Final CircularScan Position: " + circularScan.getCurrentPosition());
|
||||
System.out.println("CircularScan Moving Up: " + circularScan.isMovingUp());
|
||||
System.out.println("Request Order: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircularScanSchedulingMovingDown() {
|
||||
CircularScanScheduling circularScan = new CircularScanScheduling(50, false, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expectedOrder = Arrays.asList(39, 18, 160, 150, 90, 58, 55);
|
||||
|
||||
List<Integer> result = circularScan.execute(requests);
|
||||
assertEquals(expectedOrder, result);
|
||||
|
||||
System.out.println("Final CircularScan Position: " + circularScan.getCurrentPosition());
|
||||
System.out.println("CircularScan Moving Down: " + circularScan.isMovingUp());
|
||||
System.out.println("Request Order: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircularScanSchedulingEmptyRequests() {
|
||||
CircularScanScheduling circularScan = new CircularScanScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList();
|
||||
List<Integer> expectedOrder = Arrays.asList();
|
||||
|
||||
List<Integer> result = circularScan.execute(requests);
|
||||
assertEquals(expectedOrder, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.thealgorithms.scheduling.diskscheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class LookSchedulingTest {
|
||||
|
||||
@Test
|
||||
public void testLookSchedulingMovingUp() {
|
||||
LookScheduling lookScheduling = new LookScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expected = Arrays.asList(55, 58, 90, 150, 160, 39, 18);
|
||||
|
||||
List<Integer> result = lookScheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookSchedulingMovingDown() {
|
||||
LookScheduling lookScheduling = new LookScheduling(50, false, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expected = Arrays.asList(39, 18, 55, 58, 90, 150, 160);
|
||||
|
||||
List<Integer> result = lookScheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookSchedulingEmptyRequests() {
|
||||
LookScheduling lookScheduling = new LookScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList();
|
||||
List<Integer> expected = Arrays.asList();
|
||||
|
||||
List<Integer> result = lookScheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookSchedulingCurrentPosition() {
|
||||
LookScheduling lookScheduling = new LookScheduling(50, true, 200);
|
||||
|
||||
// Testing current position remains unchanged after scheduling.
|
||||
assertEquals(50, lookScheduling.getCurrentPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLookSchedulingPrintStatus() {
|
||||
LookScheduling lookScheduling = new LookScheduling(50, true, 200);
|
||||
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
|
||||
List<Integer> result = lookScheduling.execute(requests);
|
||||
|
||||
List<Integer> expectedOrder = Arrays.asList(55, 58, 90, 150, 160, 39, 18);
|
||||
assertEquals(expectedOrder, result);
|
||||
|
||||
System.out.println("Final LookScheduling Position: " + lookScheduling.getCurrentPosition());
|
||||
System.out.println("LookScheduling Moving Up: " + lookScheduling.isMovingUp());
|
||||
|
||||
System.out.println("Farthest Position Reached: " + lookScheduling.getFarthestPosition());
|
||||
|
||||
System.out.println("Request Order: " + result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.thealgorithms.scheduling.diskscheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SSFSchedulingTest {
|
||||
|
||||
private SSFScheduling scheduler;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
scheduler = new SSFScheduling(50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteWithEmptyList() {
|
||||
List<Integer> requests = new ArrayList<>();
|
||||
List<Integer> result = scheduler.execute(requests);
|
||||
assertTrue(result.isEmpty(), "Result should be empty for an empty request list.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteWithSingleRequest() {
|
||||
List<Integer> requests = new ArrayList<>(List.of(100));
|
||||
List<Integer> result = scheduler.execute(requests);
|
||||
assertEquals(List.of(100), result, "The only request should be served first.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteWithMultipleRequests() {
|
||||
List<Integer> requests = new ArrayList<>(List.of(10, 90, 60, 40, 30, 70));
|
||||
List<Integer> result = scheduler.execute(requests);
|
||||
assertEquals(List.of(60, 70, 90, 40, 30, 10), result, "Requests should be served in the shortest seek first order.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteWithSameDistanceRequests() {
|
||||
List<Integer> requests = new ArrayList<>(List.of(45, 55));
|
||||
List<Integer> result = scheduler.execute(requests);
|
||||
assertEquals(List.of(45, 55), result, "When distances are equal, requests should be served in the order they appear in the list.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCurrentPositionAfterExecution() {
|
||||
List<Integer> requests = new ArrayList<>(List.of(10, 90, 60, 40, 30, 70));
|
||||
scheduler.execute(requests);
|
||||
int currentPosition = scheduler.getCurrentPosition();
|
||||
assertEquals(10, currentPosition, "Current position should be the last request after execution.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.thealgorithms.scheduling.diskscheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ScanSchedulingTest {
|
||||
|
||||
@Test
|
||||
public void testScanSchedulingMovingUp() {
|
||||
ScanScheduling scanScheduling = new ScanScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expected = Arrays.asList(55, 58, 90, 150, 160, 199, 39, 18);
|
||||
|
||||
List<Integer> result = scanScheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanSchedulingMovingDown() {
|
||||
ScanScheduling scanScheduling = new ScanScheduling(50, false, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
List<Integer> expected = Arrays.asList(39, 18, 0, 55, 58, 90, 150, 160);
|
||||
|
||||
List<Integer> result = scanScheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanSchedulingEmptyRequests() {
|
||||
ScanScheduling scanScheduling = new ScanScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList();
|
||||
List<Integer> expected = Arrays.asList();
|
||||
|
||||
List<Integer> result = scanScheduling.execute(requests);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScanScheduling() {
|
||||
ScanScheduling scanScheduling = new ScanScheduling(50, true, 200);
|
||||
List<Integer> requests = Arrays.asList(55, 58, 39, 18, 90, 160, 150);
|
||||
|
||||
List<Integer> result = scanScheduling.execute(requests);
|
||||
List<Integer> expectedOrder = Arrays.asList(55, 58, 90, 150, 160, 199, 39, 18);
|
||||
assertEquals(expectedOrder, result);
|
||||
|
||||
System.out.println("Final Head Position: " + scanScheduling.getHeadPosition());
|
||||
System.out.println("Head Moving Up: " + scanScheduling.isMovingUp());
|
||||
System.out.println("Request Order: " + result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user