KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > traverse > TopologicalOrderIteratorTest


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

40 package org.jgrapht.traverse;
41
42 import java.util.*;
43
44 import org.jgrapht.*;
45 import org.jgrapht.graph.*;
46
47
48 /**
49  * Tests for TopologicalOrderIterator.
50  *
51  * @author John V. Sichi
52  * @since Apr 25, 2005
53  */

54 public class TopologicalOrderIteratorTest
55     extends EnhancedTestCase
56 {
57
58     //~ Methods ---------------------------------------------------------------
59

60     /**
61      * .
62      */

63     public void testRecipe()
64     {
65         DirectedGraph<String JavaDoc, DefaultEdge> graph =
66             new DefaultDirectedGraph<String JavaDoc, DefaultEdge>(
67                 DefaultEdge.class);
68
69         String JavaDoc [] v = new String JavaDoc [9];
70
71         v[0] = "preheat oven";
72         v[1] = "sift dry ingredients";
73         v[2] = "stir wet ingredients";
74         v[3] = "mix wet and dry ingredients";
75         v[4] = "spoon onto pan";
76         v[5] = "bake";
77         v[6] = "cool";
78         v[7] = "frost";
79         v[8] = "eat";
80
81         // add in mixed up order
82
graph.addVertex(v[4]);
83         graph.addVertex(v[8]);
84         graph.addVertex(v[1]);
85         graph.addVertex(v[3]);
86         graph.addVertex(v[7]);
87         graph.addVertex(v[6]);
88         graph.addVertex(v[0]);
89         graph.addVertex(v[2]);
90         graph.addVertex(v[5]);
91
92         // specify enough edges to guarantee deterministic total order
93
graph.addEdge(v[0], v[1]);
94         graph.addEdge(v[1], v[2]);
95         graph.addEdge(v[0], v[2]);
96         graph.addEdge(v[1], v[3]);
97         graph.addEdge(v[2], v[3]);
98         graph.addEdge(v[3], v[4]);
99         graph.addEdge(v[4], v[5]);
100         graph.addEdge(v[5], v[6]);
101         graph.addEdge(v[6], v[7]);
102         graph.addEdge(v[7], v[8]);
103         graph.addEdge(v[6], v[8]);
104
105         Iterator<String JavaDoc> iter =
106             new TopologicalOrderIterator<String JavaDoc, DefaultEdge>(graph);
107         int i = 0;
108
109         while (iter.hasNext()) {
110             assertEquals(v[i], iter.next());
111             ++i;
112         }
113     }
114 }
115
116 // End TopologicalOrderIteratorTest.java
117
Popular Tags