summaryrefslogtreecommitdiff
path: root/src/test/java/ChannelNodeTest.java
blob: c556812bc3e0dc8aebbc07af0554a17aecdea70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

class ChannelNodeTest {
    @Test void doesCorrectExceptions() {
        // Should throw an error when:
            // Invalid constructor argument
            // Invalid member function argument

        IChannelNode testNodeA = new ChannelNode();
        IChannelNode testNodeB = new ChannelNode();

        // Null map should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(null, IChannelNode.Direction.BOTH));

        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(new HashMap<>(), null));

        // Self endpoint should throw
        Map<IChannelNode, Integer> temp = new HashMap<>();
        temp.put(testNodeA, 1);

        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(temp, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(temp, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setConnections(temp, IChannelNode.Direction.BOTH));

        // Should not throw
        assertDoesNotThrow(() -> testNodeA.setConnections(new HashMap<>(), IChannelNode.Direction.INCOMING));
        assertDoesNotThrow(() -> testNodeA.setConnections(new HashMap<>(), IChannelNode.Direction.OUTGOING));
        assertDoesNotThrow(() -> testNodeA.setConnections(new HashMap<>(), IChannelNode.Direction.BOTH));



        // Null endpoint should throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, null, -1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, IChannelNode.Direction.INCOMING, 1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, IChannelNode.Direction.OUTGOING, 1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(null, IChannelNode.Direction.BOTH, 1));

        // Null direction should throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, null, 1));

        // Self endpoint should throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeA, IChannelNode.Direction.INCOMING, 1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeA, IChannelNode.Direction.OUTGOING, 1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeA, IChannelNode.Direction.BOTH, 1));

        // Negative connections should throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.INCOMING, -1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.OUTGOING, -1));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.BOTH, -1));

        // Should not throw
        assertDoesNotThrow(() -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.INCOMING, 10));
        assertDoesNotThrow(() -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.OUTGOING, 10));
        assertDoesNotThrow(() -> testNodeA.setNumConnections(testNodeB, IChannelNode.Direction.BOTH, 3));

        assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.INCOMING, 3));
        assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.OUTGOING, 5));
        assertDoesNotThrow(() -> testNodeB.setNumConnections(testNodeA, IChannelNode.Direction.BOTH, 90));



        // Null endpoint should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(null, IChannelNode.Direction.BOTH));

        // Self endpoint should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeA, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeA, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeA, IChannelNode.Direction.BOTH));

        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnection(testNodeB, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeB.addConnection(testNodeA, null));

        // Should not throw
        assertDoesNotThrow(() -> testNodeA.addConnection(testNodeB, IChannelNode.Direction.INCOMING));
        assertDoesNotThrow(() -> testNodeA.addConnection(testNodeB, IChannelNode.Direction.OUTGOING));
        assertDoesNotThrow(() -> testNodeA.addConnection(testNodeB, IChannelNode.Direction.BOTH));



        // Null iterable should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(null, IChannelNode.Direction.BOTH));

        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), null));
        assertThrows(IllegalArgumentException.class, () -> testNodeB.addConnections(List.of(new IChannelNode[]{testNodeA}), null));

        // Self endpoint should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.BOTH));

        // Should not throw
        assertDoesNotThrow(() -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), IChannelNode.Direction.INCOMING));
        assertDoesNotThrow(() -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), IChannelNode.Direction.OUTGOING));
        assertDoesNotThrow(() -> testNodeA.addConnections(List.of(new IChannelNode[]{testNodeB}), IChannelNode.Direction.BOTH));


        // Null node should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(null, IChannelNode.Direction.BOTH));

        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeB, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeB.removeConnection(testNodeA, null));

        // Self node should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeA, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeA, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnection(testNodeA, IChannelNode.Direction.BOTH));



        // Null iterable should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(null, IChannelNode.Direction.BOTH));

        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{}), null));

        // Self node should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.removeConnections(List.of(new IChannelNode[]{testNodeA}), IChannelNode.Direction.BOTH));



        // Null node should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, null));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.INCOMING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.OUTGOING));
        assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(null, IChannelNode.Direction.BOTH));

        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.connectionExists(testNodeB, null));



        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.getNumConnections(null));
        assertDoesNotThrow(() -> testNodeA.getNumConnections(IChannelNode.Direction.INCOMING));
        assertDoesNotThrow(() -> testNodeA.getNumConnections(IChannelNode.Direction.OUTGOING));
        assertDoesNotThrow(() -> testNodeA.getNumConnections(IChannelNode.Direction.BOTH));



        // Null direction should always throw
        assertThrows(IllegalArgumentException.class, () -> testNodeA.getConnections(null));
        assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.INCOMING));
        assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.OUTGOING));
        assertDoesNotThrow(() -> testNodeA.getConnections(IChannelNode.Direction.BOTH));
    }

    @Test void setters_n_getters() {
//        void addConnections(Iterable<IChannelNode> nodes, IChannelNode.Direction dir);
//        void removeConnection(IChannelNode node, IChannelNode.Direction dir);
//        void removeConnections(Iterable<IChannelNode> 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