From 29bb48a8cc8122871d8728be6fd8b853e52568bd Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Wed, 23 Jan 2019 16:14:16 -0600 Subject: Interim checkin. Network packet processing not done yet. --- src/main/NetworkPacket.java | 83 +++++++++++++++++++++++++++++++++++++++++ src/test/NetworkPacketTest.java | 59 +++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/main/NetworkPacket.java create mode 100644 src/test/NetworkPacketTest.java (limited to 'src') diff --git a/src/main/NetworkPacket.java b/src/main/NetworkPacket.java new file mode 100644 index 0000000..805dcf5 --- /dev/null +++ b/src/main/NetworkPacket.java @@ -0,0 +1,83 @@ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class NetworkPacket { + + public static class Request { + public int arrivalTime; + public int processTime; + + public Request(int arrivalTime, int processTime) { + this.arrivalTime = arrivalTime; + this.processTime = processTime; + } + + } + + public static class Response { + public boolean dropped; + public int startTime; + + public Response(boolean dropped, int startTime) { + this.dropped = dropped; + this.startTime = startTime; + } + } + + public static class Buffer { + private int size; + private ArrayList finishTime = new ArrayList(); + + public Buffer(int size) { + this.size = size; + } + public Response processRequests(Request request) { + // write your code here + if (this.size == 0) + return new Response(false,-1); + else { + if (finishTime.size() == 0) + } + } + } + private static ArrayList ReadQueries(Scanner scanner) throws IOException { + int requests_count = scanner.nextInt(); + ArrayList requests = new ArrayList(); + for (int i = 0; i < requests_count; ++i) { + int arrival_time = scanner.nextInt(); + int process_time = scanner.nextInt(); + requests.add(new Request(arrival_time, process_time)); + } + return requests; + } + + public static ArrayList processRequests(ArrayList requests, Buffer buf) { + ArrayList responses = new ArrayList<>(); + for (int i = 0; i < requests.size(); i++) { + responses.add(buf.processRequests(requests.get(i))); + } + return responses; + } + private static void PrintResponses(ArrayList responses) { + for (int i = 0; i < responses.size(); ++i) { + Response response = responses.get(i); + if (response.dropped) { + System.out.println(-1); + } else { + System.out.println(response.startTime); + } + } + } + + public static void main(String[] args) throws IOException { + Scanner scanner = new Scanner(System.in); + + int buffer_max_size = scanner.nextInt(); + Buffer buffer = new Buffer(buffer_max_size); + + ArrayList requests = ReadQueries(scanner); + ArrayList responses = processRequests(requests, buffer); + PrintResponses(responses); + } +} diff --git a/src/test/NetworkPacketTest.java b/src/test/NetworkPacketTest.java new file mode 100644 index 0000000..fca3d60 --- /dev/null +++ b/src/test/NetworkPacketTest.java @@ -0,0 +1,59 @@ +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NetworkPacketTest { + @Test + void test() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList requests = new ArrayList<>(); + ArrayList responses = NetworkPacket.processRequests(requests, buf); + assertEquals(0, responses.size()); + } + + @Test + void test1() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList requests = new ArrayList<>(); + requests.add(new NetworkPacket.Request(0, 1)); + ArrayList responses = NetworkPacket.processRequests(requests, buf); + assertEquals(1, responses.size()); + assertFalse(responses.get(0).dropped); + assertEquals(0, responses.get(0).startTime); + } + + @Test + void test2() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList requests = new ArrayList<>(); + requests.add(new NetworkPacket.Request(0, 1)); + requests.add(new NetworkPacket.Request(0, 1)); + ArrayList responses = NetworkPacket.processRequests(requests, buf); + assertEquals(2, responses.size()); + assertFalse(responses.get(0).dropped); + assertEquals(0, responses.get(0).startTime); + assertTrue(responses.get(1).dropped); + } + + @Test + void test3() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList requests = new ArrayList<>(); + requests.add(new NetworkPacket.Request(0, 1)); + requests.add(new NetworkPacket.Request(1, 1)); + ArrayList responses = NetworkPacket.processRequests(requests, buf); + assertEquals(2, responses.size()); + assertFalse(responses.get(0).dropped); + assertEquals(0, responses.get(0).startTime); + assertFalse(responses.get(1).dropped); + assertEquals(1, responses.get(1).startTime); + } +} -- cgit v1.2.3