package edu.hawaii.ics.yucheng;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import junit.framework.TestCase;
/**
* Tests the Graph class.
*/
public class GraphTest extends TestCase {
/**
* Returns a URL to a sample graph.
*
* @return A URL to a sample graph.
*/
private static URL getUrl() {
try {
return new URL("http://www2.hawaii.edu/~yucheng/ics311/hw2/sample.graph");
} catch (final MalformedURLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1a() throws Exception {
try {
new Graph(null, new ArrayList<Edge>());
fail();
} catch (final NullPointerException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1b() throws Exception {
try {
new Graph(new float[] {}, null);
fail();
} catch (final NullPointerException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1c() throws Exception {
try {
new Graph(new float[] {}, new ArrayList<Edge>());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1d() throws Exception {
try {
new Graph(new float[] { 0, 1 }, new ArrayList<Edge>());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1e() throws Exception {
try {
final ArrayList<Edge> edges = new ArrayList<Edge>();
edges.add(null);
new Graph(new float[] { 0, 1 }, edges);
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1f() throws Exception {
try {
final ArrayList<Edge> edges = new ArrayList<Edge>();
edges.add(new Edge(0, 1));
edges.add(new Edge(0, 1));
new Graph(new float[] { 0, 1 }, edges);
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1g() throws Exception {
try {
final ArrayList<Edge> edges = new ArrayList<Edge>();
edges.add(new Edge(0, 1));
edges.add(new Edge(0, 2));
edges.add(new Edge(1, 2));
edges.add(new Edge(3, 4));
new Graph(new float[] { 0, 1, 2, 3, 4 }, edges);
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor1h() throws Exception {
final ArrayList<Edge> edges = new ArrayList<Edge>();
edges.add(new Edge(0, 1));
new Graph(new float[] { 0, 1 }, edges);
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2a() throws Exception {
try {
new Graph(null, getUrl());
fail();
} catch (final NullPointerException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2b() throws Exception {
try {
new Graph(new Scanner(""), null);
fail();
} catch (final NullPointerException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2c() throws Exception {
try {
new Graph(new Scanner("0"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2d() throws Exception {
try {
new Graph(new Scanner("2\n0"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2e() throws Exception {
try {
new Graph(new Scanner("2\n1\n1\t2\t3"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2f() throws Exception {
try {
new Graph(new Scanner("2\n2\n1\t2\n1,2\n1,2\n"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2g() throws Exception {
try {
new Graph(new Scanner("2\n2\n1\t2\n3,4\n"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2h() throws Exception {
try {
new Graph(new Scanner("2\n2\n1\t2\n1,3"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2i() throws Exception {
try {
new Graph(new Scanner("a"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2j() throws Exception {
try {
new Graph(new Scanner("5\n4\n0\t1\t2\t3\t4\n1,2\n1,3\n2,3\n4,5"),
getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2k() throws Exception {
try {
new Graph(new Scanner("2\n1\n0\t1\n1,2\n\n\n2"), getUrl());
fail();
} catch (final GraphException e) {
// This is expected.
}
}
/**
* Tests the constructor.
*
* @throws Exception
*/
public void testConstructor2l() throws Exception {
new Graph(new Scanner("2\n1\n0\t1\n1,2\n\n\n"), getUrl());
}
/**
* Tests the edgeAt method.
*
* @throws Exception
*/
public void testEdgeAt1() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
try {
graph.edgeAt(2);
fail();
} catch (final IllegalArgumentException e) {
// This is expected.
}
}
/**
* Tests the edgeAt method.
*
* @throws Exception
*/
public void testEdgeAt2() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
assertNotNull(graph.edgeAt(0));
assertEquals(0, graph.edgeAt(0).first());
assertEquals(1, graph.edgeAt(0).second());
}
/**
* Tests the edges method.
*
* @throws Exception
*/
public void testEdges() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
assertEquals(1, graph.edges());
}
/**
* Tests the iterator method.
*
* @throws Exception
*/
public void testIterator() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
for (final Edge e : graph) {
assertNotNull(e);
assertEquals(0, e.first());
assertEquals(1, e.second());
}
}
/**
* Tests the toString method.
*
* @throws Exception
*/
public void testToString() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
assertEquals("V = { 0.0 1.0 }; E = { 1..2 }", graph.toString());
}
/**
* Tests the url method.
*
* @throws Exception
*/
public void testUrl() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
assertEquals(getUrl(), graph.url());
}
/**
* Tests the vertexAt method.
*
* @throws Exception
*/
public void testVertexAt1() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
try {
graph.vertexAt(3);
fail();
} catch (final IllegalArgumentException e) {
// This is expected.
}
}
/**
* Tests the vertexAt method.
*
* @throws Exception
*/
public void testVertexAt2() throws Exception {
final Scanner scanner = new Scanner("2\n1\n0.1\t2.3\n1,2");
final Graph graph = new Graph(scanner, getUrl());
assertEquals(0.1, graph.vertexAt(0), 0.0001f);
assertEquals(2.3, graph.vertexAt(1), 0.0001f);
}
/**
* Tests the vertices method.
*
* @throws Exception
*/
public void testVertices() throws Exception {
final Graph graph = new Graph(new Scanner("2\n1\n0\t1\n1,2"), getUrl());
assertEquals(2, graph.vertices());
}
}