Home | History | Annotate | Download | only in primitives
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     11  * express or implied. See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 
     15 package com.google.common.primitives;
     16 
     17 import java.util.Random;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import com.google.common.annotations.GwtCompatible;
     22 import com.google.common.annotations.GwtIncompatible;
     23 import com.google.common.testing.NullPointerTester;
     24 
     25 /**
     26  * Tests for UnsignedInts
     27  *
     28  * @author Louis Wasserman
     29  */
     30 @GwtCompatible(emulated = true)
     31 public class UnsignedIntsTest extends TestCase {
     32   private static final long[] UNSIGNED_INTS = {
     33       0L,
     34       1L,
     35       2L,
     36       3L,
     37       0x12345678L,
     38       0x5a4316b8L,
     39       0x6cf78a4bL,
     40       0xff1a618bL,
     41       0xfffffffdL,
     42       0xfffffffeL,
     43       0xffffffffL};
     44 
     45   public void testToLong() {
     46     for (long a : UNSIGNED_INTS) {
     47       assertEquals(a, UnsignedInts.toLong((int) a));
     48     }
     49   }
     50 
     51   public void testCompare() {
     52     for (long a : UNSIGNED_INTS) {
     53       for (long b : UNSIGNED_INTS) {
     54         int cmpAsLongs = Longs.compare(a, b);
     55         int cmpAsUInt = UnsignedInts.compare((int) a, (int) b);
     56         assertEquals(Integer.signum(cmpAsLongs), Integer.signum(cmpAsUInt));
     57       }
     58     }
     59   }
     60 
     61   public void testDivide() {
     62     for (long a : UNSIGNED_INTS) {
     63       for (long b : UNSIGNED_INTS) {
     64         try {
     65           assertEquals((int) (a / b), UnsignedInts.divide((int) a, (int) b));
     66           assertFalse(b == 0);
     67         } catch (ArithmeticException e) {
     68           assertEquals(0, b);
     69         }
     70       }
     71     }
     72   }
     73 
     74   public void testRemainder() {
     75     for (long a : UNSIGNED_INTS) {
     76       for (long b : UNSIGNED_INTS) {
     77         try {
     78           assertEquals((int) (a % b), UnsignedInts.remainder((int) a, (int) b));
     79           assertFalse(b == 0);
     80         } catch (ArithmeticException e) {
     81           assertEquals(0, b);
     82         }
     83       }
     84     }
     85   }
     86 
     87   @GwtIncompatible("Too slow in GWT (~3min fully optimized)")
     88   public void testDivideRemainderEuclideanProperty() {
     89     // Use a seed so that the test is deterministic:
     90     Random r = new Random(0L);
     91     for (int i = 0; i < 1000000; i++) {
     92       int dividend = r.nextInt();
     93       int divisor = r.nextInt();
     94       // Test that the Euclidean property is preserved:
     95       assertTrue(dividend
     96           - (divisor * UnsignedInts.divide(dividend, divisor) + UnsignedInts.remainder(dividend,
     97               divisor)) == 0);
     98     }
     99   }
    100 
    101   public void testParseInt() {
    102     try {
    103       for (long a : UNSIGNED_INTS) {
    104         assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a)));
    105       }
    106     } catch (NumberFormatException e) {
    107       fail(e.getMessage());
    108     }
    109 
    110     try {
    111       UnsignedInts.parseUnsignedInt(Long.toString(1L << 32));
    112       fail("Expected NumberFormatException");
    113     } catch (NumberFormatException expected) {}
    114   }
    115 
    116   public void testParseLongWithRadix() throws NumberFormatException {
    117     for (long a : UNSIGNED_INTS) {
    118       for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
    119         assertEquals((int) a, UnsignedInts.parseUnsignedInt(Long.toString(a, radix), radix));
    120       }
    121     }
    122 
    123     // loops through all legal radix values.
    124     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
    125       // tests can successfully parse a number string with this radix.
    126       String maxAsString = Long.toString((1L << 32) - 1, radix);
    127       assertEquals(-1, UnsignedInts.parseUnsignedInt(maxAsString, radix));
    128 
    129       try {
    130         // tests that we get exception whre an overflow would occur.
    131         long overflow = 1L << 32;
    132         String overflowAsString = Long.toString(overflow, radix);
    133         UnsignedInts.parseUnsignedInt(overflowAsString, radix);
    134         fail();
    135       } catch (NumberFormatException expected) {}
    136     }
    137   }
    138 
    139   public void testParseLongThrowsExceptionForInvalidRadix() {
    140     // Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX,
    141     // inclusive.
    142     try {
    143       UnsignedInts.parseUnsignedInt("0", Character.MIN_RADIX - 1);
    144       fail();
    145     } catch (NumberFormatException expected) {}
    146 
    147     try {
    148       UnsignedInts.parseUnsignedInt("0", Character.MAX_RADIX + 1);
    149       fail();
    150     } catch (NumberFormatException expected) {}
    151 
    152     // The radix is used as an array index, so try a negative value.
    153     try {
    154       UnsignedInts.parseUnsignedInt("0", -1);
    155       fail();
    156     } catch (NumberFormatException expected) {}
    157   }
    158 
    159   public void testToString() {
    160     int[] bases = {2, 5, 7, 8, 10, 16};
    161     for (long a : UNSIGNED_INTS) {
    162       for (int base : bases) {
    163         assertEquals(UnsignedInts.toString((int) a, base), Long.toString(a, base));
    164       }
    165     }
    166   }
    167 
    168   @GwtIncompatible("NullPointerTester")
    169   public void testNulls() throws Exception {
    170     NullPointerTester tester = new NullPointerTester();
    171     tester.setDefault(int[].class, new int[0]);
    172     tester.testAllPublicStaticMethods(UnsignedInts.class);
    173   }
    174 }
    175