KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > ext > MatrixExporterTest


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

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

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

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

60     private static final String JavaDoc V1 = "v1";
61     private static final String JavaDoc V2 = "v2";
62     private static final String JavaDoc V3 = "v3";
63     private static final String JavaDoc LAPLACIAN =
64         "1 1 2\n"
65         + "1 2 -1\n"
66         + "1 3 -1\n"
67         + "2 2 1\n"
68         + "2 1 -1\n"
69         + "3 3 1\n"
70         + "3 1 -1\n";
71
72     private static final String JavaDoc NORMALIZED_LAPLACIAN =
73         "1 1 1\n"
74         + "1 2 -0.7071067811865475\n"
75         + "1 3 -0.7071067811865475\n"
76         + "2 2 1\n"
77         + "2 1 -0.7071067811865475\n"
78         + "3 3 1\n"
79         + "3 1 -0.7071067811865475\n";
80
81     private static final String JavaDoc UNDIRECTED_ADJACENCY =
82         "1 2 1\n"
83         + "1 3 1\n"
84         + "1 1 2\n"
85         + "2 1 1\n"
86         + "3 1 1\n";
87
88     private static final String JavaDoc DIRECTED_ADJACENCY = "1 2 1\n"
89         + "3 1 2\n";
90
91     private static final MatrixExporter<String JavaDoc, DefaultEdge> exporter =
92         new MatrixExporter<String JavaDoc, DefaultEdge>();
93
94     //~ Methods ---------------------------------------------------------------
95

96     public void testLaplacian()
97     {
98         UndirectedGraph<String JavaDoc, DefaultEdge> g =
99             new SimpleGraph<String JavaDoc, DefaultEdge>(DefaultEdge.class);
100         g.addVertex(V1);
101         g.addVertex(V2);
102         g.addEdge(V1, V2);
103         g.addVertex(V3);
104         g.addEdge(V3, V1);
105
106         StringWriter w = new StringWriter();
107         exporter.exportLaplacianMatrix(w, g);
108         assertEquals(LAPLACIAN, w.toString());
109
110         w = new StringWriter();
111         exporter.exportNormalizedLaplacianMatrix(w, g);
112         assertEquals(NORMALIZED_LAPLACIAN, w.toString());
113     }
114
115     public void testAdjacencyUndirected()
116     {
117         UndirectedGraph<String JavaDoc, DefaultEdge> g =
118             new Pseudograph<String JavaDoc, DefaultEdge>(DefaultEdge.class);
119         g.addVertex(V1);
120         g.addVertex(V2);
121         g.addEdge(V1, V2);
122         g.addVertex(V3);
123         g.addEdge(V3, V1);
124         g.addEdge(V1, V1);
125
126         StringWriter w = new StringWriter();
127         exporter.exportAdjacencyMatrix(w, g);
128         assertEquals(UNDIRECTED_ADJACENCY, w.toString());
129     }
130
131     public void testAdjacencyDirected()
132     {
133         DirectedGraph<String JavaDoc, DefaultEdge> g =
134             new DirectedMultigraph<String JavaDoc, DefaultEdge>(DefaultEdge.class);
135         g.addVertex(V1);
136         g.addVertex(V2);
137         g.addEdge(V1, V2);
138         g.addVertex(V3);
139         g.addEdge(V3, V1);
140         g.addEdge(V3, V1);
141
142         Writer w = new StringWriter();
143         exporter.exportAdjacencyMatrix(w, g);
144         assertEquals(DIRECTED_ADJACENCY, w.toString());
145     }
146 }
147
Popular Tags