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.opt;
018:
019: import java.util.Iterator;
020:
021: import junit.framework.TestCase;
022:
023: public class OptDirectedEdgeTest extends TestCase {
024:
025: private OptDirectedNode m_inNode;
026: private OptDirectedNode m_outNode;
027: private OptDirectedNode m_otherInNode;
028: private OptDirectedNode m_otherOutNode;
029:
030: private OptDirectedEdge m_edge;
031:
032: private OptDirectedEdge m_inEdge;
033: private OptDirectedEdge m_outEdge;
034:
035: private OptDirectedEdge m_same;
036: private OptDirectedEdge m_same2;
037: private OptDirectedEdge m_opp;
038: private OptDirectedEdge m_opp2;
039: private OptDirectedEdge m_inloop;
040: private OptDirectedEdge m_outloop;
041: private OptDirectedEdge m_inoutEdge;
042: private OptDirectedEdge m_outinEdge;
043:
044: public OptDirectedEdgeTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() throws Exception {
049: m_inNode = new OptDirectedNode();
050: m_inNode.setInDegree(1);
051: m_inNode.setOutDegree(1);
052:
053: m_outNode = new OptDirectedNode();
054: m_outNode.setInDegree(1);
055: m_outNode.setOutDegree(1);
056:
057: m_edge = new OptDirectedEdge(m_inNode, m_outNode);
058: m_inNode.addOut(m_edge);
059: m_outNode.addIn(m_edge);
060:
061: m_otherInNode = new OptDirectedNode();
062: m_otherInNode.setInDegree(0);
063: m_otherInNode.setOutDegree(1);
064:
065: m_otherOutNode = new OptDirectedNode();
066: m_otherOutNode.setInDegree(1);
067: m_otherOutNode.setOutDegree(0);
068:
069: m_inEdge = new OptDirectedEdge(m_otherInNode, m_inNode);
070: m_otherInNode.addOut(m_inEdge);
071: m_inNode.addIn(m_inEdge);
072:
073: m_outEdge = new OptDirectedEdge(m_outNode, m_otherOutNode);
074: m_outNode.addOut(m_outEdge);
075: m_otherOutNode.addIn(m_outEdge);
076:
077: m_same = new OptDirectedEdge(m_inNode, m_outNode);
078: m_same2 = new OptDirectedEdge(m_inNode, m_outNode);
079:
080: m_opp = new OptDirectedEdge(m_outNode, m_inNode);
081: m_opp2 = new OptDirectedEdge(m_outNode, m_inNode);
082:
083: m_inloop = new OptDirectedEdge(m_inNode, m_inNode);
084: m_outloop = new OptDirectedEdge(m_outNode, m_outNode);
085:
086: m_inoutEdge = new OptDirectedEdge(m_inNode, m_otherInNode);
087: m_outinEdge = new OptDirectedEdge(m_otherOutNode, m_outNode);
088: }
089:
090: protected void addSame() {
091: try {
092: setUp();
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096:
097: m_inNode.setOutDegree(2);
098: m_inNode.addOut(m_edge);
099: m_inNode.addOut(m_same);
100:
101: m_outNode.setInDegree(2);
102: m_outNode.addIn(m_edge);
103: m_outNode.addIn(m_same);
104: }
105:
106: protected void addSame2() {
107: try {
108: setUp();
109: } catch (Exception e) {
110: e.printStackTrace();
111: }
112:
113: m_inNode.setOutDegree(3);
114: m_inNode.addOut(m_edge);
115: m_inNode.addOut(m_same);
116: m_inNode.addOut(m_same2);
117:
118: m_outNode.setInDegree(3);
119: m_outNode.addIn(m_edge);
120: m_outNode.addIn(m_same);
121: m_outNode.addIn(m_same2);
122: }
123:
124: protected void addOpp() {
125: try {
126: setUp();
127: } catch (Exception e) {
128: e.printStackTrace();
129: }
130:
131: m_inNode.setInDegree(2);
132: m_inNode.addIn(m_inEdge);
133: m_inNode.addIn(m_opp);
134:
135: m_outNode.setOutDegree(2);
136: m_outNode.addOut(m_outEdge);
137: m_outNode.addOut(m_opp);
138: }
139:
140: protected void addOpp2() {
141: try {
142: setUp();
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146:
147: m_inNode.setInDegree(3);
148: m_inNode.addIn(m_inEdge);
149: m_inNode.addIn(m_opp);
150: m_inNode.addIn(m_opp2);
151:
152: m_outNode.setOutDegree(3);
153: m_outNode.addOut(m_outEdge);
154: m_outNode.addOut(m_opp);
155: m_outNode.addOut(m_opp2);
156: }
157:
158: protected void addLoops() {
159: try {
160: setUp();
161: } catch (Exception e) {
162: e.printStackTrace();
163: }
164:
165: m_inNode.setInDegree(2);
166: m_inNode.addIn(m_inEdge);
167: m_inNode.addIn(m_inloop);
168:
169: m_inNode.setOutDegree(2);
170: m_inNode.addOut(m_edge);
171: m_inNode.addOut(m_inloop);
172:
173: m_outNode.setInDegree(2);
174: m_outNode.addIn(m_edge);
175: m_outNode.addIn(m_outloop);
176:
177: m_outNode.setOutDegree(2);
178: m_outNode.addOut(m_outEdge);
179: m_outNode.addOut(m_outloop);
180: }
181:
182: protected void addInOutEdges() {
183: try {
184: setUp();
185: } catch (Exception e) {
186: e.printStackTrace();
187: }
188:
189: m_inNode.setOutDegree(2);
190: m_inNode.addOut(m_edge);
191: m_inNode.addOut(m_inoutEdge);
192:
193: m_otherInNode.setInDegree(1);
194: m_otherInNode.addIn(m_inoutEdge);
195:
196: m_outNode.setInDegree(2);
197: m_outNode.addIn(m_edge);
198: m_outNode.addIn(m_outinEdge);
199:
200: m_otherOutNode.setOutDegree(1);
201: m_otherInNode.addOut(m_outinEdge);
202:
203: }
204:
205: public void test_getInNode() {
206: assertTrue(m_inNode == m_edge.getNodeA());
207: assertTrue(m_inNode == m_edge.getInNode());
208: }
209:
210: public void test_getOutNode() {
211: assertTrue(m_outNode == m_edge.getNodeB());
212: assertTrue(m_outNode == m_edge.getOutNode());
213: }
214:
215: public void test_getOtherNode() {
216: assertTrue(m_inNode == m_edge.getOtherNode(m_outNode));
217: assertTrue(m_outNode == m_edge.getOtherNode(m_inNode));
218: assertTrue(null == m_edge.getOtherNode(new OptDirectedNode()));
219: }
220:
221: public void test_reverse() {
222: assertTrue(m_edge.getInNode().getOutEdges().contains(m_edge));
223: assertTrue(m_edge.getOutNode().getInEdges().contains(m_edge));
224:
225: try {
226: m_edge.reverse();
227: assertTrue(false);
228: } catch (UnsupportedOperationException e) {
229: assertTrue(true);
230: }
231:
232: }
233:
234: public void test_getRelated() {
235: Iterator itr = m_edge.getRelated();
236:
237: OptDirectedEdge de = (OptDirectedEdge) itr.next();
238: assertTrue(de == m_inEdge || de == m_outEdge);
239:
240: de = (OptDirectedEdge) itr.next();
241: assertTrue(de == m_inEdge || de == m_outEdge);
242:
243: assertTrue(!itr.hasNext());
244:
245: //add an edge that has the same nodes, in the same direction
246: addSame();
247:
248: itr = m_edge.getRelated();
249:
250: de = (OptDirectedEdge) itr.next();
251: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same);
252:
253: de = (OptDirectedEdge) itr.next();
254: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same);
255:
256: de = (OptDirectedEdge) itr.next();
257: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same);
258:
259: assertTrue(!itr.hasNext());
260:
261: //add another edge in the same direction
262: addSame2();
263:
264: itr = m_edge.getRelated();
265:
266: de = (OptDirectedEdge) itr.next();
267: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
268: || de == m_same2);
269:
270: de = (OptDirectedEdge) itr.next();
271: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
272: || de == m_same2);
273:
274: de = (OptDirectedEdge) itr.next();
275: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
276: || de == m_same2);
277:
278: de = (OptDirectedEdge) itr.next();
279: assertTrue(de == m_inEdge || de == m_outEdge || de == m_same
280: || de == m_same2);
281:
282: assertTrue(!itr.hasNext());
283:
284: addOpp();
285: //add an edge that has the same nodes, opposite direction
286: m_edge.getInNode().addIn(m_opp);
287: m_edge.getOutNode().addOut(m_opp);
288:
289: itr = m_edge.getRelated();
290:
291: de = (OptDirectedEdge) itr.next();
292: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp);
293:
294: de = (OptDirectedEdge) itr.next();
295: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp);
296:
297: de = (OptDirectedEdge) itr.next();
298: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp);
299:
300: assertTrue(!itr.hasNext());
301:
302: //add another edge in opposite direction
303: addOpp2();
304:
305: itr = m_edge.getRelated();
306:
307: de = (OptDirectedEdge) itr.next();
308: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
309: || de == m_opp2);
310:
311: de = (OptDirectedEdge) itr.next();
312: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
313: || de == m_opp2);
314:
315: de = (OptDirectedEdge) itr.next();
316: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
317: || de == m_opp2);
318:
319: de = (OptDirectedEdge) itr.next();
320: assertTrue(de == m_inEdge || de == m_outEdge || de == m_opp
321: || de == m_opp2);
322:
323: assertTrue(!itr.hasNext());
324:
325: //add loops
326: addLoops();
327:
328: itr = m_edge.getRelated();
329:
330: de = (OptDirectedEdge) itr.next();
331: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
332: || de == m_outloop);
333:
334: de = (OptDirectedEdge) itr.next();
335: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
336: || de == m_outloop);
337:
338: de = (OptDirectedEdge) itr.next();
339: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
340: || de == m_outloop);
341:
342: de = (OptDirectedEdge) itr.next();
343: assertTrue(de == m_inEdge || de == m_outEdge || de == m_inloop
344: || de == m_outloop);
345:
346: assertTrue(!itr.hasNext());
347:
348: //add an incoming edge to the out node and an outgoing edge to the in node
349: addInOutEdges();
350:
351: itr = m_edge.getRelated();
352:
353: de = (OptDirectedEdge) itr.next();
354: assertTrue(de == m_inEdge || de == m_outEdge
355: || de == m_inoutEdge || de == m_outinEdge);
356:
357: de = (OptDirectedEdge) itr.next();
358: assertTrue(de == m_inEdge || de == m_outEdge
359: || de == m_inoutEdge || de == m_outinEdge);
360:
361: de = (OptDirectedEdge) itr.next();
362: assertTrue(de == m_inEdge || de == m_outEdge
363: || de == m_inoutEdge || de == m_outinEdge);
364:
365: de = (OptDirectedEdge) itr.next();
366: assertTrue(de == m_inEdge || de == m_outEdge
367: || de == m_inoutEdge || de == m_outinEdge);
368:
369: assertTrue(!itr.hasNext());
370:
371: }
372:
373: public void test_getInRelated() {
374: Iterator itr = m_edge.getInRelated();
375:
376: OptDirectedEdge de = (OptDirectedEdge) itr.next();
377: assertTrue(de == m_inEdge);
378:
379: assertTrue(!itr.hasNext());
380:
381: //add same edge (shouldn't show up in iterator this time)
382: addSame();
383:
384: itr = m_edge.getInRelated();
385:
386: de = (OptDirectedEdge) itr.next();
387: assertTrue(de == m_inEdge);
388:
389: assertTrue(!itr.hasNext());
390:
391: //add multiple same edges
392: addSame2();
393:
394: itr = m_edge.getInRelated();
395:
396: de = (OptDirectedEdge) itr.next();
397: assertTrue(de == m_inEdge);
398:
399: assertTrue(!itr.hasNext());
400:
401: //add opposite edge
402: addOpp();
403:
404: itr = m_edge.getInRelated();
405:
406: de = (OptDirectedEdge) itr.next();
407: assertTrue(de == m_inEdge || de == m_opp);
408:
409: de = (OptDirectedEdge) itr.next();
410: assertTrue(de == m_inEdge || de == m_opp);
411:
412: assertTrue(!itr.hasNext());
413:
414: //add multiple opposites
415: addOpp2();
416:
417: itr = m_edge.getInRelated();
418:
419: de = (OptDirectedEdge) itr.next();
420: assertTrue(de == m_inEdge || de == m_opp || de == m_opp2);
421:
422: de = (OptDirectedEdge) itr.next();
423: assertTrue(de == m_inEdge || de == m_opp || de == m_opp2);
424:
425: de = (OptDirectedEdge) itr.next();
426: assertTrue(de == m_inEdge || de == m_opp || de == m_opp2);
427:
428: assertTrue(!itr.hasNext());
429:
430: addLoops();
431:
432: //add loop
433: m_edge.getInNode().addIn(m_inloop);
434: m_edge.getInNode().addOut(m_inloop);
435:
436: itr = m_edge.getInRelated();
437:
438: de = (OptDirectedEdge) itr.next();
439: assertTrue(de == m_inEdge || de == m_inloop);
440:
441: de = (OptDirectedEdge) itr.next();
442: assertTrue(de == m_inEdge || de == m_inloop);
443:
444: assertTrue(!itr.hasNext());
445: }
446:
447: public void test_getOutRelated() {
448: Iterator itr = m_edge.getOutRelated();
449:
450: OptDirectedEdge de = (OptDirectedEdge) itr.next();
451: assertTrue(de == m_outEdge);
452:
453: assertTrue(!itr.hasNext());
454:
455: //add same edge (shouldn't show up in iterator this time)
456: addSame();
457:
458: itr = m_edge.getOutRelated();
459:
460: de = (OptDirectedEdge) itr.next();
461: assertTrue(de == m_outEdge);
462:
463: assertTrue(!itr.hasNext());
464:
465: //add multiple same edge (shouldn't show up in iterator this time)
466: addSame2();
467:
468: itr = m_edge.getOutRelated();
469:
470: de = (OptDirectedEdge) itr.next();
471: assertTrue(de == m_outEdge);
472:
473: assertTrue(!itr.hasNext());
474:
475: //add opposite edge
476: addOpp();
477:
478: itr = m_edge.getOutRelated();
479:
480: de = (OptDirectedEdge) itr.next();
481: assertTrue(de == m_outEdge || de == m_opp);
482:
483: de = (OptDirectedEdge) itr.next();
484: assertTrue(de == m_outEdge || de == m_opp);
485:
486: assertTrue(!itr.hasNext());
487:
488: //add another opposite edge
489: addOpp2();
490:
491: itr = m_edge.getOutRelated();
492:
493: de = (OptDirectedEdge) itr.next();
494: assertTrue(de == m_outEdge || de == m_opp || de == m_opp2);
495:
496: de = (OptDirectedEdge) itr.next();
497: assertTrue(de == m_outEdge || de == m_opp || de == m_opp2);
498:
499: de = (OptDirectedEdge) itr.next();
500: assertTrue(de == m_outEdge || de == m_opp || de == m_opp2);
501:
502: assertTrue(!itr.hasNext());
503:
504: //add loop
505: addLoops();
506:
507: itr = m_edge.getOutRelated();
508:
509: de = (OptDirectedEdge) itr.next();
510: assertTrue(de == m_outEdge || de == m_outloop);
511:
512: de = (OptDirectedEdge) itr.next();
513: assertTrue(de == m_outEdge || de == m_outloop);
514:
515: assertTrue(!itr.hasNext());
516: }
517: }
|