/**
 * A generic tester for GeneralList objects.  Simply create a list
 * and call GLTester.test().
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of April 2004
 */
public class TestGL
{
  public static void test(GeneralList gl) {
    SimpleOutput pen = new SimpleOutput();
    Position pos;

    pen.println("***** Empty list *****");
    print(pen, gl);

    pen.println("***** Adding elements A-E *****");
    gl.add("A");
    gl.add("B");
    gl.add("C");
    gl.add("D");
    gl.add("E");
    print(pen, gl);

    pen.println("***** Removing first element *****");
    pos = gl.first();
    gl.remove(pos);
    print(pen, gl);

    pen.println("***** Adding elements F-H *****");
    gl.add("F");
    gl.add("G");
    gl.add("H");
    print(pen, gl);

    pen.println("***** Removing last element *****");
    pos = gl.last();
    gl.remove(pos);
    print(pen, gl);

    pen.println("***** Adding elements I-K *****");
    gl.add("I");
    gl.add("J");
    gl.add("K");
    print(pen, gl);

    pen.println("***** Removing second element *****");
    pos = gl.first();
    pos = gl.next(pos);
    gl.remove(pos);
    print(pen, gl);

    pen.println("***** Adding elements L-N *****");
    gl.add("L");
    gl.add("M");
    gl.add("N");
    print(pen, gl);

    pen.println("***** Removing penultimate element *****");
    pos = gl.last();
    pos = gl.prev(pos);
    gl.remove(pos);
    print(pen, gl);

    pen.println("***** Replacing third element with O *****");
    pos = gl.first();
    pos = gl.next(pos);
    pos = gl.next(pos);
    gl.replace(pos, "O");
    print(pen, gl);
  } // test()

  /**
   * Print a list.
   */
  public static void print(SimpleOutput pen, GeneralList lst)
  {
    pen.print("(");
    if (lst.length() > 0) {
      Position pos = lst.first();
      while (!lst.isLast(pos)) {
        pen.print(lst.get(pos));
        pen.print(" ");
        pos = lst.next(pos);
      } // while
      pen.print(lst.get(pos));
    } // nonempty list
    pen.println(")");
  } // print(SimpleOutput, GeneralList)

} // class TestABGL

