View Javadoc
1   package edu.uci.ics.jung.algorithms.generators;
2   
3   
4   import junit.framework.Assert;
5   import junit.framework.TestCase;
6   
7   import com.google.common.base.Supplier;
8   
9   import edu.uci.ics.jung.graph.DirectedGraph;
10  import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
11  import edu.uci.ics.jung.graph.Graph;
12  import edu.uci.ics.jung.graph.UndirectedGraph;
13  import edu.uci.ics.jung.graph.UndirectedSparseMultigraph;
14  
15  
16  public class TestLattice2D extends TestCase {
17  	
18  	protected Supplier<UndirectedGraph<String,Number>> undirectedGraphFactory;
19      protected Supplier<DirectedGraph<String,Number>> directedGraphFactory;
20  	protected Supplier<String> vertexFactory;
21  	protected Supplier<Number> edgeFactory;
22  
23  	@Override
24  	protected void setUp() {
25  		undirectedGraphFactory = new Supplier<UndirectedGraph<String,Number>>() {
26  			public UndirectedGraph<String,Number> get() {
27  				return new UndirectedSparseMultigraph<String,Number>();
28  			}
29  		};
30  		directedGraphFactory = new Supplier<DirectedGraph<String,Number>>() {
31              public DirectedGraph<String,Number> get() {
32                  return new DirectedSparseMultigraph<String,Number>();
33              }
34          };
35  
36  		vertexFactory = new Supplier<String>() {
37  			int count;
38  			public String get() {
39  				return Character.toString((char)('A'+count++));
40  			}
41  		};
42  		edgeFactory = 
43  			new Supplier<Number>() {
44  			int count;
45  			public Number get() {
46  				return count++;
47  			}
48  		};
49  	}
50  
51  	public void testCreateSingular() 
52  	{
53  	    try
54  	    {
55  	        generate(1, 0, 0);
56  	        fail("Did not reject lattice of size < 2");
57  	    }
58  	    catch (IllegalArgumentException iae) {}
59  	}
60  	
61  	public void testget() {
62  		for (int i = 3; i <= 10; i++) {
63  		    for (int j = 0; j < 2; j++) {
64  		        for (int k = 0; k < 2; k++) {
65          			Lattice2DGenerator<String,Number> generator = generate(i, j, k);
66      			    Graph<String,Number> graph = generator.get();
67                      Assert.assertEquals(i*i, graph.getVertexCount());
68                      checkEdgeCount(generator, graph);
69  		        }
70  		    }
71  		}
72  	}
73  	
74  	protected Lattice2DGenerator<String, Number> generate(int i, int j, int k)
75  	{
76  	    return new Lattice2DGenerator<String,Number>(
77                  k == 0 ? undirectedGraphFactory : directedGraphFactory, 
78                  vertexFactory, edgeFactory,
79                  i, j == 0 ? true : false); // toroidal?
80  	}
81  	
82  	protected void checkEdgeCount(Lattice2DGenerator<String, Number> generator,
83  		Graph<String, Number> graph) 
84  	{
85          Assert.assertEquals(generator.getGridEdgeCount(), graph.getEdgeCount());
86  	}
87  }