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