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 appending a list.
012 * @author DXN
013 */
014 public class TestAppend extends TestCase {
015 final IListFactory<Object> fac = new CompositeListFactory<Object>();
016 IList<Object> mt = fac.makeEmptyList();
017 IList<Object> ne1 = fac.makeNEList("1", fac.makeNEList("2", fac.makeNEList("3", mt)));
018 IList<Object> ne2 = fac.makeNEList("a", fac.makeNEList("b", mt));
019
020 // same f as in TestReverse!
021 ILambda2<IList<Object>,Object,IList<Object>> f = new ILambda2<IList<Object>,Object,IList<Object>>() {
022 public IList<Object> apply(Object arg1, IList<Object> arg2) {
023 return fac.makeNEList(arg1, arg2);
024 }
025 };
026
027 // but we fold in a different direction, and use a different initial value
028 FoldR<Object,IList<Object>> algo = new FoldR<Object,IList<Object>>(f);
029
030 @SuppressWarnings("unchecked")
031 public void testFoldEmpty() {
032 assertEquals("Append Empty/Empty list", "()", mt.execute(algo, mt).toString());
033 }
034
035 @SuppressWarnings("unchecked")
036 public void testFoldEmptyNE() {
037 assertEquals("Append Empty/NE list", "(1, 2, 3)", mt.execute(algo, ne1).toString());
038 }
039
040 @SuppressWarnings("unchecked")
041 public void testFoldNEEmpty() {
042 assertEquals("Append NE/Empty list", "(a, b)", ne2.execute(algo, mt).toString());
043 }
044
045 @SuppressWarnings("unchecked")
046 public void testFoldNENE() {
047 assertEquals("Append NE/NE list", "(a, b, 1, 2, 3)", ne2.execute(algo, ne1).toString());
048 assertEquals("Append NE/NE list", "(1, 2, 3, a, b)", ne1.execute(algo, ne2).toString());
049 }
050
051 @SuppressWarnings("unchecked")
052 public void testEmpty() {
053 assertEquals("Append Empty/Empty list", "()", mt.execute(new Append<Object>(fac), mt).toString());
054 }
055
056 @SuppressWarnings("unchecked")
057 public void testEmptyNE() {
058 assertEquals("Append Empty/NE list", "(1, 2, 3)", mt.execute(new Append<Object>(fac), ne1).toString());
059 }
060
061 @SuppressWarnings("unchecked")
062 public void testNEEmpty() {
063 assertEquals("Append NE/Empty list", "(a, b)", ne2.execute(new Append<Object>(fac), mt).toString());
064 }
065
066 @SuppressWarnings("unchecked")
067 public void testNENE() {
068 assertEquals("Append NE/NE list", "(a, b, 1, 2, 3)", ne2.execute(new Append<Object>(fac), ne1).toString());
069 assertEquals("Append NE/NE list", "(1, 2, 3, a, b)", ne1.execute(new Append<Object>(fac), ne2).toString());
070 }
071 }