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 junit.framework.TestCase;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 import com.google.common.annotations.GwtIncompatible;
     21 import com.google.common.collect.ImmutableSet;
     22 import com.google.common.testing.EqualsTester;
     23 import com.google.common.testing.NullPointerTester;
     24 import com.google.common.testing.SerializableTester;
     25 
     26 /**
     27  * Tests for {@code UnsignedInteger}.
     28  *
     29  * @author Louis Wasserman
     30  */
     31 @GwtCompatible(emulated = true)
     32 public class UnsignedIntegerTest extends TestCase {
     33   private static final ImmutableSet<Integer> TEST_INTS;
     34 
     35   private static int force32(int value) {
     36     // GWT doesn't overflow values to make them 32-bit, so we need to force it.
     37     return value & 0xffffffff;
     38   }
     39 
     40   static {
     41     ImmutableSet.Builder<Integer> testIntsBuilder = ImmutableSet.builder();
     42     for (int i = -3; i <= 3; i++) {
     43       testIntsBuilder.add(i).add(-i).add(force32(Integer.MIN_VALUE + i))
     44           .add(force32(Integer.MAX_VALUE + i));
     45     }
     46     TEST_INTS = testIntsBuilder.build();
     47   }
     48 
     49   public void testAsUnsignedAndIntValueAreInverses() {
     50     for (int value : TEST_INTS) {
     51       assertEquals(UnsignedInts.toString(value), value, UnsignedInteger.asUnsigned(value)
     52           .intValue());
     53     }
     54   }
     55 
     56   public void testAsUnsignedLongValue() {
     57     for (int value : TEST_INTS) {
     58       long expected = value & 0xffffffffL;
     59       assertEquals(UnsignedInts.toString(value), expected, UnsignedInteger.asUnsigned(value)
     60           .longValue());
     61     }
     62   }
     63 
     64   public void testToString() {
     65     for (int value : TEST_INTS) {
     66       UnsignedInteger unsignedValue = UnsignedInteger.asUnsigned(value);
     67       assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString());
     68     }
     69   }
     70 
     71   @GwtIncompatible("too slow")
     72   public void testToStringRadix() {
     73     for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
     74       for (int l : TEST_INTS) {
     75         UnsignedInteger value = UnsignedInteger.asUnsigned(l);
     76         assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
     77       }
     78     }
     79   }
     80 
     81   public void testToStringRadixQuick() {
     82     int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36};
     83     for (int radix : radices) {
     84       for (int l : TEST_INTS) {
     85         UnsignedInteger value = UnsignedInteger.asUnsigned(l);
     86         assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix));
     87       }
     88     }
     89   }
     90 
     91   public void testFloatValue() {
     92     for (int value : TEST_INTS) {
     93       UnsignedInteger unsignedValue = UnsignedInteger.asUnsigned(value);
     94       assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue());
     95     }
     96   }
     97 
     98   public void testDoubleValue() {
     99     for (int value : TEST_INTS) {
    100       UnsignedInteger unsignedValue = UnsignedInteger.asUnsigned(value);
    101       assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue());
    102     }
    103   }
    104 
    105   public void testAdd() {
    106     for (int a : TEST_INTS) {
    107       for (int b : TEST_INTS) {
    108         UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    109         UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b);
    110         int expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).intValue();
    111         UnsignedInteger unsignedSum = aUnsigned.add(bUnsigned);
    112         assertEquals(expected, unsignedSum.intValue());
    113       }
    114     }
    115   }
    116 
    117   public void testSubtract() {
    118     for (int a : TEST_INTS) {
    119       for (int b : TEST_INTS) {
    120         UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    121         UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b);
    122         int expected =
    123             force32(aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).intValue());
    124         UnsignedInteger unsignedSub = aUnsigned.subtract(bUnsigned);
    125         assertEquals(expected, unsignedSub.intValue());
    126       }
    127     }
    128   }
    129 
    130   @GwtIncompatible("multiply")
    131   public void testMultiply() {
    132     for (int a : TEST_INTS) {
    133       for (int b : TEST_INTS) {
    134         UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    135         UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b);
    136         int expected =
    137             force32(aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).intValue());
    138         UnsignedInteger unsignedMul = aUnsigned.multiply(bUnsigned);
    139         assertEquals(aUnsigned + " * " + bUnsigned, expected, unsignedMul.intValue());
    140       }
    141     }
    142   }
    143 
    144   public void testDivide() {
    145     for (int a : TEST_INTS) {
    146       for (int b : TEST_INTS) {
    147         if (b != 0) {
    148           UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    149           UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b);
    150           int expected =
    151               aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).intValue();
    152           UnsignedInteger unsignedDiv = aUnsigned.divide(bUnsigned);
    153           assertEquals(expected, unsignedDiv.intValue());
    154         }
    155       }
    156     }
    157   }
    158 
    159   public void testDivideByZeroThrows() {
    160     for (int a : TEST_INTS) {
    161       try {
    162         UnsignedInteger.asUnsigned(a).divide(UnsignedInteger.ZERO);
    163         fail("Expected ArithmeticException");
    164       } catch (ArithmeticException expected) {}
    165     }
    166   }
    167 
    168   public void testRemainder() {
    169     for (int a : TEST_INTS) {
    170       for (int b : TEST_INTS) {
    171         if (b != 0) {
    172           UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    173           UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b);
    174           int expected =
    175               aUnsigned.bigIntegerValue().remainder(bUnsigned.bigIntegerValue()).intValue();
    176           UnsignedInteger unsignedRem = aUnsigned.remainder(bUnsigned);
    177           assertEquals(expected, unsignedRem.intValue());
    178         }
    179       }
    180     }
    181   }
    182 
    183   public void testRemainderByZero() {
    184     for (int a : TEST_INTS) {
    185       try {
    186         UnsignedInteger.asUnsigned(a).remainder(UnsignedInteger.ZERO);
    187         fail("Expected ArithmeticException");
    188       } catch (ArithmeticException expected) {}
    189     }
    190   }
    191 
    192   public void testCompare() {
    193     for (int a : TEST_INTS) {
    194       for (int b : TEST_INTS) {
    195         UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    196         UnsignedInteger bUnsigned = UnsignedInteger.asUnsigned(b);
    197         assertEquals(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()),
    198             aUnsigned.compareTo(bUnsigned));
    199       }
    200     }
    201   }
    202 
    203   @GwtIncompatible("too slow")
    204   public void testEqualsAndValueOf() {
    205     EqualsTester equalsTester = new EqualsTester();
    206     for (int a : TEST_INTS) {
    207       long value = a & 0xffffffffL;
    208       equalsTester.addEqualityGroup(UnsignedInteger.asUnsigned(a), UnsignedInteger.valueOf(value),
    209           UnsignedInteger.valueOf(Long.toString(value)),
    210           UnsignedInteger.valueOf(Long.toString(value, 16), 16));
    211     }
    212     equalsTester.testEquals();
    213   }
    214 
    215   public void testIntValue() {
    216     for (int a : TEST_INTS) {
    217       UnsignedInteger aUnsigned = UnsignedInteger.asUnsigned(a);
    218       int intValue = aUnsigned.bigIntegerValue().intValue();
    219       assertEquals(intValue, aUnsigned.intValue());
    220     }
    221   }
    222 
    223   @GwtIncompatible("serialization")
    224   public void testSerialization() {
    225     for (int a : TEST_INTS) {
    226       SerializableTester.reserializeAndAssert(UnsignedInteger.asUnsigned(a));
    227     }
    228   }
    229 
    230   @GwtIncompatible("NullPointerTester")
    231   public void testNulls() throws Exception {
    232     NullPointerTester tester = new NullPointerTester();
    233     tester.setDefault(UnsignedInteger.class, UnsignedInteger.ONE);
    234     tester.testAllPublicStaticMethods(UnsignedInteger.class);
    235   }
    236 }
    237