rwaldhoff 2003/11/30 23:30:24 Modified: functor/src/test/org/apache/commons/functor/example/kata TestAll.java Added: functor/src/test/org/apache/commons/functor/example/kata/two FunctoredChop.java BinaryChop.java BaseBinaryChop.java TestBinaryChop.java TestAll.java Log: add kata two examples Revision Changes Path 1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/FunctoredChop.java Index: FunctoredChop.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/FunctoredChop.java,v 1.1 2003/12/01 07:30:24 rwaldhoff Exp $ * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.functor.example.kata.two; import java.util.List; import org.apache.commons.functor.BinaryFunction; public class FunctoredChop extends BaseBinaryChop { public FunctoredChop(BinaryFunction function) { this.function = function; } public int find(Object seeking, List list) { return ((Number)function.evaluate(seeking,list)).intValue(); } private BinaryFunction function; } 1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/BinaryChop.java Index: BinaryChop.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/BinaryChop.java,v 1.1 2003/12/01 07:30:24 rwaldhoff Exp $ * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.functor.example.kata.two; import java.util.List; /** * See http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v * for more information on this Kata. * * @version $Revision: 1.1 $ $Date: 2003/12/01 07:30:24 $ * @author Rodney Waldhoff */ public interface BinaryChop { int find(int seeking, int[] in); int find(Object seeking, Object[] in); int find(Object seeking, List in); } 1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/BaseBinaryChop.java Index: BaseBinaryChop.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/BaseBinaryChop.java,v 1.1 2003/12/01 07:30:24 rwaldhoff Exp $ * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.functor.example.kata.two; import java.util.Arrays; import java.util.List; /** * See http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v * for more information on this Kata. * * @version $Revision: 1.1 $ $Date: 2003/12/01 07:30:24 $ * @author Rodney Waldhoff */ public abstract class BaseBinaryChop implements BinaryChop { public int find(int seeking, int[] in) { Object[] In = new Object[in.length]; for(int i=0;i<in.length;i++) { In[i] = new Integer(in[i]); } return find(new Integer(seeking), In); } public int find(Object seeking, Object[] in) { return find(seeking, Arrays.asList(in)); } public abstract int find(Object seeking, List in); } 1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/TestBinaryChop.java Index: TestBinaryChop.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/TestBinaryChop.java,v 1.1 2003/12/01 07:30:24 rwaldhoff Exp $ * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.functor.example.kata.two; import java.util.List; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.commons.functor.util.BinarySearch; /** * See http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v * for more information on this Kata. * * @version $Revision: 1.1 $ $Date: 2003/12/01 07:30:24 $ * @author Rodney Waldhoff */ public class TestBinaryChop extends TestCase { public TestBinaryChop(String testName) { super(testName); } public static Test suite() { return new TestSuite(TestBinaryChop.class); } public void setUp() throws Exception { super.setUp(); } public void tearDown() throws Exception { super.tearDown(); } private void chopTest(BinaryChop chopper) { assertEquals(-1, chopper.find(3, new int[0])); assertEquals(-1, chopper.find(3, new int[] { 1 })); assertEquals(0, chopper.find(1, new int[] { 1 })); assertEquals(0, chopper.find(1, new int[] { 1, 3, 5 })); assertEquals(1, chopper.find(3, new int[] { 1, 3, 5 })); assertEquals(2, chopper.find(5, new int[] { 1, 3, 5 })); assertEquals(-1, chopper.find(0, new int[] { 1, 3, 5 })); assertEquals(-1, chopper.find(2, new int[] { 1, 3, 5 })); assertEquals(-1, chopper.find(4, new int[] { 1, 3, 5 })); assertEquals(-1, chopper.find(6, new int[] { 1, 3, 5 })); assertEquals(0, chopper.find(1, new int[] { 1, 3, 5, 7 })); assertEquals(1, chopper.find(3, new int[] { 1, 3, 5, 7 })); assertEquals(2, chopper.find(5, new int[] { 1, 3, 5, 7 })); assertEquals(3, chopper.find(7, new int[] { 1, 3, 5, 7 })); assertEquals(-1, chopper.find(0, new int[] { 1, 3, 5, 7 })); assertEquals(-1, chopper.find(2, new int[] { 1, 3, 5, 7 })); assertEquals(-1, chopper.find(4, new int[] { 1, 3, 5, 7 })); assertEquals(-1, chopper.find(6, new int[] { 1, 3, 5, 7 })); assertEquals(-1, chopper.find(8, new int[] { 1, 3, 5, 7 })); } public void testIterative() { chopTest( new BaseBinaryChop() { public int find(Object seeking, List list) { int high = list.size(); int low = 0; int cur = 0; while(low < high) { cur = (high+low)/2; switch(((Comparable)(list.get(cur))).compareTo(seeking)) { case 0: // equal return cur; case 1: high = cur; break; case -1: if(low == cur) { cur++; } low = cur; break; } } return -1; } }); } public void testRecursive() { chopTest( new BaseBinaryChop() { public int find(Object seeking, List list) { if(list.size() == 0) { return -1; } else { int pivot = list.size()/2; int offset = 0; int comp = ((Comparable)(list.get(pivot))).compareTo(seeking); switch(comp) { case 0: return pivot; case -1: offset = find(seeking,list.subList(Math.max(pivot,1),list.size())); break; case 1: offset = find(seeking,list.subList(0,pivot)); break; } return -1 == offset ? -1 : (comp == 1) ? offset : pivot+offset; } } }); } public void testTailRecursion() { chopTest( new BaseBinaryChop() { public int find(Object seeking, List list) { return BinarySearch.execute(list,(Comparable)seeking); } }); } } 1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/TestAll.java Index: TestAll.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/two/TestAll.java,v 1.1 2003/12/01 07:30:24 rwaldhoff Exp $ * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.functor.example.kata.two; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * See http://pragprog.com/pragdave/Practices/Kata/KataTwo.rdoc,v * for more information on this Kata. * * @version $Revision: 1.1 $ $Date: 2003/12/01 07:30:24 $ * @author Rodney Waldhoff */ public class TestAll extends TestCase { public TestAll(String testName) { super(testName); } public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(TestBinaryChop.suite()); return suite; } } 1.2 +3 -4 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/TestAll.java Index: TestAll.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/TestAll.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestAll.java 1 Dec 2003 05:29:09 -0000 1.1 +++ TestAll.java 1 Dec 2003 07:30:24 -0000 1.2 @@ -71,9 +71,8 @@ public static Test suite() { TestSuite suite = new TestSuite(); - suite.addTest(org.apache.commons.functor.example.kata.one.TestAll.suite()); - + suite.addTest(org.apache.commons.functor.example.kata.two.TestAll.suite()); return suite; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]