Implement toString method for ServerNode class and add unit tests for its output

This commit is contained in:
2025-02-12 21:23:43 +01:00
parent 6b3b5fea2f
commit 3a769d6ae3
2 changed files with 43 additions and 0 deletions

View File

@@ -71,6 +71,28 @@ public class ServerNode {
return Distribution.getPositiveSample(this.unavailable, rng);
}
@Override
public String toString() {
try {
var build = new StringBuilder()
.append(this.name)
.append("[servers:")
.append(this.maxServers)
.append(", queue:")
.append(this.maxQueue)
.append(", spawn:")
.append(this.spawnArrivals)
.append(", ")
.append(Distribution.toString(this.service));
if (this.unavailable != null)
build.append(", u:").append(Distribution.toString(this.unavailable));
return build.append(']').toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ServerNode))

View File

@@ -29,6 +29,27 @@ public class TestInteractions {
assertEquals("UnavailableTime(0.1, Exponential(1.0))", Distribution.toString(unavailable));
}
@Test
public void nodeToString() {
var exp = new Distribution.Exponential(1.0);
var normal = new Distribution.Normal(3.2, 0.6);
var unavailable = new Distribution.UnavailableTime(0.1, exp);
var node = new ServerNode.Builder("Source", exp).build();
assertEquals("Source[servers:1, queue:100, spawn:0, Exponential(1.0)]", node.toString());
node = new ServerNode.Builder("Queue", normal).build();
assertEquals("Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)]", node.toString());
node = new ServerNode.Builder("Queue", normal).queue(10).servers(5).spawn(100).build();
assertEquals("Queue[servers:5, queue:10, spawn:100, Normal(3.2, 0.6)]", node.toString());
node = new ServerNode.Builder("Queue", normal).queue(10).servers(5).spawn(100).unavailable(unavailable).build();
assertEquals(
"Queue[servers:5, queue:10, spawn:100, Normal(3.2, 0.6), u:UnavailableTime(0.1, Exponential(1.0))]",
node.toString());
}
@Test
public void netToString() {
var net = new Net();