commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1344757 [3/4] - in /commons/proper/functor/trunk: ./ src/changes/ src/main/java/org/apache/commons/functor/aggregator/ src/main/java/org/apache/commons/functor/aggregator/functions/ src/site/xdoc/ src/test/java/org/apache/commons/functor/a...
Date Thu, 31 May 2012 14:56:50 GMT
Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.aggregator.AbstractListBackedAggregator;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link AbstractListBackedAggregator}. TODO: revisit after the
+ * class hierarchy change
+ */
+public class AbstractListBackedAggregatorTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new TestListBackedAggregator<Object>();
+    }
+
+    /**
+     * Ensure <code>series</code> is created in constructor.
+     */
+    @Test
+    public void testListCreated() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestListBackedAggregator<Object> fct = (TestListBackedAggregator<Object>) makeFunctor();
+        assertNotNull(fct.getSeries());
+        assertEquals(fct.getSeries().size(), 0);
+    }
+
+    /**
+     * Ensures beforeAdd/afterAdd is called correctly.
+     */
+    @Test
+    public void testAdd() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestListBackedAggregator<Object> fct = (TestListBackedAggregator<Object>) makeFunctor();
+        int calls = 31; // nearly 10 pies :)
+        for (int i = 1; i <= calls; i++) {
+            fct.add(new Object());
+            assertEquals(fct.getSeries().size(), i);
+            assertEquals(fct.callsCreateList, 0);
+        }
+    }
+
+    @Test
+    public void testReset() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestListBackedAggregator<Object> fct = (TestListBackedAggregator<Object>) makeFunctor();
+        int calls = 31; // nearly 10 pies :)
+        for (int i = 1; i <= calls; i++) {
+            fct.reset();
+            assertEquals(fct.getSeries().size(), 0);
+            assertEquals(fct.callsCreateList, 0);
+        }
+    }
+
+    /**
+     * Ensures beforeEvaluate/afterEvauate is called correctly.
+     */
+    @Test
+    public void testEvaluate() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestListBackedAggregator<Object> fct = (TestListBackedAggregator<Object>) makeFunctor();
+        TestUnaryFunction<Object> agg = (TestUnaryFunction<Object>) fct.getAggregationFunction();
+        int calls = 31; // nearly 10 pies :)
+        // test first without throwing an exception
+        for (int i = 1; i <= calls; i++) {
+            Object o = fct.evaluate();
+            assertNull(o);
+            assertEquals(fct.getSeries().size(), 0);
+            assertEquals(agg.calls, i);
+            assertEquals(fct.callsCreateList, 0);
+        }
+
+        // now throw the exception and make sure we still count ok
+        fct.resetUsage();
+        agg.exception = true;
+        boolean exc = false;
+        for (int i = 1; i <= calls; i++) {
+            Object o = null;
+            exc = false;
+            try {
+                o = fct.evaluate();
+            } catch (Exception e) {
+                exc = true;
+            }
+            assertNull(o);
+            assertTrue(exc); // make sure we have actually thrown it!
+            assertEquals(fct.getSeries().size(), 0);
+            assertEquals(agg.calls, i);
+            assertEquals(fct.callsCreateList, 0);
+        }
+    }
+
+    /**
+     * "Complete" add/evaluate/reset chain.
+     */
+    @Test
+    public void testAddEvaluateReset() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestListBackedAggregator<Object> fct = (TestListBackedAggregator<Object>) makeFunctor();
+        TestUnaryFunction<Object> agg = (TestUnaryFunction<Object>) fct.getAggregationFunction();
+        int callsAdd = 31; // nearly 10 pies :)
+        int callsEvaluate = 2; // circumference (i.e. 2 pies)
+        int callsReset = 17;
+        for (int i = 1; i <= callsAdd; i++)
+            fct.add(new Object());
+        assertEquals(fct.getSeries().size(), callsAdd);
+        assertEquals(agg.calls, 0);
+        assertEquals(fct.callsCreateList, 0);
+
+        for (int i = 1; i <= callsEvaluate; i++)
+            fct.evaluate();
+        assertEquals(fct.getSeries().size(), callsAdd);
+        assertEquals(agg.calls, callsEvaluate);
+        assertEquals(fct.callsCreateList, 0);
+
+        for (int i = 1; i <= callsReset; i++)
+            fct.reset();
+        assertEquals(fct.getSeries().size(), 0);
+        assertEquals(agg.calls, callsEvaluate);
+        assertEquals(fct.callsCreateList, 0);
+    }
+
+    @Test
+    public void testGetSize() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestListBackedAggregator<Object> fct = (TestListBackedAggregator<Object>) makeFunctor();
+        Random nAdds = new Random(); // this decides how many adds we do at each
+                                     // step
+        int callsAdd = 31;
+        int currSize = 0;
+        int maxAdds = 100; // maximum objects to add in one cycle
+        for (int i = 0; i < callsAdd; i++) {
+            int currAdds = nAdds.nextInt(maxAdds);
+            currSize += currAdds;
+            for (int j = 0; j < currAdds; j++) {
+                fct.add(new Object());
+            }
+            assertEquals(currSize, fct.getDataSize());
+        }
+    }
+
+    /**
+     * Dummy UnaryFunction which counts the number of calls to
+     * {@link #evaluate(List)}.
+     */
+    class TestUnaryFunction<T> implements UnaryFunction<List<T>, T> {
+        int     calls     = 0;
+        boolean exception = false; // when set to true, evaluate will throw an
+                                   // exception
+
+        public T evaluate(List<T> obj) {
+            calls++;
+            if (exception)
+                throw new RuntimeException();
+            return null;
+        }
+    }
+
+    /**
+     * Dummy aggregator which counts calls to before/after functions.
+     */
+    class TestListBackedAggregator<T> extends AbstractListBackedAggregator<T> {
+        int callsCreateList;
+
+        public TestListBackedAggregator() {
+            super(new TestUnaryFunction<T>());
+            resetUsage();
+        }
+
+        /**
+         * Convenience method to reset all counters to zero (rather than
+         * creating a new instance of this)
+         */
+        public void resetUsage() {
+            callsCreateList = 0;
+            TestUnaryFunction<T> fct = (TestUnaryFunction<T>) getAggregationFunction();
+            fct.calls = 0;
+        }
+
+        @Override
+        protected List<T> createList() {
+            // for the purpose of testing we'll be using a sync'd implementation
+            // of List
+            callsCreateList++;
+            return new CopyOnWriteArrayList<T>();
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractListBackedAggregatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.BinaryFunction;
+import org.apache.commons.functor.aggregator.AbstractNoStoreAggregator;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link AbstractNoStoreAggregator}. TODO: revisit after the
+ * class hierarchy change
+ */
+public class AbstractNoStoreAggregatorTest extends BaseFunctorTest {
+    private static final String INITIAL = "abc";
+
+    @Override
+    protected Object makeFunctor() throws Exception {
+        // return new TestListBackedAggregator<Object>();
+        return new TestNoStoreAggregator<Object>(new String(INITIAL));
+    }
+
+    /**
+     * Ensure data is initialised in constructor.
+     */
+    @Test
+    public void testInitialised() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestNoStoreAggregator<Object> fct = (TestNoStoreAggregator<Object>) makeFunctor();
+        assertNotNull(fct.getResult());
+        TestBinaryFunction<Object> agg = (TestBinaryFunction<Object>) fct.getAggregationFunction();
+        assertEquals(fct.callsInitialValue, 1);
+        assertEquals(agg.calls, 0);
+    }
+
+    @Test
+    public void testAdd() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestNoStoreAggregator<Object> fct = (TestNoStoreAggregator<Object>) makeFunctor();
+        TestBinaryFunction<Object> agg = (TestBinaryFunction<Object>) fct.getAggregationFunction();
+        int calls = 31; // nearly 10 pies :)
+        for (int i = 1; i <= calls; i++) {
+            fct.add(new Object());
+            assertEquals(INITIAL, fct.getResult());
+            assertEquals(fct.callsInitialValue, 1);
+            assertEquals(agg.calls, i);
+        }
+
+        // now throw the exception and make sure we still count ok
+        fct.resetUsage();
+        agg.exception = true;
+        boolean exc = false;
+        for (int i = 1; i <= calls; i++) {
+            exc = false;
+            try {
+                fct.add(new Object());
+            } catch (Exception e) {
+                exc = true;
+            }
+            assertTrue(exc); // make sure we have actually thrown it!
+            assertEquals(INITIAL, fct.getResult());
+            assertEquals(agg.calls, i);
+            assertEquals(fct.callsInitialValue, 0);
+        }
+    }
+
+    /**
+     * Ensures beforeReset/afterReset is called correctly.
+     */
+    @Test
+    public void testReset() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestNoStoreAggregator<Object> fct = (TestNoStoreAggregator<Object>) makeFunctor();
+        int calls = 31; // nearly 10 pies :)
+        for (int i = 1; i <= calls; i++) {
+            fct.reset();
+            assertEquals(INITIAL, fct.getResult());
+            assertEquals(fct.callsInitialValue, i + 1);
+        }
+    }
+
+    /**
+     * Ensures beforeEvaluate/afterEvauate is called correctly.
+     */
+    @Test
+    public void testEvaluate() throws Exception {
+        @SuppressWarnings("unchecked")
+        TestNoStoreAggregator<Object> fct = (TestNoStoreAggregator<Object>) makeFunctor();
+        TestBinaryFunction<Object> agg = (TestBinaryFunction<Object>) fct.getAggregationFunction();
+        int calls = 31; // nearly 10 pies :)
+        // test first without throwing an exception
+        for (int i = 1; i <= calls; i++) {
+            Object o = fct.evaluate();
+            assertSame(o, fct.getResult());
+            assertEquals(INITIAL, fct.getResult());
+            assertEquals(agg.calls, 0);
+            assertEquals(fct.callsInitialValue, 1);
+        }
+    }
+
+    /**
+     * Dummy binary function which always returns the first parameter.
+     */
+    class TestBinaryFunction<T> implements BinaryFunction<T, T, T> {
+        int     calls     = 0;
+        boolean exception = false; // when set to true, evaluate will throw an
+                                   // exception
+
+        public T evaluate(T left, T right) {
+            calls++;
+            if (exception)
+                throw new RuntimeException();
+            return left;
+        }
+    }
+
+    /**
+     * Dummy aggregator which counts calls to before/after functions.
+     */
+    class TestNoStoreAggregator<T> extends AbstractNoStoreAggregator<T> {
+        int callsInitialValue;
+
+        T   initial;
+
+        public TestNoStoreAggregator(T initial) {
+            super(new TestBinaryFunction<T>());
+            this.initial = initial;
+            resetUsage();
+            this.setResult(initialValue());
+        }
+
+        /**
+         * Convenience method to reset all counters to zero (rather than
+         * creating a new instance of this)
+         */
+        public void resetUsage() {
+            callsInitialValue = 0;
+            TestBinaryFunction<T> fct = (TestBinaryFunction<T>) getAggregationFunction();
+            fct.calls = 0;
+        }
+
+        @Override
+        protected T initialValue() {
+            callsInitialValue++;
+            return initial;
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractNoStoreAggregatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.AbstractTimedAggregator;
+import org.apache.commons.functor.aggregator.TimedAggregatorListener;
+import org.junit.Test;
+
+public class AbstractTimedAggregatorTest extends BaseFunctorTest {
+    /** Default timer interval used in the tests. */
+    private static final long DEFAULT_INTERVAL = 500L; // 0.5 second
+
+    /** Sleep extra msecs past the interval. */
+    private static final long SLEEP            = 10L;
+
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new SimpleStoreTimedAggregator();
+    }
+
+    @Test
+    public void testCreateNoTimer() throws Exception {
+        SimpleStoreTimedAggregator agg = (SimpleStoreTimedAggregator) makeFunctor();
+        assertEquals(agg.getInterval(), 0L);
+        assertFalse(agg.isTimerEnabled());
+        assertFalse(agg.isSharedTimer());
+        assertNull(agg.getTimerListeners());
+        assertFalse(agg.removeTimerListener(null)); // shouldn't NPE here
+                                                    // because the listeners
+                                                    // list is null!
+        agg.stop(); // shouldn't trigger any exceptions, even though there's no
+                    // timer
+    }
+
+    private void standardTimerTesting(SimpleStoreTimedAggregator agg, long interval) throws Exception {
+        final AtomicInteger count = new AtomicInteger();
+        TimedAggregatorListener<Integer> timedListener = new TimedAggregatorListener<Integer>() {
+            public void onTimer(AbstractTimedAggregator<Integer> aggregator, Integer evaluation) {
+                count.incrementAndGet();
+            }
+        };
+
+        assertEquals(agg.getInterval(), interval);
+        assertEquals(agg.getTimerListeners().size(), 0);
+        agg.addTimerListener(timedListener);
+        List<TimedAggregatorListener<Integer>> timerListeners = agg.getTimerListeners();
+        assertTrue(timerListeners.contains(timedListener));
+        TimeUnit.MILLISECONDS.sleep(interval + SLEEP);
+        // timer should have kicked in by now
+        assertTrue(count.intValue() > 0);
+
+        TimedAggregatorListener<Integer> wrongListener = new TimedAggregatorListener<Integer>() {
+            public void onTimer(AbstractTimedAggregator<Integer> aggregator, Integer evaluation) {
+                // do nothing in here
+            }
+        };
+        assertFalse(agg.removeTimerListener(wrongListener));
+        assertEquals(agg.getTimerListeners().size(), 1);
+        assertTrue(agg.removeTimerListener(timedListener));
+        int saveValue = count.get();
+        assertEquals(agg.getTimerListeners().size(), 0);
+        // give enough time for the timer to kick in again
+        TimeUnit.MILLISECONDS.sleep(interval + SLEEP);
+        assertEquals(saveValue, count.get());
+
+    }
+
+    @Test
+    public void testCreateIntervalParam() throws Exception {
+        SimpleStoreTimedAggregator agg = new SimpleStoreTimedAggregator(DEFAULT_INTERVAL);
+        assertTrue(agg.isTimerEnabled());
+        assertFalse(agg.isSharedTimer());
+        standardTimerTesting(agg, DEFAULT_INTERVAL);
+        agg.stop();
+    }
+
+    @Test
+    public void testCreateNotSharedTimer() throws Exception {
+        SimpleStoreTimedAggregator agg = new SimpleStoreTimedAggregator(DEFAULT_INTERVAL, false);
+        assertTrue(agg.isTimerEnabled());
+        assertFalse(agg.isSharedTimer());
+        standardTimerTesting(agg, DEFAULT_INTERVAL);
+        agg.stop();
+    }
+
+    @Test
+    public void testCreateSharedTimer() throws Exception {
+        SimpleStoreTimedAggregator agg = new SimpleStoreTimedAggregator(DEFAULT_INTERVAL, true);
+        assertTrue(agg.isTimerEnabled());
+        assertTrue(agg.isSharedTimer());
+        standardTimerTesting(agg, DEFAULT_INTERVAL);
+        agg.stop();
+    }
+
+    /**
+     * Simple timed aggregator which just stores the latest object and returns
+     * it when evaluating the result.
+     *
+     * @author Liviu Tudor http://about.me/liviutudor
+     */
+    class SimpleStoreTimedAggregator extends AbstractTimedAggregator<Integer> {
+        private Integer object;
+
+        public SimpleStoreTimedAggregator() {
+            super();
+        }
+
+        public SimpleStoreTimedAggregator(long interval) {
+            super(interval);
+        }
+
+        public SimpleStoreTimedAggregator(long interval, boolean useSharedTimer) {
+            super(interval, useSharedTimer);
+        }
+
+        @Override
+        protected void doAdd(Integer data) {
+            this.object = data;
+        }
+
+        @Override
+        protected Integer doEvaluate() {
+            return object;
+        }
+
+        @Override
+        protected int retrieveDataSize() {
+            return 1;
+        }
+
+        @Override
+        protected void doReset() {
+            object = new Integer(0);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/AbstractTimedAggregatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.UnaryFunction;
+import org.apache.commons.functor.aggregator.ArrayListBackedAggregator;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ArrayListBackedAggregator}. TODO: Some multi-threaded
+ * testing
+ */
+public class ArrayListBackedAggregatorTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new ArrayListBackedAggregator<Object>(new SelectFirstUnaryFunction<Object>());
+    }
+
+    @Test
+    public void testCreateList() throws Exception {
+        @SuppressWarnings("unchecked")
+        ArrayListBackedAggregator<Object> fct = (ArrayListBackedAggregator<Object>) makeFunctor();
+        assertNotNull(fct.getSeries());
+        assertTrue(fct.getSeries() instanceof ArrayList);
+        assertEquals(fct.getSeries().size(), 0);
+        // NOTE: would be good to be able to check the ArrayList capacity!
+        int initialSize = 31; // nearly 10 pies
+        fct = new ArrayListBackedAggregator<Object>(new SelectFirstUnaryFunction<Object>(), initialSize);
+        assertNotNull(fct.getSeries());
+        assertTrue(fct.getSeries() instanceof ArrayList);
+        assertEquals(fct.getSeries().size(), 0);
+    }
+
+    /**
+     * Dummy UnaryFunction which counts the number of calls to
+     * {@link #evaluate(List)} and always selects the first item in the given
+     * list or null if list is null or empty.
+     */
+    class SelectFirstUnaryFunction<T> implements UnaryFunction<List<T>, T> {
+        int     calls     = 0;
+        boolean exception = false; // when set to true, evaluate will throw an
+                                   // exception
+
+        public T evaluate(List<T> obj) {
+            calls++;
+            if (exception)
+                throw new RuntimeException();
+            if (obj == null || obj.size() == 0)
+                return null;
+            return obj.get(0);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/ArrayListBackedAggregatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoubleMaxAggregatorBinaryFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoubleMaxAggregatorBinaryFunction}.
+ */
+public class DoubleMaxAggregatorBinaryFunctionTest extends BaseFunctorTest {
+    private static final double DELTA = 0.01; // make room for some poor
+                                              // floating point support
+
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoubleMaxAggregatorBinaryFunction();
+    }
+
+    @Test
+    public void testNulls() throws Exception {
+        DoubleMaxAggregatorBinaryFunction fct = (DoubleMaxAggregatorBinaryFunction) makeFunctor();
+        Double d = fct.evaluate(null, null);
+        assertNull(d);
+        d = fct.evaluate(null, 1.0);
+        assertEquals(1.0, d.doubleValue(), DELTA);
+        d = fct.evaluate(2.0, null);
+        assertEquals(2.0, d.doubleValue(), DELTA);
+    }
+
+    @Test
+    public void testMax() throws Exception {
+        DoubleMaxAggregatorBinaryFunction fct = (DoubleMaxAggregatorBinaryFunction) makeFunctor();
+        double max = 0.0;
+        double result = 0.0;
+        double number1 = 0.0;
+        double number2 = 0.0;
+        int calls = 31;
+        Random rnd = new Random();
+        for (int i = 0; i < calls; i++) {
+            number1 = rnd.nextDouble();
+            number2 = rnd.nextDouble();
+            max = Math.max(number1, number2);
+            result = fct.evaluate(number1, number2);
+            assertEquals(result, max, DELTA);
+        }
+    }
+
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoubleMaxAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoubleMaxAggregatorFunction}.
+ */
+public class DoubleMaxAggregatorFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoubleMaxAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        DoubleMaxAggregatorFunction fct = (DoubleMaxAggregatorFunction) makeFunctor();
+        List<Double> lst = null;
+        Double res = fct.evaluate(lst);
+        assertNull(res);
+        lst = new ArrayList<Double>();
+        res = fct.evaluate(lst);
+        assertNull(res);
+    }
+
+    @Test
+    public void testSum() throws Exception {
+        DoubleMaxAggregatorFunction fct = (DoubleMaxAggregatorFunction) makeFunctor();
+        List<Double> lst = new ArrayList<Double>();
+        lst.add(0.0);
+        double res = fct.evaluate(lst);
+        assertEquals(res, 0.0, 0.01);
+        lst.add(1.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 1.0, 0.01);
+        lst.add(2.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 2.0, 0.01);
+        // finally carry out a random addition
+        lst.clear();
+        int calls = 31;
+        double max = 0.0;
+        Random rnd = new Random();
+        for (int i = 0; i < calls; i++) {
+            double random = rnd.nextDouble();
+            lst.add(random);
+            if (random > max) {
+                max = random;
+            }
+            res = fct.evaluate(lst);
+            assertEquals(res, max, 0.01);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMaxAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoubleMeanValueAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoubleMeanValueAggregatorFunction}.
+ */
+public class DoubleMeanValueAggregatorFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoubleMeanValueAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        DoubleMeanValueAggregatorFunction fct = (DoubleMeanValueAggregatorFunction)makeFunctor();
+        List<Double> lst = null;
+        Double res = fct.evaluate(lst);
+        assertNull( res );
+        lst = new ArrayList<Double>();
+        res = fct.evaluate(lst);
+        assertNull( res );
+    }
+
+    @Test
+    public void testMean() throws Exception {
+        DoubleMeanValueAggregatorFunction fct = (DoubleMeanValueAggregatorFunction)makeFunctor();
+        List<Double> lst = new ArrayList<Double>();
+        lst.add( 0.0 );
+        double res = fct.evaluate(lst);
+        assertEquals(res, 0.0, 0.01);
+        lst.add( 1.0 );
+        res = fct.evaluate( lst );
+        assertEquals( res, 0.5, 0.01 );
+        lst.add( 2.0 );
+        res = fct.evaluate( lst );
+        assertEquals( res, 1.0, 0.01 );
+        //finally carry out a random mean computation
+        int calls = 31;
+        double total;
+        for( int i = 2; i <= calls; i++ ) {
+            lst.clear();
+            total = 0.0;
+            for( int j = 1; j <= i; j++ ) {
+                lst.add( (double)j );
+                total += j;
+            }
+            total /= i;
+            res = fct.evaluate( lst );
+            assertEquals( res, total, 0.01 );
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMeanValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoubleMedianValueAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoubleMedianValueAggregatorFunction}.
+ */
+public class DoubleMedianValueAggregatorFunctionTest extends BaseFunctorTest {
+    private static final double DELTA = 0.01; // make room for some poor
+                                              // floating point precision
+
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoubleMedianValueAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        DoubleMedianValueAggregatorFunction fct = (DoubleMedianValueAggregatorFunction) makeFunctor();
+        assertTrue(fct.isUseCopy());
+        List<Double> lst = null;
+        Double res = fct.evaluate(lst);
+        assertNull(res);
+        lst = new ArrayList<Double>();
+        res = fct.evaluate(lst);
+        assertNull(res);
+    }
+
+    /**
+     * Ensure we compute the median correctly and also we don't affect the
+     * original list.
+     */
+    @Test
+    public void testMedianCopy() throws Exception {
+        DoubleMedianValueAggregatorFunction fct = new DoubleMedianValueAggregatorFunction(true);
+        assertTrue(fct.isUseCopy());
+        checkMedianCopy(fct);
+        // this is also the default behaviour so ensure that is the case
+        fct = (DoubleMedianValueAggregatorFunction) makeFunctor();
+        checkMedianCopy(fct);
+    }
+
+    /**
+     * Called by {@link #testMedianCopy()} internally.
+     */
+    void checkMedianCopy(DoubleMedianValueAggregatorFunction fct) throws Exception {
+        List<Double> lst = new ArrayList<Double>();
+        lst.add(10.0);
+        double res = fct.evaluate(lst);
+        assertEquals(res, 10.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0 });
+        lst.add(1000.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 505.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 1000.0 });
+        lst.add(30.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 1000.0, 30.0 });
+        lst.add(100.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 65.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 1000.0, 30.0, 100.0 });
+        lst.add(20.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 1000.0, 30.0, 100.0, 20.0 });
+    }
+
+    /**
+     * Ensure we compute the median correctly and also we sort the original
+     * list.
+     */
+    @Test
+    public void testMedianNotCopy() throws Exception {
+        DoubleMedianValueAggregatorFunction fct = new DoubleMedianValueAggregatorFunction(false);
+        assertFalse(fct.isUseCopy());
+        List<Double> lst = new ArrayList<Double>();
+        lst.add(10.0);
+        double res = fct.evaluate(lst);
+        assertEquals(res, 10.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0 });
+        lst.add(1000.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 505.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 1000.0 });
+        lst.add(30.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 30.0, 1000.0 });
+        lst.add(100.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 65.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 30.0, 100.0, 1000.0 });
+        lst.add(20.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30.0, DELTA);
+        assertListEqualsArray(lst, new double[] { 10.0, 20.0, 30.0, 100.0, 1000.0 });
+    }
+
+    /**
+     * Utility function to check the elements of a list are in a given order.
+     * Simply build an inline array and pass it in the <code>arr</code>
+     * parameter.
+     */
+    void assertListEqualsArray(List<Double> list, double arr[]) throws Exception {
+        assertNotNull(list);
+        assertNotNull(arr);
+        assertEquals(list.size(), arr.length);
+        for (int i = 0; i < arr.length; i++) {
+            assertEquals(list.get(i).doubleValue(), arr[i], DELTA);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleMedianValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,186 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoublePercentileAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoublePercentileAggregatorFunction}.
+ */
+public class DoublePercentileAggregatorFunctionTest extends BaseFunctorTest {
+    private static final double DELTA    = 0.01; // make room for some poor
+                                                 // floating point precision
+    private static final double DEF_PERC = 90;  // by default use the 90th
+                                                 // percentile
+
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoublePercentileAggregatorFunction(DEF_PERC);
+    }
+
+    @Test
+    public void testCreate() throws Exception {
+        // default
+        DoublePercentileAggregatorFunction fct = (DoublePercentileAggregatorFunction) makeFunctor();
+        assertTrue(fct.isUseCopy());
+        assertEquals(fct.getPercentile(), DEF_PERC, DELTA);
+
+        // different percentage
+        double perc = 50.0;
+        fct = new DoublePercentileAggregatorFunction(perc);
+        assertTrue(fct.isUseCopy());
+        assertEquals(fct.getPercentile(), perc, DELTA);
+
+        // use copy
+        fct = new DoublePercentileAggregatorFunction(perc, false);
+        assertFalse(fct.isUseCopy());
+        assertEquals(fct.getPercentile(), perc, DELTA);
+
+        // test illegal exc
+        boolean exc = false;
+        fct = null;
+        try {
+            fct = new DoublePercentileAggregatorFunction(-100);
+        } catch (IllegalArgumentException e) {
+            exc = true;
+        }
+        assertTrue(exc);
+
+        fct = null;
+        exc = false;
+        try {
+            fct = new DoublePercentileAggregatorFunction(101);
+        } catch (IllegalArgumentException e) {
+            exc = true;
+        }
+        assertTrue(exc);
+    }
+
+    @Test
+    public void testComputeRank() throws Exception {
+        List<Double> data = new ArrayList<Double>();
+        data.add(0.0);
+        data.add(1.0);
+        data.add(2.0);
+        data.add(3.0);
+        data.add(4.0);
+        // size of list is 5 now
+
+        // first item
+        DoublePercentileAggregatorFunction fct = new DoublePercentileAggregatorFunction(0.0);
+        int rank = fct.computeRank(data);
+        assertEquals(rank, 0);
+        assertEquals(data.get(rank).doubleValue(), 0.0, DELTA);
+        // last item
+        fct = new DoublePercentileAggregatorFunction(100.0);
+        rank = fct.computeRank(data);
+        assertEquals(rank, data.size() - 1);
+        assertEquals(data.get(rank).doubleValue(), 4.0, DELTA);
+
+        // middle one
+        fct = new DoublePercentileAggregatorFunction(50.0);
+        rank = fct.computeRank(data);
+        assertEquals(rank, 2);
+        assertEquals(data.get(rank).doubleValue(), 2.0, DELTA);
+
+        // 40% = 2nd item
+        fct = new DoublePercentileAggregatorFunction(40.0);
+        rank = fct.computeRank(data);
+        assertEquals(rank, 1);
+        assertEquals(data.get(rank).doubleValue(), 1.0, DELTA);
+
+        // 80% = 4th item
+        fct = new DoublePercentileAggregatorFunction(80.0);
+        rank = fct.computeRank(data);
+        assertEquals(rank, 3);
+        assertEquals(data.get(rank).doubleValue(), 3.0, DELTA);
+
+        // in between (e.g. 70%) means 3rd item
+        fct = new DoublePercentileAggregatorFunction(70.0);
+        rank = fct.computeRank(data);
+        assertEquals(rank, 2);
+        assertEquals(data.get(rank).doubleValue(), 2.0, DELTA);
+
+        // but 75% means 4th item
+        fct = new DoublePercentileAggregatorFunction(75.0);
+        rank = fct.computeRank(data);
+        assertEquals(rank, 3);
+        assertEquals(data.get(rank).doubleValue(), 3.0, DELTA);
+    }
+
+    @Test
+    public void testEvaluateNullEmpty() throws Exception {
+        DoublePercentileAggregatorFunction fct = (DoublePercentileAggregatorFunction) makeFunctor();
+        List<Double> data = null;
+        Double d = fct.evaluate(data);
+        assertNull(d);
+        data = new ArrayList<Double>();
+        d = fct.evaluate(data);
+        assertNull(d);
+    }
+
+    @Test
+    public void testEvaluateNoCopy() throws Exception {
+        // using a copy
+        DoublePercentileAggregatorFunction fct = (DoublePercentileAggregatorFunction) makeFunctor();
+        List<Double> data = new ArrayList<Double>();
+        data.add(4.0);
+        data.add(3.0);
+        data.add(2.0);
+        data.add(1.0);
+        data.add(0.0);
+        // size of list is 5 now
+
+        Double d = fct.evaluate(data);
+        assertEquals(d, 3.0, DELTA);
+        assertEquals(data.get(0), 4.0, DELTA);
+        assertEquals(data.get(1), 3.0, DELTA);
+        assertEquals(data.get(2), 2.0, DELTA);
+        assertEquals(data.get(3), 1.0, DELTA);
+        assertEquals(data.get(4), 0.0, DELTA);
+
+        // using a copy (explicitly)
+        fct = new DoublePercentileAggregatorFunction(DEF_PERC, true);
+        d = fct.evaluate(data);
+        assertEquals(d, 3.0, DELTA);
+        assertEquals(data.get(0), 4.0, DELTA);
+        assertEquals(data.get(1), 3.0, DELTA);
+        assertEquals(data.get(2), 2.0, DELTA);
+        assertEquals(data.get(3), 1.0, DELTA);
+        assertEquals(data.get(4), 0.0, DELTA);
+
+        // operate on the list directly
+        fct = new DoublePercentileAggregatorFunction(DEF_PERC, false);
+        d = fct.evaluate(data);
+        assertEquals(d, 3.0, DELTA);
+        assertEquals(data.get(4), 4.0, DELTA);
+        assertEquals(data.get(3), 3.0, DELTA);
+        assertEquals(data.get(2), 2.0, DELTA);
+        assertEquals(data.get(1), 1.0, DELTA);
+        assertEquals(data.get(0), 0.0, DELTA);
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoublePercentileAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoubleSumAggregatorBinaryFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoubleSumAggregatorBinaryFunction}.
+ */
+public class DoubleSumAggregatorBinaryFunctionTest extends BaseFunctorTest {
+    private static final double DELTA = 0.01;   //make room for some poor floating point support
+
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoubleSumAggregatorBinaryFunction();
+    }
+
+    @Test
+    public void testNulls() throws Exception {
+        DoubleSumAggregatorBinaryFunction fct = (DoubleSumAggregatorBinaryFunction)makeFunctor();
+        Double d = fct.evaluate(null, null);
+        assertNull( d );
+        d = fct.evaluate( null, 1.0 );
+        assertEquals( 1.0, d.doubleValue(), DELTA );
+        d = fct.evaluate( 2.0, null );
+        assertEquals( 2.0, d.doubleValue(), DELTA );
+    }
+
+    @Test
+    public void testSum() throws Exception {
+        DoubleSumAggregatorBinaryFunction fct = (DoubleSumAggregatorBinaryFunction)makeFunctor();
+        double total = 0.0;
+        double result = 0.0;
+        int calls = 31;
+        Random rnd = new Random();
+        for( int i = 0; i < calls; i++ ) {
+            double number = rnd.nextDouble();
+            total += number;
+            result = fct.evaluate(result, number);
+            assertEquals( result, total, DELTA );
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.DoubleSumAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link DoubleSumAggregatorFunction}.
+ */
+public class DoubleSumAggregatorFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new DoubleSumAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        DoubleSumAggregatorFunction fct = (DoubleSumAggregatorFunction) makeFunctor();
+        List<Double> lst = null;
+        Double res = fct.evaluate(lst);
+        assertNull(res);
+        lst = new ArrayList<Double>();
+        res = fct.evaluate(lst);
+        assertNull(res);
+    }
+
+    @Test
+    public void testSum() throws Exception {
+        DoubleSumAggregatorFunction fct = (DoubleSumAggregatorFunction) makeFunctor();
+        List<Double> lst = new ArrayList<Double>();
+        lst.add(0.0);
+        double res = fct.evaluate(lst);
+        assertEquals(res, 0.0, 0.01);
+        lst.add(1.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 1.0, 0.01);
+        lst.add(2.0);
+        res = fct.evaluate(lst);
+        assertEquals(res, 3.0, 0.01);
+        // finally carry out a random addition
+        lst.clear();
+        int calls = 31;
+        double total = 0.0;
+        Random rnd = new Random();
+        for (int i = 0; i < calls; i++) {
+            double random = rnd.nextDouble();
+            lst.add(random);
+            total += random;
+            res = fct.evaluate(lst);
+            assertEquals(res, total, 0.01);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/DoubleSumAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerCountAggregatorBinaryFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerCountAggregatorBinaryFunction}.
+ */
+public class IntCountAggregatorBinaryFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new IntegerCountAggregatorBinaryFunction();
+    }
+
+    @Test
+    public void testNull() throws Exception {
+        IntegerCountAggregatorBinaryFunction fct = (IntegerCountAggregatorBinaryFunction)makeFunctor();
+        Integer i = fct.evaluate(null, null);
+        assertNull( i );
+        i = fct.evaluate( null, 1 );
+        assertNull( i );
+        i = fct.evaluate( 2, null );
+        assertNotNull( i );
+        assertEquals( i.intValue(), 3 );
+    }
+
+    @Test
+    public void testCount() throws Exception {
+        IntegerCountAggregatorBinaryFunction fct = (IntegerCountAggregatorBinaryFunction)makeFunctor();
+        int count = 0;
+        int calls = 31;
+        for( int i = 1; i <= calls; i++ ) {
+            count = fct.evaluate(count, 12345);
+            assertEquals( i, count );
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntCountAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerMaxAggregatorBinaryFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerMaxAggregatorBinaryFunction}.
+ */
+public class IntMaxAggregatorBinaryFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new IntegerMaxAggregatorBinaryFunction();
+    }
+
+    @Test
+    public void testNulls() throws Exception {
+        IntegerMaxAggregatorBinaryFunction fct = (IntegerMaxAggregatorBinaryFunction) makeFunctor();
+        Integer d = fct.evaluate(null, null);
+        assertNull(d);
+        d = fct.evaluate(null, 1);
+        assertEquals(1, d.intValue());
+        d = fct.evaluate(2, null);
+        assertEquals(2, d.intValue());
+    }
+
+    @Test
+    public void testMax() throws Exception {
+        IntegerMaxAggregatorBinaryFunction fct = (IntegerMaxAggregatorBinaryFunction) makeFunctor();
+        int max = 0;
+        int result = 0;
+        int number1 = 0;
+        int number2 = 0;
+        int calls = 31;
+        Random rnd = new Random();
+        for (int i = 0; i < calls; i++) {
+            number1 = rnd.nextInt();
+            number2 = rnd.nextInt();
+            max = Math.max(number1, number2);
+            result = fct.evaluate(number1, number2);
+            assertEquals(result, max);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorBinaryFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerMaxAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerMaxAggregatorFunction}.
+ */
+public class IntMaxAggregatorFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new IntegerMaxAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        IntegerMaxAggregatorFunction fct = (IntegerMaxAggregatorFunction) makeFunctor();
+        List<Integer> lst = null;
+        Integer res = fct.evaluate(lst);
+        assertNull(res);
+        lst = new ArrayList<Integer>();
+        res = fct.evaluate(lst);
+        assertNull(res);
+    }
+
+    @Test
+    public void testSum() throws Exception {
+        IntegerMaxAggregatorFunction fct = (IntegerMaxAggregatorFunction) makeFunctor();
+        List<Integer> lst = new ArrayList<Integer>();
+        lst.add(0);
+        int res = fct.evaluate(lst);
+        assertEquals(res, 0);
+        lst.add(1);
+        res = fct.evaluate(lst);
+        assertEquals(res, 1);
+        lst.add(2);
+        res = fct.evaluate(lst);
+        assertEquals(res, 2);
+        // finally carry out a random addition
+        lst.clear();
+        int calls = 31;
+        int max = 0;
+        Random rnd = new Random();
+        for (int i = 0; i < calls; i++) {
+            int random = rnd.nextInt(Integer.MAX_VALUE / calls);    //prevent overflow
+            lst.add(random);
+            if( random > max ) {
+                max = random;
+            }
+            res = fct.evaluate(lst);
+            assertEquals(res, max);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMaxAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerMeanValueAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerMeanValueAggregatorFunction}.
+ */
+public class IntMeanValueAggregatorFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new IntegerMeanValueAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        IntegerMeanValueAggregatorFunction fct = (IntegerMeanValueAggregatorFunction) makeFunctor();
+        List<Integer> lst = null;
+        Integer res = fct.evaluate(lst);
+        assertNull(res);
+        lst = new ArrayList<Integer>();
+        res = fct.evaluate(lst);
+        assertNull(res);
+    }
+
+    @Test
+    public void testMean() throws Exception {
+        IntegerMeanValueAggregatorFunction fct = (IntegerMeanValueAggregatorFunction) makeFunctor();
+        List<Integer> lst = new ArrayList<Integer>();
+        lst.add(0);
+        int res = fct.evaluate(lst);
+        assertEquals(res, 0);
+        lst.add(1);
+        res = fct.evaluate(lst);
+        assertEquals(res, 0);
+        lst.add(2);
+        res = fct.evaluate(lst);
+        assertEquals(res, 1);
+        // finally carry out a random mean computation
+        int calls = 31;
+        int total;
+        for (int i = 2; i <= calls; i++) {
+            lst.clear();
+            total = 0;
+            for (int j = 1; j <= i; j++) {
+                lst.add(j);
+                total += j;
+            }
+            total /= i;
+            res = fct.evaluate(lst);
+            assertEquals(res, total);
+        }
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMeanValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java?rev=1344757&view=auto
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java (added)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java Thu May 31 14:56:47 2012
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.aggregator.functions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.aggregator.functions.IntegerMedianValueAggregatorFunction;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IntegerMedianValueAggregatorFunction}.
+ */
+public class IntMedianValueAggregatorFunctionTest extends BaseFunctorTest {
+    @Override
+    protected Object makeFunctor() throws Exception {
+        return new IntegerMedianValueAggregatorFunction();
+    }
+
+    @Test
+    public void testEmptyList() throws Exception {
+        IntegerMedianValueAggregatorFunction fct = (IntegerMedianValueAggregatorFunction) makeFunctor();
+        assertTrue(fct.isUseCopy());
+        List<Integer> lst = null;
+        Integer res = fct.evaluate(lst);
+        assertNull(res);
+        lst = new ArrayList<Integer>();
+        res = fct.evaluate(lst);
+        assertNull(res);
+    }
+
+    /**
+     * Ensure we compute the median correctly and also we don't affect the
+     * original list.
+     */
+    @Test
+    public void testMedianCopy() throws Exception {
+        IntegerMedianValueAggregatorFunction fct = new IntegerMedianValueAggregatorFunction(true);
+        assertTrue(fct.isUseCopy());
+        checkMedianCopy(fct);
+        // this is also the default behaviour so ensure that is the case
+        fct = (IntegerMedianValueAggregatorFunction) makeFunctor();
+        checkMedianCopy(fct);
+    }
+
+    /**
+     * Called by {@link #testMedianCopy()} internally.
+     */
+    void checkMedianCopy(IntegerMedianValueAggregatorFunction fct) throws Exception {
+        List<Integer> lst = new ArrayList<Integer>();
+        lst.add(10);
+        int res = fct.evaluate(lst);
+        assertEquals(res, 10);
+        assertListEqualsArray(lst, new int[] { 10 });
+        lst.add(1000);
+        res = fct.evaluate(lst);
+        assertEquals(res, 505);
+        assertListEqualsArray(lst, new int[] { 10, 1000 });
+        lst.add(30);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30);
+        assertListEqualsArray(lst, new int[] { 10, 1000, 30 });
+        lst.add(100);
+        res = fct.evaluate(lst);
+        assertEquals(res, 65);
+        assertListEqualsArray(lst, new int[] { 10, 1000, 30, 100 });
+        lst.add(20);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30);
+        assertListEqualsArray(lst, new int[] { 10, 1000, 30, 100, 20 });
+    }
+
+    /**
+     * Ensure we compute the median correctly and also we sort the original
+     * list.
+     */
+    @Test
+    public void testMedianNotCopy() throws Exception {
+        IntegerMedianValueAggregatorFunction fct = new IntegerMedianValueAggregatorFunction(false);
+        assertFalse(fct.isUseCopy());
+        List<Integer> lst = new ArrayList<Integer>();
+        lst.add(10);
+        int res = fct.evaluate(lst);
+        assertEquals(res, 10);
+        assertListEqualsArray(lst, new int[] { 10 });
+        lst.add(1000);
+        res = fct.evaluate(lst);
+        assertEquals(res, 505);
+        assertListEqualsArray(lst, new int[] { 10, 1000 });
+        lst.add(30);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30);
+        assertListEqualsArray(lst, new int[] { 10, 30, 1000 });
+        lst.add(100);
+        res = fct.evaluate(lst);
+        assertEquals(res, 65);
+        assertListEqualsArray(lst, new int[] { 10, 30, 100, 1000 });
+        lst.add(20);
+        res = fct.evaluate(lst);
+        assertEquals(res, 30);
+        assertListEqualsArray(lst, new int[] { 10, 20, 30, 100, 1000 });
+    }
+
+    /**
+     * Utility function to check the elements of a list are in a given order.
+     * Simply build an inline array and pass it in the <code>arr</code>
+     * parameter.
+     */
+    void assertListEqualsArray(List<Integer> list, int arr[]) throws Exception {
+        assertNotNull(list);
+        assertNotNull(arr);
+        assertEquals(list.size(), arr.length);
+        for (int i = 0; i < arr.length; i++)
+            assertEquals(list.get(i).intValue(), arr[i]);
+    }
+}

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/aggregator/functions/IntMedianValueAggregatorFunctionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message