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 OptDirectedNodeTest extends TestCase {
024: private OptDirectedNode m_node;
025: private OptDirectedNode m_inNode1;
026: private OptDirectedNode m_inNode2;
027: private OptDirectedNode m_outNode1;
028: private OptDirectedNode m_outNode2;
029:
030: private OptDirectedEdge m_inEdge1;
031: private OptDirectedEdge m_inEdge2;
032: private OptDirectedEdge m_outEdge1;
033: private OptDirectedEdge m_outEdge2;
034:
035: private OptDirectedEdge m_loop;
036:
037: public OptDirectedNodeTest(String name) {
038: super (name);
039: }
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043:
044: m_node = new OptDirectedNode();
045: // m_node.setInDegree(2);
046: // m_node.setOutDegree(2);
047:
048: m_inNode1 = new OptDirectedNode();
049: // m_inNode1.setInDegree(0);
050: // m_inNode1.setOutDegree(1);
051:
052: m_inNode2 = new OptDirectedNode();
053: // m_inNode2.setInDegree(0);
054: // m_inNode2.setOutDegree(1);
055:
056: m_outNode1 = new OptDirectedNode();
057: // m_outNode1.setInDegree(1);
058: // m_outNode1.setOutDegree(0);
059:
060: m_outNode2 = new OptDirectedNode();
061: // m_outNode2.setInDegree(1);
062: // m_outNode2.setOutDegree(0);
063:
064: m_inEdge1 = new OptDirectedEdge(m_inNode1, m_node);
065: m_inEdge2 = new OptDirectedEdge(m_inNode2, m_node);
066: m_outEdge1 = new OptDirectedEdge(m_node, m_outNode1);
067: m_outEdge2 = new OptDirectedEdge(m_node, m_outNode2);
068:
069: //dont add yet
070: m_loop = new OptDirectedEdge(m_node, m_node);
071: }
072:
073: public void test_addIn() {
074: assertTrue(m_node.getInEdges().size() == 0);
075:
076: //single in edge
077: m_node.setInDegree(1);
078: m_node.addIn(m_inEdge1);
079:
080: assertTrue(m_node.getInEdges().contains(m_inEdge1));
081: assertTrue(m_node.getInEdges().size() == 1);
082:
083: //multiple in edges, same edge
084: m_node.setInDegree(2);
085: m_node.addIn(m_inEdge1);
086: m_node.addIn(m_inEdge1);
087:
088: assertTrue(m_node.getInEdges().size() == 2);
089:
090: //multiple in edges, different
091: m_node.setInDegree(2);
092: m_node.addIn(m_inEdge1);
093: m_node.addIn(m_inEdge2);
094: assertTrue(m_node.getInEdges().contains(m_inEdge1));
095: assertTrue(m_node.getInEdges().contains(m_inEdge2));
096: assertTrue(m_node.getInEdges().size() == 2);
097: }
098:
099: public void test_addOut() {
100: assertTrue(m_node.getOutEdges().size() == 0);
101:
102: //single out edge
103: m_node.setOutDegree(1);
104: m_node.addOut(m_outEdge1);
105: assertTrue(m_node.getOutEdges().contains(m_outEdge1));
106: assertTrue(m_node.getOutEdges().size() == 1);
107:
108: //multiple out edges, same edge
109: m_node.setOutDegree(2);
110: m_node.addOut(m_outEdge1);
111: m_node.addOut(m_outEdge1);
112:
113: assertTrue(m_node.getOutEdges().size() == 2);
114:
115: //multiple out edges, different
116: m_node.setOutDegree(2);
117: m_node.addOut(m_outEdge1);
118: m_node.addOut(m_outEdge2);
119: assertTrue(m_node.getOutEdges().contains(m_outEdge1));
120: assertTrue(m_node.getOutEdges().contains(m_outEdge2));
121: assertTrue(m_node.getOutEdges().size() == 2);
122: }
123:
124: public void test_remove() {
125: m_node.setInDegree(1);
126: m_node.setOutDegree(1);
127:
128: m_node.addIn(m_inEdge1);
129: m_node.addOut(m_outEdge1);
130:
131: try {
132: m_node.remove(m_inEdge1);
133: assertTrue(true);
134: } catch (UnsupportedOperationException uoe) {
135: assertTrue(true);
136: }
137:
138: try {
139: m_node.remove(m_outEdge1);
140: assertTrue(true);
141: } catch (UnsupportedOperationException uoe) {
142: assertTrue(true);
143: }
144: }
145:
146: public void test_removeIn() {
147: //single edge
148: m_node.setInDegree(1);
149: m_node.addIn(m_inEdge1);
150:
151: try {
152: m_node.removeIn(m_inEdge1);
153: assertTrue(true);
154: } catch (UnsupportedOperationException uoe) {
155: assertTrue(true);
156: }
157: }
158:
159: public void test_removeOut() {
160: //single edge
161: m_node.setOutDegree(1);
162: m_node.addOut(m_outEdge1);
163:
164: try {
165: m_node.removeOut(m_outEdge1);
166: assertTrue(true);
167: } catch (UnsupportedOperationException uoe) {
168: assertTrue(true);
169: }
170: }
171:
172: public void test_getEdge() {
173: m_node.setInDegree(1);
174: m_node.setOutDegree(1);
175:
176: m_node.addIn(m_inEdge1);
177: m_node.addOut(m_outEdge1);
178:
179: assertSame(m_node.getEdge(m_inNode1), m_inEdge1);
180: assertSame(m_node.getEdge(m_outNode1), m_outEdge1);
181: }
182:
183: public void test_getInEdge() {
184: m_node.setInDegree(1);
185: m_node.addIn(m_inEdge1);
186: assertSame(m_node.getInEdge(m_inNode1), m_inEdge1);
187: }
188:
189: public void test_getOutEdge() {
190: m_node.setOutDegree(1);
191: m_node.addOut(m_outEdge1);
192: assertSame(m_node.getOutEdge(m_outNode1), m_outEdge1);
193: }
194:
195: public void test_getEdges_0() {
196: m_node.setInDegree(1);
197: m_node.setOutDegree(1);
198:
199: m_node.addIn(m_inEdge1);
200: m_node.addOut(m_outEdge1);
201:
202: assertTrue(m_node.getEdges().size() == 2);
203: assertTrue(m_node.getEdges().contains(m_inEdge1));
204: assertTrue(m_node.getEdges().contains(m_outEdge1));
205: }
206:
207: public void test_getEdges_1() {
208: m_node.setInDegree(2);
209: m_node.addIn(m_inEdge1);
210: m_node.addIn(m_inEdge2);
211:
212: m_node.setOutDegree(2);
213: m_node.addOut(m_outEdge1);
214: m_node.addOut(m_outEdge2);
215:
216: assertTrue(m_node.getEdges(m_inNode1).contains(m_inEdge1));
217: assertTrue(m_node.getEdges(m_inNode2).contains(m_inEdge2));
218: assertTrue(m_node.getEdges(m_outNode1).contains(m_outEdge1));
219: assertTrue(m_node.getEdges(m_outNode2).contains(m_outEdge2));
220:
221: //add duplicates
222: m_node.setInDegree(4);
223: m_node.addIn(m_inEdge1);
224: m_node.addIn(m_inEdge2);
225: m_node.addIn(m_inEdge1);
226: m_node.addIn(m_inEdge2);
227:
228: m_node.setOutDegree(4);
229: m_node.addOut(m_outEdge1);
230: m_node.addOut(m_outEdge2);
231: m_node.addOut(m_outEdge1);
232: m_node.addOut(m_outEdge2);
233:
234: assertTrue(m_node.getEdges(m_inNode1).contains(m_inEdge1));
235: assertTrue(m_node.getEdges(m_inNode1).size() == 2);
236: assertTrue(m_node.getEdges(m_inNode2).contains(m_inEdge2));
237: assertTrue(m_node.getEdges(m_inNode2).size() == 2);
238: assertTrue(m_node.getEdges(m_outNode1).contains(m_outEdge1));
239: assertTrue(m_node.getEdges(m_outNode1).size() == 2);
240: assertTrue(m_node.getEdges(m_outNode2).contains(m_outEdge2));
241: assertTrue(m_node.getEdges(m_outNode2).size() == 2);
242: }
243:
244: public void test_getInEdges_0() {
245: m_node.setInDegree(2);
246: m_node.addIn(m_inEdge1);
247: m_node.addIn(m_inEdge2);
248:
249: assertTrue(m_node.getInEdges().contains(m_inEdge1));
250: assertTrue(m_node.getInEdges().contains(m_inEdge2));
251: assertTrue(m_node.getInEdges().size() == 2);
252: }
253:
254: public void test_getInEdges_1() {
255: m_node.setInDegree(2);
256: m_node.addIn(m_inEdge1);
257: m_node.addIn(m_inEdge2);
258:
259: assertTrue(m_node.getInEdges(m_inNode1).contains(m_inEdge1));
260: assertTrue(m_node.getInEdges(m_inNode2).contains(m_inEdge2));
261:
262: m_node.setInDegree(4);
263: m_node.addIn(m_inEdge1);
264: m_node.addIn(m_inEdge2);
265: m_node.addIn(m_inEdge1);
266: m_node.addIn(m_inEdge2);
267:
268: assertTrue(m_node.getInEdges(m_inNode1).contains(m_inEdge1));
269: assertTrue(m_node.getInEdges(m_inNode1).size() == 2);
270:
271: assertTrue(m_node.getInEdges(m_inNode2).contains(m_inEdge2));
272: assertTrue(m_node.getInEdges(m_inNode2).size() == 2);
273: }
274:
275: public void test_getOutEdges_0() {
276: m_node.setOutDegree(2);
277: m_node.addOut(m_outEdge1);
278: m_node.addOut(m_outEdge2);
279:
280: assertTrue(m_node.getOutEdges().contains(m_outEdge1));
281: assertTrue(m_node.getOutEdges().contains(m_outEdge2));
282: assertTrue(m_node.getOutEdges().size() == 2);
283: }
284:
285: public void test_getOutEdges_1() {
286: m_node.setOutDegree(2);
287: m_node.addOut(m_outEdge1);
288: m_node.addOut(m_outEdge2);
289:
290: assertTrue(m_node.getOutEdges(m_outNode1).contains(m_outEdge1));
291: assertTrue(m_node.getOutEdges(m_outNode2).contains(m_outEdge2));
292:
293: m_node.setOutDegree(4);
294: m_node.addOut(m_outEdge1);
295: m_node.addOut(m_outEdge2);
296: m_node.addOut(m_outEdge1);
297: m_node.addOut(m_outEdge2);
298:
299: assertTrue(m_node.getOutEdges(m_outNode1).contains(m_outEdge1));
300: assertTrue(m_node.getOutEdges(m_outNode1).size() == 2);
301:
302: assertTrue(m_node.getOutEdges(m_outNode2).contains(m_outEdge2));
303: assertTrue(m_node.getOutEdges(m_outNode2).size() == 2);
304: }
305:
306: public void test_getDegree() {
307: m_node.setInDegree(1);
308: m_node.setOutDegree(1);
309:
310: m_node.addIn(m_inEdge1);
311: m_node.addOut(m_outEdge2);
312:
313: assertTrue(m_node.getDegree() == 2);
314: }
315:
316: public void test_getInDegree() {
317: m_node.setInDegree(1);
318: m_node.setOutDegree(1);
319:
320: m_node.addIn(m_inEdge1);
321: m_node.addOut(m_outEdge2);
322: assertTrue(m_node.getInDegree() == 1);
323: }
324:
325: public void test_getOutDegree() {
326: m_node.setInDegree(1);
327: m_node.setOutDegree(1);
328:
329: m_node.addIn(m_inEdge1);
330: m_node.addOut(m_outEdge2);
331:
332: assertTrue(m_node.getOutDegree() == 1);
333: }
334:
335: public void test_getRelated() {
336: m_node.setInDegree(2);
337: m_node.setOutDegree(2);
338:
339: m_node.addIn(m_inEdge1);
340: m_node.addIn(m_inEdge2);
341:
342: m_node.addOut(m_outEdge1);
343: m_node.addOut(m_outEdge2);
344:
345: Iterator related = m_node.getRelated();
346: OptDirectedNode dn = (OptDirectedNode) related.next();
347: assertTrue(dn == m_inNode1 || dn == m_inNode2
348: || dn == m_outNode1 || dn == m_outNode2);
349:
350: dn = (OptDirectedNode) related.next();
351: assertTrue(dn == m_inNode1 || dn == m_inNode2
352: || dn == m_outNode1 || dn == m_outNode2);
353:
354: dn = (OptDirectedNode) related.next();
355: assertTrue(dn == m_inNode1 || dn == m_inNode2
356: || dn == m_outNode1 || dn == m_outNode2);
357:
358: dn = (OptDirectedNode) related.next();
359: assertTrue(dn == m_inNode1 || dn == m_inNode2
360: || dn == m_outNode1 || dn == m_outNode2);
361:
362: assertTrue(!related.hasNext());
363:
364: //add loop
365: m_node.setInDegree(3);
366: m_node.setOutDegree(3);
367:
368: m_node.addIn(m_inEdge1);
369: m_node.addIn(m_inEdge2);
370:
371: m_node.addOut(m_outEdge1);
372: m_node.addOut(m_outEdge2);
373:
374: m_node.addIn(m_loop);
375: m_node.addOut(m_loop);
376:
377: related = m_node.getRelated();
378: dn = (OptDirectedNode) related.next();
379: assertTrue(dn == m_inNode1 || dn == m_inNode2
380: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
381:
382: dn = (OptDirectedNode) related.next();
383: assertTrue(dn == m_inNode1 || dn == m_inNode2
384: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
385:
386: dn = (OptDirectedNode) related.next();
387: assertTrue(dn == m_inNode1 || dn == m_inNode2
388: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
389:
390: dn = (OptDirectedNode) related.next();
391: assertTrue(dn == m_inNode1 || dn == m_inNode2
392: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
393:
394: dn = (OptDirectedNode) related.next();
395: assertTrue(dn == m_inNode1 || dn == m_inNode2
396: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
397:
398: dn = (OptDirectedNode) related.next();
399: assertTrue(dn == m_inNode1 || dn == m_inNode2
400: || dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
401:
402: assertTrue(!related.hasNext());
403: }
404:
405: public void test_getInRelated() {
406: m_node.setInDegree(2);
407: m_node.setOutDegree(2);
408:
409: m_node.addIn(m_inEdge1);
410: m_node.addIn(m_inEdge2);
411:
412: m_node.addOut(m_outEdge1);
413: m_node.addOut(m_outEdge2);
414:
415: Iterator related = m_node.getInRelated();
416: OptDirectedNode dn = (OptDirectedNode) related.next();
417: assertTrue(dn == m_inNode1 || dn == m_inNode2);
418:
419: dn = (OptDirectedNode) related.next();
420: assertTrue(dn == m_inNode1 || dn == m_inNode2);
421: assertTrue(!related.hasNext());
422:
423: //add a loop
424: m_node.setInDegree(3);
425: m_node.setOutDegree(3);
426:
427: m_node.addIn(m_inEdge1);
428: m_node.addIn(m_inEdge2);
429:
430: m_node.addOut(m_outEdge1);
431: m_node.addOut(m_outEdge2);
432:
433: m_node.addIn(m_loop);
434: m_node.addOut(m_loop);
435:
436: related = m_node.getInRelated();
437:
438: dn = (OptDirectedNode) related.next();
439: assertTrue(dn == m_inNode1 || dn == m_inNode2 || dn == m_node);
440:
441: dn = (OptDirectedNode) related.next();
442: assertTrue(dn == m_inNode1 || dn == m_inNode2 || dn == m_node);
443:
444: dn = (OptDirectedNode) related.next();
445: assertTrue(dn == m_inNode1 || dn == m_inNode2 || dn == m_node);
446:
447: assertTrue(!related.hasNext());
448:
449: }
450:
451: public void test_getOutRelated() {
452: m_node.setInDegree(2);
453: m_node.setOutDegree(2);
454:
455: m_node.addIn(m_inEdge1);
456: m_node.addIn(m_inEdge2);
457:
458: m_node.addOut(m_outEdge1);
459: m_node.addOut(m_outEdge2);
460:
461: Iterator related = m_node.getOutRelated();
462: OptDirectedNode dn = (OptDirectedNode) related.next();
463: assertTrue(dn == m_outNode1 || dn == m_outNode2);
464:
465: dn = (OptDirectedNode) related.next();
466: assertTrue(dn == m_outNode1 || dn == m_outNode2);
467:
468: assertTrue(!related.hasNext());
469:
470: //add a loop
471: m_node.setInDegree(3);
472: m_node.setOutDegree(3);
473:
474: m_node.addIn(m_inEdge1);
475: m_node.addIn(m_inEdge2);
476:
477: m_node.addOut(m_outEdge1);
478: m_node.addOut(m_outEdge2);
479:
480: m_node.addIn(m_loop);
481: m_node.addOut(m_loop);
482:
483: related = m_node.getOutRelated();
484:
485: dn = (OptDirectedNode) related.next();
486: assertTrue(dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
487:
488: dn = (OptDirectedNode) related.next();
489: assertTrue(dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
490:
491: dn = (OptDirectedNode) related.next();
492: assertTrue(dn == m_outNode1 || dn == m_outNode2 || dn == m_node);
493:
494: assertTrue(!related.hasNext());
495: }
496: }
|