KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > graph > SubgraphTest


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  * SubgraphTest.java
27  * ------------------------
28  * (C) Copyright 2003-2006, by Michael Behrisch and Contributors.
29  *
30  * Original Author: Michael Behrisch
31  * Contributor(s): -
32  *
33  * $Id: SubgraphTest.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 21-Sep-2004 : Initial revision (MB);
38  *
39  */

40 package org.jgrapht.graph;
41
42 import java.util.*;
43
44 import junit.framework.*;
45
46 import org.jgrapht.*;
47
48
49 /**
50  * Unit test for {@link Subgraph} class.
51  *
52  * @author Michael Behrisch
53  * @since Sep 21, 2004
54  */

55 public class SubgraphTest
56     extends TestCase
57 {
58
59     //~ Instance fields -------------------------------------------------------
60

61     private String JavaDoc v1 = "v1";
62     private String JavaDoc v2 = "v2";
63     private String JavaDoc v3 = "v3";
64     private String JavaDoc v4 = "v4";
65
66     //~ Constructors ----------------------------------------------------------
67

68     /**
69      * @see junit.framework.TestCase#TestCase(java.lang.String)
70      */

71     public SubgraphTest(String JavaDoc name)
72     {
73         super(name);
74     }
75
76     //~ Methods ---------------------------------------------------------------
77

78     /**
79      * .
80      */

81     public void testInducedSubgraphListener()
82     {
83         Graph<String JavaDoc, DefaultEdge> g = init(true);
84         Subgraph<String JavaDoc, DefaultEdge> sub =
85             new Subgraph<String JavaDoc, DefaultEdge>(g, null);
86
87         assertEquals(g.vertexSet(), sub.vertexSet());
88         assertEquals(g.edgeSet(), sub.edgeSet());
89
90         g.addEdge(v3, v4);
91
92         assertEquals(g.vertexSet(), sub.vertexSet());
93         assertEquals(g.edgeSet(), sub.edgeSet());
94     }
95
96     /**
97      * Tests Subgraph.
98      */

99     public void testSubgraph()
100     {
101         Graph<String JavaDoc, DefaultEdge> g = init(false);
102         Subgraph<String JavaDoc, DefaultEdge> sub =
103             new Subgraph<String JavaDoc, DefaultEdge>(g, null, null);
104
105         assertEquals(g.vertexSet(), sub.vertexSet());
106         assertEquals(g.edgeSet(), sub.edgeSet());
107
108         Set<String JavaDoc> vset = new HashSet<String JavaDoc>(g.vertexSet());
109         g.removeVertex(v1);
110         assertEquals(vset, sub.vertexSet()); // losing track
111

112         g = init(false);
113         vset = new HashSet<String JavaDoc>();
114         vset.add(v1);
115         sub = new Subgraph<String JavaDoc, DefaultEdge>(g, vset, null);
116         assertEquals(vset, sub.vertexSet());
117         assertEquals(0, sub.degreeOf(v1));
118         assertEquals(Collections.EMPTY_SET, sub.edgeSet());
119
120         vset.add(v2);
121         vset.add(v3);
122         sub =
123             new Subgraph<String JavaDoc, DefaultEdge>(
124                 g,
125                 vset,
126                 new HashSet<DefaultEdge>(g.getAllEdges(v1, v2)));
127         assertEquals(vset, sub.vertexSet());
128         assertEquals(1, sub.edgeSet().size());
129     }
130
131     /**
132      * .
133      */

134     public void testSubgraphListener()
135     {
136         Graph<String JavaDoc, DefaultEdge> g = init(true);
137         Subgraph<String JavaDoc, DefaultEdge> sub =
138             new Subgraph<String JavaDoc, DefaultEdge>(g, null, null);
139
140         assertEquals(g.vertexSet(), sub.vertexSet());
141         assertEquals(g.edgeSet(), sub.edgeSet());
142
143         Set<String JavaDoc> vset = new HashSet<String JavaDoc>(g.vertexSet());
144         g.removeVertex(v1);
145         vset.remove(v1);
146         assertEquals(vset, sub.vertexSet()); // not losing track
147
assertEquals(g.edgeSet(), sub.edgeSet());
148     }
149
150     private Graph<String JavaDoc, DefaultEdge> init(boolean listenable)
151     {
152         Graph<String JavaDoc, DefaultEdge> g;
153
154         if (listenable) {
155             g =
156                 new ListenableUndirectedGraph<String JavaDoc, DefaultEdge>(
157                     DefaultEdge.class);
158         } else {
159             g = new SimpleGraph<String JavaDoc, DefaultEdge>(
160                     DefaultEdge.class);
161         }
162
163         g.addVertex(v1);
164         g.addVertex(v2);
165         g.addVertex(v3);
166         g.addVertex(v4);
167         g.addEdge(v1, v2);
168         g.addEdge(v2, v3);
169         g.addEdge(v3, v1);
170         g.addEdge(v1, v4);
171
172         return g;
173     }
174 }
175
Popular Tags