KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > alg > NeighborIndexTest


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* ------------------------------
26  * NeighborIndexTest.java
27  * ------------------------------
28  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
29  *
30  * Original Author: Charles Fry
31  *
32  * $Id: NeighborIndexTest.java 504 2006-07-03 02:37:26Z perfecthash $
33  *
34  * Changes
35  * -------
36  * 12-Dec-2005 : Initial revision (CF);
37  *
38  */

39 package org.jgrapht.alg;
40
41 import java.util.*;
42
43 import junit.framework.*;
44
45 import org.jgrapht.graph.*;
46
47
48 /**
49  * .
50  *
51  * @author Charles Fry
52  */

53 public class NeighborIndexTest
54     extends TestCase
55 {
56
57     //~ Static fields/initializers --------------------------------------------
58

59     private static final String JavaDoc V1 = "v1";
60     private static final String JavaDoc V2 = "v2";
61     private static final String JavaDoc V3 = "v3";
62
63     //~ Methods ---------------------------------------------------------------
64

65     public void testNeighborSet()
66     {
67         ListenableUndirectedGraph<String JavaDoc, DefaultEdge> g =
68             new ListenableUndirectedGraph<String JavaDoc, DefaultEdge>(
69                 DefaultEdge.class);
70         g.addVertex(V1);
71         g.addVertex(V2);
72
73         g.addEdge(V1, V2);
74
75         NeighborIndex<String JavaDoc, DefaultEdge> index =
76             new NeighborIndex<String JavaDoc, DefaultEdge>(g);
77         g.addGraphListener(index);
78
79         Set neighbors = index.neighborsOf(V1);
80
81         assertEquals(1, neighbors.size());
82         assertEquals(true, neighbors.contains(V2));
83
84         g.addVertex(V3);
85         g.addEdge(V3, V1);
86
87         assertEquals(2, neighbors.size());
88         assertEquals(true, neighbors.contains(V3));
89
90         g.removeEdge(V3, V1);
91
92         assertEquals(1, neighbors.size());
93         assertEquals(false, neighbors.contains(V3));
94
95         g.removeVertex(V2);
96
97         assertEquals(0, neighbors.size());
98     }
99
100     public void testDirectedNeighborSet()
101     {
102         ListenableDirectedGraph<String JavaDoc, DefaultEdge> g =
103             new ListenableDirectedGraph<String JavaDoc, DefaultEdge>(
104                 DefaultEdge.class);
105         g.addVertex(V1);
106         g.addVertex(V2);
107
108         g.addEdge(V1, V2);
109
110         DirectedNeighborIndex<String JavaDoc, DefaultEdge> index =
111             new DirectedNeighborIndex<String JavaDoc, DefaultEdge>(g);
112         g.addGraphListener(index);
113
114         Set p = index.predecessorsOf(V1);
115         Set s = index.successorsOf(V1);
116
117         assertEquals(0, p.size());
118         assertEquals(1, s.size());
119         assertEquals(true, s.contains(V2));
120
121         g.addVertex(V3);
122         g.addEdge(V3, V1);
123
124         assertEquals(1, p.size());
125         assertEquals(1, s.size());
126         assertEquals(true, p.contains(V3));
127
128         g.removeEdge(V3, V1);
129
130         assertEquals(0, p.size());
131
132         g.removeVertex(V2);
133
134         assertEquals(0, s.size());
135     }
136 }
137
Popular Tags