/** * Returns the result of subtracting this and {@code val}. If the result would be negative, * returns the low 32 bits of the result. * * @since 14.0 */ public UnsignedInteger minus(UnsignedInteger val) { return fromIntBits(value - checkNotNull(val).value); }
/** * Returns an {@code UnsignedInteger} that is equal to {@code value}, if possible. The inverse * operation of {@link #longValue()}. */ public static UnsignedInteger valueOf(long value) { checkArgument( (value & INT_MASK) == value, "value (%s) is outside the range for an unsigned integer value", value); return fromIntBits((int) value); }
/** * Returns the result of adding this and {@code val}. If the result would have more than 32 bits, * returns the low 32 bits of the result. * * @since 14.0 */ public UnsignedInteger plus(UnsignedInteger val) { return fromIntBits(this.value + checkNotNull(val).value); }
/** * Returns the result of multiplying this and {@code val}. If the result would have more than 32 * bits, returns the low 32 bits of the result. * * @since 14.0 */ @GwtIncompatible // Does not truncate correctly public UnsignedInteger times(UnsignedInteger val) { // TODO(lowasser): make this GWT-compatible return fromIntBits(value * checkNotNull(val).value); }
/** * Returns an {@code UnsignedInteger} holding the value of the specified {@code String}, parsed as * an unsigned {@code int} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} * value */ public static UnsignedInteger valueOf(String string, int radix) { return fromIntBits(UnsignedInts.parseUnsignedInt(string, radix)); }
/** * Returns the result of dividing this by {@code val}. * * @throws ArithmeticException if {@code val} is zero * @since 14.0 */ public UnsignedInteger dividedBy(UnsignedInteger val) { return fromIntBits(UnsignedInts.divide(value, checkNotNull(val).value)); }
/** * Returns this mod {@code val}. * * @throws ArithmeticException if {@code val} is zero * @since 14.0 */ public UnsignedInteger mod(UnsignedInteger val) { return fromIntBits(UnsignedInts.remainder(value, checkNotNull(val).value)); }
public void testCompare() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); assertEquals( aUnsigned.bigIntegerValue().compareTo(bUnsigned.bigIntegerValue()), aUnsigned.compareTo(bUnsigned)); } } }
public void testDivideByZeroThrows() { for (int a : TEST_INTS) { try { UnsignedInteger unused = UnsignedInteger.fromIntBits(a).dividedBy(UnsignedInteger.ZERO); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
@GwtIncompatible // serialization public void testSerialization() { for (int a : TEST_INTS) { SerializableTester.reserializeAndAssert(UnsignedInteger.fromIntBits(a)); } }
public void testPlus() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); int expected = aUnsigned.bigIntegerValue().add(bUnsigned.bigIntegerValue()).intValue(); UnsignedInteger unsignedSum = aUnsigned.plus(bUnsigned); assertEquals(expected, unsignedSum.intValue()); } } }
public void testModByZero() { for (int a : TEST_INTS) { try { UnsignedInteger.fromIntBits(a).mod(UnsignedInteger.ZERO); fail("Expected ArithmeticException"); } catch (ArithmeticException expected) { } } }
public void testIntValue() { for (int a : TEST_INTS) { UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); int intValue = aUnsigned.bigIntegerValue().intValue(); assertEquals(intValue, aUnsigned.intValue()); } }
public void testDoubleValue() { for (int value : TEST_INTS) { UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); assertEquals(unsignedValue.bigIntegerValue().doubleValue(), unsignedValue.doubleValue()); } }
public void testMinus() { for (int a : TEST_INTS) { for (int b : TEST_INTS) { UnsignedInteger aUnsigned = UnsignedInteger.fromIntBits(a); UnsignedInteger bUnsigned = UnsignedInteger.fromIntBits(b); int expected = force32(aUnsigned.bigIntegerValue().subtract(bUnsigned.bigIntegerValue()).intValue()); UnsignedInteger unsignedSub = aUnsigned.minus(bUnsigned); assertEquals(expected, unsignedSub.intValue()); } } }
public void testToStringRadixQuick() { int[] radices = {2, 3, 5, 7, 10, 12, 16, 21, 31, 36}; for (int radix : radices) { for (int l : TEST_INTS) { UnsignedInteger value = UnsignedInteger.fromIntBits(l); assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); } } }
public void testFromIntBitsAndIntValueAreInverses() { for (int value : TEST_INTS) { assertEquals( UnsignedInts.toString(value), value, UnsignedInteger.fromIntBits(value).intValue()); } }
public void testFromIntBitsLongValue() { for (int value : TEST_INTS) { long expected = value & 0xffffffffL; assertEquals( UnsignedInts.toString(value), expected, UnsignedInteger.fromIntBits(value).longValue()); } }
@GwtIncompatible // too slow public void testToStringRadix() { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { for (int l : TEST_INTS) { UnsignedInteger value = UnsignedInteger.fromIntBits(l); assertEquals(value.bigIntegerValue().toString(radix), value.toString(radix)); } } }
public void testFloatValue() { for (int value : TEST_INTS) { UnsignedInteger unsignedValue = UnsignedInteger.fromIntBits(value); assertEquals(unsignedValue.bigIntegerValue().floatValue(), unsignedValue.floatValue()); } }