[ERA-CVS] src/org/jdaemon/test/util/functional TestFunctionalPackage.java, NONE, 1.1
Brought to you by:
jessex
|
From: <je...@23...> - 2008-10-06 10:24:56
|
Update of /cvsroot/era/src/org/jdaemon/test/util/functional In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv12559/test/util/functional Added Files: TestFunctionalPackage.java Log Message: Various changes for ERA v2 - 'This time it's Generic...' --- NEW FILE: TestFunctionalPackage.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.test.util.functional; import org.jdaemon.util.functional.*; import org.jdaemon.util.expression.*; import java.util.LinkedList; import java.util.TreeSet; import java.util.List; import java.util.Set; import java.util.Iterator; import java.io.StringWriter; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** Test class for multi-way accumulator * * @author jonathan */ class TestObject { public String stringv; public Integer intv; public TestObject(String a, Integer i) { stringv = a; intv = i; } public static UnaryFunction<TestObject, String> str_a = new AbstractUnaryFunction<TestObject, String>() { public String operate(TestObject t) { return t.stringv; } }; public static UnaryFunction<TestObject, Integer> int_a = new AbstractUnaryFunction<TestObject, Integer>() { public Integer operate(TestObject t) { return t.intv; } }; } /** * * @author jonathan */ public class TestFunctionalPackage extends TestCase { public static final UnaryFunction<Integer,Integer> plusone = new AbstractUnaryFunction<Integer,Integer>() { public Integer operate(Integer a) { return a + 1; } }; public static final BinaryFunction<Integer,Integer,Integer> plus = new AbstractBinaryFunction<Integer,Integer,Integer>() { public Integer operate(Integer a, Integer b) { return a + b; } }; public void testPlusOne() { assertEquals(plusone.operate(0),new Integer(1)); } public void testPlus() { assertEquals(plus.operate(2,2),new Integer(4)); } public void testChain() { assertEquals(Operations.Chain(plusone,plusone).operate(0), new Integer(2)); assertEquals(Operations.Chain(plus,plusone).operate(2,2), new Integer(5)); } public void testSum() { SumFunction<Integer> sum = new SumFunction<Integer>(); Number total = new Double(0.0); total = sum.operate(total,1); total = sum.operate(total,2); assertEquals(total, new Double(3.0)); } public void testCount() { CountFunction<String> count = new CountFunction<String>(); Long total = 0L; total = count.operate(total, "Hello"); total = count.operate(total, null); assertEquals(total, new Long(1)); } public void testMin() { MinFunction<String> min = new MinFunction<String>(String.CASE_INSENSITIVE_ORDER); assertEquals(min.operate("abc","def"), "abc"); } public void testMax() { MaxFunction<String> max = new MaxFunction<String>(String.CASE_INSENSITIVE_ORDER); assertEquals(max.operate("abc","def"), "def"); } private static UnaryFunction<Integer, Boolean> lessthanfive = new AbstractUnaryFunction<Integer,Boolean>() { public Boolean operate(Integer i) { return i < 5; } }; public void testFilterCollection() { Set<Integer> test = new TreeSet<Integer>(); test.add(1); test.add(3); test.add(2); test.add(7); test.add(5); Iterator<Integer> res = Operations.filter(test, lessthanfive).iterator(); assertEquals(res.next().intValue(), 1); assertEquals(res.next().intValue(), 2); assertEquals(res.next().intValue(), 3); assertEquals(res.hasNext(), false); } public void testFilterList() { List<Integer> test = new LinkedList<Integer>(); test.add(1); test.add(3); test.add(7); test.add(2); test.add(5); List<Integer> res = Operations.filter(test, lessthanfive); assertEquals(res.get(0).intValue(), 1); assertEquals(res.get(1).intValue(), 3); assertEquals(res.get(2).intValue(), 2); } public void testSizeFilterList() { List<Integer> test = new LinkedList<Integer>(); test.add(1); test.add(3); test.add(7); test.add(2); test.add(5); List<Integer> res = Operations.filter(test, lessthanfive); assertEquals(res.size(), 3); } //Not a good test, but it's here to document that this usage pattern is going to be important public void testMultipleAccumulation() { BinaryFunction<Number,Integer,Number> sum = new SumFunction<Integer>(); BinaryFunction<String, String, String> max = new MaxFunction<String>(String.CASE_INSENSITIVE_ORDER); BinaryFunction<Number,TestObject,Number> sum_int_a = Operations.ChainSecond(TestObject.int_a, sum); BinaryFunction<String,TestObject,String> max_str_a = Operations.ChainSecond(TestObject.str_a, max); FunctionAccumulator<TestObject, ? extends Object> a1 = new FunctionAccumulator<TestObject, Number>(0.0, sum_int_a); FunctionAccumulator<TestObject, ? extends Object> a2 = new FunctionAccumulator<TestObject, String>("", max_str_a); TestObject t1 = new TestObject("abcc", 27); TestObject t2 = new TestObject("def", 2); a1.accumulate(t1); a2.accumulate(t1); a1.accumulate(t2); a2.accumulate(t2); } public <T> void doTestArithmetic(final ArithmeticOperations<T> o) { BinaryFunction<T,T,T> add = o.Add(o.parameter,o.parameter); BinaryFunction<T,T,T> mul = o.Mul(o.parameter,o.parameter); BinaryFunction<T,T,T> div = o.Div(o.parameter,o.parameter); BinaryFunction<T,T,T> sub = o.Sub(o.parameter,o.parameter); T zero = o.arithmetic.zero(); T one = o.arithmetic.one(); T two = o.arithmetic.add(one, one); T four = o.arithmetic.add(two, two); assertEquals(add.operate(one,one), two); assertEquals(add.operate(one,zero), one); assertEquals(sub.operate(two,one), one); assertEquals(sub.operate(two,zero), two); assertEquals(mul.operate(two,one), two); assertEquals(mul.operate(two,zero),zero); assertEquals(mul.operate(two,two), four); assertEquals(div.operate(two,one), two); assertEquals(div.operate(two,two), one); assertEquals(div.operate(four,two), two); } public void testArithmetic() { doTestArithmetic(Operations.DOUBLE); doTestArithmetic(Operations.FLOAT); doTestArithmetic(Operations.LONG); doTestArithmetic(Operations.INTEGER); } public void testComparisons() { Integer one = new Integer(1); Integer two = new Integer(2); BinaryFunction<Integer,Integer,Boolean> eq = Operations.EQ(Parameter.INTEGER, Parameter.INTEGER); BinaryFunction<Integer,Integer,Boolean> lt = Operations.LT(Parameter.INTEGER, Parameter.INTEGER); BinaryFunction<Integer,Integer,Boolean> gt = Operations.GT(Parameter.INTEGER, Parameter.INTEGER); BinaryFunction<Integer,Integer,Boolean> lte = Operations.LTE(Parameter.INTEGER, Parameter.INTEGER); BinaryFunction<Integer,Integer,Boolean> gte = Operations.GTE(Parameter.INTEGER, Parameter.INTEGER); assertTrue(eq.operate(one, one)); assertFalse(eq.operate(one, two)); assertTrue(lt.operate(one, two)); assertFalse(lt.operate(two, one)); assertFalse(lt.operate(one, one)); assertTrue(gt.operate(two, one)); assertFalse(gt.operate(one, two)); assertFalse(gt.operate(one, one)); assertTrue(lte.operate(one, two)); assertFalse(lte.operate(two, one)); assertTrue(lte.operate(one, one)); assertTrue(gte.operate(two, one)); assertFalse(gte.operate(one, two)); assertTrue(gte.operate(one, one)); } public void testExpression() throws OutputError { BinaryFunction<Integer, Integer, Boolean> test_expr = Operations.GT(Parameter.INTEGER, Operations.INTEGER.Add(Parameter.INTEGER, 1)); StringWriter out = new StringWriter(); test_expr.write(new SimpleExpressionWriter(out)); assertFalse(test_expr.operate(1, 8)); assertTrue(test_expr.operate(8, 1)); assertFalse(test_expr.operate(7, 8)); assertFalse(test_expr.operate(8, 7)); assertEquals("a>a+1", out.toString()); } public void testBooleanExpression() throws OutputError { BinaryFunction<Integer, Integer, Boolean> test_expr = Operations.AND( Operations.GT( 4, Operations.INTEGER.Add(Parameter.INTEGER, 1)), Operations.LT( 3, Parameter.INTEGER)); StringWriter out = new StringWriter(); test_expr.write(new SimpleExpressionWriter(out)); System.out.println(out.toString()); assertTrue(test_expr.operate(2, 4)); assertFalse(test_expr.operate(3, 4)); assertFalse(test_expr.operate(2, 3)); assertFalse(test_expr.operate(3, 3)); } public static TestSuite suite() { return new TestSuite(TestFunctionalPackage.class); } public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } } |