KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AsUndirectedGraphTest.java
27  * --------------------------
28  * (C) Copyright 2003-2006, by John V. Sichi and Contributors.
29  *
30  * Original Author: John V. Sichi
31  * Contributor(s): -
32  *
33  * $Id: AsUndirectedGraphTest.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 14-Aug-2003 : Initial revision (JVS);
38  *
39  */

40 package org.jgrapht.graph;
41
42 import java.util.*;
43
44 import org.jgrapht.*;
45
46
47 /**
48  * A unit test for the AsDirectedGraph view.
49  *
50  * @author John V. Sichi
51  */

52 public class AsUndirectedGraphTest
53     extends EnhancedTestCase
54 {
55
56     //~ Instance fields -------------------------------------------------------
57

58     private DirectedGraph<String JavaDoc, DefaultEdge> directed;
59     private DefaultEdge loop;
60     private String JavaDoc v1 = "v1";
61     private String JavaDoc v2 = "v2";
62     private String JavaDoc v3 = "v3";
63     private String JavaDoc v4 = "v4";
64     private UndirectedGraph<String JavaDoc, DefaultEdge> undirected;
65
66     //~ Constructors ----------------------------------------------------------
67

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

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

78     /**
79      * .
80      */

81     public void testAddEdge()
82     {
83         try {
84             undirected.addEdge(v3, v4);
85             assertFalse();
86         } catch (UnsupportedOperationException JavaDoc e) {
87             assertTrue();
88         }
89
90         assertEquals(
91             "([v1, v2, v3, v4], [{v1,v2}, {v2,v3}, {v2,v4}, {v4,v4}])",
92             undirected.toString());
93     }
94
95     /**
96      * .
97      */

98     public void testAddVertex()
99     {
100         String JavaDoc v5 = "v5";
101
102         undirected.addVertex(v5);
103         assertEquals(true, undirected.containsVertex(v5));
104         assertEquals(true, directed.containsVertex(v5));
105     }
106
107     /**
108      * .
109      */

110     public void testDegreeOf()
111     {
112         assertEquals(1, undirected.degreeOf(v1));
113         assertEquals(3, undirected.degreeOf(v2));
114         assertEquals(1, undirected.degreeOf(v3));
115         assertEquals(3, undirected.degreeOf(v4));
116     }
117
118     /**
119      * .
120      */

121     public void testGetAllEdges()
122     {
123         Set<DefaultEdge> edges = undirected.getAllEdges(v3, v2);
124         assertEquals(1, edges.size());
125         assertEquals(directed.getEdge(v2, v3),
126             edges.iterator().next());
127
128         edges = undirected.getAllEdges(v4, v4);
129         assertEquals(1, edges.size());
130         assertEquals(loop, edges.iterator().next());
131     }
132
133     /**
134      * .
135      */

136     public void testGetEdge()
137     {
138         assertEquals(
139             directed.getEdge(v1, v2),
140             undirected.getEdge(v1, v2));
141         assertEquals(
142             directed.getEdge(v1, v2),
143             undirected.getEdge(v2, v1));
144
145         assertEquals(
146             directed.getEdge(v4, v4),
147             undirected.getEdge(v4, v4));
148     }
149
150     /**
151      * .
152      */

153     public void testRemoveEdge()
154     {
155         undirected.removeEdge(loop);
156         assertEquals(false, undirected.containsEdge(loop));
157         assertEquals(false, directed.containsEdge(loop));
158     }
159
160     /**
161      * .
162      */

163     public void testRemoveVertex()
164     {
165         undirected.removeVertex(v4);
166         assertEquals(false, undirected.containsVertex(v4));
167         assertEquals(false, directed.containsVertex(v4));
168     }
169
170     /**
171      * .
172      */

173     protected void setUp()
174     {
175         directed =
176             new DefaultDirectedGraph<String JavaDoc, DefaultEdge>(
177                 DefaultEdge.class);
178         undirected = new AsUndirectedGraph<String JavaDoc, DefaultEdge>(directed);
179
180         directed.addVertex(v1);
181         directed.addVertex(v2);
182         directed.addVertex(v3);
183         directed.addVertex(v4);
184         directed.addEdge(v1, v2);
185         directed.addEdge(v2, v3);
186         directed.addEdge(v2, v4);
187         loop = directed.addEdge(v4, v4);
188     }
189 }
190
Popular Tags