GraphRandomizerTest.java

package edu.hawaii.ics.yucheng;

import java.util.ArrayList;

import junit.framework.TestCase;

/**
 * Tests the GraphRandomizer class.
 */
public class GraphRandomizerTest extends TestCase {

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


    /**
     * Tests the create method.
     * 
     * @throws Exception
     */
    public void testCreate1() throws Exception {
        try {
            GraphRandomizer.create(0, 1);
            fail();
        } catch (final IllegalArgumentException e) {
            // This is expected.
        }
    }


    /**
     * Tests the create method.
     * 
     * @throws Exception
     */
    public void testCreate2() throws Exception {
        try {
            GraphRandomizer.create(1, 1);
            fail();
        } catch (final IllegalArgumentException e) {
            // This is expected.
        }
    }


    /**
     * Tests the create method.
     * 
     * @throws Exception
     */
    public void testCreate3() throws Exception {
        final Graph g = GraphRandomizer.create(1, 2);
        assertEquals(1, g.vertices());
        assertEquals(0, g.edges());
    }


    /**
     * Tests the create method.
     * 
     * @throws Exception
     */
    public void testCreate4() throws Exception {
        final Graph g = GraphRandomizer.create(2, 3);
        assertEquals(2, g.vertices());
        assertEquals(1, g.edges());
    }


    /**
     * Tests the create method.
     * 
     * @throws Exception
     */
    public void testCreate5() throws Exception {

        // 4 vertices => 6 possible edges; 38 possible trees
        //
        // [01] 1..2 1..3 1..4
        // [02] 1..2 1..3 1..4 2..3
        // [03] 1..2 1..3 1..4 2..3 2..4
        // [04] 1..2 1..3 1..4 2..3 2..4 3..4
        // [05] 1..2 1..3 1..4 2..3 3..4
        // [06] 1..2 1..3 1..4 2..4
        // [07] 1..2 1..3 1..4 2..4 3..4
        // [08] 1..2 1..3 1..4 3..4
        // [09] 1..2 1..3 2..3 2..4
        // [10] 1..2 1..3 2..3 2..4 3..4
        // [11] 1..2 1..3 2..3 3..4
        // [12] 1..2 1..3 2..4
        // [13] 1..2 1..3 2..4 3..4
        // [14] 1..2 1..3 3..4
        // [15] 1..2 1..4 2..3
        // [16] 1..2 1..4 2..3 2..4
        // [17] 1..2 1..4 2..3 2..4 3..4
        // [18] 1..2 1..4 2..3 3..4
        // [19] 1..2 1..4 2..4 3..4
        // [20] 1..2 1..4 3..4
        // [21] 1..2 2..3 2..4
        // [22] 1..2 2..3 2..4 3..4
        // [23] 1..2 2..3 3..4
        // [24] 1..2 2..4 3..4
        // [25] 1..3 1..4 2..3
        // [26] 1..3 1..4 2..3 2..4
        // [27] 1..3 1..4 2..3 2..4 3..4
        // [28] 1..3 1..4 2..3 3..4
        // [29] 1..3 1..4 2..4
        // [30] 1..3 1..4 2..4 3..4
        // [31] 1..3 2..3 2..4
        // [32] 1..3 2..3 2..4 3..4
        // [33] 1..3 2..3 3..4
        // [34] 1..3 2..4 3..4
        // [35] 1..4 2..3 2..4
        // [36] 1..4 2..3 2..4 3..4
        // [37] 1..4 2..3 3..4
        // [38] 1..4 2..4 3..4
        //
        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < 10000; i++) {
            final Graph g = GraphRandomizer.create(4, 5);
            final StringBuilder builder = new StringBuilder();
            for (final Edge e : g)
                builder.append(e + " ");
            final String edges = builder.toString().trim();
            if (list.contains(edges))
                continue;
            list.add(edges);
            if (list.size() == 38)
                break;
        }
        assertEquals(38, list.size());
    }
}
Valid HTML 4.01 Valid CSS