commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1566731 - /commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Date Mon, 10 Feb 2014 20:24:56 GMT
Author: sebb
Date: Mon Feb 10 20:24:56 2014
New Revision: 1566731

URL: http://svn.apache.org/r1566731
Log:
Simplify tests by extracting common code

Modified:
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java?rev=1566731&r1=1566730&r2=1566731&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Mon Feb 10 20:24:56 2014
@@ -1175,146 +1175,63 @@ public class NumberUtilsTest {
      */
     @Test
     public void testIsNumber() {
-        String val = "12345";
-        assertTrue("isNumber(String) 1 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 1 failed", checkCreateNumber(val));
-        val = "1234.5";
-        assertTrue("isNumber(String) 2 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 2 failed", checkCreateNumber(val));
-        val = ".12345";
-        assertTrue("isNumber(String) 3 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 3 failed", checkCreateNumber(val));
-        val = "1234E5";
-        assertTrue("isNumber(String) 4 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 4 failed", checkCreateNumber(val));
-        val = "1234E+5";
-        assertTrue("isNumber(String) 5 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 5 failed", checkCreateNumber(val));
-        val = "1234E-5";
-        assertTrue("isNumber(String) 6 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 6 failed", checkCreateNumber(val));
-        val = "123.4E5";
-        assertTrue("isNumber(String) 7 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 7 failed", checkCreateNumber(val));
-        val = "-1234";
-        assertTrue("isNumber(String) 8 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 8 failed", checkCreateNumber(val));
-        val = "-1234.5";
-        assertTrue("isNumber(String) 9 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 9 failed", checkCreateNumber(val));
-        val = "-.12345";
-        assertTrue("isNumber(String) 10 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 10 failed", checkCreateNumber(val));
-        val = "-1234E5";
-        assertTrue("isNumber(String) 11 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 11 failed", checkCreateNumber(val));
-        val = "0";
-        assertTrue("isNumber(String) 12 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 12 failed", checkCreateNumber(val));
-        val = "-0";
-        assertTrue("isNumber(String) 13 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 13 failed", checkCreateNumber(val));
-        val = "01234";
-        assertTrue("isNumber(String) 14 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 14 failed", checkCreateNumber(val));
-        val = "-01234";
-        assertTrue("isNumber(String) 15 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 15 failed", checkCreateNumber(val));
-        val = "0xABC123";
-        assertTrue("isNumber(String) 16 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 16 failed", checkCreateNumber(val));
-        val = "0x0";
-        assertTrue("isNumber(String) 17 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 17 failed", checkCreateNumber(val));
-        val = "123.4E21D";
-        assertTrue("isNumber(String) 19 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 19 failed", checkCreateNumber(val));
-        val = "-221.23F";
-        assertTrue("isNumber(String) 20 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 20 failed", checkCreateNumber(val));
-        val = "22338L";
-        assertTrue("isNumber(String) 21 failed", NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 21 failed", checkCreateNumber(val));
-        val = null;
-        assertTrue("isNumber(String) 1 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 1 Neg failed", !checkCreateNumber(val));
-        val = "";
-        assertTrue("isNumber(String) 2 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 2 Neg failed", !checkCreateNumber(val));
-        val = "--2.3";
-        assertTrue("isNumber(String) 3 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 3 Neg failed", !checkCreateNumber(val));
-        val = ".12.3";
-        assertTrue("isNumber(String) 4 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 4 Neg failed", !checkCreateNumber(val));
-        val = "-123E";
-        assertTrue("isNumber(String) 5 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 5 Neg failed", !checkCreateNumber(val));
-        val = "-123E+-212";
-        assertTrue("isNumber(String) 6 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 6 Neg failed", !checkCreateNumber(val));
-        val = "-123E2.12";
-        assertTrue("isNumber(String) 7 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 7 Neg failed", !checkCreateNumber(val));
-        val = "0xGF";
-        assertTrue("isNumber(String) 8 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 8 Neg failed", !checkCreateNumber(val));
-        val = "0xFAE-1";
-        assertTrue("isNumber(String) 9 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 9 Neg failed", !checkCreateNumber(val));
-        val = ".";
-        assertTrue("isNumber(String) 10 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 10 Neg failed", !checkCreateNumber(val));
-        val = "-0ABC123";
-        assertTrue("isNumber(String) 11 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 11 Neg failed", !checkCreateNumber(val));
-        val = "123.4E-D";
-        assertTrue("isNumber(String) 12 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 12 Neg failed", !checkCreateNumber(val));
-        val = "123.4ED";
-        assertTrue("isNumber(String) 13 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 13 Neg failed", !checkCreateNumber(val));
-        val = "1234E5l";
-        assertTrue("isNumber(String) 14 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 14 Neg failed", !checkCreateNumber(val));
-        val = "11a";
-        assertTrue("isNumber(String) 15 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 15 Neg failed", !checkCreateNumber(val));
-        val = "1a";
-        assertTrue("isNumber(String) 16 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 16 Neg failed", !checkCreateNumber(val));
-        val = "a";
-        assertTrue("isNumber(String) 17 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 17 Neg failed", !checkCreateNumber(val));
-        val = "11g";
-        assertTrue("isNumber(String) 18 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 18 Neg failed", !checkCreateNumber(val));
-        val = "11z";
-        assertTrue("isNumber(String) 19 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 19 Neg failed", !checkCreateNumber(val));
-        val = "11def";
-        assertTrue("isNumber(String) 20 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 20 Neg failed", !checkCreateNumber(val));
-        val = "11d11";
-        assertTrue("isNumber(String) 21 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 21 Neg failed", !checkCreateNumber(val));
-        val = "11 11";
-        assertTrue("isNumber(String) 22 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 22 Neg failed", !checkCreateNumber(val));
-        val = " 1111";
-        assertTrue("isNumber(String) 23 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 23 Neg failed", !checkCreateNumber(val));
-        val = "1111 ";
-        assertTrue("isNumber(String) 24 Neg failed", !NumberUtils.isNumber(val));
-        assertTrue("isNumber(String)/createNumber(String) 24 Neg failed", !checkCreateNumber(val));
-
-        // LANG-521
-        val = "2.";
-        assertTrue("isNumber(String) LANG-521 failed", NumberUtils.isNumber(val));
-
-        // LANG-664
-        val = "1.1L";
-        assertFalse("isNumber(String) LANG-664 failed", NumberUtils.isNumber(val));
+        compareIsNumberWithCreateNumber("12345", true);
+        compareIsNumberWithCreateNumber("1234.5", true);
+        compareIsNumberWithCreateNumber(".12345", true);
+        compareIsNumberWithCreateNumber("1234E5", true);
+        compareIsNumberWithCreateNumber("1234E+5", true);
+        compareIsNumberWithCreateNumber("1234E-5", true);
+        compareIsNumberWithCreateNumber("123.4E5", true);
+        compareIsNumberWithCreateNumber("-1234", true);
+        compareIsNumberWithCreateNumber("-1234.5", true);
+        compareIsNumberWithCreateNumber("-.12345", true);
+        compareIsNumberWithCreateNumber("-1234E5", true);
+        compareIsNumberWithCreateNumber("0", true);
+        compareIsNumberWithCreateNumber("-0", true);
+        compareIsNumberWithCreateNumber("01234", true);
+        compareIsNumberWithCreateNumber("-01234", true);
+        compareIsNumberWithCreateNumber("-0xABC123", true);
+        compareIsNumberWithCreateNumber("-0x0", true);
+        compareIsNumberWithCreateNumber("123.4E21D", true);
+        compareIsNumberWithCreateNumber("-221.23F", true);
+        compareIsNumberWithCreateNumber("22338L", true);
+
+        compareIsNumberWithCreateNumber(null, false);
+        compareIsNumberWithCreateNumber("", false);
+        compareIsNumberWithCreateNumber("--2.3", false);
+        compareIsNumberWithCreateNumber(".12.3", false);
+        compareIsNumberWithCreateNumber("-123E", false);
+        compareIsNumberWithCreateNumber("-123E+-212", false);
+        compareIsNumberWithCreateNumber("-123E2.12", false);
+        compareIsNumberWithCreateNumber("0xGF", false);
+        compareIsNumberWithCreateNumber("0xFAE-1", false);
+        compareIsNumberWithCreateNumber(".", false);
+        compareIsNumberWithCreateNumber("-0ABC123", false);
+        compareIsNumberWithCreateNumber("123.4E-D", false);
+        compareIsNumberWithCreateNumber("123.4ED", false);
+        compareIsNumberWithCreateNumber("1234E5l", false);
+        compareIsNumberWithCreateNumber("11a", false);
+        compareIsNumberWithCreateNumber("1a", false);
+        compareIsNumberWithCreateNumber("a", false);
+        compareIsNumberWithCreateNumber("11g", false);
+        compareIsNumberWithCreateNumber("11z", false);
+        compareIsNumberWithCreateNumber("11def", false);
+        compareIsNumberWithCreateNumber("11d11", false);
+        compareIsNumberWithCreateNumber("11 11", false);
+        compareIsNumberWithCreateNumber(" 1111", false);
+        compareIsNumberWithCreateNumber("1111 ", false);
+
+        compareIsNumberWithCreateNumber("2.", true); // LANG-521
+        compareIsNumberWithCreateNumber("1.1L", false); // LANG-664
+    }
+
+    private void compareIsNumberWithCreateNumber(final String val, final boolean expected)
{
+        final boolean isValid = NumberUtils.isNumber(val);
+        final boolean canCreate = checkCreateNumber(val);
+        if (isValid == expected && canCreate == expected) {
+            return;
+        }
+        fail("Expecting "+ expected + " for isNumber/createNumber using \"" + val + "\" but
got " + isValid + " and " + canCreate);
     }
 
     private boolean checkCreateNumber(final String val) {



Mime
View raw message