JadeSolverTest.java

package edu.hawaii.ics.yucheng;

import java.util.ArrayList;
import java.util.Collection;

import junit.framework.TestCase;

/**
 * Tests the JadeSolver class.
 */
public class JadeSolverTest extends TestCase {

    static Graph newGraph(final float[] vertices, final int[] edges) {
        final Collection<Edge> collection = new ArrayList<Edge>();
        for (int i = 0; i < edges.length; i += 2)
            collection.add(new Edge(edges[i], edges[i + 1]));
        return new Graph(vertices, collection);
    }


    /**
     * Tests the animate method.
     * 
     * @throws Exception
     */
    public void testAnimate() throws Exception {
        final JadeSolver solver = new JadeSolver();
        solver.startAnimation(1, 0);
        final float[] v = { 0, 1, 2, 3 };
        final int[] e = { 0, 1, 1, 2, 2, 3 };
        final Graph g = newGraph(v, e);
        solver.solve(g);
    }


    /**
     * Tests the constructor.
     * 
     * @throws Exception
     */
    public void testConstructor() throws Exception {
        new JadeSolver();
    }


    /**
     * Tests the getSolution method.
     * 
     * @throws Exception
     */
    public void testGetSolution1() throws Exception {
        try {
            JadeSolver.getSolution(0.0f, null, null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the getSolution method.
     * 
     * @throws Exception
     */
    public void testGetSolution2() throws Exception {
        new JadeSolver() {
            void test() {
                final float[] v = { 0, 1, 2, 3 };
                final int[] e = { 0, 1, 1, 2, 2, 3 };
                final Graph g = newGraph(v, e);
                final GraphData gd = new GraphData(g);
                new SolutionMatrix(gd).getSolution();
            }
        }.test();
    }


    /**
     * Tests the getSolution method.
     * 
     * @throws Exception
     */
    public void testGetSolution3() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    final float[] v = { 0, 1, 2, 3 };
                    final int[] e = { 0, 1, 1, 2, 2, 3 };
                    final Graph g = newGraph(v, e);
                    final GraphData gd = new GraphData(g);
                    new SolutionMatrix(gd).getSolution(null, 1.0f);
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the GraphData constructor method.
     * 
     * @throws Exception
     */
    public void testGraphDataConstructor() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    new GraphData(null);
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the GraphSolution getSolution method.
     * 
     * @throws Exception
     */
    public void testGraphDataGetSolution() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    final float[] v = { 0, 1, 2, 3 };
                    final int[] e = { 0, 1, 1, 2, 2, 3 };
                    final Graph g = newGraph(v, e);
                    final GraphData gd = new GraphData(g);
                    new VertexNode(gd, 100);
                    fail();
                } catch (final IndexOutOfBoundsException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the GraphData toString method.
     * 
     * @throws Exception
     */
    public void testGraphDataToString() throws Exception {
        new JadeSolver() {
            void test() {
                final float[] v = { 0.0f };
                final int[] e = {};
                final String s = new GraphData(newGraph(v, e)).toString();
                assertEquals("{\n  [0:0.0] --> null\n}", s);
            }
        }.test();
    }


    /**
     * Tests the mark method.
     * 
     * @throws Exception
     */
    public void testMark1() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    final float[] v = { 0, 1, 2, 3 };
                    final int[] e = { 0, 1, 1, 2, 2, 3 };
                    final Graph g = newGraph(v, e);
                    final GraphData gd = new GraphData(g);
                    new SolutionMatrix(gd).mark(null, gd.nodeAt(0));
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the mark method.
     * 
     * @throws Exception
     */
    public void testMark2() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    final float[] v = { 0, 1, 2, 3 };
                    final int[] e = { 0, 1, 1, 2, 2, 3 };
                    final Graph g = newGraph(v, e);
                    final GraphData gd = new GraphData(g);
                    new SolutionMatrix(gd).mark(gd.nodeAt(0), null);
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the constructor.
     * 
     * @throws Exception
     */
    public void testSolve1() throws Exception {
        final JadeSolver solver = new JadeSolver();
        try {
            solver.solve(null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the constructor.
     * 
     * @throws Exception
     */
    public void testSolve2() throws Exception {
        final JadeSolver solver = new JadeSolver();
        final float[] v = { 0, 1, 2, 3 };
        final int[] e = { 0, 1, 1, 2, 2, 3 };
        final Graph g = newGraph(v, e);
        final GraphSolution s = solver.solve(g);
        assertTrue(s.hasRoot());
        assertEquals(2, s.root());
        assertTrue(s.hasWeight());
        assertEquals(4.0f, s.weight(), 0.0001f);
    }


    /**
     * Tests the toString method.
     * 
     * @throws Exception
     */
    public void testToString() throws Exception {
        new JadeSolver() {
            void test() {
                final float[] v = { 0, 1, 2, 3 };
                final int[] e = { 0, 1, 1, 2, 2, 3 };
                final Graph g = newGraph(v, e);
                final GraphData gd = new GraphData(g);
                assertEquals("[1:1.0] --> { 0 2 }", gd.nodeAt(1).toString());
            }
        }.test();
    }


    /**
     * Tests the VertexNode constructor.
     * 
     * @throws Exception
     */
    public void testVertexNodeConstructor() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    final float[] v = { 0, 1, 2, 3 };
                    final int[] e = { 0, 1, 1, 2, 2, 3 };
                    final Graph g = newGraph(v, e);
                    final GraphData gd = new GraphData(g);
                    new SolutionMatrix(gd).mark(gd.nodeAt(0), null);
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the VertexNode constructor.
     * 
     * @throws Exception
     */
    public void testVertexNodeConstructor1() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    new VertexNode(null, 0);
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }


    /**
     * Tests the VertexNode constructor.
     * 
     * @throws Exception
     */
    public void testVertexNodeConstructor2() throws Exception {
        new JadeSolver() {
            void test() {
                try {
                    new VertexNode(null, 0);
                    fail();
                } catch (final NullPointerException e) {
                    // This is expected.
                }
            }
        }.test();
    }
}
Valid HTML 4.01 Valid CSS