package listFW.test; import junit.framework.TestCase; import fp.*; import listFW.*; import listFW.visitor.*; import listFW.factory.*; /** * Testing reversing a list. * @author DXN */ public class TestReverse extends TestCase { final IListFactory fac = new CompositeListFactory(); IList mt = fac.makeEmptyList(); // same f as in TestAppend! ILambda2,String,IList> f = new ILambda2,String,IList>() { public IList apply(String arg1, IList arg2) { return fac.makeNEList(arg1, arg2); } }; // but we fold in a different direction, and use a different initial value FoldL> algo = new FoldL>(f); @SuppressWarnings("unchecked") public void testFoldEmpty() { assertEquals("Reverse Empty list", "()", mt.execute(algo, mt).toString()); } @SuppressWarnings("unchecked") public void testFoldNonEmpty() { IList L = fac.makeNEList("a", mt); assertEquals("Reverse (a)", "(a)", L.execute(algo, mt).toString()); L = fac.makeNEList("b", L); assertEquals("Reverse ( b, a)", "(a, b)", L.execute(algo, mt).toString()); L = fac.makeNEList("c", L); assertEquals("Reverse ( c, b, a)", "(a, b, c)", L.execute(algo, mt).toString()); } @SuppressWarnings("unchecked") public void testEmpty() { assertEquals("Reverse Empty list", "()", mt.execute(new Reverse(), fac).toString()); } @SuppressWarnings("unchecked") public void testNonEmpty() { IList L = fac.makeNEList("a", mt); assertEquals("Reverse (a)", "(a)", L.execute(new Reverse(), fac).toString()); L = fac.makeNEList("b", L); assertEquals("Reverse ( b, a)", "(a, b)", L.execute(new Reverse(), fac).toString()); L = fac.makeNEList("c", L); assertEquals("Reverse ( c, b, a)", "(a, b, c)", L.execute(new Reverse(), fac).toString()); } }