KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > generate > RandomGraphGeneratorTest


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  * RandomGraphGeneratorTest.java
27  * -----------------
28  * (C) Copyright 2005-2006, by Assaf Lehr and Contributors.
29  *
30  * Original Author: Assaf Lehr
31  * Contributor(s): -
32  *
33  * $Id: RandomGraphGeneratorTest.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  */

38 package org.jgrapht.generate;
39
40 import junit.framework.*;
41
42 import org.jgrapht.*;
43 import org.jgrapht.experimental.isomorphism.*;
44 import org.jgrapht.graph.*;
45
46
47 /**
48  * @author Assaf
49  * @since Aug 6, 2005
50  */

51 public class RandomGraphGeneratorTest
52     extends TestCase
53 {
54
55     //~ Methods ---------------------------------------------------------------
56

57     public void testGenerateGraph2()
58     {
59         Graph [] graphArray = testGenerateDirectedGraph();
60
61         assertTrue(EdgeTopologyCompare.compare(graphArray[0], graphArray[1]));
62         // cannot assert false , cause it may be true once in a while (random)
63
// but it generally should work.
64
// assertFalse(EdgeTopologyCompare.compare(graphArray[1],graphArray[2]));
65
}
66
67     /**
68      * Creates 3 graphs with the same numOfVertex and numOfEdges. The first two
69      * are generated using the same RandomGraphGenerator; the third is generated
70      * using a new instance. The graphs are <code>directedGragh1,
71      * directedGragh2, directedGragh3</code>
72      */

73     private static Graph [] testGenerateDirectedGraph()
74     {
75         final int numOfVertex = 11;
76         final int numOfEdges = 110; // simple undirected max = N(v)x(N(v)-1)
77
RandomGraphGenerator<Integer JavaDoc, DefaultEdge> randomGen =
78             new RandomGraphGenerator<Integer JavaDoc, DefaultEdge>(
79                 numOfVertex,
80                 numOfEdges);
81
82         Graph<Integer JavaDoc, DefaultEdge> directedGragh1 =
83             new SimpleDirectedGraph<Integer JavaDoc, DefaultEdge>(
84                 DefaultEdge.class);
85
86         randomGen.generateGraph(
87             directedGragh1,
88             new IntegerVertexFactory(),
89             null);
90
91         // use the same randomGen
92
Graph<Integer JavaDoc, DefaultEdge> directedGragh2 =
93             new SimpleDirectedGraph<Integer JavaDoc, DefaultEdge>(
94                 DefaultEdge.class);
95
96         randomGen.generateGraph(
97             directedGragh2,
98             new IntegerVertexFactory(),
99             null);
100
101         // use new randomGen here
102
RandomGraphGenerator<Integer JavaDoc, DefaultEdge> newRandomGen =
103             new RandomGraphGenerator<Integer JavaDoc, DefaultEdge>(
104                 numOfVertex,
105                 numOfEdges);
106
107         Graph<Integer JavaDoc, DefaultEdge> directedGragh3 =
108             new SimpleDirectedGraph<Integer JavaDoc, DefaultEdge>(
109                 DefaultEdge.class);
110
111         newRandomGen.generateGraph(
112             directedGragh3,
113             new IntegerVertexFactory(),
114             null);
115
116         return new Graph [] {
117                 directedGragh1,
118                 directedGragh2,
119                 directedGragh3
120             };
121     }
122 }
123
Popular Tags