Home | History | Annotate | Download | only in lang
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.java.lang;
     18 
     19 import java.math.BigInteger;
     20 import java.util.Properties;
     21 
     22 public class LongTest extends junit.framework.TestCase {
     23 
     24     public void testSystemProperties() {
     25         Properties originalProperties = System.getProperties();
     26         try {
     27             Properties testProperties = new Properties();
     28             testProperties.put("testIncLong", "string");
     29             System.setProperties(testProperties);
     30             assertNull(Long.getLong("testIncLong"));
     31             assertEquals(new Long(4), Long.getLong("testIncLong", 4L));
     32             assertEquals(new Long(4), Long.getLong("testIncLong", new Long(4)));
     33         } finally {
     34             System.setProperties(originalProperties);
     35         }
     36     }
     37 
     38     public void testCompare() throws Exception {
     39         final long min = Long.MIN_VALUE;
     40         final long zero = 0L;
     41         final long max = Long.MAX_VALUE;
     42         assertTrue(Long.compare(max,  max)  == 0);
     43         assertTrue(Long.compare(min,  min)  == 0);
     44         assertTrue(Long.compare(zero, zero) == 0);
     45         assertTrue(Long.compare(max,  zero) > 0);
     46         assertTrue(Long.compare(max,  min)  > 0);
     47         assertTrue(Long.compare(zero, max)  < 0);
     48         assertTrue(Long.compare(zero, min)  > 0);
     49         assertTrue(Long.compare(min,  zero) < 0);
     50         assertTrue(Long.compare(min,  max)  < 0);
     51     }
     52 
     53     public void testSignum() throws Exception {
     54         assertEquals(0, Long.signum(0));
     55         assertEquals(1, Long.signum(1));
     56         assertEquals(-1, Long.signum(-1));
     57         assertEquals(1, Long.signum(Long.MAX_VALUE));
     58         assertEquals(-1, Long.signum(Long.MIN_VALUE));
     59     }
     60 
     61     /*
     62     public void testParsePositiveLong() throws Exception {
     63         assertEquals(0, Long.parsePositiveLong("0", 10));
     64         assertEquals(473, Long.parsePositiveLong("473", 10));
     65         assertEquals(255, Long.parsePositiveLong("FF", 16));
     66 
     67         try {
     68             Long.parsePositiveLong("-1", 10);
     69             fail();
     70         } catch (NumberFormatException e) {}
     71 
     72         try {
     73             Long.parsePositiveLong("+1", 10);
     74             fail();
     75         } catch (NumberFormatException e) {}
     76 
     77         try {
     78             Long.parsePositiveLong("+0", 16);
     79             fail();
     80         } catch (NumberFormatException e) {}
     81     }
     82     */
     83 
     84     public void testParseLong() throws Exception {
     85         assertEquals(0, Long.parseLong("+0", 10));
     86         assertEquals(473, Long.parseLong("+473", 10));
     87         assertEquals(255, Long.parseLong("+FF", 16));
     88         assertEquals(102, Long.parseLong("+1100110", 2));
     89         assertEquals(Long.MAX_VALUE, Long.parseLong("+" + Long.MAX_VALUE, 10));
     90         assertEquals(411787, Long.parseLong("Kona", 27));
     91         assertEquals(411787, Long.parseLong("+Kona", 27));
     92         assertEquals(-145, Long.parseLong("-145", 10));
     93 
     94         try {
     95             Long.parseLong("--1", 10); // multiple sign chars
     96             fail();
     97         } catch (NumberFormatException expected) {}
     98 
     99         try {
    100             Long.parseLong("++1", 10); // multiple sign chars
    101             fail();
    102         } catch (NumberFormatException expected) {}
    103 
    104         try {
    105             Long.parseLong("Kona", 10); // base to small
    106             fail();
    107         } catch (NumberFormatException expected) {}
    108     }
    109 
    110     public void testDecodeLong() throws Exception {
    111         assertEquals(0, Long.decode("+0").longValue());
    112         assertEquals(473, Long.decode("+473").longValue());
    113         assertEquals(255, Long.decode("+0xFF").longValue());
    114         assertEquals(16, Long.decode("+020").longValue());
    115         assertEquals(Long.MAX_VALUE, Long.decode("+" + Long.MAX_VALUE).longValue());
    116         assertEquals(-73, Long.decode("-73").longValue());
    117         assertEquals(-255, Long.decode("-0xFF").longValue());
    118         assertEquals(255, Long.decode("+#FF").longValue());
    119         assertEquals(-255, Long.decode("-#FF").longValue());
    120 
    121         try {
    122             Long.decode("--1"); // multiple sign chars
    123             fail();
    124         } catch (NumberFormatException expected) {}
    125 
    126         try {
    127             Long.decode("++1"); // multiple sign chars
    128             fail();
    129         } catch (NumberFormatException expected) {}
    130 
    131         try {
    132             Long.decode("+-1"); // multiple sign chars
    133             fail();
    134         } catch (NumberFormatException expected) {}
    135 
    136         try {
    137             Long.decode("Kona"); // invalid number
    138             fail();
    139         } catch (NumberFormatException expected) {}
    140     }
    141 
    142     public void testStaticHashCode() {
    143         assertEquals(Long.valueOf(567L).hashCode(), Long.hashCode(567L));
    144     }
    145 
    146     public void testMax() {
    147         long a = 567L;
    148         long b = 578L;
    149         assertEquals(Math.max(a, b), Long.max(a, b));
    150     }
    151 
    152     public void testMin() {
    153         long a = 567L;
    154         long b = 578L;
    155         assertEquals(Math.min(a, b), Long.min(a, b));
    156     }
    157 
    158     public void testSum() {
    159         long a = 567L;
    160         long b = 578L;
    161         assertEquals(a + b, Long.sum(a, b));
    162     }
    163 
    164     public void testBYTES() {
    165         assertEquals(8, Long.BYTES);
    166     }
    167 
    168     public void testCompareUnsigned() {
    169         long[] ordVals = {0L, 1L, 23L, 456L, 0x7fff_ffff_ffff_ffffL, 0x8000_0000_0000_0000L,
    170                 0xffff_ffff_ffff_ffffL};
    171         for(int i = 0; i < ordVals.length; ++i) {
    172             for(int j = 0; j < ordVals.length; ++j) {
    173                 assertEquals(Integer.compare(i, j),
    174                         Long.compareUnsigned(ordVals[i], ordVals[j]));
    175             }
    176         }
    177     }
    178 
    179     public void testDivideAndRemainderUnsigned() {
    180         BigInteger[] vals = {
    181                 BigInteger.ONE,
    182                 BigInteger.valueOf(23L),
    183                 BigInteger.valueOf(456L),
    184                 BigInteger.valueOf(0x7fff_ffff_ffff_ffffL),
    185                 BigInteger.valueOf(0x7fff_ffff_ffff_ffffL).add(BigInteger.ONE),
    186                 BigInteger.valueOf(2).shiftLeft(63).subtract(BigInteger.ONE)
    187         };
    188 
    189         for(BigInteger dividend : vals) {
    190             for(BigInteger divisor : vals) {
    191                 long uq = Long.divideUnsigned(dividend.longValue(), divisor.longValue());
    192                 long ur = Long.remainderUnsigned(dividend.longValue(), divisor.longValue());
    193                 assertEquals(dividend.divide(divisor).longValue(), uq);
    194                 assertEquals(dividend.remainder(divisor).longValue(), ur);
    195                 assertEquals(dividend.longValue(), uq * divisor.longValue() + ur);
    196             }
    197         }
    198 
    199         for(BigInteger dividend : vals) {
    200             try {
    201                 Long.divideUnsigned(dividend.longValue(), 0);
    202                 fail();
    203             } catch (ArithmeticException expected) { }
    204             try {
    205                 Long.remainderUnsigned(dividend.longValue(), 0);
    206                 fail();
    207             } catch (ArithmeticException expected) { }
    208         }
    209     }
    210 
    211     public void testParseUnsignedLong() {
    212         long[] vals = {0L, 1L, 23L, 456L, 0x7fff_ffff_ffff_ffffL, 0x8000_0000_0000_0000L,
    213                 0xffff_ffff_ffff_ffffL};
    214 
    215         for(long val : vals) {
    216             // Special radices
    217             assertEquals(val, Long.parseUnsignedLong(Long.toBinaryString(val), 2));
    218             assertEquals(val, Long.parseUnsignedLong(Long.toOctalString(val), 8));
    219             assertEquals(val, Long.parseUnsignedLong(Long.toUnsignedString(val)));
    220             assertEquals(val, Long.parseUnsignedLong(Long.toHexString(val), 16));
    221 
    222             for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; ++radix) {
    223                 assertEquals(val,
    224                         Long.parseUnsignedLong(Long.toUnsignedString(val, radix), radix));
    225             }
    226         }
    227 
    228         try {
    229             Long.parseUnsignedLong("-1");
    230             fail();
    231         } catch (NumberFormatException expected) { }
    232         try {
    233             Long.parseUnsignedLong("123", 2);
    234             fail();
    235         } catch (NumberFormatException expected) { }
    236         try {
    237             Long.parseUnsignedLong(null, 2);
    238             fail();
    239         } catch (NumberFormatException expected) { }
    240         try {
    241             Long.parseUnsignedLong("0", Character.MAX_RADIX + 1);
    242             fail();
    243         } catch (NumberFormatException expected) { }
    244         try {
    245             Long.parseUnsignedLong("0", Character.MIN_RADIX - 1);
    246             fail();
    247         } catch (NumberFormatException expected) { }
    248     }
    249 
    250     public void testToUnsignedString() {
    251         long[] vals = {0L, 1L, 23L, 456L, 0x7fff_ffff_ffff_ffffL, 0x8000_0000_0000_0000L,
    252                 0xffff_ffff_ffff_ffffL};
    253 
    254         for(long val : vals) {
    255             // Special radices
    256             assertTrue(Long.toUnsignedString(val, 2).equals(Long.toBinaryString(val)));
    257             assertTrue(Long.toUnsignedString(val, 8).equals(Long.toOctalString(val)));
    258             assertTrue(Long.toUnsignedString(val, 10).equals(Long.toUnsignedString(val)));
    259             assertTrue(Long.toUnsignedString(val, 16).equals(Long.toHexString(val)));
    260 
    261             for(int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; ++radix) {
    262                 int upper = (int) (val >>> 32), lower = (int) val;
    263                 BigInteger b = (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
    264                         add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
    265 
    266                 assertTrue(Long.toUnsignedString(val, radix).equals(b.toString(radix)));
    267             }
    268 
    269             // Behavior is not defined by Java API specification if the radix falls outside of valid
    270             // range, thus we don't test for such cases.
    271         }
    272     }
    273 }
    274