/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mediaframeworktest.unit; import android.test.suitebuilder.annotation.SmallTest; import android.util.Range; import android.util.Rational; /** *
 * adb shell am instrument \
 *      -e class 'com.android.mediaframeworktest.unit.RangeTest' \
 *      -w com.android.mediaframeworktest/.MediaFrameworkUnitTestRunner
 * 
*/ public class RangeTest extends junit.framework.TestCase { @SmallTest public void testConstructor() { // Trivial, same range Range intRange = new Range(1, 1); assertLower(intRange, 1); assertUpper(intRange, 1); // Different values in range Range intRange2 = new Range(100, 200); assertLower(intRange2, 100); assertUpper(intRange2, 200); Range floatRange = new Range(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY); assertLower(floatRange, Float.NEGATIVE_INFINITY); assertUpper(floatRange, Float.POSITIVE_INFINITY); } @SmallTest public void testIllegalValues() { // Test NPEs try { new Range(null, null); fail("Expected exception to be thrown for (null, null)"); } catch (NullPointerException e) { // OK: both args are null } try { new Range(null, 0); fail("Expected exception to be thrown for (null, 0)"); } catch (NullPointerException e) { // OK: left arg is null } try { new Range(0, null); fail("Expected exception to be thrown for (0, null)"); } catch (NullPointerException e) { // OK: right arg is null } // Test IAEs try { new Range(50, -50); fail("Expected exception to be thrown for (50, -50)"); } catch (IllegalArgumentException e) { // OK: 50 > -50 so it fails } try { new Range(0.0f, Float.NEGATIVE_INFINITY); fail("Expected exception to be thrown for (0.0f, -Infinity)"); } catch (IllegalArgumentException e) { // OK: 0.0f is > NEGATIVE_INFINITY, so it fails } } @SmallTest public void testEquals() { Range oneHalf = Range.create(1.0f, 2.0f); Range oneHalf2 = new Range(1.0f, 2.0f); assertEquals(oneHalf, oneHalf2); assertHashCodeEquals(oneHalf, oneHalf2); Range twoThirds = new Range(2.0f, 3.0f); Range twoThirds2 = Range.create(2.0f, 3.0f); assertEquals(twoThirds, twoThirds2); assertHashCodeEquals(twoThirds, twoThirds2); Range negativeOneTenthPositiveOneTenth = new Range(new Rational(-1, 10), new Rational(1, 10)); Range negativeOneTenthPositiveOneTenth2 = Range.create(new Rational(-1, 10), new Rational(1, 10)); assertEquals(negativeOneTenthPositiveOneTenth, negativeOneTenthPositiveOneTenth2); assertHashCodeEquals(negativeOneTenthPositiveOneTenth, negativeOneTenthPositiveOneTenth2); } @SmallTest public void testInRange() { Range hundredOneTwo = Range.create(100, 200); assertInRange(hundredOneTwo, 100); assertInRange(hundredOneTwo, 200); assertInRange(hundredOneTwo, 150); assertOutOfRange(hundredOneTwo, 99); assertOutOfRange(hundredOneTwo, 201); assertOutOfRange(hundredOneTwo, 100000); Range 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 negativeOneTenthPositiveOneTenth = new Range(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)); } private static > void assertInRange(Range object, T needle) { assertAction("in-range", object, needle, true, object.contains(needle)); } private static > void assertOutOfRange(Range object, T needle) { assertAction("out-of-range", object, needle, false, object.contains(needle)); } private static > void assertUpper(Range object, T expected) { assertAction("upper", object, expected, object.getUpper()); } private static > void assertLower(Range object, T expected) { assertAction("lower", object, expected, object.getLower()); } private static void assertAction(String action, T object, T2 expected, T2 actual) { assertEquals("Expected " + object + " " + action + " to be ", expected, actual); } private static void assertAction(String action, T object, T2 needle, boolean expected, boolean actual) { String expectedMessage = expected ? action : ("not " + action); assertEquals("Expected " + needle + " to be " + expectedMessage + " of " + object, expected, actual); } private static > void assertHashCodeEquals( Range left, Range right) { assertEquals("Left hash code for " + left + " expected to be equal to right hash code for " + right, left.hashCode(), right.hashCode()); } }