1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.util.cts; 18 19 import android.test.suitebuilder.annotation.SmallTest; 20 import android.util.Range; 21 import android.util.Rational; 22 23 public class RangeTest extends junit.framework.TestCase { 24 25 @SmallTest testConstructor()26 public void testConstructor() { 27 // Trivial, same range 28 Range<Integer> intRange = new Range<Integer>(1, 1); 29 30 assertLower(intRange, 1); 31 assertUpper(intRange, 1); 32 33 // Different values in range 34 Range<Integer> intRange2 = new Range<Integer>(100, 200); 35 assertLower(intRange2, 100); 36 assertUpper(intRange2, 200); 37 38 Range<Float> floatRange = new Range<Float>(Float.NEGATIVE_INFINITY, 39 Float.POSITIVE_INFINITY); 40 assertLower(floatRange, Float.NEGATIVE_INFINITY); 41 assertUpper(floatRange, Float.POSITIVE_INFINITY); 42 } 43 44 @SmallTest testIllegalValues()45 public void testIllegalValues() { 46 // Test NPEs 47 try { 48 new Range<Integer>(null, null); 49 fail("Expected exception to be thrown for (null, null)"); 50 } catch (NullPointerException e) { 51 // OK: both args are null 52 } 53 54 try { 55 new Range<Integer>(null, 0); 56 fail("Expected exception to be thrown for (null, 0)"); 57 } catch (NullPointerException e) { 58 // OK: left arg is null 59 } 60 61 try { 62 new Range<Integer>(0, null); 63 fail("Expected exception to be thrown for (0, null)"); 64 } catch (NullPointerException e) { 65 // OK: right arg is null 66 } 67 68 // Test IAEs 69 70 try { 71 new Range<Integer>(50, -50); 72 fail("Expected exception to be thrown for (50, -50)"); 73 } catch (IllegalArgumentException e) { 74 // OK: 50 > -50 so it fails 75 } 76 77 try { 78 new Range<Float>(0.0f, Float.NEGATIVE_INFINITY); 79 fail("Expected exception to be thrown for (0.0f, -Infinity)"); 80 } catch (IllegalArgumentException e) { 81 // OK: 0.0f is > NEGATIVE_INFINITY, so it fails 82 } 83 } 84 85 @SmallTest testEquals()86 public void testEquals() { 87 Range<Float> oneHalf = Range.create(1.0f, 2.0f); 88 Range<Float> oneHalf2 = new Range<Float>(1.0f, 2.0f); 89 assertEquals(oneHalf, oneHalf2); 90 assertHashCodeEquals(oneHalf, oneHalf2); 91 92 Range<Float> twoThirds = new Range<Float>(2.0f, 3.0f); 93 Range<Float> twoThirds2 = Range.create(2.0f, 3.0f); 94 assertEquals(twoThirds, twoThirds2); 95 assertHashCodeEquals(twoThirds, twoThirds2); 96 97 Range<Rational> negativeOneTenthPositiveOneTenth = 98 new Range<Rational>(new Rational(-1, 10), new Rational(1, 10)); 99 Range<Rational> negativeOneTenthPositiveOneTenth2 = 100 Range.create(new Rational(-1, 10), new Rational(1, 10)); 101 assertEquals(negativeOneTenthPositiveOneTenth, negativeOneTenthPositiveOneTenth2); 102 assertHashCodeEquals(negativeOneTenthPositiveOneTenth, negativeOneTenthPositiveOneTenth2); 103 } 104 105 @SmallTest testInRange()106 public void testInRange() { 107 Range<Integer> hundredOneTwo = Range.create(100, 200); 108 109 assertInRange(hundredOneTwo, 100); 110 assertInRange(hundredOneTwo, 200); 111 assertInRange(hundredOneTwo, 150); 112 assertOutOfRange(hundredOneTwo, 99); 113 assertOutOfRange(hundredOneTwo, 201); 114 assertOutOfRange(hundredOneTwo, 100000); 115 116 Range<Float> infinities = Range.create(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY); 117 118 assertInRange(infinities, Float.NEGATIVE_INFINITY); 119 assertInRange(infinities, Float.POSITIVE_INFINITY); 120 assertInRange(infinities, 0.0f); 121 assertOutOfRange(infinities, Float.NaN); 122 123 Range<Rational> negativeOneTenthPositiveOneTenth = 124 new Range<Rational>(new Rational(-1, 10), new Rational(1, 10)); 125 assertInRange(negativeOneTenthPositiveOneTenth, new Rational(-1, 10)); 126 assertInRange(negativeOneTenthPositiveOneTenth, new Rational(1, 10)); 127 assertInRange(negativeOneTenthPositiveOneTenth, Rational.ZERO); 128 assertOutOfRange(negativeOneTenthPositiveOneTenth, new Rational(-100, 1)); 129 assertOutOfRange(negativeOneTenthPositiveOneTenth, new Rational(100, 1)); 130 } 131 assertInRange(Range<T> object, T needle)132 private static <T extends Comparable<? super T>> void assertInRange(Range<T> object, T needle) { 133 assertAction("in-range", object, needle, true, object.contains(needle)); 134 } 135 assertOutOfRange(Range<T> object, T needle)136 private static <T extends Comparable<? super T>> void assertOutOfRange(Range<T> object, 137 T needle) { 138 assertAction("out-of-range", object, needle, false, object.contains(needle)); 139 } 140 assertUpper(Range<T> object, T expected)141 private static <T extends Comparable<? super T>> void assertUpper(Range<T> object, T expected) { 142 assertAction("upper", object, expected, object.getUpper()); 143 } 144 assertLower(Range<T> object, T expected)145 private static <T extends Comparable<? super T>> void assertLower(Range<T> object, T expected) { 146 assertAction("lower", object, expected, object.getLower()); 147 } 148 assertAction(String action, T object, T2 expected, T2 actual)149 private static <T, T2> void assertAction(String action, T object, T2 expected, 150 T2 actual) { 151 assertEquals("Expected " + object + " " + action + " to be ", 152 expected, actual); 153 } 154 assertAction(String action, T object, T2 needle, boolean expected, boolean actual)155 private static <T, T2> void assertAction(String action, T object, T2 needle, boolean expected, 156 boolean actual) { 157 String expectedMessage = expected ? action : ("not " + action); 158 assertEquals("Expected " + needle + " to be " + expectedMessage + " of " + object, 159 expected, actual); 160 } 161 assertHashCodeEquals( Range<T> left, Range<T> right)162 private static <T extends Comparable<? super T>> void assertHashCodeEquals( 163 Range<T> left, Range<T> right) { 164 assertEquals("Left hash code for " + left + 165 " expected to be equal to right hash code for " + right, 166 left.hashCode(), right.hashCode()); 167 } 168 } 169