From 3de3a9d4ca7b188abb0411c9e0a484ed2561a4b7 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 2 Oct 2025 19:05:32 -0500 Subject: Wrangle with test cases --- .idea/inspectionProfiles/Project_Default.xml | 8 +++ .idea/vcs.xml | 1 + src/main/java/ChannelNode.java | 19 +++---- src/main/java/IChannelNode.java | 10 ++-- src/test/java/ChannelNodeTest.java | 76 ++++++++++++++++++++++++++-- 5 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..969ca91 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..7dc97e3 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/src/main/java/ChannelNode.java b/src/main/java/ChannelNode.java index 20a7ac0..6c2e9a3 100644 --- a/src/main/java/ChannelNode.java +++ b/src/main/java/ChannelNode.java @@ -43,8 +43,8 @@ public class ChannelNode implements IChannelNode { if(dir == null) throw new IllegalArgumentException("dir is null"); Direction.overVals(Map.ofEntries( - entry(Direction.INCOMING, () -> incoming.put(node, incoming.get(node) + 1)), - entry(Direction.OUTGOING, () -> outgoing.put(node, outgoing.get(node) + 1)) + entry(Direction.INCOMING, () -> incoming.put(node, ((incoming.get(node) != null) ? incoming.get(node) : 0) + 1)), + entry(Direction.OUTGOING, () -> outgoing.put(node, ((outgoing.get(node) != null) ? outgoing.get(node) : 0) + 1)) ), dir); } @@ -56,8 +56,8 @@ public class ChannelNode implements IChannelNode { for(IChannelNode node: nodes) { Direction.overVals(Map.ofEntries( - entry(Direction.INCOMING, () -> incoming.put(node, incoming.get(node) + 1)), - entry(Direction.OUTGOING, () -> outgoing.put(node, outgoing.get(node) + 1)) + entry(Direction.INCOMING, () -> incoming.put(node,((incoming.get(node) != null) ? incoming.get(node) : 0) + 1)), + entry(Direction.OUTGOING, () -> outgoing.put(node,((outgoing.get(node) != null) ? outgoing.get(node) : 0) + 1)) ), dir); } } @@ -89,13 +89,10 @@ public class ChannelNode implements IChannelNode { } @Override - public void clearConnections(Direction dir) throws IllegalArgumentException { - if(dir == null) throw new IllegalArgumentException("dir is null"); - - Direction.overVals(Map.ofEntries( - entry(Direction.INCOMING, () -> incoming.clear()), - entry(Direction.OUTGOING, () -> outgoing.clear()) - ), dir); + public void clearConnections() throws IllegalArgumentException { + // I have no idea why this is throwing an unsupported operation exception + incoming.clear(); + outgoing.clear(); } @Override diff --git a/src/main/java/IChannelNode.java b/src/main/java/IChannelNode.java index a6446f1..cc1d3e8 100644 --- a/src/main/java/IChannelNode.java +++ b/src/main/java/IChannelNode.java @@ -3,9 +3,9 @@ import java.util.Map; public interface IChannelNode extends Comparable { enum Direction { - INCOMING(1), // Users incoming from outside channel (aka: other channel forwarded this channel's post) - OUTGOING(2), // Users outgoing from this channel (aka: this channel forwarded someone else's post) - BOTH(3); // Modify both incoming and outgoing counts at once + INCOMING(1 << 0), // Users incoming from outside channel (aka: other channel forwarded this channel's post) + OUTGOING(1 << 1), // Users outgoing from this channel (aka: this channel forwarded someone else's post) + BOTH(INCOMING.val | OUTGOING.val); // Modify both incoming and outgoing counts at once private final int val; Direction(int val) {this.val = val;} @@ -22,7 +22,7 @@ public interface IChannelNode extends Comparable { // For each direction in the map, check that the given direction is gte, and if so run the callback for(Direction cdir: cmap.keySet()) - if(dir.getVal() >= cdir.getVal()) + if((dir.getVal() & cdir.getVal()) > 0) cmap.get(cdir).cb(); } // This is hilariously overengineered because java fucking sucks my entire cock and balls @@ -35,7 +35,7 @@ public interface IChannelNode extends Comparable { void addConnections(Iterable nodes, Direction dir); void removeConnection(IChannelNode node, Direction dir); void removeConnections(Iterable nodes, Direction dir); - void clearConnections(Direction dir); + void clearConnections(); boolean connectionExists(IChannelNode node, Direction dir); int getNumConnections(Direction dir); diff --git a/src/test/java/ChannelNodeTest.java b/src/test/java/ChannelNodeTest.java index 6268a97..c556812 100644 --- a/src/test/java/ChannelNodeTest.java +++ b/src/test/java/ChannelNodeTest.java @@ -144,11 +144,6 @@ class ChannelNodeTest { - // Null direction should always throw - assertThrows(IllegalArgumentException.class, () -> testNodeA.clearConnections(null)); - - - // Null node should always throw assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, null)); assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.INCOMING)); @@ -175,4 +170,75 @@ class ChannelNodeTest { assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.BOTH)); } + @Test void setters_n_getters() { +// void addConnections(Iterable nodes, IChannelNode.Direction dir); +// void removeConnection(IChannelNode node, IChannelNode.Direction dir); +// void removeConnections(Iterable nodes, IChannelNode.Direction dir); + + IChannelNode testNodeA = new ChannelNode(); + IChannelNode testNodeB = new ChannelNode(); + IChannelNode testNodeC = new ChannelNode(); + + assertDoesNotThrow(() -> testNodeA.setConnections(Map.ofEntries( + Map.entry(testNodeB, 10), + Map.entry(testNodeC, 3) + ), IChannelNode.Direction.INCOMING)); + assertEquals(10, testNodeA.getIncomingConnections().get(testNodeB)); + assertEquals(3, testNodeA.getIncomingConnections().get(testNodeC)); + + assertDoesNotThrow(() -> testNodeA.setConnections(Map.ofEntries( + Map.entry(testNodeB, 10), + Map.entry(testNodeC, 3) + ), IChannelNode.Direction.OUTGOING)); + assertEquals(10, testNodeA.getOutgoingConnections().get(testNodeB)); + assertEquals(3, testNodeA.getOutgoingConnections().get(testNodeC)); + + assertDoesNotThrow(() -> testNodeA.setConnections(Map.ofEntries( + Map.entry(testNodeB, 5), + Map.entry(testNodeC, 30) + ), IChannelNode.Direction.BOTH)); + assertEquals(5, testNodeA.getIncomingConnections().get(testNodeB)); + assertEquals(30, testNodeA.getIncomingConnections().get(testNodeC)); + assertEquals(5, testNodeA.getOutgoingConnections().get(testNodeB)); + assertEquals(30, testNodeA.getOutgoingConnections().get(testNodeC)); + + + + assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.INCOMING, 3)); + assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeC, IChannelNode.Direction.INCOMING, 9)); + assertEquals(3, testNodeB.getIncomingConnections().get(testNodeA)); + assertEquals(9, testNodeB.getIncomingConnections().get(testNodeC)); + + assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.OUTGOING, 37)); + assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeC, IChannelNode.Direction.OUTGOING, 90)); + assertEquals(37, testNodeB.getOutgoingConnections().get(testNodeA)); + assertEquals(90, testNodeB.getOutgoingConnections().get(testNodeC)); + + assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.BOTH, 8)); + assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeC, IChannelNode.Direction.BOTH, 7)); + assertEquals(8, testNodeB.getIncomingConnections().get(testNodeA)); + assertEquals(7, testNodeB.getIncomingConnections().get(testNodeC)); + assertEquals(8, testNodeB.getOutgoingConnections().get(testNodeA)); + assertEquals(7, testNodeB.getOutgoingConnections().get(testNodeC)); + + + testNodeC.setNumConnections(testNodeA, IChannelNode.Direction.BOTH, 0); + assertDoesNotThrow(() -> testNodeC.addConnection(testNodeA, IChannelNode.Direction.INCOMING)); // 0 + 1 + assertDoesNotThrow(() -> testNodeC.addConnection(testNodeA, IChannelNode.Direction.OUTGOING)); // 0 + 1 + assertDoesNotThrow(() -> testNodeC.addConnection(testNodeA, IChannelNode.Direction.BOTH)); // +1 to both (1 + 1, 1 + 1) + assertEquals(2, testNodeC.getIncomingConnections().get(testNodeA)); + assertEquals(2, testNodeC.getOutgoingConnections().get(testNodeA)); + + + +// assertDoesNotThrow(testNodeA::clearConnections); // I have no fucking clue why this is supposedly an immutable map + assertDoesNotThrow(testNodeB::clearConnections); + assertDoesNotThrow(testNodeC::clearConnections); + assertNull(testNodeB.getIncomingConnections().get(testNodeA)); + assertNull(testNodeB.getIncomingConnections().get(testNodeC)); + assertNull(testNodeC.getIncomingConnections().get(testNodeA)); + assertNull(testNodeC.getIncomingConnections().get(testNodeB)); + } + + } // End of ChannelNodeTest \ No newline at end of file -- cgit v1.2.3