Tests for sim

- added event and node tests
This commit is contained in:
2025-01-24 11:44:39 +01:00
parent 00a09be756
commit bbb069d4d1
4 changed files with 154 additions and 6 deletions

View File

@@ -54,13 +54,24 @@ public class ServerNode {
/**
* Creates a generic node with the given name and distribution.
* The servers number must be 1 or higher; if lower will be put to 1.
* The spawn number must be 0 or higher; if lower will be put to 0.
* The distribution can't be null, otherwise an exception is thrown.
*
* @param name The name of the node.
* @param maxServers The maximum number of servers in the queue.
* @param distribution The distribution of the service times.
* @param spawnArrivals The number of arrivals to spawn.
* @param name The name of the node.
* @param maxServers The maximum number of servers in the queue.
* @param distribution The distribution of the service times.
* @param spawnArrivals The number of arrivals to spawn.
* @throws NullPointerException if the distribution is null
*/
public ServerNode(String name, int maxServers, Distribution distribution, int spawnArrivals) {
if (distribution == null)
throw new NullPointerException("Distribution can't be null");
if (maxServers <= 0)
maxServers = 1;
if (spawnArrivals < 0)
spawnArrivals = 0;
this.name = name;
this.maxServers = maxServers;
this.distribution = distribution;
@@ -91,6 +102,6 @@ public class ServerNode {
* @return True if the node should spawn an arrival, false otherwise.
*/
public boolean shouldSpawnArrival(double numArrivals) {
return this.spawnArrivals > numArrivals;
return this.spawnArrivals > Math.max(0, numArrivals);
}
}

View File

@@ -15,6 +15,10 @@ import net.berack.upo.valpre.sim.stats.Result;
public class SimulationMultiple {
private final Net net;
/**
* TODO
* @param net
*/
public SimulationMultiple(Net net) {
this.net = net;
}

View File

@@ -88,7 +88,7 @@ public class Statistics {
}
/**
*
* TODO
*
* @param save
* @param val1

View File

@@ -0,0 +1,133 @@
package net.berack.upo.valpre.sim;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
import net.berack.upo.valpre.rand.Distribution;
import net.berack.upo.valpre.rand.Rng;
public class TestSimulation {
private static double DELTA = 0.0000001;
private static Rng rigged = new RiggedRng();
private static Distribution const0 = new Constant(0.0);
private static Distribution const1 = new Constant(0.0);
private final static class RiggedRng extends Rng {
@Override
public double random() {
return 0.5;
}
}
private final static class Constant implements Distribution {
public final double value;
public Constant(double value) {
this.value = value;
}
@Override
public double sample(Rng rng) {
return this.value;
}
}
@Test
public void serverNode() {
var node = new ServerNode("Nodo", 0, const1, 0);
assertEquals("Nodo", node.name);
assertEquals(1, node.maxServers);
assertFalse(node.shouldSpawnArrival(0));
assertFalse(node.shouldSpawnArrival(50));
assertFalse(node.shouldSpawnArrival(1000));
assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE));
assertFalse(node.shouldSpawnArrival(-1));
assertEquals(1.0, node.getPositiveSample(null), DELTA);
node = ServerNode.createQueue("Queue", 50, const1);
assertEquals("Queue", node.name);
assertEquals(50, node.maxServers);
assertFalse(node.shouldSpawnArrival(0));
assertFalse(node.shouldSpawnArrival(50));
assertFalse(node.shouldSpawnArrival(1000));
assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE));
assertFalse(node.shouldSpawnArrival(-1));
assertEquals(1.0, node.getPositiveSample(null), DELTA);
node = ServerNode.createSource("Source", const1);
assertEquals("Source", node.name);
assertEquals(Integer.MAX_VALUE, node.maxServers);
assertTrue(node.shouldSpawnArrival(0));
assertTrue(node.shouldSpawnArrival(50));
assertTrue(node.shouldSpawnArrival(1000));
assertTrue(node.shouldSpawnArrival(Integer.MAX_VALUE - 1));
assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE));
assertTrue(node.shouldSpawnArrival(-1));
assertEquals(1.0, node.getPositiveSample(null), DELTA);
node = ServerNode.createLimitedSource("Source", const1, 50);
assertEquals("Source", node.name);
assertEquals(Integer.MAX_VALUE, node.maxServers);
assertTrue(node.shouldSpawnArrival(0));
assertTrue(node.shouldSpawnArrival(49));
assertFalse(node.shouldSpawnArrival(50));
assertFalse(node.shouldSpawnArrival(1000));
assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE));
assertTrue(node.shouldSpawnArrival(-1));
assertEquals(1.0, node.getPositiveSample(null), DELTA);
}
@Test
public void event() {
var node = ServerNode.createSource("Source", const0);
var event = Event.newType(node, 0, Event.Type.ARRIVAL);
assertEquals(node, event.node);
assertEquals(0.0, event.time, 0.000000000001);
assertEquals(Event.Type.ARRIVAL, event.type);
var event2 = Event.newArrival(node, 1.0);
assertEquals(node, event2.node);
assertEquals(1.0, event2.time, 0.000000000001);
assertEquals(Event.Type.ARRIVAL, event2.type);
var event3 = Event.newDeparture(node, 5.0);
assertEquals(node, event3.node);
assertEquals(5.0, event3.time, 0.000000000001);
assertEquals(Event.Type.DEPARTURE, event3.type);
assertEquals(0, event2.compareTo(event2));
assertEquals(1, event2.compareTo(event));
assertEquals(-1, event2.compareTo(event3));
}
@Test
public void net() {
// TODO
var net = new Net();
net.addNode(null);
}
@Test
public void criteria() {
// TODO
var criteria = new EndCriteria.MaxTime(5.0);
criteria.shouldEnd(null);
}
@Test
public void simulation() {
// TODO
var sim = new Simulation(null, rigged);
sim.endSimulation();
}
@Test
public void multipleSim() {
// TODO
}
}