You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/06/28 20:42:04 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang/math NumberUtilsTest.java

scolebourne    2003/06/28 11:42:04

  Modified:    lang/src/java/org/apache/commons/lang/math NumberUtils.java
               lang/src/test/org/apache/commons/lang/math
                        NumberUtilsTest.java
  Log:
  Add min/max by array functions
  bug 21068, from Matthew Hawthorne
  
  Revision  Changes    Path
  1.2       +282 -3    jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java
  
  Index: NumberUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/NumberUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NumberUtils.java	24 Jun 2003 21:14:50 -0000	1.1
  +++ NumberUtils.java	28 Jun 2003 18:42:03 -0000	1.2
  @@ -53,8 +53,10 @@
    */
   package org.apache.commons.lang.math;
   
  -import java.math.BigInteger;
   import java.math.BigDecimal;
  +import java.math.BigInteger;
  +
  +import org.apache.commons.lang.NullArgumentException;
   
   /**
    * <p>Provides extra functionality for Java Number classes.</p>
  @@ -65,7 +67,8 @@
    * @author <a href="mailto:steve.downey@netfolio.com">Steve Downey</a>
    * @author Eric Pugh
    * @author Phil Steitz
  - * @since 1.0
  + * @author Matthew Hawthorne
  + * @since 2.0
    * @version $Id$
    */
   public final class NumberUtils {
  @@ -437,6 +440,281 @@
           return bd;
       }
   
  +    // Min in array
  +    //--------------------------------------------------------------------
  +    /**
  +     * Returns the minimum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static long min(long[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns min
  +        long min = array[0];
  +        for (int i = 1; i < array.length; i++) {
  +            if (array[i] < min) {
  +                min = array[i];
  +            }
  +        }
  +    
  +        return min;
  +    }
  +
  +    /**
  +     * Returns the minimum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static int min(int[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns min
  +        int min = array[0];
  +        for (int j = 1; j < array.length; j++) {
  +            if (array[j] < min) {
  +                min = array[j];
  +            }
  +        }
  +    
  +        return min;
  +    }
  +
  +    /**
  +     * Returns the minimum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static short min(short[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns min
  +        short min = array[0];
  +        for (int i = 1; i < array.length; i++) {
  +            if (array[i] < min) {
  +                min = array[i];
  +            }
  +        }
  +    
  +        return min;
  +    }
  +
  +     /**
  +     * Returns the minimum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static double min(double[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns min
  +        double min = array[0];
  +        for (int i = 1; i < array.length; i++) {
  +            if (array[i] < min) {
  +                min = array[i];
  +            }
  +        }
  +    
  +        return min;
  +    }
  +
  +    /**
  +     * Returns the minimum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static float min(float[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns min
  +        float min = array[0];
  +        for (int i = 1; i < array.length; i++) {
  +            if (array[i] < min) {
  +                min = array[i];
  +            }
  +        }
  +    
  +        return min;
  +    }
  +
  +    // Max in array
  +    //--------------------------------------------------------------------
  +    /**
  +     * Returns the maximum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static long max(long[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +
  +        // Finds and returns max
  +        long max = array[0];
  +        for (int j = 1; j < array.length; j++) {
  +            if (array[j] > max) {
  +                max = array[j];
  +            }
  +        }
  +
  +        return max;
  +    }
  +
  +    /**
  +     * Returns the maximum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static int max(int[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns max
  +        int max = array[0];
  +        for (int j = 1; j < array.length; j++) {
  +            if (array[j] > max) {
  +                max = array[j];
  +            }
  +        }
  +    
  +        return max;
  +    }
  +
  +    /**
  +     * Returns the maximum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static short max(short[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns max
  +        short max = array[0];
  +        for (int i = 1; i < array.length; i++) {
  +            if (array[i] > max) {
  +                max = array[i];
  +            }
  +        }
  +    
  +        return max;
  +    }
  +
  +    /**
  +     * Returns the maximum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static double max(double[] array) {
  +        // Validates input
  +        if (array== null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +    
  +        // Finds and returns max
  +        double max = array[0];
  +        for (int j = 1; j < array.length; j++) {
  +            if (array[j] > max) {
  +                max = array[j];
  +            }
  +        }
  +    
  +        return max;
  +    }
  +
  +    /**
  +     * Returns the maximum value in an array.
  +     * 
  +     * @param array  an array
  +     * @return the minimum value in the array
  +     * @throws NullArgumentException if <code>array</code> is <code>null</code>
  +     * @throws IllegalArgumentException if <code>array</code> is empty
  +     */
  +    public static float max(float[] array) {
  +        // Validates input
  +        if (array == null) {
  +            throw new NullArgumentException("Array");
  +        } else if (array.length == 0) {
  +            throw new IllegalArgumentException("Array cannot be empty.");
  +        }
  +
  +        // Finds and returns max
  +        float max = array[0];
  +        for (int j = 1; j < array.length; j++) {
  +            if (array[j] > max) {
  +                max = array[j];
  +            }
  +        }
  +
  +        return max;
  +    }
  +     
  +    // 3 param min
       //-----------------------------------------------------------------------
       /**
        * <p>Gets the minimum of three <code>long</code> values.</p>
  @@ -538,6 +816,7 @@
           return Math.min(Math.min(a, b), c);
       }
   
  +    // 3 param max
       //-----------------------------------------------------------------------
       /**
        * <p>Gets the maximum of three <code>long</code> values.</p>
  
  
  
  1.2       +301 -1    jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
  
  Index: NumberUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/math/NumberUtilsTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NumberUtilsTest.java	24 Jun 2003 21:14:51 -0000	1.1
  +++ NumberUtilsTest.java	28 Jun 2003 18:42:04 -0000	1.2
  @@ -59,6 +59,7 @@
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  +import junit.textui.TestRunner;
   
   import org.apache.commons.lang.SystemUtils;
   
  @@ -70,6 +71,7 @@
    * @author Eric Pugh
    * @author Phil Steitz
    * @author Stephen Colebourne
  + * @author Matthew Hawthorne
    * @version $Id$
    */
   public class NumberUtilsTest extends TestCase {
  @@ -78,6 +80,10 @@
           super(name);
       }
   
  +    public static void main(String[] args) {
  +        TestRunner.run(suite());
  +    }
  +    
       public static Test suite() {
           TestSuite suite = new TestSuite(NumberUtilsTest.class);
           suite.setName("NumberUtils Tests");
  @@ -160,6 +166,300 @@
   
       public void testCreateBigDecimal() {
           assertEquals("createBigDecimal(String) failed", new BigDecimal("1234.5"), NumberUtils.createBigDecimal("1234.5"));
  +    }
  +
  +    // min/max tests
  +    // ----------------------------------------------------------------------
  +    public void testMinLong() {
  +        final long[] l = null;
  +        try {
  +            NumberUtils.min(l);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.min(new long[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "min(long[]) failed for array length 1",
  +            5,
  +            NumberUtils.min(new long[] { 5 }));
  +
  +        assertEquals(
  +            "min(long[]) failed for array length 2",
  +            6,
  +            NumberUtils.min(new long[] { 6, 9 }));
  +
  +        assertEquals(
  +            "min(long[]) failed for array length 5",
  +            -10,
  +            NumberUtils.min(new long[] { -10, -5, 0, 5, 10 }));
  +    }
  +
  +    public void testMinInt() {
  +        final int[] i = null;
  +        try {
  +            NumberUtils.min(i);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.min(new int[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "min(int[]) failed for array length 1",
  +            5,
  +            NumberUtils.min(new int[] { 5 }));
  +
  +        assertEquals(
  +            "min(int[]) failed for array length 2",
  +            6,
  +            NumberUtils.min(new int[] { 6, 9 }));
  +
  +        assertEquals(
  +            "min(int[]) failed for array length 5",
  +            -10,
  +            NumberUtils.min(new int[] { -10, -5, 0, 5, 10 }));
  +    }
  +
  +    public void testMinShort() {
  +        final short[] s = null;
  +        try {
  +            NumberUtils.min(s);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.min(new short[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "min(short[]) failed for array length 1",
  +            5,
  +            NumberUtils.min(new short[] { 5 }));
  +
  +        assertEquals(
  +            "min(short[]) failed for array length 2",
  +            6,
  +            NumberUtils.min(new short[] { 6, 9 }));
  +
  +        assertEquals(
  +            "min(short[]) failed for array length 5",
  +            -10,
  +            NumberUtils.min(new short[] { -10, -5, 0, 5, 10 }));
  +    }
  +
  +    public void testMinDouble() {
  +        final double[] d = null;
  +        try {
  +            NumberUtils.min(d);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.min(new double[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "min(double[]) failed for array length 1",
  +            5.12,
  +            NumberUtils.min(new double[] { 5.12 }),
  +            0);
  +
  +        assertEquals(
  +            "min(double[]) failed for array length 2",
  +            6.23,
  +            NumberUtils.min(new double[] { 6.23, 9.34 }),
  +            0);
  +
  +        assertEquals(
  +            "min(double[]) failed for array length 5",
  +            -10.45,
  +            NumberUtils.min(new double[] { -10.45, -5.56, 0, 5.67, 10.78 }),
  +            0);
  +    }
  +
  +    public void testMinFloat() {
  +        final float[] f = null;
  +        try {
  +            NumberUtils.min(f);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.min(new float[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "min(float[]) failed for array length 1",
  +            5.9f,
  +            NumberUtils.min(new float[] { 5.9f }),
  +            0);
  +
  +        assertEquals(
  +            "min(float[]) failed for array length 2",
  +            6.8f,
  +            NumberUtils.min(new float[] { 6.8f, 9.7f }),
  +            0);
  +
  +        assertEquals(
  +            "min(float[]) failed for array length 5",
  +            -10.6f,
  +            NumberUtils.min(new float[] { -10.6f, -5.5f, 0, 5.4f, 10.3f }),
  +            0);
  +    }
  +
  +    public void testMaxLong() {
  +        final long[] l = null;
  +        try {
  +            NumberUtils.max(l);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.max(new long[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "max(long[]) failed for array length 1",
  +            5,
  +            NumberUtils.max(new long[] { 5 }));
  +
  +        assertEquals(
  +            "max(long[]) failed for array length 2",
  +            9,
  +            NumberUtils.max(new long[] { 6, 9 }));
  +
  +        assertEquals(
  +            "max(long[]) failed for array length 5",
  +            10,
  +            NumberUtils.max(new long[] { -10, -5, 0, 5, 10 }));
  +    }
  +
  +    public void testMaxInt() {
  +        final int[] i = null;
  +        try {
  +            NumberUtils.max(i);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.max(new int[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "max(int[]) failed for array length 1",
  +            5,
  +            NumberUtils.max(new int[] { 5 }));
  +
  +        assertEquals(
  +            "max(int[]) failed for array length 2",
  +            9,
  +            NumberUtils.max(new int[] { 6, 9 }));
  +
  +        assertEquals(
  +            "max(int[]) failed for array length 5",
  +            10,
  +            NumberUtils.max(new int[] { -10, -5, 0, 5, 10 }));
  +    }
  +
  +    public void testMaxShort() {
  +        final short[] s = null;
  +        try {
  +            NumberUtils.max(s);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.max(new short[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "max(short[]) failed for array length 1",
  +            5,
  +            NumberUtils.max(new short[] { 5 }));
  +
  +        assertEquals(
  +            "max(short[]) failed for array length 2",
  +            9,
  +            NumberUtils.max(new short[] { 6, 9 }));
  +
  +        assertEquals(
  +            "max(short[]) failed for array length 5",
  +            10,
  +            NumberUtils.max(new short[] { -10, -5, 0, 5, 10 }));
  +    }
  +
  +    public void testMaxDouble() {
  +        final double[] d = null;
  +        try {
  +            NumberUtils.max(d);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.max(new double[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "max(double[]) failed for array length 1",
  +            5.1f,
  +            NumberUtils.max(new double[] { 5.1f }),
  +            0);
  +
  +        assertEquals(
  +            "max(double[]) failed for array length 2",
  +            9.2f,
  +            NumberUtils.max(new double[] { 6.3f, 9.2f }),
  +            0);
  +
  +        assertEquals(
  +            "max(double[]) failed for float length 5",
  +            10.4f,
  +            NumberUtils.max(new double[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
  +            0);
  +    }
  + 
  +    public void testMaxFloat() {
  +        final float[] f = null;
  +        try {
  +            NumberUtils.max(f);
  +            fail("No exception was thrown for null input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        try {
  +            NumberUtils.max(new float[0]);
  +            fail("No exception was thrown for empty input.");
  +        } catch (IllegalArgumentException ex) {}
  +
  +        assertEquals(
  +            "max(float[]) failed for array length 1",
  +            5.1f,
  +            NumberUtils.max(new float[] { 5.1f }),
  +            0);
  +
  +        assertEquals(
  +            "max(float[]) failed for array length 2",
  +            9.2f,
  +            NumberUtils.max(new float[] { 6.3f, 9.2f }),
  +            0);
  +
  +        assertEquals(
  +            "max(float[]) failed for float length 5",
  +            10.4f,
  +            NumberUtils.max(new float[] { -10.5f, -5.6f, 0, 5.7f, 10.4f }),
  +            0);
       }
   
       public void testMinimumLong() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org