jgrapht-0.8.3/ 0000755 0001750 0001750 00000000000 11752244360 013373 5 ustar giovanni giovanni jgrapht-0.8.3/build.xml 0000644 0001750 0001750 00000050050 11706124627 015216 0 ustar giovanni giovanni
project.version=$${${svntag}.name}
JAVA_HOME=${java.home}
If you get a junit task lookup failure, you need to add the junit.jar to
the classpath, possibly by copying it into the ANT_HOME/lib folder.
See http://ant.apache.org/manual/OptionalTasks/junit.html for more.
replaceme
Starting release build at ${checkout.dir}/${build.file.name}
Checking out files of SVN tag: ${svntag}
Variable $${svntag} must be specified for selected target.
jgrapht-0.8.3/README.html 0000644 0001750 0001750 00000055036 11706124625 015227 0 ustar giovanni giovanni
JGraphT Release Notes
J GraphT 0.8.3
Released: January, 2012
Written by Barak Naveh (barak_naveh@users.sourceforge.net )
and Contributors.
(C) Copyright 2003-2012, by Barak Naveh and Contributors. All rights
reserved.
Please address all contributions, suggestions, and inquiries to the
current project administrator, John Sichi .
Introduction
JGraphT is a free Java class library that provides mathematical
graph-theory objects and algorithms. It runs on Java 2 Platform
(requires JDK 1.6 or later).
JGraphT is licensed under the terms of the GNU Lesser General Public
License (LGPL). A copy of the license is
included in the download.
Please note that JGraphT is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Please refer to the license for details.
Contents
jgrapht-jdk1.6.jar
the compiled JGraphT library for JRE 1.6
README.html
this file
licence-LGPL.txt
GNU Lesser General Public License
javadoc/
Javadoc documentation
lib/
libraries required for build:
junit.jar
jgraph.jar
TGGraphLayout.jar
src/
source code
testsrc/
source code of unit tests
META-INF/MANIFEST.MF
meta information for use as an OSGi plug-in (e.g. within an Eclipse RCP application)
build.properties
describes content of OSGi plug-in export
build.xml
ant buildfile
Getting Started
The package org.jgrapht.demo
includes small demo
applications to help you get started. If you spawn your own demo app and
think others can use it, please send it to us and we will add it to that
package.
Upgrading Versions
To help upgrading, JGraphT maintains a one-version-backwards
compatibility. While this compatibility is not a hard promise, it is generally
respected. (This policy was not followed for the jump from 0.6.0 to 0.7.0
due to the pervasive changes required for generics.) You can upgrade via:
The safe way : compile your app with the JGraphT
version that immediately follows your existing version and
follow the deprecation notes, if they exist, and modify your
application accordingly. Then move to the next version, and on,
until you're current.
The fast way : go to the latest JGraphT right away - if it
works, you're done.
Reading the change history is always
recommended.
Documentation
A local copy of the Javadoc HTML files is included in this distribution.
The latest version of these files is also available on-line at http://www.jgrapht.org/javadoc .
Dependencies
JGraphT requires JDK 1.6 or later to build.
JUnit is a unit testing framework. You need JUnit only if you want to
run the unit tests. The junit.jar
runtime file is included in
this distribution. JUnit is licensed under the terms of the IBM Common
Public License. You can find out more about JUnit and/or download the
latest version from http://www.junit.org . The JUnit tests included
with JGraphT have been created using JUnit 3.8.1.
XMLUnit extends JUnit with XML capabilities. You need XMLUnit
only if you want to run the unit tests. The
xmlunit-1.0.jar
runtime file is included in this
distribution. XMLUnit is licensed under the terms of the BSD
License. You can find out more about XMLUnit and/or download the
latest version from http://xmlunit.sourceforge.net .
JGraph is a graph visualization and editing component. You
need JGraph only if you want to create graph visualizations using
the JGraphT-to-JGraph adapter. The jgraph.jar
runtime
file of JGraph is included in this distribution (file
lib/lib-readme.txt has version information). JGraph is licensed
under the terms of the GNU Lesser General Public License
(LGPL). You can find more about JGraph and/or download the latest
version from http://www.sourceforge.net/projects/jgraph .
Touchgraph is a graph visualization and layout component. You
need Touchgraph only if you want to create graph visualizations
using the JGraphT-to-Touchgraph converter. The
TGGraphLayout.jar
runtime file of Touchgraph is included
in this distribution (file lib/lib-readme.txt has version
information). Touchgraph is licensed under the terms of an Apache-style
License. You can find more about Touchgraph
and/or download the latest version from http://sourceforge.net/projects/touchgraph .
Online Resources
The JGraphT website is at: http://www.jgrapht.org . You can
use this site to:
Obtain the latest version : latest version and all previous
versions of JGraphT are available online.
Report bugs : if you have any comments, suggestions or bugs you
want to report.
Get support : if you have questions or need help with
JGraphT.
There is also a wiki set up
for everyone in the JGraphT community to share information about the
project.
Your Improvements
If you add improvements to JGraphT please send them to us. We will add them
to the next release so that everyone can enjoy them. You might also benefit
from it: others may fix bugs in your source files or may continue to enhance
them.
History
Changes to JGraphT in each version:
version 0.8.3 (20-Jan-2012): fix regression in
DOTExporter inadvertently introduced by 0.8.2 changes. Add
GridGraphGenerator, contributed by Assaf Mizrachi.
Return coloring from ChromaticNumber, contributed by Harshal Vora.
Fix bugs in KSP, contributed by Guillaume Boulmier; note that
these bugfixes worsen the running time. Fix an object identity
bug in CycleDetector, contributed by Matt Sarjent.
Add StoerWagnerMinimumCut, contributed by Robby McKilliam.
Fix MANIFEST.MF, spotted by Olly. Make
FloydWarshallShortestPaths.getShortestPaths unidirectional,
contributed by Yuriy Nakonechnyy.
version 0.8.2 (27-Nov-2010): Clean up
FibonacciHeapNode constructor, as suggested by Johan
Henriksson. Optimize and enhance FloydWarshallShortestPaths,
contributed by Soren Davidsen. Optimize ChromaticNumber,
pointed out by gpaschos@netscape.net. Add unit test for
FloydWarshallShortestPaths for bug noticed by
Andrea Pagani. Add vertex factory validation to
RandomGraphGenerator to prevent a confusing problem
encountered by Andrea Pagani. Add KruskalMinimumSpanningTree and
UnionFind, contributed by Tom Conerly. Add attributes to
DOTExporter, based on suggestion from Chris Lott. Fix
inefficient assertion in TopologicalOrderIterator, spotted by
Peter Lawrey. Fix induced subgraph bug with addition of edge
to underlying graph, contributed by Michele Mancioppi.
Make getEdgeWeight delegate to DefaultWeightedEdge.getWeight,
spotted by Michael Lindig. Add maven support,
contributed by Adrian Marte.
version 0.8.1 (3-Jul-2009): Enhanced GmlExporter with
customized labels and ID's, contributed by Trevor Harmon. Added
new algorithms HamiltonianCycle, ChromaticNumber and
EulerianCircuit, plus new generators HyperCubeGraphGenerator,
StarGraphGenerator, and CompleteBipartiteGraphGenerator, all
contributed by Andrew Newell. Fix bug with vertices which are
equals but not identity-same in graphs allowing loops, spotted by
Michael Michaud. Fix bug in EquivalenceIsomorphismInspector,
reported by Tim Engler. Add toString for shortest paths wrapper,
spotted by Achim Beutel. Add FloydWarshallShortestPaths,
contributed by Tom Larkworthy. Enhance DijskstraShortestPath to
support GraphPath interface. Add GraphUnion (with directed and
undirected variants), contributed by Ilya Razenshteyn.
version 0.8.0 (Sept-2008): Moved to JDK 1.6. Fixed
problem with RandomGraphGenerator reported by Mario Rossi. Added
CompleteGraphGenerator, contributed by Tim Shearouse. Fixed
FibonacciHeap performance problem reported by Jason Lenderman.
Made DotExporter reject illegal vertex ID's, contributed by Holger
Brandl. Fixed bogus assertion for topological sort over empty
graph, spotted by Harris Lin. Added scale-free graph generator
and EdmondsKarpMaximumFlow, contributed by Ilya Razenshteyn.
Added DirectedAcyclicGraph, contributed by Peter Giles. Added
protected getWeight accessor to DefaultWeightedEdge, likewise
getSource and getTarget on DefaultEdge. Optimized iterators to
skip calling event firing routines when there are no listeners,
and used ArrayDeque in a number of places, per suggestion from
Ross Judson. Improvements to StrongConnectivityInspector and OSGi
bundle support contributed by Christian Soltenborn.
version 0.7.3 (Jan-2008): Patch to
JGraphModelAdapter.removeVertex provided by Hookahey. Added
ParanoidGraph. Removed obsolete ArrayUtil (spotted by Boente).
Added GraphPath, and used it to fix mistake in 0.7.2
(k-shortest-paths was returning a private data structure,
as discovered by numerous users). Fixed
EdgeReversedGraph.getAllEdges (spotted by
neumanns@users.sf.net). Fixed incorrect assertion in
TopologicalOrderIterator constructor. Enabled assertions
in JUnit tests. Fixed NPE in BellmanFordShortestPath.getCost.
Fixed a few problems spotted by findbugs.
version 0.7.2 (Sept-2007): Added
TransitiveClosure, contributed by Vinayak Borkar. Added
biconnectivity/cutpoint inspection, k-shortest-paths, and masked
subgraphs, all contributed by Guillaume Boulmier. Made some
Graphs helper methods even more generic, as suggested by JongSoo.
Test and fixes for (Directed)NeighborIndex submitted by Andrew
Berman. Added AsUnweighted(Directed)Graph and AsWeightedGraph,
contributed by Lucas Scharenbroich. Dropped support for retroweaver.
version 0.7.1 (March-2007): Fixed some bugs in
CycleDetector reported by Khanh Vu, and added more testcases for
it. Fixed bugs in DepthFirstIterator reported by Welson Sun, and
added WHITE/GRAY/BLACK states and vertexFinished listener event.
Exposed Subgraph.getBase(), and parameterized Subgraph on graph
type (per suggestion from Aaron Harnly). Added EdgeReversedView.
Added GmlExporter (contributed by Dimitrios Michail), plus
DOTExporter and GraphMLExporter (both contributed by Trevor
Harmon). Enhanced TopologicalOrderIterator to take an optional
Queue parameter for tie-breaking (per suggestion from JongSoo
Park). Fixed some documentation errors reported by Guillaume
Boulmier.
version 0.7.0 (July-2006) : Upgraded to JDK 1.5
(generics support added by Christian Hammer with help from Hartmut
Benz and John Sichi). Added (Directed)NeighborIndex and
MatrixExporter, contributed by Charles Fry. Added BellmanFord,
contributed by Guillaume Boulmier of France Telecom. Removed
never-used LabeledElement. Renamed package from org._3pq.jgrapht
to org.jgrapht. Made various breaking change to interfaces; edge
collections are now Sets, not Lists. Added Touchgraph converter,
contributed by Carl Anderson
version 0.6.0 (July-2005) : Upgraded to JDK 1.4, taking
advantage of its new linked hash set/map containers to make
edge/vertex set order-deterministic. Added support for custom edge
lists. Fixed various serialization and Subgraph issues. Added to
JGraphModelAdapter
support for JGraph's "dangling"
edges; its constructors have slightly changed and now forbid
null
values. Improved interface to
DijskstraShortestPath
, and added radius support to
ClosestFirstIterator
. Added new
StrongConnectivityInspector
algorithm (contributed by
Christian Soltenborn) and TopologicalOrderIterator
(contributed by Marden Neubert). Deleted deprecated
TraverseUtils
. Upgraded to JGraph 5.6.1.1.
version 0.5.3 (June-2004) : Removed Subgraph verification of
element's identity to base graph, upgraded to JGraph 4.0, Added the
VisioExporter which was contributed by Avner Linder, minor bug fixes and
improvements.
version 0.5.2 (March-2004) : Serialization improvements, fixes
to subgraphs and listenable graphs, added support for JGraph->JGraphT
change propagation for JGraph adapter (contributed by Erik Postma),
upgraded to JGraph 3.1, various bug fixes and improvements.
version 0.5.1 (November-2003) : Semantics of Graph.clone() has
changed, please check the documentation if you're using it. Added
Dijkstra's shortest path, vertex cover approximations, new graph generation
framework, upgraded to JGraph 3.0, various bug fixes and API
improvements.
version 0.5.0 (14-Aug-2003) : a new connectivity inspector
added, edge API refactored to be simpler, an improved ant build, an
improved event model, all known bugs were fixed, documentation
clarifications, other small improvements. API of 0.5.0 is not 100% backward
compatible with 0.4.1 but upgrade is simple and straightforward.
version 0.4.1 (05-Aug-2003) : A new adapter to JGraph that
provides graph visualizations, new depth-first and breadth-first iteration
algorithms, various bug fixes and refactoring, moved unit-tests to a
separate folder hierarchy and added more unit-tests.
version 0.4.0 (July-2003) : Initial public release.
Contributors
JGraphT wouldn't be the library it is today without the source
contributions and suggestions made by the authors:
Barak Naveh
(project founder)
John V
Sichi (current project administrator)
Liviu
Rau
Nathan
Fiedler
Michael
Behrisch
Linda
Buisman
Erik Postma
Mikael Hansen
Avner Linder
Marden Neubert
Christian
Soltenborn
Christian
Hammer
Ewgenij Proschak
Hartmut
Benz
Charles
Fry
Guillaume Boulmier
Carl Anderson
Khanh Vu
Aaron Harnly
Dimitrios Michail
Welson Sun
Trevor Harmon
David Black-Schaffer
Vinayak Borkar
Andrew Berman
Lucas Scharenbroich
Hookahey
Tim Shearouse
Holger Brandl
Ilya Razenshteyn
Peter Giles
Andrew Newell
Tim Engler
Tom Larkworthy
Soren Davidsen
Andrea Pagani
Tom Conerly
Michele Mancioppi
Adrian Marte
Assaf Mizrachi
Harshal Vora
Matt Sarjent
Robby McKilliam
Yuriy Nakonechnyy
(if we have missed your name on this list, please email us to get it
fixed).
Other people have also helped in different ways: reporting bugs,
requesting features, commenting, and by merely asking very good questions.
Many thanks to all of you.
Regards,
Barak Naveh
JGraphT Project Creator
John Sichi
JGraphT Project Administrator
© Copyright 2003-2008, by Barak
Naveh and Contributors. All rights reserved.
jgrapht-0.8.3/lib/ 0000755 0001750 0001750 00000000000 11752244360 014141 5 ustar giovanni giovanni jgrapht-0.8.3/lib/lib-readme.txt 0000644 0001750 0001750 00000003777 11706124626 016722 0 ustar giovanni giovanni ==================
Library Folder
==================
This folder contains jar files of libraries used by JGraphT:
-----------------------------------
jgraph.jar (version 5.8.3.2-Munich)
-----------------------------------
The runtime library of the JGraph project. The JGraph library is licensed under
the terms of the GNU Lesser General Public License (LGPL), as with JGraphT.
You can find out more about JGraph and/or download the latest version from
http://www.jgraph.com. You will also find there the source code of the
JGraph library.
Note: the JGraph jar in this folder is the build for Java 1.4.
-------------------------
junit.jar (version 3.8.1)
-------------------------
The runtime library of the JUnit testing framework. The JUnit library is
licensed under the terms of the IBM Common Public License.
You can find out more about JUnit and/or download the latest version from
http://www.junit.org. You will also find there the source code of the JUnit
library.
--------------------------------
TGGraphLayout.jar (version 1.22)
--------------------------------
The layout library of the Touchgraph project. The Touchgraph library is
licensed under an Apache-style license; see TG-APACHE-LICENSE.txt.
You can find out more about Touchgraph and/or download the latest version from
http://www.touchgraph.com. You can find the source code of the
Touchgraph library at http://sourceforge.net/projects/touchgraph.
------------------------
svn*.jar (version 1.0.0)
------------------------
The svn ant task from http://subclipse.tigris.org/svnant/svn.html
----------------------------
xmlunit1.0.jar (version 1.0)
----------------------------
The runtime library for the XMLUnit extension to JUnit. The XMLUnit
library is licensed under the terms of the BSD License.
You can find out more about XMLUnit and/or download the latest version
from http://xmlunit.sourceforge.net. You will also find there the
source code of the xmlunit library.
jgrapht-0.8.3/testsrc/ 0000755 0001750 0001750 00000000000 11706124625 015062 5 ustar giovanni giovanni jgrapht-0.8.3/testsrc/org/ 0000755 0001750 0001750 00000000000 11706124625 015651 5 ustar giovanni giovanni jgrapht-0.8.3/testsrc/org/jgrapht/ 0000755 0001750 0001750 00000000000 11752244360 017310 5 ustar giovanni giovanni jgrapht-0.8.3/testsrc/org/jgrapht/graph/ 0000755 0001750 0001750 00000000000 11752244360 020411 5 ustar giovanni giovanni jgrapht-0.8.3/testsrc/org/jgrapht/graph/SubgraphTest.java 0000644 0001750 0001750 00000013332 11706124627 023673 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------
* SubgraphTest.java
* ------------------------
* (C) Copyright 2003-2008, by Michael Behrisch and Contributors.
*
* Original Author: Michael Behrisch
* Contributor(s): -
*
* $Id: SubgraphTest.java 722 2010-11-24 06:22:07Z perfecthash $
*
* Changes
* -------
* 21-Sep-2004 : Initial revision (MB);
*
*/
package org.jgrapht.graph;
import java.util.*;
import junit.framework.*;
import org.jgrapht.*;
/**
* Unit test for {@link Subgraph} class.
*
* @author Michael Behrisch
* @since Sep 21, 2004
*/
public class SubgraphTest
extends TestCase
{
//~ Instance fields --------------------------------------------------------
private String v1 = "v1";
private String v2 = "v2";
private String v3 = "v3";
private String v4 = "v4";
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public SubgraphTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* .
*/
public void testInducedSubgraphListener()
{
UndirectedGraph g = init(true);
UndirectedSubgraph sub =
new UndirectedSubgraph(g, null, null);
assertEquals(g.vertexSet(), sub.vertexSet());
assertEquals(g.edgeSet(), sub.edgeSet());
g.addEdge(v3, v4);
assertEquals(g.vertexSet(), sub.vertexSet());
assertEquals(g.edgeSet(), sub.edgeSet());
}
/**
* Tests Subgraph.
*/
public void testSubgraph()
{
UndirectedGraph g = init(false);
UndirectedSubgraph sub =
new UndirectedSubgraph(g, null, null);
assertEquals(g.vertexSet(), sub.vertexSet());
assertEquals(g.edgeSet(), sub.edgeSet());
Set vset = new HashSet(g.vertexSet());
g.removeVertex(v1);
assertEquals(vset, sub.vertexSet()); // losing track
g = init(false);
vset = new HashSet();
vset.add(v1);
sub = new UndirectedSubgraph(g, vset, null);
assertEquals(vset, sub.vertexSet());
assertEquals(0, sub.degreeOf(v1));
assertEquals(Collections.EMPTY_SET, sub.edgeSet());
vset.add(v2);
vset.add(v3);
sub =
new UndirectedSubgraph(
g,
vset,
new HashSet(g.getAllEdges(v1, v2)));
assertEquals(vset, sub.vertexSet());
assertEquals(1, sub.edgeSet().size());
}
/**
* .
*/
public void testSubgraphListener()
{
UndirectedGraph g = init(true);
UndirectedSubgraph sub =
new UndirectedSubgraph(g, null, null);
assertEquals(g.vertexSet(), sub.vertexSet());
assertEquals(g.edgeSet(), sub.edgeSet());
Set vset = new HashSet(g.vertexSet());
g.removeVertex(v1);
vset.remove(v1);
assertEquals(vset, sub.vertexSet()); // not losing track
assertEquals(g.edgeSet(), sub.edgeSet());
}
private UndirectedGraph init(boolean listenable)
{
UndirectedGraph g;
if (listenable) {
g = new ListenableUndirectedGraph(
DefaultEdge.class);
} else {
g = new SimpleGraph(
DefaultEdge.class);
}
g.addVertex(v1);
g.addVertex(v2);
g.addVertex(v3);
g.addVertex(v4);
g.addEdge(v1, v2);
g.addEdge(v2, v3);
g.addEdge(v3, v1);
g.addEdge(v1, v4);
return g;
}
public void testInducedSubgraphUnderlyingEdgeAddition()
{
ListenableGraph baseGraph =
new ListenableUndirectedGraph(
DefaultEdge.class);
baseGraph.addVertex(v1);
baseGraph.addVertex(v2);
Set initialVertexes = new LinkedHashSet();
initialVertexes.add(v1);
Subgraph> subgraph =
new Subgraph>(
baseGraph,
initialVertexes,
null);
baseGraph.addEdge(v1, v2);
assertFalse(subgraph.containsEdge(v1, v2));
}
}
// End SubgraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/ListenableGraphTest.java 0000644 0001750 0001750 00000015270 11706124627 025167 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------
* ListenableGraphTest.java
* ------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: ListenableGraphTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 03-Aug-2003 : Initial revision (BN);
* 10-Aug-2003 : Adaptation to new event model (BN);
*
*/
package org.jgrapht.graph;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.event.*;
/**
* Unit test for {@link ListenableGraph} class.
*
* @author Barak Naveh
* @since Aug 3, 2003
*/
public class ListenableGraphTest
extends TestCase
{
//~ Instance fields --------------------------------------------------------
DefaultEdge lastAddedEdge;
DefaultEdge lastRemovedEdge;
Object lastAddedVertex;
Object lastRemovedVertex;
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public ListenableGraphTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* Tests GraphListener listener.
*/
public void testGraphListener()
{
init();
ListenableGraph g =
new ListenableUndirectedGraph(
DefaultEdge.class);
GraphListener listener = new MyGraphListner();
g.addGraphListener(listener);
String v1 = "v1";
String v2 = "v2";
// test vertex notification
g.addVertex(v1);
assertEquals(v1, lastAddedVertex);
assertEquals(null, lastRemovedVertex);
init();
g.removeVertex(v1);
assertEquals(v1, lastRemovedVertex);
assertEquals(null, lastAddedVertex);
// test edge notification
g.addVertex(v1);
g.addVertex(v2);
init();
DefaultEdge e = g.addEdge(v1, v2);
assertEquals(e, lastAddedEdge);
assertEquals(null, lastRemovedEdge);
init();
assertTrue(g.removeEdge(e));
assertEquals(e, lastRemovedEdge);
assertEquals(null, lastAddedEdge);
g.removeVertex(v1);
g.removeVertex(v2);
//
// test notification stops when removing listener
//
g.removeGraphListener(listener);
init();
g.addVertex(v1);
g.addVertex(v2);
e = g.addEdge(v1, v2);
g.removeEdge(e);
assertEquals(null, lastAddedEdge);
assertEquals(null, lastAddedVertex);
assertEquals(null, lastRemovedEdge);
assertEquals(null, lastRemovedVertex);
}
/**
* Tests VertexSetListener listener.
*/
public void testVertexSetListener()
{
init();
ListenableGraph g =
new ListenableUndirectedGraph(
DefaultEdge.class);
VertexSetListener listener = new MyGraphListner();
g.addVertexSetListener(listener);
String v1 = "v1";
String v2 = "v2";
// test vertex notification
g.addVertex(v1);
assertEquals(v1, lastAddedVertex);
assertEquals(null, lastRemovedVertex);
init();
g.removeVertex(v1);
assertEquals(v1, lastRemovedVertex);
assertEquals(null, lastAddedVertex);
// test edge notification
g.addVertex(v1);
g.addVertex(v2);
init();
DefaultEdge e = g.addEdge(v1, v2);
assertEquals(null, lastAddedEdge);
assertEquals(null, lastRemovedEdge);
init();
assertTrue(g.removeEdge(e));
assertEquals(null, lastRemovedEdge);
assertEquals(null, lastAddedEdge);
g.removeVertex(v1);
g.removeVertex(v2);
//
// test notification stops when removing listener
//
g.removeVertexSetListener(listener);
init();
g.addVertex(v1);
g.addVertex(v2);
e = g.addEdge(v1, v2);
g.removeEdge(e);
assertEquals(null, lastAddedEdge);
assertEquals(null, lastAddedVertex);
assertEquals(null, lastRemovedEdge);
assertEquals(null, lastRemovedVertex);
}
private void init()
{
lastAddedEdge = null;
lastAddedVertex = null;
lastRemovedEdge = null;
lastRemovedVertex = null;
}
//~ Inner Classes ----------------------------------------------------------
/**
* A listener on the tested graph.
*
* @author Barak Naveh
* @since Aug 3, 2003
*/
private class MyGraphListner
implements GraphListener
{
/**
* @see GraphListener#edgeAdded(GraphEdgeChangeEvent)
*/
public void edgeAdded(GraphEdgeChangeEvent e)
{
lastAddedEdge = e.getEdge();
}
/**
* @see GraphListener#edgeRemoved(GraphEdgeChangeEvent)
*/
public void edgeRemoved(GraphEdgeChangeEvent e)
{
lastRemovedEdge = e.getEdge();
}
/**
* @see VertexSetListener#vertexAdded(GraphVertexChangeEvent)
*/
public void vertexAdded(GraphVertexChangeEvent e)
{
lastAddedVertex = e.getVertex();
}
/**
* @see VertexSetListener#vertexRemoved(GraphVertexChangeEvent)
*/
public void vertexRemoved(GraphVertexChangeEvent e)
{
lastRemovedVertex = e.getVertex();
}
}
}
// End ListenableGraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/SerializationTest.java 0000644 0001750 0001750 00000007122 11706124627 024735 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------
* SerializationTest.java
* --------------
* (C) Copyright 2003-2008, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: SerializationTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 06-Oct-2003 : Initial revision (JVS);
*
*/
package org.jgrapht.graph;
import java.io.*;
import org.jgrapht.*;
/**
* SerializationTest tests serialization and deserialization of JGraphT objects.
*
* @author John V. Sichi
*/
public class SerializationTest
extends EnhancedTestCase
{
//~ Instance fields --------------------------------------------------------
private String v1 = "v1";
private String v2 = "v2";
private String v3 = "v3";
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public SerializationTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* Tests serialization of DirectedMultigraph.
*/
@SuppressWarnings("unchecked")
public void testDirectedMultigraph()
throws Exception
{
DirectedMultigraph graph =
new DirectedMultigraph(
DefaultEdge.class);
graph.addVertex(v1);
graph.addVertex(v2);
graph.addVertex(v3);
graph.addEdge(v1, v2);
graph.addEdge(v2, v3);
graph.addEdge(v2, v3);
graph =
(DirectedMultigraph) serializeAndDeserialize(
graph);
assertTrue(graph.containsVertex(v1));
assertTrue(graph.containsVertex(v2));
assertTrue(graph.containsVertex(v3));
assertTrue(graph.containsEdge(v1, v2));
assertTrue(graph.containsEdge(v2, v3));
assertEquals(1, graph.edgesOf(v1).size());
assertEquals(3, graph.edgesOf(v2).size());
assertEquals(2, graph.edgesOf(v3).size());
}
private Object serializeAndDeserialize(Object obj)
throws Exception
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
obj = in.readObject();
return obj;
}
}
// End SerializationTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/AsUndirectedGraphTest.java 0000644 0001750 0001750 00000011621 11706124627 025453 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* AsUndirectedGraphTest.java
* --------------------------
* (C) Copyright 2003-2008, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: AsUndirectedGraphTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 14-Aug-2003 : Initial revision (JVS);
*
*/
package org.jgrapht.graph;
import java.util.*;
import org.jgrapht.*;
/**
* A unit test for the AsDirectedGraph view.
*
* @author John V. Sichi
*/
public class AsUndirectedGraphTest
extends EnhancedTestCase
{
//~ Instance fields --------------------------------------------------------
private DirectedGraph directed;
private DefaultEdge loop;
private String v1 = "v1";
private String v2 = "v2";
private String v3 = "v3";
private String v4 = "v4";
private UndirectedGraph undirected;
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public AsUndirectedGraphTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* .
*/
public void testAddEdge()
{
try {
undirected.addEdge(v3, v4);
assertFalse();
} catch (UnsupportedOperationException e) {
assertTrue();
}
assertEquals(
"([v1, v2, v3, v4], [{v1,v2}, {v2,v3}, {v2,v4}, {v4,v4}])",
undirected.toString());
}
/**
* .
*/
public void testAddVertex()
{
String v5 = "v5";
undirected.addVertex(v5);
assertEquals(true, undirected.containsVertex(v5));
assertEquals(true, directed.containsVertex(v5));
}
/**
* .
*/
public void testDegreeOf()
{
assertEquals(1, undirected.degreeOf(v1));
assertEquals(3, undirected.degreeOf(v2));
assertEquals(1, undirected.degreeOf(v3));
assertEquals(3, undirected.degreeOf(v4));
}
/**
* .
*/
public void testGetAllEdges()
{
Set edges = undirected.getAllEdges(v3, v2);
assertEquals(1, edges.size());
assertEquals(directed.getEdge(v2, v3),
edges.iterator().next());
edges = undirected.getAllEdges(v4, v4);
assertEquals(1, edges.size());
assertEquals(loop, edges.iterator().next());
}
/**
* .
*/
public void testGetEdge()
{
assertEquals(
directed.getEdge(v1, v2),
undirected.getEdge(v1, v2));
assertEquals(
directed.getEdge(v1, v2),
undirected.getEdge(v2, v1));
assertEquals(
directed.getEdge(v4, v4),
undirected.getEdge(v4, v4));
}
/**
* .
*/
public void testRemoveEdge()
{
undirected.removeEdge(loop);
assertEquals(false, undirected.containsEdge(loop));
assertEquals(false, directed.containsEdge(loop));
}
/**
* .
*/
public void testRemoveVertex()
{
undirected.removeVertex(v4);
assertEquals(false, undirected.containsVertex(v4));
assertEquals(false, directed.containsVertex(v4));
}
/**
* .
*/
protected void setUp()
{
directed =
new DefaultDirectedGraph(
DefaultEdge.class);
undirected = new AsUndirectedGraph(directed);
directed.addVertex(v1);
directed.addVertex(v2);
directed.addVertex(v3);
directed.addVertex(v4);
directed.addEdge(v1, v2);
directed.addEdge(v2, v3);
directed.addEdge(v2, v4);
loop = directed.addEdge(v4, v4);
}
}
// End AsUndirectedGraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/AllGraphTests.java 0000644 0001750 0001750 00000005165 11706124627 024002 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------
* AllGraphTests.java
* -----------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: AllGraphTests.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 03-Aug-2003 : Initial revision (BN);
*
*/
package org.jgrapht.graph;
import junit.framework.*;
/**
* A TestSuite for all tests in this package.
*
* @author Barak Naveh
* @since Aug 3, 2003
*/
public final class AllGraphTests
{
//~ Constructors -----------------------------------------------------------
private AllGraphTests()
{
} // ensure non-instantiability.
//~ Methods ----------------------------------------------------------------
/**
* Creates a test suite for all tests in this package.
*
* @return a test suite for all tests in this package.
*/
public static Test suite()
{
TestSuite suite = new TestSuite();
// $JUnit-BEGIN$
suite.addTest(new TestSuite(DefaultDirectedGraphTest.class));
suite.addTest(new TestSuite(ListenableGraphTest.class));
suite.addTest(new TestSuite(SimpleDirectedGraphTest.class));
suite.addTest(new TestSuite(AsUndirectedGraphTest.class));
suite.addTest(new TestSuite(AsUnweightedGraphTest.class));
suite.addTest(new TestSuite(CloneTest.class));
suite.addTest(new TestSuite(SerializationTest.class));
suite.addTest(new TestSuite(GenericGraphsTest.class));
// $JUnit-END$
return suite;
}
}
// End AllGraphTests.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/SimpleDirectedGraphTest.java 0000644 0001750 0001750 00000031140 11706124627 025774 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ----------------------------
* SimpleDirectedGraphTest.java
* ----------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: SimpleDirectedGraphTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 25-Jul-2003 : Initial revision (BN);
*
*/
package org.jgrapht.graph;
import java.util.*;
import org.jgrapht.*;
/**
* A unit test for simple directed graph.
*
* @author Barak Naveh
* @since Jul 25, 2003
*/
public class SimpleDirectedGraphTest
extends EnhancedTestCase
{
//~ Instance fields --------------------------------------------------------
DirectedGraph gEmpty;
private DirectedGraph g1;
private DirectedGraph g2;
private DirectedGraph g3;
private DirectedGraph g4;
private DefaultEdge eLoop;
private EdgeFactory eFactory;
private String v1 = "v1";
private String v2 = "v2";
private String v3 = "v3";
private String v4 = "v4";
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public SimpleDirectedGraphTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* Class to test for boolean addEdge(V, V, E)
*/
public void testAddEdgeEdge()
{
init();
try {
g1.addEdge(v1, v1, eLoop); // loops not allowed
assertFalse();
} catch (IllegalArgumentException e) {
assertTrue();
}
try {
g3.addEdge(v1, v1, null);
assertFalse(); // NPE
} catch (NullPointerException e) {
assertTrue();
}
DefaultEdge e = eFactory.createEdge(v2, v1);
try {
g1.addEdge("ya", "ya", e); // no such vertex in graph
assertFalse();
} catch (IllegalArgumentException ile) {
assertTrue();
}
assertEquals(false, g2.addEdge(v2, v1, e));
assertEquals(false, g3.addEdge(v2, v1, e));
assertEquals(true, g4.addEdge(v2, v1, e));
}
/**
* Class to test for Edge addEdge(Object, Object)
*/
public void testAddEdgeObjectObject()
{
init();
try {
g1.addEdge(v1, v1); // loops not allowed
assertFalse();
} catch (IllegalArgumentException e) {
assertTrue();
}
try {
g3.addEdge(null, null);
assertFalse(); // NPE
} catch (NullPointerException e) {
assertTrue();
}
try {
g1.addEdge(v2, v1); // no such vertex in graph
assertFalse();
} catch (IllegalArgumentException ile) {
assertTrue();
}
assertNull(g2.addEdge(v2, v1));
assertNull(g3.addEdge(v2, v1));
assertNotNull(g4.addEdge(v2, v1));
}
/**
* .
*/
public void testAddVertex()
{
init();
assertEquals(1, g1.vertexSet().size());
assertEquals(2, g2.vertexSet().size());
assertEquals(3, g3.vertexSet().size());
assertEquals(4, g4.vertexSet().size());
assertFalse(g1.addVertex(v1));
assertTrue(g1.addVertex(v2));
assertEquals(2, g1.vertexSet().size());
}
/**
* Class to test for boolean containsEdge(Edge)
*/
public void testContainsEdgeEdge()
{
init();
// TODO Implement containsEdge().
}
/**
* Class to test for boolean containsEdge(Object, Object)
*/
public void testContainsEdgeObjectObject()
{
init();
assertFalse(g1.containsEdge(v1, v2));
assertFalse(g1.containsEdge(v1, v1));
assertTrue(g2.containsEdge(v1, v2));
assertTrue(g2.containsEdge(v2, v1));
assertTrue(g3.containsEdge(v1, v2));
assertTrue(g3.containsEdge(v2, v1));
assertTrue(g3.containsEdge(v3, v2));
assertTrue(g3.containsEdge(v2, v3));
assertTrue(g3.containsEdge(v1, v3));
assertTrue(g3.containsEdge(v3, v1));
assertFalse(g4.containsEdge(v1, v4));
g4.addEdge(v1, v4);
assertTrue(g4.containsEdge(v1, v4));
assertFalse(g3.containsEdge(v4, v2));
assertFalse(g3.containsEdge(null, null));
}
/**
* .
*/
public void testContainsVertex()
{
init();
// TODO Implement containsVertex().
}
/**
* .
*/
public void testEdgeSet()
{
init();
// TODO Implement edgeSet().
}
/**
* .
*/
public void testEdgesOf()
{
init();
assertEquals(g4.edgesOf(v1).size(), 2);
assertEquals(g3.edgesOf(v1).size(), 4);
Iterator iter = g3.edgesOf(v1).iterator();
int count = 0;
while (iter.hasNext()) {
iter.next();
count++;
}
assertEquals(count, 4);
}
/**
* .
*/
public void testGetAllEdges()
{
init(); // TODO Implement getAllEdges().
}
/**
* .
*/
public void testGetEdge()
{
init(); // TODO Implement getEdge().
}
/**
* .
*/
public void testGetEdgeFactory()
{
init(); // TODO Implement getEdgeFactory().
}
/**
* .
*/
public void testInDegreeOf()
{
init();
assertEquals(0, g1.inDegreeOf(v1));
assertEquals(1, g2.inDegreeOf(v1));
assertEquals(1, g2.inDegreeOf(v2));
assertEquals(2, g3.inDegreeOf(v1));
assertEquals(2, g3.inDegreeOf(v2));
assertEquals(2, g3.inDegreeOf(v3));
assertEquals(1, g4.inDegreeOf(v1));
assertEquals(1, g4.inDegreeOf(v2));
assertEquals(1, g4.inDegreeOf(v3));
assertEquals(1, g4.inDegreeOf(v4));
try {
g3.inDegreeOf(new String());
assertFalse();
} catch (IllegalArgumentException e) {
assertTrue();
}
try {
g3.inDegreeOf(null);
assertFalse();
} catch (NullPointerException e) {
assertTrue();
}
}
/**
* .
*/
public void testIncomingOutgoingEdgesOf()
{
init();
Set e1to2 = g2.outgoingEdgesOf(v1);
Set e2from1 = g2.incomingEdgesOf(v2);
assertEquals(e1to2, e2from1);
}
/**
* .
*/
public void testOutDegreeOf()
{
init(); // TODO Implement outDegreeOf().
}
/**
* .
*/
public void testOutgoingEdgesOf()
{
init(); // TODO Implement outgoingEdgesOf().
}
/**
* Class to test for boolean removeEdge(Edge)
*/
public void testRemoveEdgeEdge()
{
init();
assertEquals(g4.edgeSet().size(), 4);
g4.removeEdge(v1, v2);
assertEquals(g4.edgeSet().size(), 3);
assertFalse(g4.removeEdge(eLoop));
assertTrue(g4.removeEdge(g4.getEdge(v2, v3)));
assertEquals(g4.edgeSet().size(), 2);
}
/**
* Class to test for Edge removeEdge(Object, Object)
*/
public void testRemoveEdgeObjectObject()
{
init(); // TODO Implement removeEdge().
}
/**
* .
*/
public void testRemoveVertex()
{
init();
assertEquals(4, g4.vertexSet().size());
assertTrue(g4.removeVertex(v1));
assertEquals(3, g4.vertexSet().size());
assertEquals(2, g4.edgeSet().size());
assertFalse(g4.removeVertex(v1));
assertTrue(g4.removeVertex(v2));
assertEquals(1, g4.edgeSet().size());
assertTrue(g4.removeVertex(v3));
assertEquals(0, g4.edgeSet().size());
assertEquals(1, g4.vertexSet().size());
assertTrue(g4.removeVertex(v4));
assertEquals(0, g4.vertexSet().size());
}
/**
* .
*/
public void testVertexSet()
{
init(); // TODO Implement vertexSet().
}
public void testReversedView()
{
init();
DirectedGraph g =
new SimpleDirectedGraph(DefaultEdge.class);
DirectedGraph r =
new EdgeReversedGraph(g);
g.addVertex(v1);
g.addVertex(v2);
DefaultEdge e = g.addEdge(v1, v2);
verifyReversal(g, r, e);
// We have implicitly verified that r is backed by g for additive
// operations (since we constructed it before adding anything to g).
// Now verify for deletion.
g.removeEdge(e);
assertTrue(r.edgeSet().isEmpty());
assertEquals(0, r.inDegreeOf(v1));
assertEquals(0, r.outDegreeOf(v1));
assertEquals(0, r.inDegreeOf(v2));
assertEquals(0, r.outDegreeOf(v2));
assertTrue(r.incomingEdgesOf(v1).isEmpty());
assertTrue(r.outgoingEdgesOf(v1).isEmpty());
assertTrue(r.incomingEdgesOf(v2).isEmpty());
assertTrue(r.outgoingEdgesOf(v2).isEmpty());
}
private void verifyReversal(
DirectedGraph g,
DirectedGraph r,
DefaultEdge e)
{
assertTrue(r.containsVertex(v1));
assertTrue(r.containsVertex(v2));
assertEquals(g.vertexSet(), r.vertexSet());
assertEquals(g.edgeSet(), r.edgeSet());
assertTrue(r.containsEdge(v2, v1));
assertSame(e, r.getEdge(v2, v1));
assertFalse(r.containsEdge(v1, v2));
assertNull(r.getEdge(v1, v2));
Set s = r.getAllEdges(v1, v2);
assertEquals(0, s.size());
s = r.getAllEdges(v2, v1);
assertEquals(1, s.size());
assertSame(e, s.iterator().next());
assertEquals(1, r.inDegreeOf(v1));
assertEquals(0, r.inDegreeOf(v2));
assertEquals(0, r.outDegreeOf(v1));
assertEquals(1, r.outDegreeOf(v2));
assertEquals(g.edgeSet(), r.incomingEdgesOf(v1));
assertTrue(r.outgoingEdgesOf(v1).isEmpty());
assertTrue(r.incomingEdgesOf(v2).isEmpty());
assertEquals(g.edgeSet(), r.outgoingEdgesOf(v2));
assertSame(v2, r.getEdgeSource(e));
assertSame(v1, r.getEdgeTarget(e));
assertEquals("([v1, v2], [(v2,v1)])", r.toString());
}
private void init()
{
gEmpty =
new SimpleDirectedGraph(
DefaultEdge.class);
g1 = new SimpleDirectedGraph(
DefaultEdge.class);
g2 = new SimpleDirectedGraph(
DefaultEdge.class);
g3 = new SimpleDirectedGraph(
DefaultEdge.class);
g4 = new SimpleDirectedGraph(
DefaultEdge.class);
eFactory = g1.getEdgeFactory();
eLoop = eFactory.createEdge(v1, v1);
g1.addVertex(v1);
g2.addVertex(v1);
g2.addVertex(v2);
g2.addEdge(v1, v2);
g2.addEdge(v2, v1);
g3.addVertex(v1);
g3.addVertex(v2);
g3.addVertex(v3);
g3.addEdge(v1, v2);
g3.addEdge(v2, v1);
g3.addEdge(v2, v3);
g3.addEdge(v3, v2);
g3.addEdge(v3, v1);
g3.addEdge(v1, v3);
g4.addVertex(v1);
g4.addVertex(v2);
g4.addVertex(v3);
g4.addVertex(v4);
g4.addEdge(v1, v2);
g4.addEdge(v2, v3);
g4.addEdge(v3, v4);
g4.addEdge(v4, v1);
}
}
// End SimpleDirectedGraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/CloneTest.java 0000644 0001750 0001750 00000010121 11706124627 023151 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------
* CloneTest.java
* --------------
* (C) Copyright 2003-2008, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: CloneTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 06-Oct-2003 : Initial revision (JVS);
*
*/
package org.jgrapht.graph;
import org.jgrapht.*;
/**
* A unit test for a cloning bug, adapted from a forum entry from Linda Buisman.
*
* @author John V. Sichi
* @since Oct 6, 2003
*/
public class CloneTest
extends EnhancedTestCase
{
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public CloneTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* Test graph cloning.
*/
@SuppressWarnings("unchecked")
public void testCloneSpecificsBug()
{
SimpleGraph g1 =
new SimpleGraph(DefaultEdge.class);
String one = "1";
String two = "2";
String three = "3";
g1.addVertex(one);
g1.addVertex(two);
g1.addVertex(three);
g1.addEdge(one, two);
g1.addEdge(two, three);
SimpleGraph g2 =
(SimpleGraph) g1.clone(); // Type-safty
// warning OK with
// clone
assertEquals(2, g2.edgeSet().size());
assertNotNull(g2.getEdge(one, two));
assertTrue(g2.removeEdge(g2.getEdge(one, two)));
assertNotNull(g2.removeEdge("2", "3"));
assertTrue(g2.edgeSet().isEmpty());
}
/**
* Tests usage of {@link ParanoidGraph} for detecting broken vertex
* implementations.
*/
public void testParanoidGraph()
{
BrokenVertex v1 = new BrokenVertex(1);
BrokenVertex v2 = new BrokenVertex(2);
BrokenVertex v3 = new BrokenVertex(1);
SimpleGraph g =
new SimpleGraph(DefaultEdge.class);
ParanoidGraph pg =
new ParanoidGraph(g);
pg.addVertex(v1);
pg.addVertex(v2);
try {
pg.addVertex(v3);
// should not get here
assertFalse();
} catch (IllegalArgumentException ex) {
// expected, swallow
}
}
//~ Inner Classes ----------------------------------------------------------
private class BrokenVertex
{
private int x;
BrokenVertex(int x)
{
this.x = x;
}
public boolean equals(Object other)
{
if (!(other instanceof BrokenVertex)) {
return false;
}
return x == ((BrokenVertex) other).x;
}
}
}
// End CloneTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/AsUnweightedGraphTest.java 0000644 0001750 0001750 00000010546 11706124627 025475 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* AsUnweightedGraphTest.java
* --------------------------
* (C) Copyright 2007-2008, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: AsUnweightedGraphTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 22-Sep-2007 : Initial revision (JVS);
*
*/
package org.jgrapht.graph;
import org.jgrapht.*;
/**
* A unit test for the AsUnweighted[Directed]Graph views.
*
* @author John V. Sichi
*/
public class AsUnweightedGraphTest
extends EnhancedTestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final String v1 = "v1";
private static final String v2 = "v2";
private static final String v3 = "v3";
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public AsUnweightedGraphTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* .
*/
public void testDirected()
{
DefaultDirectedWeightedGraph directed =
new DefaultDirectedWeightedGraph(
DefaultWeightedEdge.class);
constructWeighted(directed);
AsUnweightedDirectedGraph unweighted =
new AsUnweightedDirectedGraph(
directed);
checkView(directed, unweighted);
}
/**
* .
*/
public void testUndirected()
{
WeightedGraph undirected =
new SimpleWeightedGraph(
DefaultWeightedEdge.class);
constructWeighted(undirected);
AsUnweightedGraph unweighted =
new AsUnweightedGraph(
undirected);
checkView(undirected, unweighted);
}
private void constructWeighted(
WeightedGraph weighted)
{
weighted.addVertex(v1);
weighted.addVertex(v2);
weighted.addVertex(v3);
Graphs.addEdge(weighted, v1, v2, 3.0);
assertEquals(
3.0,
weighted.getEdgeWeight(
weighted.getEdge(v1, v2)));
}
private void checkView(
WeightedGraph weighted,
Graph unweighted)
{
assertEquals(
WeightedGraph.DEFAULT_EDGE_WEIGHT,
unweighted.getEdgeWeight(
unweighted.getEdge(v1, v2)));
Graphs.addEdge(weighted, v2, v3, 5.0);
assertEquals(
WeightedGraph.DEFAULT_EDGE_WEIGHT,
unweighted.getEdgeWeight(
unweighted.getEdge(v2, v3)));
unweighted.addEdge(v3, v1);
assertEquals(
WeightedGraph.DEFAULT_EDGE_WEIGHT,
unweighted.getEdgeWeight(
unweighted.getEdge(v3, v1)));
assertEquals(
WeightedGraph.DEFAULT_EDGE_WEIGHT,
weighted.getEdgeWeight(
weighted.getEdge(v3, v1)));
}
}
// End AsUnweightedGraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/DefaultDirectedGraphTest.java 0000644 0001750 0001750 00000013156 11706124627 026136 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------------
* DefaultDirectedGraphTest.java
* -----------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: DefaultDirectedGraphTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 09-Aug-2003 : Initial revision (BN);
*
*/
package org.jgrapht.graph;
import java.util.*;
import org.jgrapht.*;
/**
* A unit test for directed multigraph.
*
* @author Barak Naveh
* @since Aug 9, 2003
*/
public class DefaultDirectedGraphTest
extends EnhancedTestCase
{
//~ Instance fields --------------------------------------------------------
private String v1 = "v1";
private String v2 = "v2";
private String v3 = "v3";
//~ Methods ----------------------------------------------------------------
/**
* .
*/
public void testEdgeSetFactory()
{
DirectedMultigraph g =
new DirectedMultigraph(
DefaultEdge.class);
g.setEdgeSetFactory(new LinkedHashSetFactory());
initMultiTriangleWithMultiLoop(g);
}
/**
* .
*/
public void testEdgeOrderDeterminism()
{
DirectedGraph g =
new DirectedMultigraph(
DefaultEdge.class);
g.addVertex(v1);
g.addVertex(v2);
g.addVertex(v3);
DefaultEdge e1 = g.addEdge(v1, v2);
DefaultEdge e2 = g.addEdge(v2, v3);
DefaultEdge e3 = g.addEdge(v3, v1);
Iterator iter = g.edgeSet().iterator();
assertEquals(e1, iter.next());
assertEquals(e2, iter.next());
assertEquals(e3, iter.next());
// some bonus tests
assertTrue(Graphs.testIncidence(g, e1, v1));
assertTrue(Graphs.testIncidence(g, e1, v2));
assertFalse(Graphs.testIncidence(g, e1, v3));
assertEquals(v2, Graphs.getOppositeVertex(g, e1, v1));
assertEquals(v1, Graphs.getOppositeVertex(g, e1, v2));
assertEquals(
"([v1, v2, v3], [(v1,v2), (v2,v3), (v3,v1)])",
g.toString());
}
/**
* .
*/
public void testEdgesOf()
{
DirectedGraph g =
createMultiTriangleWithMultiLoop();
assertEquals(3, g.edgesOf(v1).size());
assertEquals(2, g.edgesOf(v2).size());
}
/**
* .
*/
public void testGetAllEdges()
{
DirectedGraph g =
createMultiTriangleWithMultiLoop();
Set loops = g.getAllEdges(v1, v1);
assertEquals(1, loops.size());
}
/**
* .
*/
public void testInDegreeOf()
{
DirectedGraph g =
createMultiTriangleWithMultiLoop();
assertEquals(2, g.inDegreeOf(v1));
assertEquals(1, g.inDegreeOf(v2));
}
/**
* .
*/
public void testOutDegreeOf()
{
DirectedGraph g =
createMultiTriangleWithMultiLoop();
assertEquals(2, g.outDegreeOf(v1));
assertEquals(1, g.outDegreeOf(v2));
}
/**
* .
*/
public void testVertexOrderDeterminism()
{
DirectedGraph g =
createMultiTriangleWithMultiLoop();
Iterator iter = g.vertexSet().iterator();
assertEquals(v1, iter.next());
assertEquals(v2, iter.next());
assertEquals(v3, iter.next());
}
private DirectedGraph
createMultiTriangleWithMultiLoop()
{
DirectedGraph g =
new DirectedMultigraph(
DefaultEdge.class);
initMultiTriangleWithMultiLoop(g);
return g;
}
private void initMultiTriangleWithMultiLoop(
DirectedGraph g)
{
g.addVertex(v1);
g.addVertex(v2);
g.addVertex(v3);
g.addEdge(v1, v1);
g.addEdge(v1, v2);
g.addEdge(v2, v3);
g.addEdge(v3, v1);
}
//~ Inner Classes ----------------------------------------------------------
private static class LinkedHashSetFactory
implements EdgeSetFactory
{
/**
* .
*
* @param vertex
*
* @return an empty list.
*/
public Set createEdgeSet(V vertex)
{
return new LinkedHashSet();
}
}
}
// End DefaultDirectedGraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/GenericGraphsTest.java 0000644 0001750 0001750 00000015256 11706124627 024650 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* GenericGraphsTest.java
* --------------------------
* (C) Copyright 2006-2008, by HartmutBenz and Contributors.
*
* Original Author: Hartmut Benz
* Contributor(s): John V. Sichi
*
* $Id: GenericGraphsTest.java 701 2010-01-04 08:36:24Z perfecthash $
*
* Changes
* -------
* ??-???-2006 : Initial revision (HB);
*
*/
package org.jgrapht.graph;
import org.jgrapht.*;
/**
* A unit test for graph generic vertex/edge parameters.
*
* @author Hartmut Benz
*/
public class GenericGraphsTest
extends EnhancedTestCase
{
//~ Instance fields --------------------------------------------------------
Graph objectGraph;
Graph fooFooGraph;
Graph barBarGraph;
Graph fooBarGraph;
//~ Constructors -----------------------------------------------------------
/**
* @see junit.framework.TestCase#TestCase(java.lang.String)
*/
public GenericGraphsTest(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
// ~ Methods ---------------------------------------------------------------
public void testLegalInsertStringGraph()
{
String v1 = "Vertex1";
Object v2 = "Vertex2";
objectGraph.addVertex(v1);
objectGraph.addVertex(v2);
objectGraph.addEdge(v1, v2);
}
public void testLegalInsertFooGraph()
{
FooVertex v1 = new FooVertex();
FooVertex v2 = new FooVertex();
BarVertex vb1 = new BarVertex();
BarVertex vb2 = new BarVertex();
fooFooGraph.addVertex(v1);
fooFooGraph.addVertex(v2);
fooFooGraph.addVertex(vb1);
fooFooGraph.addVertex(vb2);
fooFooGraph.addEdge(v1, v2);
fooFooGraph.addEdge(vb1, vb2);
fooFooGraph.addEdge(v1, vb2);
fooFooGraph.addEdge(v1, v2, new BarEdge());
fooFooGraph.addEdge(v1, vb2, new BarEdge());
fooFooGraph.addEdge(vb1, vb2, new BarEdge());
}
public void testLegalInsertBarGraph()
{
BarVertex v1 = new BarVertex();
BarVertex v2 = new BarVertex();
barBarGraph.addVertex(v1);
barBarGraph.addVertex(v2);
barBarGraph.addEdge(v1, v2);
}
public void testLegalInsertFooBarGraph()
{
FooVertex v1 = new FooVertex();
FooVertex v2 = new FooVertex();
BarVertex vb1 = new BarVertex();
BarVertex vb2 = new BarVertex();
fooFooGraph.addVertex(v1);
fooFooGraph.addVertex(v2);
fooFooGraph.addVertex(vb1);
fooFooGraph.addVertex(vb2);
fooFooGraph.addEdge(v1, v2);
fooFooGraph.addEdge(vb1, vb2);
fooFooGraph.addEdge(v1, vb2);
}
public void testAlissaHacker()
{
DirectedGraph g =
new DefaultDirectedGraph(CustomEdge.class);
g.addVertex("a");
g.addVertex("b");
g.addEdge("a", "b");
CustomEdge custom = g.getEdge("a", "b");
String s = custom.toString();
assertEquals("Alissa P. Hacker approves the edge from a to b", s);
}
public void testEqualButNotSameVertex()
{
EquivVertex v1 = new EquivVertex();
EquivVertex v2 = new EquivVertex();
EquivGraph g = new EquivGraph();
g.addVertex(v1);
g.addVertex(v2);
g.addEdge(v1, v2, new DefaultEdge());
assertEquals(2, g.degreeOf(v1));
assertEquals(2, g.degreeOf(v2));
}
/**
* .
*/
protected void setUp()
{
objectGraph =
new DefaultDirectedGraph(
DefaultEdge.class);
fooFooGraph = new SimpleGraph(FooEdge.class);
barBarGraph = new SimpleGraph(BarEdge.class);
}
//~ Inner Classes ----------------------------------------------------------
public static class CustomEdge
extends DefaultEdge
{
private static final long serialVersionUID = 1L;
public String toString()
{
return "Alissa P. Hacker approves the edge from " + getSource()
+ " to " + getTarget();
}
}
public static class EquivVertex
{
public boolean equals(Object o)
{
return true;
}
public int hashCode()
{
return 1;
}
}
public static class EquivGraph
extends AbstractBaseGraph
implements UndirectedGraph
{
/**
*/
private static final long serialVersionUID = 8647217182401022498L;
public EquivGraph()
{
super(
new ClassBasedEdgeFactory(
DefaultEdge.class),
true,
true);
}
}
public static class FooEdge
extends DefaultEdge
{
private static final long serialVersionUID = 1L;
}
private class FooVertex
{
String str;
public FooVertex()
{
super();
str = "empty foo";
}
public FooVertex(String s)
{
str = s;
}
}
public static class BarEdge
extends FooEdge
{
private static final long serialVersionUID = 1L;
}
private class BarVertex
extends FooVertex
{
public BarVertex()
{
super("empty bar");
}
public BarVertex(String s)
{
super(s);
}
}
}
// End GenericGraphsTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/graph/AsWeightedGraphTest.java 0000644 0001750 0001750 00000011411 11706124627 025122 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* --------------------------
* AsWeightedGraphTest.java
* --------------------------
* (C) Copyright 2007, by Lucas J. Scharenbroich and Contributors.
*
* Original Author: Lucas J. Scharenbroich
* Contributor(s): John V. Sichi
*
* $Id: AsWeightedGraphTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 22-Sep-2007 : Initial revision (JVS);
*
*/
package org.jgrapht.graph;
import java.util.*;
import org.jgrapht.*;
/**
* A unit test for the AsWeightedGraph view.
*
* @author Lucas J. Scharenbroich
*/
public class AsWeightedGraphTest
extends EnhancedTestCase
{
//~ Instance fields --------------------------------------------------------
public SimpleWeightedGraph weightedGraph;
public SimpleGraph unweightedGraph;
//~ Methods ----------------------------------------------------------------
public void setUp()
{
weightedGraph =
new SimpleWeightedGraph(
DefaultWeightedEdge.class);
unweightedGraph =
new SimpleGraph(DefaultEdge.class);
// Create a very simple graph
weightedGraph.addVertex("v1");
weightedGraph.addVertex("v2");
weightedGraph.addVertex("v3");
unweightedGraph.addVertex("v1");
unweightedGraph.addVertex("v2");
unweightedGraph.addVertex("v3");
weightedGraph.setEdgeWeight(weightedGraph.addEdge("v1", "v2"), 1.);
weightedGraph.setEdgeWeight(weightedGraph.addEdge("v2", "v3"), 2.);
weightedGraph.setEdgeWeight(weightedGraph.addEdge("v3", "v1"), 3.);
unweightedGraph.addEdge("v1", "v2");
unweightedGraph.addEdge("v2", "v3");
unweightedGraph.addEdge("v3", "v1");
}
public void tearDown()
{
}
public void test1()
{
Map weightMap1 =
new HashMap();
Map weightMap2 =
new HashMap();
DefaultEdge e1 = unweightedGraph.getEdge("v1", "v2");
DefaultEdge e2 = unweightedGraph.getEdge("v2", "v3");
DefaultEdge e3 = unweightedGraph.getEdge("v3", "v1");
DefaultWeightedEdge e4 = weightedGraph.getEdge("v1", "v2");
DefaultWeightedEdge e5 = weightedGraph.getEdge("v2", "v3");
DefaultWeightedEdge e6 = weightedGraph.getEdge("v3", "v1");
weightMap1.put(e1, 9.0);
weightMap2.put(e4, 9.0);
weightMap2.put(e6, 8.0);
assertEquals(
unweightedGraph.getEdgeWeight(e1),
WeightedGraph.DEFAULT_EDGE_WEIGHT);
WeightedGraph g1 =
new AsWeightedGraph(
unweightedGraph,
weightMap1);
WeightedGraph g2 =
new AsWeightedGraph(
weightedGraph,
weightMap2);
assertEquals(g1.getEdgeWeight(e1), 9.0);
assertEquals(g1.getEdgeWeight(e2), WeightedGraph.DEFAULT_EDGE_WEIGHT);
assertEquals(g1.getEdgeWeight(e3), WeightedGraph.DEFAULT_EDGE_WEIGHT);
assertEquals(g2.getEdgeWeight(e4), 9.0);
assertEquals(g2.getEdgeWeight(e5), 2.0);
assertEquals(g2.getEdgeWeight(e6), 8.0);
g1.setEdgeWeight(e2, 5.0);
g2.setEdgeWeight(e5, 5.0);
assertEquals(g1.getEdgeWeight(e2), 5.0);
assertEquals(
unweightedGraph.getEdgeWeight(e2),
WeightedGraph.DEFAULT_EDGE_WEIGHT);
assertEquals(g2.getEdgeWeight(e5), 5.0);
assertEquals(weightedGraph.getEdgeWeight(e5), 5.0);
}
}
// End AsWeightedGraphTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/ext/ 0000755 0001750 0001750 00000000000 11752244360 020110 5 ustar giovanni giovanni jgrapht-0.8.3/testsrc/org/jgrapht/ext/DOTExporterTest.java 0000644 0001750 0001750 00000011541 11706124627 023776 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------------
* DOTExporterTest.java
* ------------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Trevor Harmon
*
*/
package org.jgrapht.ext;
import java.io.*;
import java.util.*;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
/**
* .
*
* @author Trevor Harmon
*/
public class DOTExporterTest
extends TestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final String V1 = "v1";
private static final String V2 = "v2";
private static final String V3 = "v3";
private static final String NL = System.getProperty("line.separator");
// TODO jvs 23-Dec-2006: externalized diff-based testing framework
private static final String UNDIRECTED =
"graph G {" + NL
+ " 1 [ label=\"a\" ];" + NL
+ " 2 [ x=\"y\" ];" + NL
+ " 3;" + NL
+ " 1 -- 2;" + NL
+ " 3 -- 1;" + NL
+ "}" + NL;
//~ Methods ----------------------------------------------------------------
public void testUndirected()
{
UndirectedGraph g =
new SimpleGraph(DefaultEdge.class);
g.addVertex(V1);
g.addVertex(V2);
g.addEdge(V1, V2);
g.addVertex(V3);
g.addEdge(V3, V1);
StringWriter w = new StringWriter();
ComponentAttributeProvider vertexAttributeProvider =
new ComponentAttributeProvider() {
public Map getComponentAttributes(String v)
{
Map map =
new LinkedHashMap();
if (v.equals(V1)) {
map.put("label", "a");
} else if (v.equals(V2)) {
map.put("x", "y");
} else {
map = null;
}
return map;
}
};
DOTExporter exporter =
new DOTExporter(
new IntegerNameProvider(),
null,
null,
vertexAttributeProvider,
null);
exporter.export(w, g);
assertEquals(UNDIRECTED, w.toString());
}
public void testValidNodeIDs()
{
DOTExporter exporter =
new DOTExporter(
new StringNameProvider(),
new StringNameProvider(),
null);
List validVertices =
Arrays.asList(
"-9.78",
"-.5",
"12",
"a",
"12",
"abc_78",
"\"--34asdf\"");
for (String vertex : validVertices) {
Graph graph =
new DefaultDirectedGraph(
DefaultEdge.class);
graph.addVertex(vertex);
exporter.export(new StringWriter(), graph);
}
List invalidVertices =
Arrays.asList("2test", "--4", "foo-bar", "", "t:32");
for (String vertex : invalidVertices) {
Graph graph =
new DefaultDirectedGraph(
DefaultEdge.class);
graph.addVertex(vertex);
try {
exporter.export(new StringWriter(), graph);
Assert.fail(vertex);
} catch (RuntimeException re) {
// this is a negative test so exception is expected
}
}
}
}
// End DOTExporterTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/ext/MatrixExporterTest.java 0000644 0001750 0001750 00000010661 11706124627 024616 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------------
* MatrixExporterTest.java
* ------------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Charles Fry
*
* $Id: MatrixExporterTest.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 12-Dec-2005 : Initial revision (CF);
*
*/
package org.jgrapht.ext;
import java.io.*;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
/**
* .
*
* @author Charles Fry
*/
public class MatrixExporterTest
extends TestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final String V1 = "v1";
private static final String V2 = "v2";
private static final String V3 = "v3";
private static final String NL = System.getProperty("line.separator");
// TODO jvs 23-Dec-2006: externalized diff-based testing framework
private static final String LAPLACIAN =
"1 1 2" + NL
+ "1 2 -1" + NL
+ "1 3 -1" + NL
+ "2 2 1" + NL
+ "2 1 -1" + NL
+ "3 3 1" + NL
+ "3 1 -1" + NL;
private static final String NORMALIZED_LAPLACIAN =
"1 1 1" + NL
+ "1 2 -0.7071067811865475" + NL
+ "1 3 -0.7071067811865475" + NL
+ "2 2 1" + NL
+ "2 1 -0.7071067811865475" + NL
+ "3 3 1" + NL
+ "3 1 -0.7071067811865475" + NL;
private static final String UNDIRECTED_ADJACENCY =
"1 2 1" + NL
+ "1 3 1" + NL
+ "1 1 2" + NL
+ "2 1 1" + NL
+ "3 1 1" + NL;
private static final String DIRECTED_ADJACENCY =
"1 2 1" + NL
+ "3 1 2" + NL;
private static final MatrixExporter exporter =
new MatrixExporter();
//~ Methods ----------------------------------------------------------------
public void testLaplacian()
{
UndirectedGraph g =
new SimpleGraph(DefaultEdge.class);
g.addVertex(V1);
g.addVertex(V2);
g.addEdge(V1, V2);
g.addVertex(V3);
g.addEdge(V3, V1);
StringWriter w = new StringWriter();
exporter.exportLaplacianMatrix(w, g);
assertEquals(LAPLACIAN, w.toString());
w = new StringWriter();
exporter.exportNormalizedLaplacianMatrix(w, g);
assertEquals(NORMALIZED_LAPLACIAN, w.toString());
}
public void testAdjacencyUndirected()
{
UndirectedGraph g =
new Pseudograph(DefaultEdge.class);
g.addVertex(V1);
g.addVertex(V2);
g.addEdge(V1, V2);
g.addVertex(V3);
g.addEdge(V3, V1);
g.addEdge(V1, V1);
StringWriter w = new StringWriter();
exporter.exportAdjacencyMatrix(w, g);
assertEquals(UNDIRECTED_ADJACENCY, w.toString());
}
public void testAdjacencyDirected()
{
DirectedGraph g =
new DirectedMultigraph(DefaultEdge.class);
g.addVertex(V1);
g.addVertex(V2);
g.addEdge(V1, V2);
g.addVertex(V3);
g.addEdge(V3, V1);
g.addEdge(V3, V1);
Writer w = new StringWriter();
exporter.exportAdjacencyMatrix(w, g);
assertEquals(DIRECTED_ADJACENCY, w.toString());
}
}
// End MatrixExporterTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/ext/GraphMLExporterTest.java 0000644 0001750 0001750 00000006735 11706124627 024653 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------------
* GraphMLExporterTest.java
* ------------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Trevor Harmon
*
*/
package org.jgrapht.ext;
import java.io.*;
import junit.framework.*;
import org.custommonkey.xmlunit.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
/**
* .
*
* @author Trevor Harmon
*/
public class GraphMLExporterTest
extends TestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final String V1 = "v1";
private static final String V2 = "v2";
private static final String V3 = "v3";
private static final String NL = System.getProperty("line.separator");
// TODO jvs 23-Dec-2006: externalized diff-based testing framework
private static final String UNDIRECTED =
"" + NL
+ ""
+ NL
+ "" + NL
+ "" + NL
+ "" + NL
+ "" + NL
+ "" + NL
+ "" + NL
+ " " + NL
+ " " + NL;
private static final GraphMLExporter exporter =
new GraphMLExporter();
//~ Methods ----------------------------------------------------------------
public void testUndirected()
throws Exception
{
UndirectedGraph g =
new SimpleGraph(DefaultEdge.class);
g.addVertex(V1);
g.addVertex(V2);
g.addEdge(V1, V2);
g.addVertex(V3);
g.addEdge(V3, V1);
StringWriter w = new StringWriter();
exporter.export(w, g);
if (System.getProperty("java.vm.version").startsWith("1.4")) {
// NOTE jvs 16-Mar-2007: XML prefix mapping comes out
// with missing info on 1.4, so skip the verification part
// of the test.
return;
}
XMLAssert.assertXMLEqual(UNDIRECTED, w.toString());
}
}
// End GraphMLExporterTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/ext/GmlExporterTest.java 0000644 0001750 0001750 00000006577 11706124627 024104 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ------------------------------
* GmlExporterTest.java
* ------------------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: John V. Sichi
*
* $Id: GmlExporterTest.java 648 2008-12-19 04:10:28Z vocaro $
*
* Changes
* -------
* 23-Dec-2006 : Initial revision (JVS);
*
*/
package org.jgrapht.ext;
import java.io.*;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
/**
* .
*
* @author John V. Sichi
*/
public class GmlExporterTest
extends TestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final String V1 = "v1";
private static final String V2 = "v2";
private static final String V3 = "v3";
private static final String NL = System.getProperty("line.separator");
// TODO jvs 23-Dec-2006: externalized diff-based testing framework
private static final String UNDIRECTED =
"Creator \"JGraphT GML Exporter\"" + NL
+ "Version 1" + NL
+ "graph" + NL
+ "[" + NL
+ "\tlabel \"\"" + NL
+ "\tdirected 0" + NL
+ "\tnode" + NL
+ "\t[" + NL
+ "\t\tid 1" + NL
+ "\t]" + NL
+ "\tnode" + NL
+ "\t[" + NL
+ "\t\tid 2" + NL
+ "\t]" + NL
+ "\tnode" + NL
+ "\t[" + NL
+ "\t\tid 3" + NL
+ "\t]" + NL
+ "\tedge" + NL
+ "\t[" + NL
+ "\t\tid 1" + NL
+ "\t\tsource 1" + NL
+ "\t\ttarget 2" + NL
+ "\t]" + NL
+ "\tedge" + NL
+ "\t[" + NL
+ "\t\tid 2" + NL
+ "\t\tsource 3" + NL
+ "\t\ttarget 1" + NL
+ "\t]" + NL
+ "]" + NL;
private static final GmlExporter exporter =
new GmlExporter();
//~ Methods ----------------------------------------------------------------
public void testUndirected()
{
UndirectedGraph g =
new SimpleGraph(DefaultEdge.class);
g.addVertex(V1);
g.addVertex(V2);
g.addEdge(V1, V2);
g.addVertex(V3);
g.addEdge(V3, V1);
StringWriter w = new StringWriter();
exporter.export(w, g);
assertEquals(UNDIRECTED, w.toString());
}
}
// End GmlExporterTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/EnhancedTestCase.java 0000644 0001750 0001750 00000004433 11706124626 023321 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* ---------------------
* EnhancedTestCase.java
* ---------------------
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* Original Author: Barak Naveh
* Contributor(s): -
*
* $Id: EnhancedTestCase.java 645 2008-09-30 19:44:48Z perfecthash $
*
* Changes
* -------
* 24-Jul-2003 : Initial revision (BN);
*
*/
package org.jgrapht;
import junit.framework.*;
/**
* A little extension to JUnit's TestCase.
*
* @author Barak Naveh
* @since Jul 25, 2003
*/
public abstract class EnhancedTestCase
extends TestCase
{
//~ Constructors -----------------------------------------------------------
/**
* @see TestCase#TestCase()
*/
public EnhancedTestCase()
{
super();
}
/**
* @see TestCase#TestCase(java.lang.String)
*/
public EnhancedTestCase(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
/**
* It means: it's wrong that we got here.
*/
public void assertFalse()
{
assertTrue(false);
}
/**
* It means: it's right that we got here.
*/
public void assertTrue()
{
assertTrue(true);
}
}
// End EnhancedTestCase.java
jgrapht-0.8.3/testsrc/org/jgrapht/generate/ 0000755 0001750 0001750 00000000000 11752244360 021102 5 ustar giovanni giovanni jgrapht-0.8.3/testsrc/org/jgrapht/generate/GraphGeneratorTest.java 0000644 0001750 0001750 00000035256 11706124627 025532 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------------
* GraphGeneratorTest.java
* -----------------------
* (C) Copyright 2003-2008, by John V. Sichi and Contributors.
*
* Original Author: John V. Sichi
* Contributor(s): -
*
* $Id: GraphGeneratorTest.java 738 2011-01-19 03:55:06Z perfecthash $
*
* Changes
* -------
* 17-Sep-2003 : Initial revision (JVS);
* 07-May-2006 : Changed from List to Set (JVS);
*
*/
package org.jgrapht.generate;
import java.util.*;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.alg.*;
import org.jgrapht.graph.*;
/**
* .
*
* @author John V. Sichi
* @since Sep 17, 2003
*/
public class GraphGeneratorTest
extends TestCase
{
//~ Static fields/initializers ---------------------------------------------
private static final int SIZE = 10;
//~ Instance fields --------------------------------------------------------
private VertexFactory vertexFactory =
new VertexFactory() {
private int i;
public Object createVertex()
{
return new Integer(++i);
}
};
//~ Methods ----------------------------------------------------------------
/**
* .
*/
public void testEmptyGraphGenerator()
{
GraphGenerator gen =
new EmptyGraphGenerator(SIZE);
DirectedGraph g =
new DefaultDirectedGraph(DefaultEdge.class);
Map resultMap = new HashMap();
gen.generateGraph(g, vertexFactory, resultMap);
assertEquals(SIZE, g.vertexSet().size());
assertEquals(0, g.edgeSet().size());
assertTrue(resultMap.isEmpty());
}
/**
* .
*/
public void testLinearGraphGenerator()
{
GraphGenerator gen =
new LinearGraphGenerator(SIZE);
DirectedGraph g =
new DefaultDirectedGraph(DefaultEdge.class);
Map resultMap = new HashMap();
gen.generateGraph(g, vertexFactory, resultMap);
assertEquals(SIZE, g.vertexSet().size());
assertEquals(SIZE - 1, g.edgeSet().size());
Object startVertex = resultMap.get(LinearGraphGenerator.START_VERTEX);
Object endVertex = resultMap.get(LinearGraphGenerator.END_VERTEX);
Iterator vertexIter = g.vertexSet().iterator();
while (vertexIter.hasNext()) {
Object vertex = vertexIter.next();
if (vertex == startVertex) {
assertEquals(0, g.inDegreeOf(vertex));
assertEquals(1, g.outDegreeOf(vertex));
continue;
}
if (vertex == endVertex) {
assertEquals(1, g.inDegreeOf(vertex));
assertEquals(0, g.outDegreeOf(vertex));
continue;
}
assertEquals(1, g.inDegreeOf(vertex));
assertEquals(1, g.outDegreeOf(vertex));
}
}
/**
* .
*/
public void testRingGraphGenerator()
{
GraphGenerator gen =
new RingGraphGenerator(SIZE);
DirectedGraph g =
new DefaultDirectedGraph(DefaultEdge.class);
Map resultMap = new HashMap();
gen.generateGraph(g, vertexFactory, resultMap);
assertEquals(SIZE, g.vertexSet().size());
assertEquals(SIZE, g.edgeSet().size());
Object startVertex = g.vertexSet().iterator().next();
assertEquals(1, g.outDegreeOf(startVertex));
Object nextVertex = startVertex;
Set seen = new HashSet();
for (int i = 0; i < SIZE; ++i) {
DefaultEdge nextEdge =
g.outgoingEdgesOf(nextVertex).iterator().next();
nextVertex = g.getEdgeTarget(nextEdge);
assertEquals(1, g.inDegreeOf(nextVertex));
assertEquals(1, g.outDegreeOf(nextVertex));
assertTrue(!seen.contains(nextVertex));
seen.add(nextVertex);
}
// do you ever get the feeling you're going in circles?
assertTrue(nextVertex == startVertex);
assertTrue(resultMap.isEmpty());
}
/**
* .
*/
public void testCompleteGraphGenerator()
{
Graph completeGraph =
new SimpleGraph(DefaultEdge.class);
CompleteGraphGenerator completeGenerator =
new CompleteGraphGenerator(10);
completeGenerator.generateGraph(
completeGraph,
new ClassBasedVertexFactory(Object.class),
null);
// complete graph with 10 vertices has 10*(10-1)/2 = 45 edges
assertEquals(45, completeGraph.edgeSet().size());
}
/**
* .
*/
public void testScaleFreeGraphGenerator()
{
DirectedGraph graph =
new DefaultDirectedGraph(DefaultEdge.class);
ScaleFreeGraphGenerator generator =
new ScaleFreeGraphGenerator(500);
generator.generateGraph(graph, vertexFactory, null);
ConnectivityInspector inspector =
new ConnectivityInspector(graph);
assertTrue(
"generated graph is not connected",
inspector.isGraphConnected());
try {
generator = new ScaleFreeGraphGenerator(-50);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
}
try {
generator =
new ScaleFreeGraphGenerator(-50, 31337);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
}
generator = new ScaleFreeGraphGenerator(0);
DirectedGraph empty =
new DefaultDirectedGraph(DefaultEdge.class);
generator.generateGraph(empty, vertexFactory, null);
assertTrue("non-empty graph generated", empty.vertexSet().size() == 0);
}
/**
* .
*/
public void testCompleteBipartiteGraphGenerator()
{
Graph completeBipartiteGraph =
new SimpleGraph(
DefaultEdge.class);
CompleteBipartiteGraphGenerator completeBipartiteGenerator =
new CompleteBipartiteGraphGenerator(
10,
4);
completeBipartiteGenerator.generateGraph(
completeBipartiteGraph,
new ClassBasedVertexFactory(Object.class),
null);
// Complete bipartite graph with 10 and 4 vertices should have 14
// total vertices and 4*10=40 total edges
assertEquals(14, completeBipartiteGraph.vertexSet().size());
assertEquals(40, completeBipartiteGraph.edgeSet().size());
}
/**
* .
*/
public void testHyperCubeGraphGenerator()
{
Graph hyperCubeGraph =
new SimpleGraph(
DefaultEdge.class);
HyperCubeGraphGenerator hyperCubeGenerator =
new HyperCubeGraphGenerator(
4);
hyperCubeGenerator.generateGraph(
hyperCubeGraph,
new ClassBasedVertexFactory(Object.class),
null);
// Hypercube of 4 dimensions should have 2^4=16 vertices and
// 4*2^(4-1)=32 total edges
assertEquals(16, hyperCubeGraph.vertexSet().size());
assertEquals(32, hyperCubeGraph.edgeSet().size());
}
/**
* .
*/
public void testStarGraphGenerator()
{
Map map = new HashMap();
Graph starGraph =
new SimpleGraph(
DefaultEdge.class);
StarGraphGenerator starGenerator =
new StarGraphGenerator(
10);
starGenerator.generateGraph(
starGraph,
new ClassBasedVertexFactory(Object.class),
map);
// Star graph of order 10 should have 10 vertices and 9 edges
assertEquals(9, starGraph.edgeSet().size());
assertEquals(10, starGraph.vertexSet().size());
assertTrue(map.get(StarGraphGenerator.CENTER_VERTEX) != null);
}
/**
* .
*/
public void testGridGraphGenerator()
{
int rows = 3;
int cols = 4;
//the form of these two classes helps debugging
class StringVertexFactory
implements VertexFactory
{
int index = 1;
@Override public String createVertex()
{
return String.valueOf(index++);
}
}
class StringEdgeFactory
implements EdgeFactory
{
@Override public String createEdge(
String sourceVertex,
String targetVertex)
{
return new String(sourceVertex + '-' + targetVertex);
}
}
GridGraphGenerator generator =
new GridGraphGenerator(rows, cols);
Map resultMap = new HashMap();
//validating a directed and undirected graph
Graph directedGridGraph =
new DefaultDirectedGraph(new StringEdgeFactory());
generator.generateGraph(
directedGridGraph,
new StringVertexFactory(),
resultMap);
validateGridGraphGenerator(rows, cols, directedGridGraph, resultMap);
resultMap.clear();
Graph undirectedGridGraph =
new SimpleGraph(new StringEdgeFactory());
generator.generateGraph(
undirectedGridGraph,
new StringVertexFactory(),
resultMap);
validateGridGraphGenerator(rows, cols, undirectedGridGraph, resultMap);
}
public void validateGridGraphGenerator(
int rows,
int cols,
Graph gridGraph,
Map resultMap)
{
// graph structure validations
int expectedVerticeNum = rows * cols;
assertEquals(
"number of vertices is wrong (" + gridGraph
.vertexSet().size()
+ "), should be " + expectedVerticeNum,
expectedVerticeNum,
gridGraph.vertexSet().size());
int expectedEdgesNum =
(((rows - 1) * cols) + ((cols - 1) * rows))
* ((gridGraph instanceof UndirectedGraph) ? 1 : 2);
assertEquals(
"number of edges is wrong (" + gridGraph
.edgeSet().size()
+ "), should be " + expectedEdgesNum,
expectedEdgesNum,
gridGraph.edgeSet().size());
int cornerVertices = 0, borderVertices = 0, innerVertices = 0,
neighborsSize;
int expCornerVertices = 4;
int expBorderVertices =
Math.max(((rows - 2) * 2) + ((cols - 2) * 2), 0);
int expInnerVertices = Math.max((rows - 2) * (cols - 2), 0);
Set neighbors = new HashSet();
for (String v : gridGraph.vertexSet()) {
neighbors.clear();
neighbors.addAll(Graphs.neighborListOf(gridGraph, v));
neighborsSize = neighbors.size();
assertTrue(
"vertex with illegal number of neighbors (" + neighborsSize
+ ").",
(neighborsSize == 2)
|| (neighborsSize == 3)
|| (neighborsSize == 4));
if (neighborsSize == 2) {
cornerVertices++;
} else if (neighborsSize == 3) {
borderVertices++;
} else if (neighborsSize == 4) {
innerVertices++;
}
}
assertEquals(
"there should be exactly " + expCornerVertices
+ " corner (with two neighbors) vertices. "
+ " actual number is " + cornerVertices + ".",
expCornerVertices,
cornerVertices);
assertEquals(
"there should be exactly " + expBorderVertices
+ " border (with three neighbors) vertices. "
+ " actual number is " + borderVertices + ".",
expBorderVertices,
borderVertices);
assertEquals(
"there should be exactly " + expInnerVertices
+ " inner (with four neighbors) vertices. "
+ " actual number is " + innerVertices + ".",
expInnerVertices,
innerVertices);
// result map validations
Set keys = resultMap.keySet();
assertEquals(
"result map contains should contains exactly 4 corner verices",
4,
keys.size());
for (String key : keys) {
neighbors.clear();
neighbors.addAll(
Graphs.neighborListOf(gridGraph, resultMap.get(key)));
neighborsSize = neighbors.size();
assertEquals(
"corner vertex should have exactly 2 neighbors",
2,
neighborsSize);
}
}
}
// End GraphGeneratorTest.java
jgrapht-0.8.3/testsrc/org/jgrapht/generate/RandomGraphGeneratorTest.java 0000644 0001750 0001750 00000011567 11706124627 026672 0 ustar giovanni giovanni /* ==========================================
* JGraphT : a free Java graph-theory library
* ==========================================
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2008, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------
* RandomGraphGeneratorTest.java
* -----------------
* (C) Copyright 2005-2008, by Assaf Lehr and Contributors.
*
* Original Author: Assaf Lehr
* Contributor(s): -
*
* $Id: RandomGraphGeneratorTest.java 704 2010-02-28 02:29:09Z perfecthash $
*
* Changes
* -------
*/
package org.jgrapht.generate;
import java.util.*;
import junit.framework.*;
import org.jgrapht.*;
import org.jgrapht.experimental.isomorphism.*;
import org.jgrapht.graph.*;
/**
* @author Assaf
* @since Aug 6, 2005
*/
public class RandomGraphGeneratorTest
extends TestCase
{
//~ Methods ----------------------------------------------------------------
public void testGenerateDirectedGraph()
{
List> graphArray =
new ArrayList>();
for (int i = 0; i < 3; ++i) {
graphArray.add(
new SimpleDirectedGraph(
DefaultEdge.class));
}
generateGraphs(graphArray, 11, 100);
assertTrue(
EdgeTopologyCompare.compare(graphArray.get(0), graphArray.get(1)));
// cannot assert false , cause it may be true once in a while (random)
// but it generally should work.
// assertFalse(EdgeTopologyCompare.compare(graphArray.get(1),graphArray.get(2)));
}
public void testGenerateListenableUndirectedGraph()
{
List> graphArray =
new ArrayList>();
for (int i = 0; i < 3; ++i) {
graphArray.add(
new ListenableUndirectedGraph(
DefaultEdge.class));
}
generateGraphs(graphArray, 11, 50);
assertTrue(
EdgeTopologyCompare.compare(graphArray.get(0), graphArray.get(1)));
}
public void testBadVertexFactory()
{
RandomGraphGenerator randomGen =
new RandomGraphGenerator(
10,
3);
Graph graph =
new SimpleDirectedGraph(DefaultEdge.class);
try {
randomGen.generateGraph(
graph,
new ClassBasedVertexFactory(String.class),
null);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException ex) {
// expected
}
}
/**
* Generates 3 graphs with the same numOfVertex and numOfEdges. The first
* two are generated using the same RandomGraphGenerator; the third is
* generated using a new instance.
*
* @param graphs array of graphs to generate
* @param numOfVertex number of vertices to generate per graph
* @param numOfEdges number of edges to generate per graph
*/
private static void generateGraphs(
List> graphs,
int numOfVertex,
int numOfEdges)
{
RandomGraphGenerator randomGen =
new RandomGraphGenerator(
numOfVertex,
numOfEdges);
randomGen.generateGraph(
graphs.get(0),
new IntegerVertexFactory(),
null);
// use the same randomGen
randomGen.generateGraph(
graphs.get(1),
new IntegerVertexFactory(),
null);
// use new randomGen here
RandomGraphGenerator