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 static com.google.common.truth.Truth.assertThat; 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import com.google.common.annotations.GwtCompatible; 21 import com.google.common.annotations.GwtIncompatible; 22 import com.google.common.annotations.J2ktIncompatible; 23 import com.google.common.collect.ImmutableSet; 24 import com.google.common.testing.EqualsTester; 25 import com.google.common.testing.NullPointerTester; 26 import com.google.common.testing.SerializableTester; 27 import java.math.BigInteger; 28 import junit.framework.TestCase; 29 30 /** 31 * Tests for {@code UnsignedLong}. 32 * 33 * @author Louis Wasserman 34 */ 35 @GwtCompatible(emulated = true) 36 public class UnsignedLongTest extends TestCase { 37 private static final ImmutableSet<Long> TEST_LONGS; 38 private static final ImmutableSet<BigInteger> TEST_BIG_INTEGERS; 39 40 static { 41 ImmutableSet.Builder<Long> testLongsBuilder = ImmutableSet.builder(); 42 ImmutableSet.Builder<BigInteger> testBigIntegersBuilder = ImmutableSet.builder(); 43 44 // The values here look like 111...11101...010 in binary, where the initial 111...1110 takes 45 // up exactly as many bits as can be represented in the significand (24 for float, 53 for 46 // double). That final 0 should be rounded up to 1 because the remaining bits make that number 47 // slightly nearer. 48 long floatConversionTest = 0xfffffe8000000002L; 49 long doubleConversionTest = 0xfffffffffffff402L; 50 51 for (long i = -3; i <= 3; i++) { 52 testLongsBuilder 53 .add(i) 54 .add(Long.MAX_VALUE + i) 55 .add(Long.MIN_VALUE + i) 56 .add(Integer.MIN_VALUE + i) 57 .add(Integer.MAX_VALUE + i) 58 .add(floatConversionTest + i) 59 .add(doubleConversionTest + i); 60 BigInteger bigI = BigInteger.valueOf(i); 61 testBigIntegersBuilder 62 .add(bigI) 63 .add(BigInteger.valueOf(Long.MAX_VALUE).add(bigI)) 64 .add(BigInteger.valueOf(Long.MIN_VALUE).add(bigI)) 65 .add(BigInteger.valueOf(Integer.MAX_VALUE).add(bigI)) 66 .add(BigInteger.valueOf(Integer.MIN_VALUE).add(bigI)) 67 .add(BigInteger.ONE.shiftLeft(63).add(bigI)) add(bigI)68 .add(BigInteger.ONE.shiftLeft(64).add(bigI)); 69 } 70 TEST_LONGS = testLongsBuilder.build(); 71 TEST_BIG_INTEGERS = testBigIntegersBuilder.build(); 72 } 73 testAsUnsignedAndLongValueAreInverses()74 public void testAsUnsignedAndLongValueAreInverses() { 75 for (long value : TEST_LONGS) { 76 assertWithMessage(UnsignedLongs.toString(value)) 77 .that(UnsignedLong.fromLongBits(value).longValue()) 78 .isEqualTo(value); 79 } 80 } 81 testAsUnsignedBigIntegerValue()82 public void testAsUnsignedBigIntegerValue() { 83 for (long value : TEST_LONGS) { 84 BigInteger expected = 85 (value >= 0) 86 ? BigInteger.valueOf(value) 87 : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64)); 88 assertWithMessage(UnsignedLongs.toString(value)) 89 .that(UnsignedLong.fromLongBits(value).bigIntegerValue()) 90 .isEqualTo(expected); 91 } 92 } 93 testValueOfLong()94 public void testValueOfLong() { 95 for (long value : TEST_LONGS) { 96 boolean expectSuccess = value >= 0; 97 try { 98 assertThat(UnsignedLong.valueOf(value).longValue()).isEqualTo(value); 99 assertThat(expectSuccess).isTrue(); 100 } catch (IllegalArgumentException e) { 101 assertThat(expectSuccess).isFalse(); 102 } 103 } 104 } 105 106 @J2ktIncompatible // TODO(b/285562794): Wrong result for j2kt testValueOfBigInteger()107 public void testValueOfBigInteger() { 108 BigInteger min = BigInteger.ZERO; 109 BigInteger max = UnsignedLong.MAX_VALUE.bigIntegerValue(); 110 for (BigInteger big : TEST_BIG_INTEGERS) { 111 boolean expectSuccess = big.compareTo(min) >= 0 && big.compareTo(max) <= 0; 112 try { 113 assertThat(UnsignedLong.valueOf(big).bigIntegerValue()).isEqualTo(big); 114 assertThat(expectSuccess).isTrue(); 115 } catch (IllegalArgumentException e) { 116 assertThat(expectSuccess).isFalse(); 117 } 118 } 119 } 120 testToString()121 public void testToString() { 122 for (long value : TEST_LONGS) { 123 UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); 124 assertThat(unsignedValue.toString()).isEqualTo(unsignedValue.bigIntegerValue().toString()); 125 } 126 } 127 128 @J2ktIncompatible 129 @GwtIncompatible // too slow testToStringRadix()130 public void testToStringRadix() { 131 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { 132 for (long l : TEST_LONGS) { 133 UnsignedLong value = UnsignedLong.fromLongBits(l); 134 assertThat(value.toString(radix)).isEqualTo(value.bigIntegerValue().toString(radix)); 135 } 136 } 137 } 138 testToStringRadixQuick()139 public void testToStringRadixQuick() { 140 int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36}; 141 for (int radix : radices) { 142 for (long l : TEST_LONGS) { 143 UnsignedLong value = UnsignedLong.fromLongBits(l); 144 assertThat(value.toString(radix)).isEqualTo(value.bigIntegerValue().toString(radix)); 145 } 146 } 147 } 148 149 @AndroidIncompatible // b/28251030, re-enable when the fix is everywhere we run this test testFloatValue()150 public void testFloatValue() { 151 for (long value : TEST_LONGS) { 152 UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); 153 assertWithMessage("Float value of " + unsignedValue) 154 .that(unsignedValue.floatValue()) 155 .isWithin(0.0f) 156 .of(unsignedValue.bigIntegerValue().floatValue()); 157 } 158 } 159 testDoubleValue()160 public void testDoubleValue() { 161 for (long value : TEST_LONGS) { 162 UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); 163 assertWithMessage("Double value of " + unsignedValue) 164 .that(unsignedValue.doubleValue()) 165 .isWithin(0.0) 166 .of(unsignedValue.bigIntegerValue().doubleValue()); 167 } 168 } 169 170 @J2ktIncompatible // TODO(b/285562794): Wrong result for j2kt testPlus()171 public void testPlus() { 172 for (long a : TEST_LONGS) { 173 for (long b : TEST_LONGS) { 174 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 175 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); 176 long expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).longValue(); 177 UnsignedLong unsignedSum = aUnsigned.plus(bUnsigned); 178 assertThat(unsignedSum.longValue()).isEqualTo(expected); 179 } 180 } 181 } 182 183 @J2ktIncompatible // TODO(b/285562794): Wrong result for j2kt testMinus()184 public void testMinus() { 185 for (long a : TEST_LONGS) { 186 for (long b : TEST_LONGS) { 187 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 188 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); 189 long expected = 190 aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).longValue(); 191 UnsignedLong unsignedSub = aUnsigned.minus(bUnsigned); 192 assertThat(unsignedSub.longValue()).isEqualTo(expected); 193 } 194 } 195 } 196 197 @J2ktIncompatible // TODO(b/285562794): Wrong result for j2kt testTimes()198 public void testTimes() { 199 for (long a : TEST_LONGS) { 200 for (long b : TEST_LONGS) { 201 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 202 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); 203 long expected = 204 aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).longValue(); 205 UnsignedLong unsignedMul = aUnsigned.times(bUnsigned); 206 assertThat(unsignedMul.longValue()).isEqualTo(expected); 207 } 208 } 209 } 210 211 @J2ktIncompatible // TODO(b/285562794): Wrong result for j2kt testDividedBy()212 public void testDividedBy() { 213 for (long a : TEST_LONGS) { 214 for (long b : TEST_LONGS) { 215 if (b != 0) { 216 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 217 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); 218 long expected = 219 aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).longValue(); 220 UnsignedLong unsignedDiv = aUnsigned.dividedBy(bUnsigned); 221 assertThat(unsignedDiv.longValue()).isEqualTo(expected); 222 } 223 } 224 } 225 } 226 testDivideByZeroThrows()227 public void testDivideByZeroThrows() { 228 for (long a : TEST_LONGS) { 229 try { 230 UnsignedLong.fromLongBits(a).dividedBy(UnsignedLong.ZERO); 231 fail("Expected ArithmeticException"); 232 } catch (ArithmeticException expected) { 233 } 234 } 235 } 236 237 @J2ktIncompatible // TODO(b/285538920): Wrong result for j2kt testMod()238 public void testMod() { 239 for (long a : TEST_LONGS) { 240 for (long b : TEST_LONGS) { 241 if (b != 0) { 242 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 243 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); 244 long expected = 245 aUnsigned.bigIntegerValue().remainder(bUnsigned.bigIntegerValue()).longValue(); 246 UnsignedLong unsignedRem = aUnsigned.mod(bUnsigned); 247 assertThat(unsignedRem.longValue()).isEqualTo(expected); 248 } 249 } 250 } 251 } 252 testModByZero()253 public void testModByZero() { 254 for (long a : TEST_LONGS) { 255 try { 256 UnsignedLong.fromLongBits(a).mod(UnsignedLong.ZERO); 257 fail("Expected ArithmeticException"); 258 } catch (ArithmeticException expected) { 259 } 260 } 261 } 262 testCompare()263 public void testCompare() { 264 for (long a : TEST_LONGS) { 265 for (long b : TEST_LONGS) { 266 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 267 UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); 268 assertThat(aUnsigned.compareTo(bUnsigned)) 269 .isEqualTo(aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue())); 270 } 271 } 272 } 273 274 @J2ktIncompatible 275 @GwtIncompatible // too slow testEquals()276 public void testEquals() { 277 EqualsTester equalsTester = new EqualsTester(); 278 for (long a : TEST_LONGS) { 279 BigInteger big = 280 (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64)); 281 equalsTester.addEqualityGroup( 282 UnsignedLong.fromLongBits(a), 283 UnsignedLong.valueOf(big), 284 UnsignedLong.valueOf(big.toString()), 285 UnsignedLong.valueOf(big.toString(16), 16)); 286 } 287 equalsTester.testEquals(); 288 } 289 testIntValue()290 public void testIntValue() { 291 for (long a : TEST_LONGS) { 292 UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); 293 int intValue = aUnsigned.bigIntegerValue().intValue(); 294 assertThat(aUnsigned.intValue()).isEqualTo(intValue); 295 } 296 } 297 298 @J2ktIncompatible 299 @GwtIncompatible // serialization testSerialization()300 public void testSerialization() { 301 for (long a : TEST_LONGS) { 302 SerializableTester.reserializeAndAssert(UnsignedLong.fromLongBits(a)); 303 } 304 } 305 306 @J2ktIncompatible 307 @GwtIncompatible // NullPointerTester testNulls()308 public void testNulls() { 309 new NullPointerTester().testAllPublicStaticMethods(UnsignedLong.class); 310 } 311 } 312