001 package listFW.test;
002
003 import junit.framework.TestCase;
004
005 import fp.*;
006 import listFW.*;
007 import listFW.visitor.*;
008 import listFW.factory.*;
009
010 /**
011 * Testing reversing a list.
012 * @author DXN
013 */
014 public class TestReverse extends TestCase {
015 final IListFactory<String> fac = new CompositeListFactory<String>();
016 IList<String> mt = fac.makeEmptyList();
017
018 // same f as in TestAppend!
019 ILambda2<IList<String>,String,IList<String>> f =
020 new ILambda2<IList<String>,String,IList<String>>() {
021 public IList<String> apply(String arg1, IList<String> arg2) {
022 return fac.makeNEList(arg1, arg2);
023 }
024 };
025
026 // but we fold in a different direction, and use a different initial value
027 FoldL<String,IList<String>> algo = new FoldL<String,IList<String>>(f);
028
029 @SuppressWarnings("unchecked")
030 public void testFoldEmpty() {
031 assertEquals("Reverse Empty list", "()", mt.execute(algo, mt).toString());
032 }
033
034 @SuppressWarnings("unchecked")
035 public void testFoldNonEmpty() {
036 IList<String> L = fac.makeNEList("a", mt);
037 assertEquals("Reverse (a)", "(a)", L.execute(algo, mt).toString());
038 L = fac.makeNEList("b", L);
039 assertEquals("Reverse ( b, a)", "(a, b)", L.execute(algo, mt).toString());
040 L = fac.makeNEList("c", L);
041 assertEquals("Reverse ( c, b, a)", "(a, b, c)", L.execute(algo, mt).toString());
042 }
043
044 @SuppressWarnings("unchecked")
045 public void testEmpty() {
046 assertEquals("Reverse Empty list", "()", mt.execute(new Reverse<String>(), fac).toString());
047 }
048
049 @SuppressWarnings("unchecked")
050 public void testNonEmpty() {
051 IList<String> L = fac.makeNEList("a", mt);
052 assertEquals("Reverse (a)", "(a)", L.execute(new Reverse<String>(), fac).toString());
053 L = fac.makeNEList("b", L);
054 assertEquals("Reverse ( b, a)", "(a, b)", L.execute(new Reverse<String>(), fac).toString());
055 L = fac.makeNEList("c", L);
056 assertEquals("Reverse ( c, b, a)", "(a, b, c)", L.execute(new Reverse<String>(), fac).toString());
057 }
058
059 }