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 com.google.common.annotations.GwtCompatible; 18 import com.google.common.annotations.GwtIncompatible; 19 import com.google.common.collect.ImmutableSet; 20 import com.google.common.testing.EqualsTester; 21 import com.google.common.testing.NullPointerTester; 22 import com.google.common.testing.SerializableTester; 23 import java.math.BigInteger; 24 import junit.framework.TestCase; 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 private static final ImmutableSet<Long> TEST_LONGS; 35 force32(int value)36 private static int force32(int value) { 37 // GWT doesn't consistently overflow values to make them 32-bit, so we need to force it. 38 return value & 0xffffffff; 39 } 40 41 static { 42 ImmutableSet.Builder<Integer> testIntsBuilder = ImmutableSet.builder(); 43 ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder(); 44 for (int i = -3; i <= 3; i++) { 45 testIntsBuilder 46 .add(i) 47 .add(force32(Integer.MIN_VALUE + i)) 48 .add(force32(Integer.MAX_VALUE + i)); 49 testLongsBuilder 50 .add((long) i) 51 .add((long) Integer.MIN_VALUE + i) 52 .add((long) Integer.MAX_VALUE + i) 53 .add((1L << 32) + i); 54 } 55 TEST_INTS = testIntsBuilder.build(); 56 TEST_LONGS = testLongsBuilder.build(); 57 } 58 testFromIntBitsAndIntValueAreInverses()59 public void testFromIntBitsAndIntValueAreInverses() { 60 for (int value : TEST_INTS) { 61 assertEquals( 62 UnsignedInts.toString(value), value, UnsignedInteger.fromIntBits(value).intValue()); 63 } 64 } 65 testFromIntBitsLongValue()66 public void testFromIntBitsLongValue() { 67 for (int value : TEST_INTS) { 68 long expected = value & 0xffffffffL; 69 assertEquals( 70 UnsignedInts.toString(value), expected, UnsignedInteger.fromIntBits(value).longValue()); 71 } 72 } 73 testValueOfLong()74 public void testValueOfLong() { 75 long min = 0; 76 long max = (1L << 32) - 1; 77 for (long value : TEST_LONGS) { 78 boolean expectSuccess = value >= min && value <= max; 79 try { 80 assertEquals(value, UnsignedInteger.valueOf(value).longValue()); 81 assertTrue(expectSuccess); 82 } catch (IllegalArgumentException e) { 83 assertFalse(expectSuccess); 84 } 85 } 86 } 87 testValueOfBigInteger()88 public void testValueOfBigInteger() { 89 long min = 0; 90 long max = (1L << 32) - 1; 91 for (long value : TEST_LONGS) { 92 boolean expectSuccess = value >= min && value <= max; 93 try { 94 assertEquals(value, UnsignedInteger.valueOf(BigInteger.valueOf(value)).longValue()); 95 assertTrue(expectSuccess); 96 } catch (IllegalArgumentException e) { 97 assertFalse(expectSuccess); 98 } 99 } 100 } 101 testToString()102 public void testToString() { 103 for (int value : TEST_INTS) { 104 UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); 105 assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString()); 106 } 107 } 108 109 @GwtIncompatible // too slow testToStringRadix()110 public void testToStringRadix() { 111 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { 112 for (int l : TEST_INTS) { 113 UnsignedInteger value = UnsignedInteger.fromIntBits(l); 114 assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); 115 } 116 } 117 } 118 testToStringRadixQuick()119 public void testToStringRadixQuick() { 120 int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36}; 121 for (int radix : radices) { 122 for (int l : TEST_INTS) { 123 UnsignedInteger value = UnsignedInteger.fromIntBits(l); 124 assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); 125 } 126 } 127 } 128 testFloatValue()129 public void testFloatValue() { 130 for (int value : TEST_INTS) { 131 UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); 132 assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue()); 133 } 134 } 135 testDoubleValue()136 public void testDoubleValue() { 137 for (int value : TEST_INTS) { 138 UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); 139 assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue()); 140 } 141 } 142 testPlus()143 public void testPlus() { 144 for (int a : TEST_INTS) { 145 for (int b : TEST_INTS) { 146 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 147 UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); 148 int expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).intValue(); 149 UnsignedInteger unsignedSum = aUnsigned.plus(bUnsigned); 150 assertEquals(expected, unsignedSum.intValue()); 151 } 152 } 153 } 154 testMinus()155 public void testMinus() { 156 for (int a : TEST_INTS) { 157 for (int b : TEST_INTS) { 158 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 159 UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); 160 int expected = 161 force32(aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).intValue()); 162 UnsignedInteger unsignedSub = aUnsigned.minus(bUnsigned); 163 assertEquals(expected, unsignedSub.intValue()); 164 } 165 } 166 } 167 168 @GwtIncompatible // multiply testTimes()169 public void testTimes() { 170 for (int a : TEST_INTS) { 171 for (int b : TEST_INTS) { 172 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 173 UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); 174 int expected = 175 force32(aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).intValue()); 176 UnsignedInteger unsignedMul = aUnsigned.times(bUnsigned); 177 assertEquals(aUnsigned + " * " + bUnsigned, expected, unsignedMul.intValue()); 178 } 179 } 180 } 181 testDividedBy()182 public void testDividedBy() { 183 for (int a : TEST_INTS) { 184 for (int b : TEST_INTS) { 185 if (b != 0) { 186 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 187 UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); 188 int expected = aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).intValue(); 189 UnsignedInteger unsignedDiv = aUnsigned.dividedBy(bUnsigned); 190 assertEquals(expected, unsignedDiv.intValue()); 191 } 192 } 193 } 194 } 195 testDivideByZeroThrows()196 public void testDivideByZeroThrows() { 197 for (int a : TEST_INTS) { 198 try { 199 UnsignedInteger unused = UnsignedInteger.fromIntBits(a).dividedBy(UnsignedInteger.ZERO); 200 fail("Expected ArithmeticException"); 201 } catch (ArithmeticException expected) { 202 } 203 } 204 } 205 testMod()206 public void testMod() { 207 for (int a : TEST_INTS) { 208 for (int b : TEST_INTS) { 209 if (b != 0) { 210 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 211 UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); 212 int expected = aUnsigned.bigIntegerValue().mod(bUnsigned.bigIntegerValue()).intValue(); 213 UnsignedInteger unsignedRem = aUnsigned.mod(bUnsigned); 214 assertEquals(expected, unsignedRem.intValue()); 215 } 216 } 217 } 218 } 219 testModByZero()220 public void testModByZero() { 221 for (int a : TEST_INTS) { 222 try { 223 UnsignedInteger.fromIntBits(a).mod(UnsignedInteger.ZERO); 224 fail("Expected ArithmeticException"); 225 } catch (ArithmeticException expected) { 226 } 227 } 228 } 229 testCompare()230 public void testCompare() { 231 for (int a : TEST_INTS) { 232 for (int b : TEST_INTS) { 233 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 234 UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); 235 assertEquals( 236 aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()), 237 aUnsigned.compareTo(bUnsigned)); 238 } 239 } 240 } 241 242 @GwtIncompatible // too slow testEquals()243 public void testEquals() { 244 EqualsTester equalsTester = new EqualsTester(); 245 for (int a : TEST_INTS) { 246 long value = a & 0xffffffffL; 247 equalsTester.addEqualityGroup( 248 UnsignedInteger.fromIntBits(a), 249 UnsignedInteger.valueOf(value), 250 UnsignedInteger.valueOf(Long.toString(value)), 251 UnsignedInteger.valueOf(Long.toString(value, 16), 16)); 252 } 253 equalsTester.testEquals(); 254 } 255 testIntValue()256 public void testIntValue() { 257 for (int a : TEST_INTS) { 258 UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); 259 int intValue = aUnsigned.bigIntegerValue().intValue(); 260 assertEquals(intValue, aUnsigned.intValue()); 261 } 262 } 263 264 @GwtIncompatible // serialization testSerialization()265 public void testSerialization() { 266 for (int a : TEST_INTS) { 267 SerializableTester.reserializeAndAssert(UnsignedInteger.fromIntBits(a)); 268 } 269 } 270 271 @GwtIncompatible // NullPointerTester testNulls()272 public void testNulls() { 273 new NullPointerTester().testAllPublicStaticMethods(UnsignedInteger.class); 274 } 275 } 276