package edu.hawaii.ics.yucheng; import java.util.ArrayList; import junit.framework.TestCase; /** * Tests the GraphSolution class. */ public class GraphSolutionTest extends TestCase { /** * Tests the constructor. * * @throws Exception */ public void testConstructor1() throws Exception { final GraphSolution s = new GraphSolution(new ArrayList<Edge>()); assertFalse(s.hasRoot()); assertFalse(s.hasWeight()); } /** * Tests the constructor. * * @throws Exception */ public void testConstructor2() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges, 0); assertTrue(s.hasRoot()); assertEquals(0, s.root()); assertFalse(s.hasWeight()); } /** * Tests the constructor. * * @throws Exception */ public void testConstructor3a() throws Exception { try { new GraphSolution(null, 0, 0.0f); fail(); } catch (final NullPointerException e) { // This is expected. } } /** * Tests the constructor. * * @throws Exception */ public void testConstructor3b() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(null); try { new GraphSolution(edges, 0, 0.0f); fail(); } catch (final GraphException e) { // This is expected. } } /** * Tests the constructor. * * @throws Exception */ public void testConstructor3c() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); edges.add(new Edge(0, 1)); try { new GraphSolution(edges, 0, 0.0f); fail(); } catch (final GraphException e) { // This is expected. } } /** * Tests the constructor. * * @throws Exception */ public void testConstructor3d() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); try { new GraphSolution(edges, 2, 0.0f); fail(); } catch (final GraphException e) { // This is expected. } } /** * Tests the constructor. * * @throws Exception */ public void testConstructor3e() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); try { new GraphSolution(edges, -2, 0.0f); fail(); } catch (final GraphException e) { // This is expected. } } /** * Tests the hasRoot method. * * @throws Exception */ public void testHasRoot() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges, 0, 0.0f); assertTrue(s.hasRoot()); } /** * Tests the hasWeight method. * * @throws Exception */ public void testHasWeight() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges, 0, 0.0f); assertTrue(s.hasWeight()); } /** * Tests the iterator method. * * @throws Exception */ public void testIterator() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges, 0, 0.0f); int count = 0; for (final Edge e : s) { assertNotNull(e); assertEquals(0, e.first()); assertEquals(1, e.second()); count++; } assertEquals(1, count); } /** * Tests the root method. * * @throws Exception */ public void testRoot1() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges); try { s.root(); fail(); } catch (final GraphException e) { // This is expected. } } /** * Tests the root method. * * @throws Exception */ public void testRoot2() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges, 0); assertEquals(0, s.root()); } /** * Tests the weight method. * * @throws Exception */ public void testWeight1() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges); try { s.weight(); fail(); } catch (final GraphException e) { // This is expected. } } /** * Tests the weight method. * * @throws Exception */ public void testWeight2() throws Exception { final ArrayList<Edge> edges = new ArrayList<Edge>(); edges.add(new Edge(0, 1)); final GraphSolution s = new GraphSolution(edges, 0, 50.0f); assertEquals(50.0f, s.weight(), 0.0001f); } }