use of android.util.Rational in project android_frameworks_base by ResurrectionRemix.
the class Utils method parseRationalRange.
static Range<Rational> parseRationalRange(Object o, Range<Rational> fallback) {
try {
String s = (String) o;
int ix = s.indexOf('-');
if (ix >= 0) {
return Range.create(Rational.parseRational(s.substring(0, ix)), Rational.parseRational(s.substring(ix + 1)));
}
Rational value = Rational.parseRational(s);
return Range.create(value, value);
} catch (ClassCastException e) {
} catch (NumberFormatException e) {
} catch (NullPointerException e) {
return fallback;
} catch (IllegalArgumentException e) {
}
Log.w(TAG, "could not parse rational range '" + o + "'");
return fallback;
}
use of android.util.Rational in project android_frameworks_base by ResurrectionRemix.
the class RangeTest method testEquals.
@SmallTest
public void testEquals() {
Range<Float> oneHalf = Range.create(1.0f, 2.0f);
Range<Float> oneHalf2 = new Range<Float>(1.0f, 2.0f);
assertEquals(oneHalf, oneHalf2);
assertHashCodeEquals(oneHalf, oneHalf2);
Range<Float> twoThirds = new Range<Float>(2.0f, 3.0f);
Range<Float> twoThirds2 = Range.create(2.0f, 3.0f);
assertEquals(twoThirds, twoThirds2);
assertHashCodeEquals(twoThirds, twoThirds2);
Range<Rational> negativeOneTenthPositiveOneTenth = new Range<Rational>(new Rational(-1, 10), new Rational(1, 10));
Range<Rational> negativeOneTenthPositiveOneTenth2 = Range.create(new Rational(-1, 10), new Rational(1, 10));
assertEquals(negativeOneTenthPositiveOneTenth, negativeOneTenthPositiveOneTenth2);
assertHashCodeEquals(negativeOneTenthPositiveOneTenth, negativeOneTenthPositiveOneTenth2);
}
use of android.util.Rational in project android_frameworks_base by ResurrectionRemix.
the class RationalTest method testReduction.
@SmallTest
public void testReduction() {
Rational moreComplicated = new Rational(5 * 78, 7 * 78);
assertEquals(new Rational(5, 7), moreComplicated);
assertEquals(5, moreComplicated.getNumerator());
assertEquals(7, moreComplicated.getDenominator());
Rational posInf = new Rational(5, 0);
assertEquals(1, posInf.getNumerator());
assertEquals(0, posInf.getDenominator());
assertEquals(POSITIVE_INFINITY, posInf);
Rational negInf = new Rational(-100, 0);
assertEquals(-1, negInf.getNumerator());
assertEquals(0, negInf.getDenominator());
assertEquals(NEGATIVE_INFINITY, negInf);
Rational zero = new Rational(0, -100);
assertEquals(0, zero.getNumerator());
assertEquals(1, zero.getDenominator());
assertEquals(ZERO, zero);
Rational flipSigns = new Rational(1, -1);
assertEquals(-1, flipSigns.getNumerator());
assertEquals(1, flipSigns.getDenominator());
Rational flipAndReduce = new Rational(100, -200);
assertEquals(-1, flipAndReduce.getNumerator());
assertEquals(2, flipAndReduce.getDenominator());
}
use of android.util.Rational in project android_frameworks_base by ResurrectionRemix.
the class RationalTest method createIllegalRational.
private static Rational createIllegalRational(int numerator, int denominator) {
Rational r = new Rational(numerator, denominator);
mutateField(r, "mNumerator", numerator);
mutateField(r, "mDenominator", denominator);
return r;
}
use of android.util.Rational in project android_frameworks_base by ResurrectionRemix.
the class RationalTest method testSerialize.
@SmallTest
public void testSerialize() throws ClassNotFoundException, IOException {
/*
* Check correct [de]serialization
*/
assertEqualsAfterSerializing(ZERO);
assertEqualsAfterSerializing(NaN);
assertEqualsAfterSerializing(NEGATIVE_INFINITY);
assertEqualsAfterSerializing(POSITIVE_INFINITY);
assertEqualsAfterSerializing(UNIT);
assertEqualsAfterSerializing(new Rational(100, 200));
assertEqualsAfterSerializing(new Rational(-100, 200));
assertEqualsAfterSerializing(new Rational(5, 1));
assertEqualsAfterSerializing(new Rational(Integer.MAX_VALUE, Integer.MIN_VALUE));
/*
* Check bad deserialization fails
*/
try {
// [0, 100] , should be [0, 1]
Rational badZero = createIllegalRational(0, 100);
Rational results = serializeRoundTrip(badZero);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
// [100, 0] , should be [1, 0]
Rational badPosInfinity = createIllegalRational(100, 0);
Rational results = serializeRoundTrip(badPosInfinity);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
Rational badNegInfinity = // [-100, 0] , should be [-1, 0]
createIllegalRational(-100, 0);
Rational results = serializeRoundTrip(badNegInfinity);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
// [2,4] , should be [1, 2]
Rational badReduced = createIllegalRational(2, 4);
Rational results = serializeRoundTrip(badReduced);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
try {
// [-2, 4] should be [-1, 2]
Rational badReducedNeg = createIllegalRational(-2, 4);
Rational results = serializeRoundTrip(badReducedNeg);
fail("Deserializing " + results + " should not have succeeded");
} catch (InvalidObjectException e) {
// OK
}
}
Aggregations