From 3a6cbab3d53e5b04a6ea10df7854e4f3bce4f3a3 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Thu, 21 Feb 2019 19:54:08 -0600 Subject: Parallel processing job queue done! Fun exercise. Facing a difficult problem, always take small/concrete and concrete steps, and you'll get there! Writing down pseudo code helped, along with creating a subclass implementing the Comparable interface. Good thing that I've done that before and have example to follow. --- src/test/JobQueueTest.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/JobQueueTest.java (limited to 'src/test') diff --git a/src/test/JobQueueTest.java b/src/test/JobQueueTest.java new file mode 100644 index 0000000..e0e8a29 --- /dev/null +++ b/src/test/JobQueueTest.java @@ -0,0 +1,33 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class JobQueueTest { + @Test + void test() { + int numWorkers = 2; + int[] jobs = {1, 2, 3, 4, 5}; + JobQueue jobQueue = new JobQueue(); + jobQueue.numWorkers = numWorkers; + jobQueue.jobs = jobs; + jobQueue.assignJobsImproved(); + assertEquals(5, jobQueue.assignedWorker.length); + assertEquals(5, jobQueue.startTime.length); + assertArrayEquals(new int[]{0, 1, 0, 1, 0}, jobQueue.assignedWorker); + assertArrayEquals(new long[]{0, 0, 1, 2, 4}, jobQueue.startTime); + } + + @Test + void test1() { + int numWorkers = 4; + int[] jobs = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + JobQueue jobQueue = new JobQueue(); + jobQueue.numWorkers = numWorkers; + jobQueue.jobs = jobs; + jobQueue.assignJobsImproved(); + assertEquals(20, jobQueue.assignedWorker.length); + assertEquals(20, jobQueue.startTime.length); + assertArrayEquals(new int[]{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}, jobQueue.assignedWorker); + assertArrayEquals(new long[]{0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}, jobQueue.startTime); + } +} \ No newline at end of file -- cgit v1.2.3