EdgeTest.java

package edu.hawaii.ics.yucheng;

import java.util.Scanner;

import junit.framework.TestCase;

/**
 * Tests the Edge class.
 */
public class EdgeTest extends TestCase {

    /**
     * Tests the compareTo method.
     * 
     * @throws Exception
     */
    public void testCompareTo() throws Exception {
        final Edge edge = new Edge(1, 3);
        assertEquals(1, edge.compareTo(null));
        assertEquals(0, edge.compareTo(new Edge(1, 3)));
        assertEquals(0, edge.compareTo(new Edge(3, 1)));
        assertTrue(edge.compareTo(new Edge(0, 3)) > 0);
        assertTrue(edge.compareTo(new Edge(2, 3)) < 0);
        assertTrue(edge.compareTo(new Edge(1, 2)) > 0);
        assertTrue(edge.compareTo(new Edge(1, 4)) < 0);
    }


    /**
     * Tests the first constructor executes normally.
     * 
     * @throws Exception
     */
    public void testConstructor1a() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertEquals(0, edge.first());
        assertEquals(1, edge.second());
    }


    /**
     * Tests the first constructor executes normally.
     * 
     * @throws Exception
     */
    public void testConstructor1b() throws Exception {
        final Edge edge = new Edge(1, 0);
        assertEquals(0, edge.first());
        assertEquals(1, edge.second());
    }


    /**
     * Tests the first constructor rejects bad arguments.
     * 
     * @throws Exception
     */
    public void testConstructor1c() throws Exception {
        try {
            new Edge(-1, 0);
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the first constructor rejects bad arguments.
     * 
     * @throws Exception
     */
    public void testConstructor1d() throws Exception {
        try {
            new Edge(0, -1);
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the first constructor rejects bad arguments.
     * 
     * @throws Exception
     */
    public void testConstructor1e() throws Exception {
        try {
            new Edge(0, 0);
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second constructor executes normally.
     * 
     * @throws Exception
     */
    public void testConstructor2a() throws Exception {
        final Edge edge = new Edge(new Scanner("1,2"));
        assertEquals(0, edge.first());
        assertEquals(1, edge.second());
    }


    /**
     * Tests the second constructor executes normally.
     * 
     * @throws Exception
     */
    public void testConstructor2b() throws Exception {
        final Edge edge = new Edge(new Scanner("2,1"));
        assertEquals(0, edge.first());
        assertEquals(1, edge.second());
    }


    /**
     * Tests the second constructor rejects a null scanner.
     * 
     * @throws Exception
     */
    public void testConstructor2c() throws Exception {
        try {
            new Edge(null);
            fail();
        } catch (final NullPointerException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second constructor rejects too many vertices.
     * 
     * @throws Exception
     */
    public void testConstructor2d() throws Exception {
        try {
            new Edge(new Scanner("1,2,3"));
            fail();
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second constructor rejects a small first vertex.
     * 
     * @throws Exception
     */
    public void testConstructor2e() throws Exception {
        try {
            new Edge(new Scanner("0,1"));
            fail();
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second constructor rejects a small second vertex.
     * 
     * @throws Exception
     */
    public void testConstructor2f() throws Exception {
        try {
            new Edge(new Scanner("1,0"));
            fail();
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second constructor rejects vertices equal to each other.
     * 
     * @throws Exception
     */
    public void testConstructor2g() throws Exception {
        try {
            new Edge(new Scanner("1,1"));
            fail();
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second constructor rejects non-ASCII values.
     * 
     * @throws Exception
     */
    public void testConstructor2h() throws Exception {
        try {
            new Edge(new Scanner("a,b"));
            fail();
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the contains method.
     * 
     * @throws Exception
     */
    public void testContains() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertTrue(edge.contains(0));
        assertTrue(edge.contains(1));
        assertFalse(edge.contains(2));
    }


    /**
     * Tests the equals method.
     * 
     * @throws Exception
     */
    public void testEquals() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertFalse(edge.equals(null));
        assertFalse(edge.equals(""));
        assertFalse(edge.equals(new Edge(1, 2)));
        assertTrue(edge.equals(edge));
        assertTrue(edge.equals(new Edge(0, 1)));
    }


    /**
     * Tests the first method.
     * 
     * @throws Exception
     */
    public void testFirst() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertEquals(0, edge.first());
    }


    /**
     * Tests the getOpposite method.
     * 
     * @throws Exception
     */
    public void testGetOpposite1() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertEquals(1, edge.getOpposite(0));
        assertEquals(0, edge.getOpposite(1));
    }


    /**
     * Tests the getOpposite method.
     * 
     * @throws Exception
     */
    public void testGetOpposite2() throws Exception {
        final Edge edge = new Edge(0, 1);
        try {
            edge.getOpposite(2);
            fail();
        } catch (final GraphException e) {
            // This is expected.
        }
    }


    /**
     * Tests the second method.
     * 
     * @throws Exception
     */
    public void testSecond() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertEquals(1, edge.second());
    }


    /**
     * Tests the toString method.
     * 
     * @throws Exception
     */
    public void testToString() throws Exception {
        final Edge edge = new Edge(0, 1);
        assertEquals("1..2", edge.toString());
    }
}
Valid HTML 4.01 Valid CSS