1 16 package org.apache.commons.lang; 17 18 import java.math.BigDecimal ; 19 import java.math.BigInteger ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 25 35 public class NumberUtilsTest extends TestCase { 36 37 public NumberUtilsTest(String name) { 38 super(name); 39 } 40 41 public static Test suite() { 42 TestSuite suite = new TestSuite(NumberUtilsTest.class); 43 suite.setName("NumberUtils Tests"); 44 return suite; 45 } 46 47 49 52 public void testStringToIntString() { 53 assertTrue("stringToInt(String) 1 failed", NumberUtils.stringToInt("12345") == 12345); 54 assertTrue("stringToInt(String) 2 failed", NumberUtils.stringToInt("abc") == 0); 55 } 56 57 60 public void testStringToIntStringI() { 61 assertTrue("stringToInt(String,int) 1 failed", NumberUtils.stringToInt("12345", 5) == 12345); 62 assertTrue("stringToInt(String,int) 2 failed", NumberUtils.stringToInt("1234.5", 5) == 5); 63 } 64 65 public void testCreateNumber() { 66 assertEquals("createNumber(String) 1 failed", new Float ("1234.5"), NumberUtils.createNumber("1234.5")); 68 assertEquals("createNumber(String) 2 failed", new Integer ("12345"), NumberUtils.createNumber("12345")); 69 assertEquals("createNumber(String) 3 failed", new Double ("1234.5"), NumberUtils.createNumber("1234.5D")); 70 assertEquals("createNumber(String) 4 failed", new Float ("1234.5"), NumberUtils.createNumber("1234.5F")); 71 assertEquals("createNumber(String) 5 failed", new Long (Integer.MAX_VALUE + 1L), NumberUtils.createNumber("" + (Integer.MAX_VALUE + 1L))); 72 assertEquals("createNumber(String) 6 failed", new BigInteger (Long.MAX_VALUE + "0"), NumberUtils.createNumber(Long.MAX_VALUE + "0L")); 73 assertEquals("createNumber(String) 7 failed", new Long (12345), NumberUtils.createNumber("12345L")); 74 assertEquals("createNumber(String) 8 failed", new Float ("-1234.5"), NumberUtils.createNumber("-1234.5")); 75 assertEquals("createNumber(String) 9 failed", new Integer ("-12345"), NumberUtils.createNumber("-12345")); 76 assertTrue("createNumber(String) 10 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue()); 77 assertTrue("createNumber(String) 11 failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue()); 78 assertEquals("createNumber(String) 12 failed", new Double ("1.1E200"), NumberUtils.createNumber("1.1E200")); 79 assertEquals("createNumber(String) 13 failed", new Float ("1.1E20"), NumberUtils.createNumber("1.1E20")); 80 assertEquals("createNumber(String) 14 failed", new Double ("-1.1E200"), NumberUtils.createNumber("-1.1E200")); 81 assertEquals("createNumber(String) 15 failed", new Double ("1.1E-200"), NumberUtils.createNumber("1.1E-200")); 82 assertEquals("createNumber(String) 16 failed", new Double ("1.1E-200"), NumberUtils.createNumber("1.1E-200")); 83 84 if(SystemUtils.isJavaVersionAtLeast(1.3f)) { 86 assertEquals("createNumber(String) 15 failed", new BigDecimal ("1.1E-700"), NumberUtils.createNumber("1.1E-700F")); 87 } 88 assertEquals( 89 "createNumber(String) 16 failed", 90 new Long ("10" + Integer.MAX_VALUE), 91 NumberUtils.createNumber("10" + Integer.MAX_VALUE + "L")); 92 assertEquals( 93 "createNumber(String) 17 failed", 94 new Long ("10" + Integer.MAX_VALUE), 95 NumberUtils.createNumber("10" + Integer.MAX_VALUE)); 96 assertEquals( 97 "createNumber(String) 18 failed", 98 new BigInteger ("10" + Long.MAX_VALUE), 99 NumberUtils.createNumber("10" + Long.MAX_VALUE)); 100 101 } 102 103 public void testCreateFloat() { 104 assertEquals("createFloat(String) failed", new Float ("1234.5"), NumberUtils.createFloat("1234.5")); 105 } 106 107 public void testCreateDouble() { 108 assertEquals("createDouble(String) failed", new Double ("1234.5"), NumberUtils.createDouble("1234.5")); 109 } 110 111 public void testCreateInteger() { 112 assertEquals("createInteger(String) failed", new Integer ("12345"), NumberUtils.createInteger("12345")); 113 } 114 115 public void testCreateLong() { 116 assertEquals("createInteger(String) failed", new Long ("12345"), NumberUtils.createLong("12345")); 117 } 118 119 public void testCreateBigInteger() { 120 assertEquals("createBigInteger(String) failed", new BigInteger ("12345"), NumberUtils.createBigInteger("12345")); 121 } 122 123 public void testCreateBigDecimal() { 124 assertEquals("createBigDecimal(String) failed", new BigDecimal ("1234.5"), NumberUtils.createBigDecimal("1234.5")); 125 } 126 127 public void testMinimumLong() { 128 assertEquals("minimum(long,long,long) 1 failed", 12345L, NumberUtils.minimum(12345L, 12345L + 1L, 12345L + 2L)); 129 assertEquals("minimum(long,long,long) 2 failed", 12345L, NumberUtils.minimum(12345L + 1L, 12345L, 12345 + 2L)); 130 assertEquals("minimum(long,long,long) 3 failed", 12345L, NumberUtils.minimum(12345L + 1L, 12345L + 2L, 12345L)); 131 assertEquals("minimum(long,long,long) 4 failed", 12345L, NumberUtils.minimum(12345L + 1L, 12345L, 12345L)); 132 assertEquals("minimum(long,long,long) 5 failed", 12345L, NumberUtils.minimum(12345L, 12345L, 12345L)); 133 134 } 135 136 public void testMinimumInt() { 137 assertEquals("minimum(int,int,int) 1 failed", 12345, NumberUtils.minimum(12345, 12345 + 1, 12345 + 2)); 138 assertEquals("minimum(int,int,int) 2 failed", 12345, NumberUtils.minimum(12345 + 1, 12345, 12345 + 2)); 139 assertEquals("minimum(int,int,int) 3 failed", 12345, NumberUtils.minimum(12345 + 1, 12345 + 2, 12345)); 140 assertEquals("minimum(int,int,int) 4 failed", 12345, NumberUtils.minimum(12345 + 1, 12345, 12345)); 141 assertEquals("minimum(int,int,int) 5 failed", 12345, NumberUtils.minimum(12345, 12345, 12345)); 142 143 } 144 145 public void testMaximumLong() { 146 assertEquals("maximum(long,long,long) 1 failed", 12345L, NumberUtils.maximum(12345L, 12345L - 1L, 12345L - 2L)); 147 assertEquals("maximum(long,long,long) 2 failed", 12345L, NumberUtils.maximum(12345L - 1L, 12345L, 12345L - 2L)); 148 assertEquals("maximum(long,long,long) 3 failed", 12345L, NumberUtils.maximum(12345L - 1L, 12345L - 2L, 12345L)); 149 assertEquals("maximum(long,long,long) 4 failed", 12345L, NumberUtils.maximum(12345L - 1L, 12345L, 12345L)); 150 assertEquals("maximum(long,long,long) 5 failed", 12345L, NumberUtils.maximum(12345L, 12345L, 12345L)); 151 152 } 153 154 public void testMaximumInt() { 155 assertEquals("maximum(int,int,int) 1 failed", 12345, NumberUtils.maximum(12345, 12345 - 1, 12345 - 2)); 156 assertEquals("maximum(int,int,int) 2 failed", 12345, NumberUtils.maximum(12345 - 1, 12345, 12345 - 2)); 157 assertEquals("maximum(int,int,int) 3 failed", 12345, NumberUtils.maximum(12345 - 1, 12345 - 2, 12345)); 158 assertEquals("maximum(int,int,int) 4 failed", 12345, NumberUtils.maximum(12345 - 1, 12345, 12345)); 159 assertEquals("maximum(int,int,int) 5 failed", 12345, NumberUtils.maximum(12345, 12345, 12345)); 160 161 } 162 163 public void testCompareDouble() { 164 assertTrue(NumberUtils.compare(Double.NaN, Double.NaN) == 0); 165 assertTrue(NumberUtils.compare(Double.NaN, Double.POSITIVE_INFINITY) == +1); 166 assertTrue(NumberUtils.compare(Double.NaN, Double.MAX_VALUE) == +1); 167 assertTrue(NumberUtils.compare(Double.NaN, 1.2d) == +1); 168 assertTrue(NumberUtils.compare(Double.NaN, 0.0d) == +1); 169 assertTrue(NumberUtils.compare(Double.NaN, -0.0d) == +1); 170 assertTrue(NumberUtils.compare(Double.NaN, -1.2d) == +1); 171 assertTrue(NumberUtils.compare(Double.NaN, -Double.MAX_VALUE) == +1); 172 assertTrue(NumberUtils.compare(Double.NaN, Double.NEGATIVE_INFINITY) == +1); 173 174 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.NaN) == -1); 175 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) == 0); 176 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.MAX_VALUE) == +1); 177 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 1.2d) == +1); 178 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 0.0d) == +1); 179 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -0.0d) == +1); 180 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -1.2d) == +1); 181 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -Double.MAX_VALUE) == +1); 182 assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY) == +1); 183 184 assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NaN) == -1); 185 assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1); 186 assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.MAX_VALUE) == 0); 187 assertTrue(NumberUtils.compare(Double.MAX_VALUE, 1.2d) == +1); 188 assertTrue(NumberUtils.compare(Double.MAX_VALUE, 0.0d) == +1); 189 assertTrue(NumberUtils.compare(Double.MAX_VALUE, -0.0d) == +1); 190 assertTrue(NumberUtils.compare(Double.MAX_VALUE, -1.2d) == +1); 191 assertTrue(NumberUtils.compare(Double.MAX_VALUE, -Double.MAX_VALUE) == +1); 192 assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1); 193 194 assertTrue(NumberUtils.compare(1.2d, Double.NaN) == -1); 195 assertTrue(NumberUtils.compare(1.2d, Double.POSITIVE_INFINITY) == -1); 196 assertTrue(NumberUtils.compare(1.2d, Double.MAX_VALUE) == -1); 197 assertTrue(NumberUtils.compare(1.2d, 1.2d) == 0); 198 assertTrue(NumberUtils.compare(1.2d, 0.0d) == +1); 199 assertTrue(NumberUtils.compare(1.2d, -0.0d) == +1); 200 assertTrue(NumberUtils.compare(1.2d, -1.2d) == +1); 201 assertTrue(NumberUtils.compare(1.2d, -Double.MAX_VALUE) == +1); 202 assertTrue(NumberUtils.compare(1.2d, Double.NEGATIVE_INFINITY) == +1); 203 204 assertTrue(NumberUtils.compare(0.0d, Double.NaN) == -1); 205 assertTrue(NumberUtils.compare(0.0d, Double.POSITIVE_INFINITY) == -1); 206 assertTrue(NumberUtils.compare(0.0d, Double.MAX_VALUE) == -1); 207 assertTrue(NumberUtils.compare(0.0d, 1.2d) == -1); 208 assertTrue(NumberUtils.compare(0.0d, 0.0d) == 0); 209 assertTrue(NumberUtils.compare(0.0d, -0.0d) == +1); 210 assertTrue(NumberUtils.compare(0.0d, -1.2d) == +1); 211 assertTrue(NumberUtils.compare(0.0d, -Double.MAX_VALUE) == +1); 212 assertTrue(NumberUtils.compare(0.0d, Double.NEGATIVE_INFINITY) == +1); 213 214 assertTrue(NumberUtils.compare(-0.0d, Double.NaN) == -1); 215 assertTrue(NumberUtils.compare(-0.0d, Double.POSITIVE_INFINITY) == -1); 216 assertTrue(NumberUtils.compare(-0.0d, Double.MAX_VALUE) == -1); 217 assertTrue(NumberUtils.compare(-0.0d, 1.2d) == -1); 218 assertTrue(NumberUtils.compare(-0.0d, 0.0d) == -1); 219 assertTrue(NumberUtils.compare(-0.0d, -0.0d) == 0); 220 assertTrue(NumberUtils.compare(-0.0d, -1.2d) == +1); 221 assertTrue(NumberUtils.compare(-0.0d, -Double.MAX_VALUE) == +1); 222 assertTrue(NumberUtils.compare(-0.0d, Double.NEGATIVE_INFINITY) == +1); 223 224 assertTrue(NumberUtils.compare(-1.2d, Double.NaN) == -1); 225 assertTrue(NumberUtils.compare(-1.2d, Double.POSITIVE_INFINITY) == -1); 226 assertTrue(NumberUtils.compare(-1.2d, Double.MAX_VALUE) == -1); 227 assertTrue(NumberUtils.compare(-1.2d, 1.2d) == -1); 228 assertTrue(NumberUtils.compare(-1.2d, 0.0d) == -1); 229 assertTrue(NumberUtils.compare(-1.2d, -0.0d) == -1); 230 assertTrue(NumberUtils.compare(-1.2d, -1.2d) == 0); 231 assertTrue(NumberUtils.compare(-1.2d, -Double.MAX_VALUE) == +1); 232 assertTrue(NumberUtils.compare(-1.2d, Double.NEGATIVE_INFINITY) == +1); 233 234 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NaN) == -1); 235 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1); 236 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.MAX_VALUE) == -1); 237 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 1.2d) == -1); 238 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 0.0d) == -1); 239 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -0.0d) == -1); 240 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -1.2d) == -1); 241 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -Double.MAX_VALUE) == 0); 242 assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1); 243 244 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.NaN) == -1); 245 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY) == -1); 246 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.MAX_VALUE) == -1); 247 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 1.2d) == -1); 248 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 0.0d) == -1); 249 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -0.0d) == -1); 250 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -1.2d) == -1); 251 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE) == -1); 252 assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) == 0); 253 } 254 255 public void testCompareFloat() { 256 assertTrue(NumberUtils.compare(Float.NaN, Float.NaN) == 0); 257 assertTrue(NumberUtils.compare(Float.NaN, Float.POSITIVE_INFINITY) == +1); 258 assertTrue(NumberUtils.compare(Float.NaN, Float.MAX_VALUE) == +1); 259 assertTrue(NumberUtils.compare(Float.NaN, 1.2f) == +1); 260 assertTrue(NumberUtils.compare(Float.NaN, 0.0f) == +1); 261 assertTrue(NumberUtils.compare(Float.NaN, -0.0f) == +1); 262 assertTrue(NumberUtils.compare(Float.NaN, -1.2f) == +1); 263 assertTrue(NumberUtils.compare(Float.NaN, -Float.MAX_VALUE) == +1); 264 assertTrue(NumberUtils.compare(Float.NaN, Float.NEGATIVE_INFINITY) == +1); 265 266 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.NaN) == -1); 267 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) == 0); 268 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.MAX_VALUE) == +1); 269 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 1.2f) == +1); 270 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 0.0f) == +1); 271 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -0.0f) == +1); 272 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -1.2f) == +1); 273 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -Float.MAX_VALUE) == +1); 274 assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY) == +1); 275 276 assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NaN) == -1); 277 assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1); 278 assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.MAX_VALUE) == 0); 279 assertTrue(NumberUtils.compare(Float.MAX_VALUE, 1.2f) == +1); 280 assertTrue(NumberUtils.compare(Float.MAX_VALUE, 0.0f) == +1); 281 assertTrue(NumberUtils.compare(Float.MAX_VALUE, -0.0f) == +1); 282 assertTrue(NumberUtils.compare(Float.MAX_VALUE, -1.2f) == +1); 283 assertTrue(NumberUtils.compare(Float.MAX_VALUE, -Float.MAX_VALUE) == +1); 284 assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1); 285 286 assertTrue(NumberUtils.compare(1.2f, Float.NaN) == -1); 287 assertTrue(NumberUtils.compare(1.2f, Float.POSITIVE_INFINITY) == -1); 288 assertTrue(NumberUtils.compare(1.2f, Float.MAX_VALUE) == -1); 289 assertTrue(NumberUtils.compare(1.2f, 1.2f) == 0); 290 assertTrue(NumberUtils.compare(1.2f, 0.0f) == +1); 291 assertTrue(NumberUtils.compare(1.2f, -0.0f) == +1); 292 assertTrue(NumberUtils.compare(1.2f, -1.2f) == +1); 293 assertTrue(NumberUtils.compare(1.2f, -Float.MAX_VALUE) == +1); 294 assertTrue(NumberUtils.compare(1.2f, Float.NEGATIVE_INFINITY) == +1); 295 296 assertTrue(NumberUtils.compare(0.0f, Float.NaN) == -1); 297 assertTrue(NumberUtils.compare(0.0f, Float.POSITIVE_INFINITY) == -1); 298 assertTrue(NumberUtils.compare(0.0f, Float.MAX_VALUE) == -1); 299 assertTrue(NumberUtils.compare(0.0f, 1.2f) == -1); 300 assertTrue(NumberUtils.compare(0.0f, 0.0f) == 0); 301 assertTrue(NumberUtils.compare(0.0f, -0.0f) == +1); 302 assertTrue(NumberUtils.compare(0.0f, -1.2f) == +1); 303 assertTrue(NumberUtils.compare(0.0f, -Float.MAX_VALUE) == +1); 304 assertTrue(NumberUtils.compare(0.0f, Float.NEGATIVE_INFINITY) == +1); 305 306 assertTrue(NumberUtils.compare(-0.0f, Float.NaN) == -1); 307 assertTrue(NumberUtils.compare(-0.0f, Float.POSITIVE_INFINITY) == -1); 308 assertTrue(NumberUtils.compare(-0.0f, Float.MAX_VALUE) == -1); 309 assertTrue(NumberUtils.compare(-0.0f, 1.2f) == -1); 310 assertTrue(NumberUtils.compare(-0.0f, 0.0f) == -1); 311 assertTrue(NumberUtils.compare(-0.0f, -0.0f) == 0); 312 assertTrue(NumberUtils.compare(-0.0f, -1.2f) == +1); 313 assertTrue(NumberUtils.compare(-0.0f, -Float.MAX_VALUE) == +1); 314 assertTrue(NumberUtils.compare(-0.0f, Float.NEGATIVE_INFINITY) == +1); 315 316 assertTrue(NumberUtils.compare(-1.2f, Float.NaN) == -1); 317 assertTrue(NumberUtils.compare(-1.2f, Float.POSITIVE_INFINITY) == -1); 318 assertTrue(NumberUtils.compare(-1.2f, Float.MAX_VALUE) == -1); 319 assertTrue(NumberUtils.compare(-1.2f, 1.2f) == -1); 320 assertTrue(NumberUtils.compare(-1.2f, 0.0f) == -1); 321 assertTrue(NumberUtils.compare(-1.2f, -0.0f) == -1); 322 assertTrue(NumberUtils.compare(-1.2f, -1.2f) == 0); 323 assertTrue(NumberUtils.compare(-1.2f, -Float.MAX_VALUE) == +1); 324 assertTrue(NumberUtils.compare(-1.2f, Float.NEGATIVE_INFINITY) == +1); 325 326 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NaN) == -1); 327 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1); 328 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.MAX_VALUE) == -1); 329 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 1.2f) == -1); 330 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 0.0f) == -1); 331 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -0.0f) == -1); 332 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -1.2f) == -1); 333 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -Float.MAX_VALUE) == 0); 334 assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1); 335 336 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.NaN) == -1); 337 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY) == -1); 338 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.MAX_VALUE) == -1); 339 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 1.2f) == -1); 340 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 0.0f) == -1); 341 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -0.0f) == -1); 342 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -1.2f) == -1); 343 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE) == -1); 344 assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) == 0); 345 } 346 347 public void testIsDigits() { 348 assertEquals("isDigits(null) failed", false, NumberUtils.isDigits(null)); 349 assertEquals("isDigits('') failed", false, NumberUtils.isDigits("")); 350 assertEquals("isDigits(String) failed", true, NumberUtils.isDigits("12345")); 351 assertEquals("isDigits(String) neg 1 failed", false, NumberUtils.isDigits("1234.5")); 352 assertEquals("isDigits(String) neg 3 failed", false, NumberUtils.isDigits("1ab")); 353 assertEquals("isDigits(String) neg 4 failed", false, NumberUtils.isDigits("abc")); 354 } 355 356 360 public void testIsNumber() { 361 String val = "12345"; 362 assertTrue("isNumber(String) 1 failed", NumberUtils.isNumber(val)); 363 assertTrue("isNumber(String)/createNumber(String) 1 failed", checkCreateNumber(val)); 364 val = "1234.5"; 365 assertTrue("isNumber(String) 2 failed", NumberUtils.isNumber(val)); 366 assertTrue("isNumber(String)/createNumber(String) 2 failed", checkCreateNumber(val)); 367 val = ".12345"; 368 assertTrue("isNumber(String) 3 failed", NumberUtils.isNumber(val)); 369 assertTrue("isNumber(String)/createNumber(String) 3 failed", checkCreateNumber(val)); 370 val = "1234E5"; 371 assertTrue("isNumber(String) 4 failed", NumberUtils.isNumber(val)); 372 assertTrue("isNumber(String)/createNumber(String) 4 failed", checkCreateNumber(val)); 373 val = "1234E+5"; 374 assertTrue("isNumber(String) 5 failed", NumberUtils.isNumber(val)); 375 assertTrue("isNumber(String)/createNumber(String) 5 failed", checkCreateNumber(val)); 376 val = "1234E-5"; 377 assertTrue("isNumber(String) 6 failed", NumberUtils.isNumber(val)); 378 assertTrue("isNumber(String)/createNumber(String) 6 failed", checkCreateNumber(val)); 379 val = "123.4E5"; 380 assertTrue("isNumber(String) 7 failed", NumberUtils.isNumber(val)); 381 assertTrue("isNumber(String)/createNumber(String) 7 failed", checkCreateNumber(val)); 382 val = "-1234"; 383 assertTrue("isNumber(String) 8 failed", NumberUtils.isNumber(val)); 384 assertTrue("isNumber(String)/createNumber(String) 8 failed", checkCreateNumber(val)); 385 val = "-1234.5"; 386 assertTrue("isNumber(String) 9 failed", NumberUtils.isNumber(val)); 387 assertTrue("isNumber(String)/createNumber(String) 9 failed", checkCreateNumber(val)); 388 val = "-.12345"; 389 assertTrue("isNumber(String) 10 failed", NumberUtils.isNumber(val)); 390 assertTrue("isNumber(String)/createNumber(String) 10 failed", checkCreateNumber(val)); 391 val = "-1234E5"; 392 assertTrue("isNumber(String) 11 failed", NumberUtils.isNumber(val)); 393 assertTrue("isNumber(String)/createNumber(String) 11 failed", checkCreateNumber(val)); 394 val = "0"; 395 assertTrue("isNumber(String) 12 failed", NumberUtils.isNumber(val)); 396 assertTrue("isNumber(String)/createNumber(String) 12 failed", checkCreateNumber(val)); 397 val = "-0"; 398 assertTrue("isNumber(String) 13 failed", NumberUtils.isNumber(val)); 399 assertTrue("isNumber(String)/createNumber(String) 13 failed", checkCreateNumber(val)); 400 val = "01234"; 401 assertTrue("isNumber(String) 14 failed", NumberUtils.isNumber(val)); 402 assertTrue("isNumber(String)/createNumber(String) 14 failed", checkCreateNumber(val)); 403 val = "-01234"; 404 assertTrue("isNumber(String) 15 failed", NumberUtils.isNumber(val)); 405 assertTrue("isNumber(String)/createNumber(String) 15 failed", checkCreateNumber(val)); 406 val = "0xABC123"; 407 assertTrue("isNumber(String) 16 failed", NumberUtils.isNumber(val)); 408 assertTrue("isNumber(String)/createNumber(String) 16 failed", checkCreateNumber(val)); 409 val = "0x0"; 410 assertTrue("isNumber(String) 17 failed", NumberUtils.isNumber(val)); 411 assertTrue("isNumber(String)/createNumber(String) 17 failed", checkCreateNumber(val)); 412 val = "123.4E21D"; 413 assertTrue("isNumber(String) 19 failed", NumberUtils.isNumber(val)); 414 assertTrue("isNumber(String)/createNumber(String) 19 failed", checkCreateNumber(val)); 415 val = "-221.23F"; 416 assertTrue("isNumber(String) 20 failed", NumberUtils.isNumber(val)); 417 assertTrue("isNumber(String)/createNumber(String) 20 failed", checkCreateNumber(val)); 418 val = "22338L"; 419 assertTrue("isNumber(String) 21 failed", NumberUtils.isNumber(val)); 420 assertTrue("isNumber(String)/createNumber(String) 21 failed", checkCreateNumber(val)); 421 val = null; 422 assertTrue("isNumber(String) 1 Neg failed", !NumberUtils.isNumber(val)); 423 assertTrue("isNumber(String)/createNumber(String) 1 Neg failed", !checkCreateNumber(val)); 424 val = ""; 425 assertTrue("isNumber(String) 2 Neg failed", !NumberUtils.isNumber(val)); 426 assertTrue("isNumber(String)/createNumber(String) 2 Neg failed", !checkCreateNumber(val)); 427 val = "--2.3"; 428 assertTrue("isNumber(String) 3 Neg failed", !NumberUtils.isNumber(val)); 429 assertTrue("isNumber(String)/createNumber(String) 3 Neg failed", !checkCreateNumber(val)); 430 val = ".12.3"; 431 assertTrue("isNumber(String) 4 Neg failed", !NumberUtils.isNumber(val)); 432 assertTrue("isNumber(String)/createNumber(String) 4 Neg failed", !checkCreateNumber(val)); 433 val = "-123E"; 434 assertTrue("isNumber(String) 5 Neg failed", !NumberUtils.isNumber(val)); 435 assertTrue("isNumber(String)/createNumber(String) 5 Neg failed", !checkCreateNumber(val)); 436 val = "-123E+-212"; 437 assertTrue("isNumber(String) 6 Neg failed", !NumberUtils.isNumber(val)); 438 assertTrue("isNumber(String)/createNumber(String) 6 Neg failed", !checkCreateNumber(val)); 439 val = "-123E2.12"; 440 assertTrue("isNumber(String) 7 Neg failed", !NumberUtils.isNumber(val)); 441 assertTrue("isNumber(String)/createNumber(String) 7 Neg failed", !checkCreateNumber(val)); 442 val = "0xGF"; 443 assertTrue("isNumber(String) 8 Neg failed", !NumberUtils.isNumber(val)); 444 assertTrue("isNumber(String)/createNumber(String) 8 Neg failed", !checkCreateNumber(val)); 445 val = "0xFAE-1"; 446 assertTrue("isNumber(String) 9 Neg failed", !NumberUtils.isNumber(val)); 447 assertTrue("isNumber(String)/createNumber(String) 9 Neg failed", !checkCreateNumber(val)); 448 val = "."; 449 assertTrue("isNumber(String) 10 Neg failed", !NumberUtils.isNumber(val)); 450 assertTrue("isNumber(String)/createNumber(String) 10 Neg failed", !checkCreateNumber(val)); 451 val = "-0ABC123"; 452 assertTrue("isNumber(String) 11 Neg failed", !NumberUtils.isNumber(val)); 453 assertTrue("isNumber(String)/createNumber(String) 11 Neg failed", !checkCreateNumber(val)); 454 val = "123.4E-D"; 455 assertTrue("isNumber(String) 12 Neg failed", !NumberUtils.isNumber(val)); 456 assertTrue("isNumber(String)/createNumber(String) 12 Neg failed", !checkCreateNumber(val)); 457 val = "123.4ED"; 458 assertTrue("isNumber(String) 13 Neg failed", !NumberUtils.isNumber(val)); 459 assertTrue("isNumber(String)/createNumber(String) 13 Neg failed", !checkCreateNumber(val)); 460 val = "1234E5l"; 461 assertTrue("isNumber(String) 14 Neg failed", !NumberUtils.isNumber(val)); 462 assertTrue("isNumber(String)/createNumber(String) 14 Neg failed", !checkCreateNumber(val)); 463 val = "11a"; 464 assertTrue("isNumber(String) 15 Neg failed", !NumberUtils.isNumber(val)); 465 assertTrue("isNumber(String)/createNumber(String) 15 Neg failed", !checkCreateNumber(val)); 466 val = "1a"; 467 assertTrue("isNumber(String) 16 Neg failed", !NumberUtils.isNumber(val)); 468 assertTrue("isNumber(String)/createNumber(String) 16 Neg failed", !checkCreateNumber(val)); 469 val = "a"; 470 assertTrue("isNumber(String) 17 Neg failed", !NumberUtils.isNumber(val)); 471 assertTrue("isNumber(String)/createNumber(String) 17 Neg failed", !checkCreateNumber(val)); 472 val = "11g"; 473 assertTrue("isNumber(String) 18 Neg failed", !NumberUtils.isNumber(val)); 474 assertTrue("isNumber(String)/createNumber(String) 18 Neg failed", !checkCreateNumber(val)); 475 val = "11z"; 476 assertTrue("isNumber(String) 19 Neg failed", !NumberUtils.isNumber(val)); 477 assertTrue("isNumber(String)/createNumber(String) 19 Neg failed", !checkCreateNumber(val)); 478 val = "11def"; 479 assertTrue("isNumber(String) 20 Neg failed", !NumberUtils.isNumber(val)); 480 assertTrue("isNumber(String)/createNumber(String) 20 Neg failed", !checkCreateNumber(val)); 481 val = "11d11"; 482 assertTrue("isNumber(String) 21 Neg failed", !NumberUtils.isNumber(val)); 483 assertTrue("isNumber(String)/createNumber(String) 21 Neg failed", !checkCreateNumber(val)); 484 485 } 486 487 public void testIsNumberInvalidInput() { 488 String val = "0x"; 489 assertEquals("isNumber() with 0x wasn't false", false, NumberUtils.isNumber(val)); 490 val = "0x3x3"; 491 assertEquals("isNumber() with 0x3x3 wasn't false", false, NumberUtils.isNumber(val)); 492 val = "20EE-3"; 493 assertEquals("isNumber() with 20EE-3 wasn't false", false, NumberUtils.isNumber(val)); 494 val = "2435q"; 495 assertEquals("isNumber() with 2435q wasn't false", false, NumberUtils.isNumber(val)); 496 val = "."; 497 assertEquals("isNumber() with . wasn't false", false, NumberUtils.isNumber(val)); 498 499 } 500 501 private boolean checkCreateNumber(String val) { 502 try { 503 Object obj = NumberUtils.createNumber(val); 504 if (obj == null) { 505 return false; 506 } 507 return true; 508 } catch (NumberFormatException e) { 509 return false; 510 } catch (NullPointerException e) { 511 return false; 512 } 513 } 514 515 public void testPublicNoArgConstructor() { 516 try { 517 NumberUtils nu = new NumberUtils(); 518 } catch( Exception e ) { 519 fail( "Error calling public no-arg constructor" ); 520 } 521 } 522 523 } 524 | Popular Tags |