import junit.framework.TestCase;

/**
 * Tester for Tree's toString.
 * 
 * Tests children and depth separately and jointly to help isolate errors.
 */
public class TreeToStringTester extends TestCase {
  
  private String LS = System.getProperty("line.separator");

  /** Extreme case: just root node. */
  public void testSingleNode() {
    Tree<String> t = new Tree<String>();
    t.setRootData("R");
    assertEquals(t.toString(), "R" + LS);
  }

  /** Isolate children: root with multiple child leaves */
  public void testChildren() {
    Tree<String> t = new Tree<String>();
    t.setRootData("R");
    for (int c = 0; c < 4; ++c) {
      Tree<String> st = new Tree<String>();
      st.setRootData("C" + c);
      t.addChildTree(st);
    }
    assertEquals(t.toString(),
                "R" + LS
                  + "  C0" + LS + "  C1" + LS + "  C2" + LS + "  C3" + LS);
  }

  /** Extreme case, isolate depth: `linear' tree with significant depth. */
  public void testDepth() {
    Tree<String> t = new Tree<String>();
    t.setRootData("R");
    Tree<String> st = t;
    for (int c = 1; c < 4; ++c) {
      Tree<String> stc = new Tree<String>();
      stc.setRootData("D" + c);
      st.addChildTree(stc);
      st = stc;
    }
    assertEquals(t.toString(),
                "R" + LS
                + "  D1" + LS
                + "    D2" + LS
                + "      D3" + LS);
  }

  /** Typical: multiple children at various depths. */
  public void testChildrenAndDepth() {
    Tree<String> t = new Tree<String>();
    t.setRootData("R");
    Tree<String> st = new Tree<String>();
    st.setRootData("A0");
    t.addChildTree(st);
    st = new Tree<String>();
    st.setRootData("A1");
    t.addChildTree(st);
    for (int c = 0; c < 3; ++c) {
      Tree<String> stc = new Tree<String>();
      stc.setRootData("B" + c);
      st.addChildTree(stc);
    }
    st = new Tree<String>();
    st.setRootData("A2");
    t.addChildTree(st);
    assertEquals(t.toString(),
                "R" + LS + "  A0" + LS + "  A1" + LS
                + "    B0" + LS + "    B1" + LS + "    B2" + LS
                + "  A2" + LS);
  }

  /** Special case: null data, at root and a child. */
  public void testNullData() {
    Tree<String> t = new Tree<String>();
    t.setRootData(null);
    Tree<String> st = new Tree<String>();
    st.setRootData(null);
    t.addChildTree(st);
    assertEquals(t.toString(), "null" + LS + "  null" + LS);
  }

}

