1 /* 2 * Copyright 2013, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.util; 33 34 import junit.framework.Assert; 35 import org.junit.Test; 36 37 public class NumberUtilsTest { 38 @Test isLikelyFloatTest()39 public void isLikelyFloatTest() { 40 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.23f))); 41 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.0f))); 42 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.NaN))); 43 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY))); 44 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.POSITIVE_INFINITY))); 45 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1e-30f))); 46 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1000f))); 47 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1f))); 48 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(-1f))); 49 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(-5f))); 50 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.3333f))); 51 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(4.5f))); 52 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(.1f))); 53 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(50000f))); 54 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.MAX_VALUE))); 55 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits((float)Math.PI))); 56 Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits((float)Math.E))); 57 58 Assert.assertTrue(NumberUtils.isLikelyFloat(2139095039)); 59 60 61 // Float.MIN_VALUE is equivalent to integer value 1 - this should be detected as an integer 62 //Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(Float.MIN_VALUE))); 63 64 // This one doesn't quite work. It has a series of 2 0's, but that is probably not enough to strip off normally 65 //Assert.assertTrue(NumberUtils.isLikelyFloat(Float.floatToRawIntBits(1.33333f))); 66 67 Assert.assertFalse(NumberUtils.isLikelyFloat(0)); 68 Assert.assertFalse(NumberUtils.isLikelyFloat(1)); 69 Assert.assertFalse(NumberUtils.isLikelyFloat(10)); 70 Assert.assertFalse(NumberUtils.isLikelyFloat(100)); 71 Assert.assertFalse(NumberUtils.isLikelyFloat(1000)); 72 Assert.assertFalse(NumberUtils.isLikelyFloat(1024)); 73 Assert.assertFalse(NumberUtils.isLikelyFloat(1234)); 74 Assert.assertFalse(NumberUtils.isLikelyFloat(-5)); 75 Assert.assertFalse(NumberUtils.isLikelyFloat(-13)); 76 Assert.assertFalse(NumberUtils.isLikelyFloat(-123)); 77 Assert.assertFalse(NumberUtils.isLikelyFloat(20000000)); 78 Assert.assertFalse(NumberUtils.isLikelyFloat(2000000000)); 79 Assert.assertFalse(NumberUtils.isLikelyFloat(-2000000000)); 80 Assert.assertFalse(NumberUtils.isLikelyFloat(Integer.MAX_VALUE)); 81 Assert.assertFalse(NumberUtils.isLikelyFloat(Integer.MIN_VALUE)); 82 Assert.assertFalse(NumberUtils.isLikelyFloat(Short.MIN_VALUE)); 83 Assert.assertFalse(NumberUtils.isLikelyFloat(Short.MAX_VALUE)); 84 } 85 86 @Test isLikelyDoubleTest()87 public void isLikelyDoubleTest() { 88 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1.23f))); 89 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1.0f))); 90 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Double.NaN))); 91 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY))); 92 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY))); 93 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1e-30f))); 94 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1000f))); 95 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1f))); 96 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(-1f))); 97 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(-5f))); 98 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1.3333f))); 99 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(1.33333f))); 100 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(4.5f))); 101 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(.1f))); 102 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(50000f))); 103 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Double.MAX_VALUE))); 104 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Math.PI))); 105 Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Math.E))); 106 107 // Double.MIN_VALUE is equivalent to integer value 1 - this should be detected as an integer 108 //Assert.assertTrue(NumberUtils.isLikelyDouble(Double.doubleToRawLongBits(Double.MIN_VALUE))); 109 110 Assert.assertFalse(NumberUtils.isLikelyDouble(0)); 111 Assert.assertFalse(NumberUtils.isLikelyDouble(1)); 112 Assert.assertFalse(NumberUtils.isLikelyDouble(10)); 113 Assert.assertFalse(NumberUtils.isLikelyDouble(100)); 114 Assert.assertFalse(NumberUtils.isLikelyDouble(1000)); 115 Assert.assertFalse(NumberUtils.isLikelyDouble(1024)); 116 Assert.assertFalse(NumberUtils.isLikelyDouble(1234)); 117 Assert.assertFalse(NumberUtils.isLikelyDouble(-5)); 118 Assert.assertFalse(NumberUtils.isLikelyDouble(-13)); 119 Assert.assertFalse(NumberUtils.isLikelyDouble(-123)); 120 Assert.assertFalse(NumberUtils.isLikelyDouble(20000000)); 121 Assert.assertFalse(NumberUtils.isLikelyDouble(2000000000)); 122 Assert.assertFalse(NumberUtils.isLikelyDouble(-2000000000)); 123 Assert.assertFalse(NumberUtils.isLikelyDouble(Integer.MAX_VALUE)); 124 Assert.assertFalse(NumberUtils.isLikelyDouble(Integer.MIN_VALUE)); 125 Assert.assertFalse(NumberUtils.isLikelyDouble(Short.MIN_VALUE)); 126 Assert.assertFalse(NumberUtils.isLikelyDouble(Short.MAX_VALUE)); 127 } 128 } 129