Refine search
@GwtIncompatible // too slow public void testEquals() { EqualsTester equalsTester = new EqualsTester(); for (long a : TEST_LONGS) { BigInteger big = (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64)); equalsTester.addEqualityGroup( UnsignedLong.fromLongBits(a), UnsignedLong.valueOf(big), UnsignedLong.valueOf(big.toString()), UnsignedLong.valueOf(big.toString(16), 16)); } equalsTester.testEquals(); }
public void testPlus() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); long expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).longValue(); UnsignedLong unsignedSum = aUnsigned.plus(bUnsigned); assertEquals(expected, unsignedSum.longValue()); } } }
/** * Returns an {@code UnsignedLong} corresponding to a given bit representation. The argument is * interpreted as an unsigned 64-bit value. Specifically, the sign bit of {@code bits} is * interpreted as a normal bit, and all other bits are treated as usual. * * <p>If the argument is nonnegative, the returned result will be equal to {@code bits}, * otherwise, the result will be equal to {@code 2^64 + bits}. * * <p>To represent decimal constants less than {@code 2^63}, consider {@link #valueOf(long)} * instead. * * @since 14.0 */ public static UnsignedLong fromLongBits(long bits) { // TODO(lowasser): consider caching small values, like Long.valueOf return new UnsignedLong(bits); }
public void testMinus() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); long expected = aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).longValue(); UnsignedLong unsignedSub = aUnsigned.minus(bUnsigned); assertEquals(expected, unsignedSub.longValue()); } } }
/** * Reads a timestamp that is the number of hundreds of nanoseconds since Jan 1 1601 * (see http://integriography.wordpress.com/2010/01/16/using-phython-to-parse-and-present-windows-64-bit-timestamps/) * * @return the date corresponding to the timestamp */ public Date readFileTime() { UnsignedLong hundredsOfNanosecondsSinceJan11601 = readQWord(); long millisecondsSinceJan11601 = hundredsOfNanosecondsSinceJan11601.dividedBy(UnsignedLong.valueOf(10000)).longValue(); long millisecondsSinceEpoch = millisecondsSinceJan11601 - EPOCH_OFFSET; return new Date(millisecondsSinceEpoch); }
public void testCompare() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); assertEquals( aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()), aUnsigned.compareTo(bUnsigned)); } } }
public void testAsUnsignedBigIntegerValue() { for (long value : TEST_LONGS) { BigInteger expected = (value >= 0) ? BigInteger.valueOf(value) : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64)); assertEquals( UnsignedLongs.toString(value), expected, UnsignedLong.fromLongBits(value).bigIntegerValue()); } }
/** * Returns a {@code UnsignedLong} representing the same value as the specified {@code BigInteger}. * This is the inverse operation of {@link #bigIntegerValue()}. * * @throws IllegalArgumentException if {@code value} is negative or {@code value >= 2^64} */ @CanIgnoreReturnValue public static UnsignedLong valueOf(BigInteger value) { checkNotNull(value); checkArgument( value.signum() >= 0 && value.bitLength() <= Long.SIZE, "value (%s) is outside the range for an unsigned long value", value); return fromLongBits(value.longValue()); }
public void testToString() { for (long value : TEST_LONGS) { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertEquals(unsignedValue.bigIntegerValue().toString(), unsignedValue.toString()); } }
public void testTimes() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); long expected = aUnsigned.bigIntegerValue().multiply(bUnsigned.bigIntegerValue()).longValue(); UnsignedLong unsignedMul = aUnsigned.times(bUnsigned); assertEquals(expected, unsignedMul.longValue()); } } }
public void testDividedBy() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { if (b != 0) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); long expected = aUnsigned.bigIntegerValue().divide(bUnsigned.bigIntegerValue()).longValue(); UnsignedLong unsignedDiv = aUnsigned.dividedBy(bUnsigned); assertEquals(expected, unsignedDiv.longValue()); } } } }
public void testMod() { for (long a : TEST_LONGS) { for (long b : TEST_LONGS) { if (b != 0) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); UnsignedLong bUnsigned = UnsignedLong.fromLongBits(b); long expected = aUnsigned.bigIntegerValue().remainder(bUnsigned.bigIntegerValue()).longValue(); UnsignedLong unsignedRem = aUnsigned.mod(bUnsigned); assertEquals(expected, unsignedRem.longValue()); } } } }
public void testIntValue() { for (long a : TEST_LONGS) { UnsignedLong aUnsigned = UnsignedLong.fromLongBits(a); int intValue = aUnsigned.bigIntegerValue().intValue(); assertEquals(intValue, aUnsigned.intValue()); } }
public void testValueOfBigInteger() { BigInteger min = BigInteger.ZERO; BigInteger max = UnsignedLong.MAX_VALUE.bigIntegerValue(); for (BigInteger big : TEST_BIG_INTEGERS) { boolean expectSuccess = big.compareTo(min) >= 0 && big.compareTo(max) <= 0; try { assertEquals(big, UnsignedLong.valueOf(big).bigIntegerValue()); assertTrue(expectSuccess); } catch (IllegalArgumentException e) { assertFalse(expectSuccess); } } }
public void testDoubleValue() { for (long value : TEST_LONGS) { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue()); } }
public void testFloatValue() { for (long value : TEST_LONGS) { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue()); } }
/** * Returns this modulo {@code val}. * * @since 14.0 */ public UnsignedLong mod(UnsignedLong val) { return fromLongBits(UnsignedLongs.remainder(value, checkNotNull(val).value)); }
public void testAsUnsignedAndLongValueAreInverses() { for (long value : TEST_LONGS) { assertEquals( UnsignedLongs.toString(value), value, UnsignedLong.fromLongBits(value).longValue()); } }
/** * Returns the result of dividing this by {@code val}. * * @since 14.0 */ public UnsignedLong dividedBy(UnsignedLong val) { return fromLongBits(UnsignedLongs.divide(value, checkNotNull(val).value)); }
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as an * unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and {@link * Character#MAX_RADIX} */ @CanIgnoreReturnValue public static UnsignedLong valueOf(String string, int radix) { return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); }