1 /* 2 * Copyright (C) 2013 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 com.android.mediaframeworktest.unit; 18 19 import android.test.suitebuilder.annotation.SmallTest; 20 import android.hardware.camera2.Rational; 21 22 /** 23 * <pre> 24 * adb shell am instrument \ 25 * -e class 'com.android.mediaframeworktest.unit.RationalTest' \ 26 * -w com.android.mediaframeworktest/.MediaFrameworkUnitTestRunner 27 * </pre> 28 */ 29 public class RationalTest extends junit.framework.TestCase { 30 @SmallTest 31 public void testConstructor() { 32 33 // Simple case 34 Rational r = new Rational(1, 2); 35 assertEquals(1, r.getNumerator()); 36 assertEquals(2, r.getDenominator()); 37 38 // Denominator negative 39 r = new Rational(-1, 2); 40 assertEquals(-1, r.getNumerator()); 41 assertEquals(2, r.getDenominator()); 42 43 // Numerator negative 44 r = new Rational(1, -2); 45 assertEquals(-1, r.getNumerator()); 46 assertEquals(2, r.getDenominator()); 47 48 // Both negative 49 r = new Rational(-1, -2); 50 assertEquals(1, r.getNumerator()); 51 assertEquals(2, r.getDenominator()); 52 53 // Infinity. 54 r = new Rational(1, 0); 55 assertEquals(0, r.getNumerator()); 56 assertEquals(0, r.getDenominator()); 57 58 // Negative infinity. 59 r = new Rational(-1, 0); 60 assertEquals(0, r.getNumerator()); 61 assertEquals(0, r.getDenominator()); 62 63 // NaN. 64 r = new Rational(0, 0); 65 assertEquals(0, r.getNumerator()); 66 assertEquals(0, r.getDenominator()); 67 } 68 69 @SmallTest 70 public void testGcd() { 71 Rational r = new Rational(1, 2); 72 assertEquals(1, r.gcd()); 73 74 Rational twoThirds = new Rational(2, 3); 75 assertEquals(1, twoThirds.gcd()); 76 77 Rational moreComplicated2 = new Rational(5*78, 7*78); 78 assertEquals(78, moreComplicated2.gcd()); 79 80 Rational oneHalf = new Rational(-1, 2); 81 assertEquals(1, oneHalf.gcd()); 82 83 twoThirds = new Rational(-2, 3); 84 assertEquals(1, twoThirds.gcd()); 85 } 86 87 @SmallTest 88 public void testEquals() { 89 Rational r = new Rational(1, 2); 90 assertEquals(1, r.getNumerator()); 91 assertEquals(2, r.getDenominator()); 92 93 assertEquals(r, r); 94 assertFalse(r.equals(null)); 95 assertFalse(r.equals(new Object())); 96 97 Rational twoThirds = new Rational(2, 3); 98 assertFalse(r.equals(twoThirds)); 99 assertFalse(twoThirds.equals(r)); 100 101 Rational fourSixths = new Rational(4, 6); 102 assertEquals(twoThirds, fourSixths); 103 assertEquals(fourSixths, twoThirds); 104 105 Rational moreComplicated = new Rational(5*6*7*8*9, 1*2*3*4*5); 106 Rational moreComplicated2 = new Rational(5*6*7*8*9*78, 1*2*3*4*5*78); 107 assertEquals(moreComplicated, moreComplicated2); 108 assertEquals(moreComplicated2, moreComplicated); 109 110 // Ensure negatives are fine 111 twoThirds = new Rational(-2, 3); 112 fourSixths = new Rational(-4, 6); 113 assertEquals(twoThirds, fourSixths); 114 assertEquals(fourSixths, twoThirds); 115 116 moreComplicated = new Rational(-5*6*7*8*9, 1*2*3*4*5); 117 moreComplicated2 = new Rational(-5*6*7*8*9*78, 1*2*3*4*5*78); 118 assertEquals(moreComplicated, moreComplicated2); 119 assertEquals(moreComplicated2, moreComplicated); 120 121 Rational nan = new Rational(0, 0); 122 Rational nan2 = new Rational(0, 0); 123 assertTrue(nan.equals(nan)); 124 assertTrue(nan.equals(nan2)); 125 assertTrue(nan2.equals(nan)); 126 assertFalse(nan.equals(r)); 127 assertFalse(r.equals(nan)); 128 129 // Infinities of the same sign are equal. 130 Rational posInf = new Rational(1, 0); 131 Rational posInf2 = new Rational(2, 0); 132 Rational negInf = new Rational(-1, 0); 133 Rational negInf2 = new Rational(-2, 0); 134 assertEquals(posInf, posInf); 135 assertEquals(negInf, negInf); 136 assertEquals(posInf, posInf2); 137 assertEquals(negInf, negInf2); 138 139 // Infinities aren't equal to anything else. 140 assertFalse(posInf.equals(negInf)); 141 assertFalse(negInf.equals(posInf)); 142 assertFalse(negInf.equals(r)); 143 assertFalse(posInf.equals(r)); 144 assertFalse(r.equals(negInf)); 145 assertFalse(r.equals(posInf)); 146 assertFalse(posInf.equals(nan)); 147 assertFalse(negInf.equals(nan)); 148 assertFalse(nan.equals(posInf)); 149 assertFalse(nan.equals(negInf)); 150 } 151 } 152