001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.graph.structure.basic;
018:
019: import java.util.Iterator;
020:
021: import junit.framework.TestCase;
022:
023: public class BasicDirectedNodeTest extends TestCase {
024:
025: private BasicDirectedNode m_node;
026: private BasicDirectedNode m_inNode1;
027: private BasicDirectedNode m_inNode2;
028: private BasicDirectedNode m_outNode1;
029: private BasicDirectedNode m_outNode2;
030:
031: private BasicDirectedEdge m_inEdge1;
032: private BasicDirectedEdge m_inEdge2;
033: private BasicDirectedEdge m_outEdge1;
034: private BasicDirectedEdge m_outEdge2;
035:
036: private BasicDirectedEdge m_loop;
037:
038: public BasicDirectedNodeTest(String name) {
039: super (name);
040: }
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044:
045: m_node = new BasicDirectedNode();
046: m_inNode1 = new BasicDirectedNode();
047: m_inNode2 = new BasicDirectedNode();
048: m_outNode1 = new BasicDirectedNode();
049: m_outNode2 = new BasicDirectedNode();
050:
051: m_inEdge1 = new BasicDirectedEdge(m_inNode1, m_node);
052: m_inEdge2 = new BasicDirectedEdge(m_inNode2, m_node);
053: m_outEdge1 = new BasicDirectedEdge(m_node, m_outNode1);
054: m_outEdge2 = new BasicDirectedEdge(m_node, m_outNode2);
055:
056: m_loop = new BasicDirectedEdge(m_node, m_node);
057: }
058:
059: public void test_addIn() {
060: assertTrue(m_node.getInEdges().size() == 0);
061:
062: //single in edge
063: m_node.addIn(m_inEdge1);
064: assertTrue(m_node.getInEdges().contains(m_inEdge1));
065: assertTrue(m_node.getInEdges().size() == 1);
066:
067: //multiple in edges, same edge
068: m_node.addIn(m_inEdge1);
069: assertTrue(m_node.getInEdges().size() == 2);
070: m_node.removeIn(m_inEdge1);
071:
072: //multiple in edges, different
073: m_node.addIn(m_inEdge2);
074: assertTrue(m_node.getInEdges().contains(m_inEdge1));
075: assertTrue(m_node.getInEdges().contains(m_inEdge2));
076: assertTrue(m_node.getInEdges().size() == 2);
077: }
078:
079: public void test_addOut() {
080: assertTrue(m_node.getOutEdges().size() == 0);
081:
082: //single out edge
083: m_node.addOut(m_outEdge1);
084: assertTrue(m_node.getOutEdges().contains(m_outEdge1));
085: assertTrue(m_node.getOutEdges().size() == 1);
086:
087: //multiple out edges, same edge
088: m_node.addOut(m_outEdge1);
089: assertTrue(m_node.getOutEdges().size() == 2);
090: m_node.removeOut(m_outEdge1);
091:
092: //multiple out edges, different
093: m_node.addOut(m_outEdge2);
094: assertTrue(m_node.getOutEdges().contains(m_outEdge1));
095: assertTrue(m_node.getOutEdges().contains(m_outEdge2));
096: assertTrue(m_node.getOutEdges().size() == 2);
097: }
098:
099: public void test_remove() {
100: m_node.addIn(m_inEdge1);
101: m_node.addOut(m_outEdge1);
102:
103: m_node.remove(m_inEdge1);
104: m_node.remove(m_outEdge1);
105:
106: assertTrue(m_node.getInEdges().size() == 0);
107: assertTrue(m_node.getOutEdges().size() == 0);
108: }
109:
110: public void test_removeIn() {
111: //single edge
112: m_node.addIn(m_inEdge1);
113: m_node.removeIn(m_inEdge1);
114: assertTrue(m_node.getInEdges().isEmpty());
115:
116: //multiple edges same
117: m_node.addIn(m_inEdge1);
118: m_node.addIn(m_inEdge1);
119: assertTrue(m_node.getInEdges().size() == 2);
120:
121: m_node.removeIn(m_inEdge1);
122: assertTrue(m_node.getInEdges().size() == 1);
123:
124: m_node.removeIn(m_inEdge1);
125: assertTrue(m_node.getInEdges().size() == 0);
126:
127: //multiple edges different
128: m_node.addIn(m_inEdge1);
129: m_node.addIn(m_inEdge2);
130:
131: m_node.removeIn(m_inEdge1);
132: assertTrue(m_node.getInEdges().size() == 1);
133: assertTrue(m_node.getInEdges().contains(m_inEdge2));
134:
135: m_node.removeIn(m_inEdge2);
136: assertTrue(m_node.getInEdges().size() == 0);
137: }
138:
139: public void test_removeOut() {
140: //single edge
141: m_node.addOut(m_outEdge1);
142: m_node.removeOut(m_outEdge1);
143: assertTrue(m_node.getOutEdges().isEmpty());
144:
145: //multiple edges same
146: m_node.addOut(m_outEdge1);
147: m_node.addOut(m_outEdge1);
148: assertTrue(m_node.getOutEdges().size() == 2);
149:
150: m_node.removeOut(m_outEdge1);
151: assertTrue(m_node.getOutEdges().size() == 1);
152:
153: m_node.removeOut(m_outEdge1);
154: assertTrue(m_node.getOutEdges().size() == 0);
155:
156: //multiple edges different
157: m_node.addOut(m_outEdge1);
158: m_node.addOut(m_outEdge2);
159:
160: m_node.removeOut(m_outEdge1);
161: assertTrue(m_node.getOutEdges().size() == 1);
162: assertTrue(m_node.getOutEdges().contains(m_outEdge2));
163:
164: m_node.removeOut(m_outEdge2);
165: assertTrue(m_node.getOutEdges().size() == 0);
166: }
167:
168: public void test_getEdge() {
169: m_node.addIn(m_inEdge1);
170: m_node.addOut(m_outEdge1);
171:
172: assertSame(m_node.getEdge(m_inNode1), m_inEdge1);
173: assertSame(m_node.getEdge(m_outNode1), m_outEdge1);
174: }
175:
176: public void test_getInEdge() {
177: m_node.addIn(m_inEdge1);
178: assertSame(m_node.getInEdge(m_inNode1), m_inEdge1);
179: }
180:
181: public void test_getOutEdge() {
182: m_node.addOut(m_outEdge1);
183: assertSame(m_node.getOutEdge(m_outNode1), m_outEdge1);
184: }
185:
186: public void test_getEdges_0() {
187: m_node.addIn(m_inEdge1);
188: m_node.addOut(m_outEdge1);
189:
190: assertTrue(m_node.getEdges().size() == 2);
191: assertTrue(m_node.getEdges().contains(m_inEdge1));
192: assertTrue(m_node.getEdges().contains(m_outEdge1));
193: }
194:
195: public void test_getEdges_1() {
196: m_node.addIn(m_inEdge1);
197: m_node.addIn(m_inEdge2);
198:
199: m_node.addOut(m_outEdge1);
200: m_node.addOut(m_outEdge2);
201:
202: assertTrue(m_node.getEdges(m_inNode1).contains(m_inEdge1));
203: assertTrue(m_node.getEdges(m_inNode2).contains(m_inEdge2));
204: assertTrue(m_node.getEdges(m_outNode1).contains(m_outEdge1));
205: assertTrue(m_node.getEdges(m_outNode2).contains(m_outEdge2));
206:
207: //add duplicates
208: m_node.addIn(m_inEdge1);
209: m_node.addIn(m_inEdge2);
210:
211: m_node.addOut(m_outEdge1);
212: m_node.addOut(m_outEdge2);
213:
214: assertTrue(m_node.getEdges(m_inNode1).contains(m_inEdge1));
215: assertTrue(m_node.getEdges(m_inNode1).size() == 2);
216: assertTrue(m_node.getEdges(m_inNode2).contains(m_inEdge2));
217: assertTrue(m_node.getEdges(m_inNode2).size() == 2);
218: assertTrue(m_node.getEdges(m_outNode1).contains(m_outEdge1));
219: assertTrue(m_node.getEdges(m_outNode1).size() == 2);
220: assertTrue(m_node.getEdges(m_outNode2).contains(m_outEdge2));
221: assertTrue(m_node.getEdges(m_outNode2).size() == 2);
222: }
223:
224: public void test_getInEdges_0() {
225: m_node.addIn(m_inEdge1);
226: m_node.addIn(m_inEdge2);
227:
228: assertTrue(m_node.getInEdges().contains(m_inEdge1));
229: assertTrue(m_node.getInEdges().contains(m_inEdge2));
230: assertTrue(m_node.getInEdges().size() == 2);
231: }
232:
233: public void test_getInEdges_1() {
234: m_node.addIn(m_inEdge1);
235: m_node.addIn(m_inEdge2);
236:
237: assertTrue(m_node.getInEdges(m_inNode1).contains(m_inEdge1));
238: assertTrue(m_node.getInEdges(m_inNode2).contains(m_inEdge2));
239:
240: m_node.addIn(m_inEdge1);
241: m_node.addIn(m_inEdge2);
242:
243: assertTrue(m_node.getInEdges(m_inNode1).contains(m_inEdge1));
244: assertTrue(m_node.getInEdges(m_inNode1).size() == 2);
245:
246: assertTrue(m_node.getInEdges(m_inNode2).contains(m_inEdge2));
247: assertTrue(m_node.getInEdges(m_inNode2).size() == 2);
248: }
249:
250: public void test_getOutEdges_0() {
251: m_node.addOut(m_outEdge1);
252: m_node.addOut(m_outEdge2);
253:
254: assertTrue(m_node.getOutEdges().contains(m_outEdge1));
255: assertTrue(m_node.getOutEdges().contains(m_outEdge2));
256: assertTrue(m_node.getOutEdges().size() == 2);
257: }
258:
259: public void test_getOutEdges_1() {
260: m_node.addOut(m_outEdge1);
261: m_node.addOut(m_outEdge2);
262:
263: assertTrue(m_node.getOutEdges(m_outNode1).contains(m_outEdge1));
264: assertTrue(m_node.getOutEdges(m_outNode2).contains(m_outEdge2));
265:
266: m_node.addOut(m_outEdge1);
267: m_node.addOut(m_outEdge2);
268:
269: assertTrue(m_node.getOutEdges(m_outNode1).contains(m_outEdge1));
270: assertTrue(m_node.getOutEdges(m_outNode1).size() == 2);
271:
272: assertTrue(m_node.getOutEdges(m_outNode2).contains(m_outEdge2));
273: assertTrue(m_node.getOutEdges(m_outNode2).size() == 2);
274: }
275:
276: public void test_getDegree() {
277: m_node.addIn(m_inEdge1);
278: m_node.addOut(m_outEdge2);
279: assertTrue(m_node.getDegree() == 2);
280: }
281:
282: public void test_getInDegree() {
283: m_node.addIn(m_inEdge1);
284: m_node.addOut(m_outEdge2);
285: assertTrue(m_node.getInDegree() == 1);
286: }
287:
288: public void test_getOutDegree_0() {
289: m_node.addIn(m_inEdge1);
290: m_node.addOut(m_outEdge2);
291:
292: assertTrue(m_node.getOutDegree() == 1);
293: }
294:
295: public void test_getRelated() {
296: m_node.addIn(m_inEdge1);
297: m_node.addIn(m_inEdge2);
298:
299: m_node.addOut(m_outEdge1);
300: m_node.addOut(m_outEdge2);
301:
302: Iterator related = m_node.getRelated();
303: BasicDirectedNode dn = (BasicDirectedNode) related.next();
304: assertTrue(dn == m_inNode1 || dn == m_inNode2
305: || dn == m_outNode1 || dn == m_outNode2);
306:
307: dn = (BasicDirectedNode) related.next();
308: assertTrue(dn == m_inNode1 || dn == m_inNode2
309: || dn == m_outNode1 || dn == m_outNode2);
310:
311: dn = (BasicDirectedNode) related.next();
312: assertTrue(dn == m_inNode1 || dn == m_inNode2
313: || dn == m_outNode1 || dn == m_outNode2);
314:
315: dn = (BasicDirectedNode) related.next();
316: assertTrue(dn == m_inNode1 || dn == m_inNode2
317: || dn == m_outNode1 || dn == m_outNode2);
318:
319: assertTrue(!related.hasNext());
320:
321: //add loop
322: m_node.addIn(m_loop);
323: m_node.addOut(m_loop);
324:
325: related = m_node.getRelated();
326: dn = (BasicDirectedNode) related.next();
327: assertTrue(dn == m_inNode1 || dn == m_inNode2
328: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
329:
330: dn = (BasicDirectedNode) related.next();
331: assertTrue(dn == m_inNode1 || dn == m_inNode2
332: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
333:
334: dn = (BasicDirectedNode) related.next();
335: assertTrue(dn == m_inNode1 || dn == m_inNode2
336: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
337:
338: dn = (BasicDirectedNode) related.next();
339: assertTrue(dn == m_inNode1 || dn == m_inNode2
340: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
341:
342: dn = (BasicDirectedNode) related.next();
343: assertTrue(dn == m_inNode1 || dn == m_inNode2
344: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
345:
346: dn = (BasicDirectedNode) related.next();
347: assertTrue(dn == m_inNode1 || dn == m_inNode2
348: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
349:
350: assertTrue(!related.hasNext());
351: }
352:
353: public void test_getInRelated() {
354: m_node.addIn(m_inEdge1);
355: m_node.addIn(m_inEdge2);
356:
357: m_node.addOut(m_outEdge1);
358: m_node.addOut(m_outEdge2);
359:
360: Iterator related = m_node.getInRelated();
361: BasicDirectedNode dn = (BasicDirectedNode) related.next();
362: assertTrue(dn == m_inNode1 || dn == m_inNode2);
363:
364: dn = (BasicDirectedNode) related.next();
365: assertTrue(dn == m_inNode1 || dn == m_inNode2);
366: assertTrue(!related.hasNext());
367:
368: //add a loop
369: m_node.addIn(m_loop);
370: m_node.addOut(m_loop);
371:
372: related = m_node.getInRelated();
373:
374: dn = (BasicDirectedNode) related.next();
375: assertTrue(dn == m_inNode1 || dn == m_inNode2 || dn == m_node);
376:
377: dn = (BasicDirectedNode) related.next();
378: assertTrue(dn == m_inNode1 || dn == m_inNode2 || dn == m_node);
379:
380: dn = (BasicDirectedNode) related.next();
381: assertTrue(dn == m_inNode1 || dn == m_inNode2 || dn == m_node);
382:
383: assertTrue(!related.hasNext());
384:
385: }
386:
387: public void test_getOutRelated() {
388: m_node.addIn(m_inEdge1);
389: m_node.addIn(m_inEdge2);
390:
391: m_node.addOut(m_outEdge1);
392: m_node.addOut(m_outEdge2);
393:
394: Iterator related = m_node.getOutRelated();
395: BasicDirectedNode dn = (BasicDirectedNode) related.next();
396: assertTrue(dn == m_outNode1 || dn == m_outNode2);
397:
398: dn = (BasicDirectedNode) related.next();
399: assertTrue(dn == m_outNode1 || dn == m_outNode2);
400:
401: assertTrue(!related.hasNext());
402:
403: //add a loop
404: m_node.addIn(m_loop);
405: m_node.addOut(m_loop);
406:
407: related = m_node.getOutRelated();
408:
409: dn = (BasicDirectedNode) related.next();
410: assertTrue(dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
411:
412: dn = (BasicDirectedNode) related.next();
413: assertTrue(dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
414:
415: dn = (BasicDirectedNode) related.next();
416: assertTrue(dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
417:
418: assertTrue(!related.hasNext());
419: }
420: }
|