11 changed files with 657 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<classpath> |
|||
<classpathentry kind="src" path="src"/> |
|||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13"> |
|||
<attributes> |
|||
<attribute name="module" value="true"/> |
|||
</attributes> |
|||
</classpathentry> |
|||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"> |
|||
<attributes> |
|||
<attribute name="module" value="true"/> |
|||
</attributes> |
|||
</classpathentry> |
|||
<classpathentry kind="output" path="bin"/> |
|||
</classpath> |
@ -0,0 +1,17 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<projectDescription> |
|||
<name>SimplePriorityQueue</name> |
|||
<comment></comment> |
|||
<projects> |
|||
</projects> |
|||
<buildSpec> |
|||
<buildCommand> |
|||
<name>org.eclipse.jdt.core.javabuilder</name> |
|||
<arguments> |
|||
</arguments> |
|||
</buildCommand> |
|||
</buildSpec> |
|||
<natures> |
|||
<nature>org.eclipse.jdt.core.javanature</nature> |
|||
</natures> |
|||
</projectDescription> |
@ -0,0 +1,6 @@ |
|||
module testat { |
|||
requires junit; |
|||
requires org.junit.jupiter.api; |
|||
|
|||
opens testat_g11; |
|||
} |
@ -0,0 +1,123 @@ |
|||
package solution_g11; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
import testat_g11.IPDetermination; |
|||
import testat_g11.IPriorityQueue; |
|||
import testat_g11.Ticket; |
|||
import testat_g11.TicketStatus; |
|||
|
|||
public class SimplePriorityQueue00000<T extends Ticket> implements IPriorityQueue<T> { |
|||
|
|||
private IPDetermination<Ticket> prioDet; |
|||
private int size; |
|||
private T[] tickets; |
|||
|
|||
public SimplePriorityQueue00000(IPDetermination<Ticket> prioDet) { |
|||
super(); |
|||
this.prioDet = prioDet; |
|||
|
|||
tickets = (T[]) java.lang.reflect.Array.newInstance(Ticket.class, 10); |
|||
size = 0; |
|||
} |
|||
|
|||
@Override |
|||
public boolean add(T t) { |
|||
if (t == null) { |
|||
return false; |
|||
} |
|||
if (t.getPriority() < 0) { |
|||
return false; |
|||
} |
|||
int priority = setPriority(t, t.getPriority()); |
|||
if (priority >= 0) { |
|||
return true; // fehler
|
|||
} else { |
|||
tickets[size] = t; |
|||
size++; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public T poll() { |
|||
if (size == 0) { |
|||
return null; |
|||
} |
|||
T ticket = tickets[0]; |
|||
for (int i = 1; i < size; i++) { |
|||
if (prioDet.calculate(tickets[i]) > prioDet.calculate(ticket)) { |
|||
ticket.incrementAge(); |
|||
ticket = tickets[i]; |
|||
} else { |
|||
tickets[i].incrementAge(); |
|||
} |
|||
} |
|||
remove(ticket); |
|||
return ticket; |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
return index >= 0; |
|||
} |
|||
|
|||
private int getIndexOfTicket(T t) { |
|||
for (int i = 0; i < size; i++) { |
|||
if (tickets[i].equals(t)) { |
|||
return i; |
|||
} |
|||
|
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
@Override |
|||
public int getTotalPriority(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index >= 0) { |
|||
return prioDet.calculate(tickets[index]); |
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
@Override |
|||
public int setPriority(T t, int newPriority) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index >= 0) { |
|||
tickets[index].setPriority(newPriority); |
|||
return prioDet.calculate(tickets[index]); |
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
@Override |
|||
public void forAll(Consumer<T> action) { |
|||
for (int i = 0; i < size; i++) { |
|||
action.accept(tickets[i]); |
|||
} |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public boolean remove(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index < 0) { |
|||
return false; |
|||
} |
|||
t.setStatus(TicketStatus.FINISHED); |
|||
for (int i = index; i < size - 1; i++) { |
|||
tickets[i] = tickets[i + 1]; |
|||
} |
|||
tickets[size - 1] = null; |
|||
size = size - 1; |
|||
return true; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,45 @@ |
|||
package solution_g11; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import testat_g11.ASimplePriorityQueueBaseTest; |
|||
import testat_g11.IPDetermination; |
|||
import testat_g11.IPriorityQueue; |
|||
import testat_g11.Ticket; |
|||
|
|||
public class SimplePriorityQueueTest00000 extends ASimplePriorityQueueBaseTest { |
|||
|
|||
protected static IPDetermination<Ticket> prioDetermination = new IPDetermination<>() { |
|||
|
|||
@Override |
|||
public int calculate(Ticket t) { |
|||
// TODO Auto-generated method stub
|
|||
return IPDetermination.super.calculate(t); |
|||
} |
|||
|
|||
}; |
|||
|
|||
@Override |
|||
protected IPriorityQueue<Ticket> getInstance() { |
|||
// TODO Auto-generated method stub
|
|||
return new SimplePriorityQueue00000<Ticket>(new IPDetermination<Ticket>() { |
|||
}); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll5() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.poll(); |
|||
queue.poll(); |
|||
queue.add(t8); |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(t2, actual); |
|||
actual = queue.poll(); |
|||
assertEquals(t8, actual); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,276 @@ |
|||
package testat_g11; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.fail; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
import org.junit.jupiter.api.BeforeAll; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
public abstract class ASimplePriorityQueueBaseTest { |
|||
|
|||
private int counter = 0; |
|||
private Consumer<Ticket> countLenght = s -> { |
|||
counter += ((String) s.getPayload()).length(); |
|||
}; |
|||
|
|||
protected IPriorityQueue<Ticket> queue; |
|||
protected Ticket t1 = new Ticket("Auftrag 1", 3); |
|||
protected Ticket t2 = new Ticket("Beschwerde 1", 1); |
|||
protected Ticket t3 = new Ticket("Beschwerde 2", 2); |
|||
protected Ticket t4 = new Ticket("Notfall 1", 8); |
|||
protected Ticket t5 = new Ticket("Bagatelle", 1); |
|||
protected Ticket t6 = new Ticket("Beschwerde 1", 6); |
|||
protected Ticket t7 = new Ticket("Auftrag 1", 4); |
|||
protected Ticket t8 = new Ticket("Beschwerde 2", 2); |
|||
protected Ticket t9 = new Ticket("Auftrag 2", -1); |
|||
protected Ticket t10 = new Ticket("Notfall 2", 9); |
|||
|
|||
protected abstract IPriorityQueue<Ticket> getInstance(); |
|||
|
|||
@BeforeAll |
|||
static void setUpBeforeClass() throws Exception { |
|||
} |
|||
|
|||
@BeforeEach |
|||
void setUp() throws Exception { |
|||
queue = getInstance(); |
|||
counter = 0; |
|||
} |
|||
|
|||
@Test |
|||
public void testAdd1() { |
|||
boolean actual = queue.add(t1); |
|||
assertEquals(true, actual); |
|||
assertEquals(1, queue.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testAdd2() { |
|||
queue.add(t1); |
|||
boolean actual = queue.add(t7); |
|||
assertEquals(true, actual); |
|||
assertEquals(1, queue.size()); |
|||
assertEquals(t1.getPriority(), 4); |
|||
} |
|||
|
|||
@Test |
|||
public void testAdd3() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.add(t4); |
|||
boolean actual = queue.add(t5); |
|||
assertEquals(true, actual); |
|||
assertEquals(5, queue.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testAdd4() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.add(t4); |
|||
queue.add(t5); |
|||
boolean actual = queue.add(t7); |
|||
assertEquals(true, actual); |
|||
assertEquals(5, queue.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testAdd9() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.add(t4); |
|||
queue.add(t5); |
|||
boolean actual = queue.add(t9); |
|||
assertEquals(false, actual); |
|||
assertEquals(5, queue.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll1() { |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(null, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll2() { |
|||
queue.add(t1); |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(t1, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll3() { |
|||
queue.add(t1); |
|||
queue.poll(); |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(null, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll4() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(t1, actual); |
|||
actual = queue.poll(); |
|||
assertEquals(t3, actual); |
|||
actual = queue.poll(); |
|||
assertEquals(t2, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll6() { |
|||
queue.add(t2); |
|||
queue.add(t5); |
|||
Ticket actual = queue.poll(); |
|||
if (actual.equals(t5)) { |
|||
if (t2.getBeginOfTicket().before(t5.getBeginOfTicket())) { |
|||
fail("t2 ist älter"); |
|||
} else { |
|||
assertEquals(true, true); |
|||
} |
|||
} else { |
|||
assertEquals(t2, actual); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
public void testGetContains1() { |
|||
var actual = queue.contains(t1); |
|||
assertEquals(false, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetContains2() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
var actual = queue.contains(t2); |
|||
assertEquals(true, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetContains3() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.poll(); |
|||
var actual = queue.contains(t1); |
|||
assertEquals(false, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetRemove1() { |
|||
var actual = queue.remove(t1); |
|||
assertEquals(false, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetRemove2() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
var actual = queue.remove(t1); |
|||
assertEquals(true, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetRemove3() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.remove(t1); |
|||
var actual = queue.contains(t1); |
|||
assertEquals(false, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void testGetRemove4() { |
|||
t1.setStatus(TicketStatus.CREATED); |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.remove(t1); |
|||
var actual = t1.getStatus(); |
|||
assertEquals(TicketStatus.FINISHED, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void getPriority1() { |
|||
var actual = queue.getTotalPriority(t1); |
|||
assertEquals(-1, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void getPriority2() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
|
|||
var actual = queue.getTotalPriority(t3); |
|||
assertEquals(2, actual); |
|||
} |
|||
|
|||
@Test |
|||
public void getAge() { |
|||
} |
|||
|
|||
@Test |
|||
public void setPriority1() { |
|||
var actual = queue.setPriority(t1, 1); |
|||
assertEquals(-1, actual); |
|||
}; |
|||
|
|||
@Test |
|||
public void setPriority2() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
var actual = queue.setPriority(t2, 5); |
|||
assertEquals(5, actual); |
|||
}; |
|||
|
|||
@Test |
|||
public void setPriority3() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.poll(); |
|||
queue.setPriority(t2, 0); |
|||
var actual = queue.getTotalPriority(t2); |
|||
assertEquals(1, actual); |
|||
}; |
|||
|
|||
@Test |
|||
public void setPriority4() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.setPriority(t2, 5); |
|||
var actual = queue.poll(); |
|||
assertEquals(t2, actual); |
|||
}; |
|||
|
|||
@Test |
|||
public void testForAll1() { |
|||
queue.forAll(null); |
|||
assertEquals(true, true); |
|||
}; |
|||
|
|||
@Test |
|||
public void testForAll2() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
|
|||
queue.forAll(countLenght); |
|||
assertEquals(33, counter); |
|||
}; |
|||
} |
@ -0,0 +1,33 @@ |
|||
package testat_g11; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
public abstract class ASimplePriorityQueueTest extends ASimplePriorityQueueBaseTest { |
|||
|
|||
protected static IPDetermination<Ticket> prioDetermination = new IPDetermination<Ticket>() { |
|||
@Override |
|||
public int calculate(Ticket t) { |
|||
// TODO Auto-generated method stub
|
|||
return IPDetermination.super.calculate(t); |
|||
} |
|||
|
|||
}; |
|||
|
|||
// Ageing
|
|||
@Test |
|||
public void testPoll5() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.poll(); |
|||
queue.poll(); |
|||
queue.add(t8); |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(t2, actual); |
|||
actual = queue.poll(); |
|||
assertEquals(t8, actual); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,11 @@ |
|||
package testat_g11; |
|||
|
|||
public interface IPDetermination<T extends Ticket> { |
|||
|
|||
default int calculate(T t) { |
|||
if (t instanceof Ticket) { |
|||
return (((Ticket) t).getAge() + ((Ticket) t).getPriority()); |
|||
} |
|||
return 0; |
|||
} |
|||
} |
@ -0,0 +1,29 @@ |
|||
package testat_g11; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
public interface IPriorityQueue<T extends Ticket> { |
|||
|
|||
// Nimmt t in die Queue auf, sofern t != null und t noch nicht enthalten und die
|
|||
// Priorität >= 0 ist.
|
|||
// ist t bereits enthalten, wird es nicht erneut aufgenommen, aber die Priorität
|
|||
// des bestehenden Tickets durch die Priorität von t ersetzt.
|
|||
// Rückgabe: False, wenn t == null, true sonst
|
|||
boolean add(T t); |
|||
|
|||
// Entfernt das Element mit der größten Priorität aus der Queue
|
|||
T poll(); |
|||
|
|||
boolean contains(T t); |
|||
|
|||
boolean remove(T t); |
|||
|
|||
int getTotalPriority(T t); |
|||
|
|||
int setPriority(T t, int newPriority); |
|||
|
|||
int size(); |
|||
|
|||
void forAll(Consumer<T> action); |
|||
|
|||
} |
@ -0,0 +1,97 @@ |
|||
package testat_g11; |
|||
|
|||
import java.util.Date; |
|||
|
|||
public class Ticket { |
|||
|
|||
private TicketStatus status; |
|||
private Date beginOfTicket; |
|||
private Date endOfTicket; |
|||
private Date beginOfWork; |
|||
private Date endOfWork; |
|||
private int priority; |
|||
private int age; |
|||
private Object payload; |
|||
|
|||
public Ticket(Object payload, int priority) { |
|||
super(); |
|||
this.payload = payload; |
|||
this.priority = priority; |
|||
this.age = 0; |
|||
this.status = TicketStatus.CREATED; |
|||
} |
|||
|
|||
public TicketStatus getStatus() { |
|||
return status; |
|||
} |
|||
|
|||
public int getPriority() { |
|||
return priority; |
|||
} |
|||
|
|||
public void setPriority(int priority) { |
|||
this.priority = priority; |
|||
} |
|||
|
|||
public int getAge() { |
|||
return age; |
|||
} |
|||
|
|||
public void incrementAge() { |
|||
this.age += 1; |
|||
} |
|||
|
|||
public void setStatus(TicketStatus status) { |
|||
this.status = status; |
|||
} |
|||
|
|||
public Date getBeginOfTicket() { |
|||
return beginOfTicket; |
|||
} |
|||
|
|||
public void setBeginOfTicket() { |
|||
this.beginOfTicket = new Date(); |
|||
} |
|||
|
|||
public Date getEndOfTicket() { |
|||
return endOfTicket; |
|||
} |
|||
|
|||
public void setEndOfTicket() { |
|||
this.endOfTicket = new Date(); |
|||
} |
|||
|
|||
public Date getBeginOfWork() { |
|||
return beginOfWork; |
|||
} |
|||
|
|||
public void setBeginOfWork() { |
|||
this.beginOfWork = new Date(); |
|||
} |
|||
|
|||
public Date getEndOfWork() { |
|||
return endOfWork; |
|||
} |
|||
|
|||
public void setEndOfWork() { |
|||
this.endOfWork = new Date(); |
|||
} |
|||
|
|||
public Object getPayload() { |
|||
return payload; |
|||
} |
|||
|
|||
public void setPayload(Object payload) { |
|||
this.payload = payload; |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object o) { |
|||
if (o instanceof Ticket) { |
|||
Ticket that = (Ticket) o; |
|||
return this.payload.equals(that.payload); |
|||
} |
|||
return false; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
package testat_g11; |
|||
|
|||
public enum TicketStatus { |
|||
CREATED, WAIT_FOR_PROCESSING, IN_PROGRESS, WAIT_FOR_ACTION, FINISHED, UNKNOWN |
|||
} |
Loading…
Reference in new issue