Search in sources :

Example 31 with Rational

use of android.util.Rational in project android_frameworks_base by crdroidandroid.

the class RationalTest method testValueConversions.

@SmallTest
public void testValueConversions() {
    // Unit, simple case
    assertValueEquals(UNIT, 1.0f);
    assertValueEquals(UNIT, 1.0);
    assertValueEquals(UNIT, 1L);
    assertValueEquals(UNIT, 1);
    assertValueEquals(UNIT, (short) 1);
    // Zero, simple case
    assertValueEquals(ZERO, 0.0f);
    assertValueEquals(ZERO, 0.0);
    assertValueEquals(ZERO, 0L);
    assertValueEquals(ZERO, 0);
    assertValueEquals(ZERO, (short) 0);
    // NaN is 0 for integers, not-a-number for floating point
    assertValueEquals(NaN, Float.NaN);
    assertValueEquals(NaN, Double.NaN);
    assertValueEquals(NaN, 0L);
    assertValueEquals(NaN, 0);
    assertValueEquals(NaN, (short) 0);
    // Positive infinity, saturates upwards for integers
    assertValueEquals(POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
    assertValueEquals(POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
    assertValueEquals(POSITIVE_INFINITY, Long.MAX_VALUE);
    assertValueEquals(POSITIVE_INFINITY, Integer.MAX_VALUE);
    assertValueEquals(POSITIVE_INFINITY, (short) -1);
    // Negative infinity, saturates downwards for integers
    assertValueEquals(NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
    assertValueEquals(NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
    assertValueEquals(NEGATIVE_INFINITY, Long.MIN_VALUE);
    assertValueEquals(NEGATIVE_INFINITY, Integer.MIN_VALUE);
    assertValueEquals(NEGATIVE_INFINITY, (short) 0);
    // Normal finite values, round down for integers
    final Rational oneQuarter = new Rational(1, 4);
    assertValueEquals(oneQuarter, 1.0f / 4.0f);
    assertValueEquals(oneQuarter, 1.0 / 4.0);
    assertValueEquals(oneQuarter, 0L);
    assertValueEquals(oneQuarter, 0);
    assertValueEquals(oneQuarter, (short) 0);
    final Rational nineFifths = new Rational(9, 5);
    assertValueEquals(nineFifths, 9.0f / 5.0f);
    assertValueEquals(nineFifths, 9.0 / 5.0);
    assertValueEquals(nineFifths, 1L);
    assertValueEquals(nineFifths, 1);
    assertValueEquals(nineFifths, (short) 1);
    final Rational negativeHundred = new Rational(-1000, 10);
    assertValueEquals(negativeHundred, -100.f / 1.f);
    assertValueEquals(negativeHundred, -100.0 / 1.0);
    assertValueEquals(negativeHundred, -100L);
    assertValueEquals(negativeHundred, -100);
    assertValueEquals(negativeHundred, (short) -100);
    // Short truncates if the result is too large
    assertValueEquals(new Rational(Integer.MAX_VALUE, 1), (short) Integer.MAX_VALUE);
    assertValueEquals(new Rational(0x00FFFFFF, 1), (short) 0x00FFFFFF);
    assertValueEquals(new Rational(0x00FF00FF, 1), (short) 0x00FF00FF);
}
Also used : Rational(android.util.Rational) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 32 with Rational

use of android.util.Rational in project android_frameworks_base by AOSPA.

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;
}
Also used : Rational(android.util.Rational)

Example 33 with Rational

use of android.util.Rational in project android_frameworks_base by AOSPA.

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
    }
}
Also used : Rational(android.util.Rational) InvalidObjectException(java.io.InvalidObjectException) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 34 with Rational

use of android.util.Rational in project android_frameworks_base by AOSPA.

the class RangeTest method testInRange.

@SmallTest
public void testInRange() {
    Range<Integer> hundredOneTwo = Range.create(100, 200);
    assertInRange(hundredOneTwo, 100);
    assertInRange(hundredOneTwo, 200);
    assertInRange(hundredOneTwo, 150);
    assertOutOfRange(hundredOneTwo, 99);
    assertOutOfRange(hundredOneTwo, 201);
    assertOutOfRange(hundredOneTwo, 100000);
    Range<Float> infinities = Range.create(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY);
    assertInRange(infinities, Float.NEGATIVE_INFINITY);
    assertInRange(infinities, Float.POSITIVE_INFINITY);
    assertInRange(infinities, 0.0f);
    assertOutOfRange(infinities, Float.NaN);
    Range<Rational> negativeOneTenthPositiveOneTenth = new Range<Rational>(new Rational(-1, 10), new Rational(1, 10));
    assertInRange(negativeOneTenthPositiveOneTenth, new Rational(-1, 10));
    assertInRange(negativeOneTenthPositiveOneTenth, new Rational(1, 10));
    assertInRange(negativeOneTenthPositiveOneTenth, Rational.ZERO);
    assertOutOfRange(negativeOneTenthPositiveOneTenth, new Rational(-100, 1));
    assertOutOfRange(negativeOneTenthPositiveOneTenth, new Rational(100, 1));
}
Also used : Rational(android.util.Rational) Range(android.util.Range) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 35 with Rational

use of android.util.Rational in project android_frameworks_base by AOSPA.

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;
}
Also used : Rational(android.util.Rational)

Aggregations

Rational (android.util.Rational)96 SmallTest (android.test.suitebuilder.annotation.SmallTest)50 Range (android.util.Range)15 Size (android.util.Size)6 Point (android.graphics.Point)5 Pair (android.util.Pair)5 InvalidObjectException (java.io.InvalidObjectException)5 PictureInPictureParams (android.app.PictureInPictureParams)4 TargetApi (android.annotation.TargetApi)3 MediaWrapper (org.videolan.medialibrary.media.MediaWrapper)2 SuppressLint (android.annotation.SuppressLint)1 PendingIntent (android.app.PendingIntent)1 RemoteAction (android.app.RemoteAction)1 Intent (android.content.Intent)1 Rect (android.graphics.Rect)1 RequiresApi (androidx.annotation.RequiresApi)1 RequiresPermission (androidx.annotation.RequiresPermission)1 CameraSelector (androidx.camera.core.CameraSelector)1 StringBuilder (java.lang.StringBuilder)1 DecimalFormat (java.text.DecimalFormat)1