// $Id: FoldLeftTest.m4,v 1.1 1999/11/15 05:36:20 leavens Exp leavens $
include(testmacros.m4)
import lib.*;

public class FoldLeftTest extends Tester {

    public static void main(String [] argv) {
        System.out.println("");
        System.out.println("Test $RCSfile: FoldLeftTest.m4,v $ of $Date: 1999/11/15 05:36:20 $");
        System.out.println("");

        Cons myList
            = new Cons(new Double(1.0),
                       new Cons(2.0,
                                new Cons(5.0,
                                         new Cons(4.0, null))));
        Iterator myIter = new ConsIterator(myList);
        BinaryOp add = new BinaryOp() {
                public Object value(Object left, Object right) {
                    return new Double(((Double)left).doubleValue()
                                      + ((Double)right).doubleValue());
                }
            };
        BinaryOp sub = new BinaryOp() {
                public Object value(Object left, Object right) {
                    return new Double(((Double)left).doubleValue()
                                      - ((Double)right).doubleValue());
                }
            };
        BinaryOp div = new BinaryOp() {
                public Object value(Object left, Object right) {
                    return new Double(((Double)left).doubleValue()
                                      / ((Double)right).doubleValue());
                }
            };

        FoldLeft sumNull = new FoldLeft(add, new Double(0.0),
                                    new ConsIterator(null));
        FoldLeft diffNull = new FoldLeft(sub, new Double(0.0),
                                     new ConsIterator(null));
        FoldLeft fracNull = new FoldLeft(div, new Double(1.0),
                                     new ConsIterator(null));
        expectEvalsTo(sumNull.value(), new Double(0.0));
        expectEvalsTo(diffNull.value(), new Double(0.0));
        expectEvalsTo(fracNull.value(), new Double(1.0));

        FoldLeft sum = new FoldLeft(add, new Double(0.0),
                                    new ConsIterator(myList));
        FoldLeft diff = new FoldLeft(sub, new Double(0.0),
                                     new ConsIterator(myList));
        FoldLeft frac = new FoldLeft(div, new Double(1.0),
                                     new ConsIterator(myList));
        expectEvalsTo(sum.value(), new Double(12.0));
        expectEvalsTo(diff.value(), new Double(-12.0));
        expectEvalsTo(frac.value(), new Double(0.025));

	testReport();

        System.err.println("The following will be an infinite loop,");
        System.err.println("if you don't wait to compute the result until the value method is called.");
        System.err.println("Stop the program if it takes more than a few seconds to end now.");

        Iterator ones = new Iterator() {
                public boolean hasMore() { return true; }
                public void advance() { }
                public Object getElement() { return new Double(1.0); }
            };
        new FoldLeft(add, new Double(0.0), ones);
    }

}
