RationalTest.java revision b519cc52ecba8f44da31173c9fc90a7b66d52b79
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 17package com.android.mediaframeworktest.unit; 18 19import android.test.suitebuilder.annotation.SmallTest; 20import android.hardware.photography.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 */ 29public 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 // Dividing by zero is not allowed 54 try { 55 r = new Rational(1, 0); 56 fail("Expected Rational constructor to throw an IllegalArgumentException"); 57 } catch(IllegalArgumentException e) { 58 } 59 } 60 61 @SmallTest 62 public void testGcd() { 63 Rational r = new Rational(1, 2); 64 assertEquals(1, r.gcd()); 65 66 Rational twoThirds = new Rational(2, 3); 67 assertEquals(1, twoThirds.gcd()); 68 69 Rational moreComplicated2 = new Rational(5*78, 7*78); 70 assertEquals(78, moreComplicated2.gcd()); 71 72 Rational oneHalf = new Rational(-1, 2); 73 assertEquals(1, oneHalf.gcd()); 74 75 twoThirds = new Rational(-2, 3); 76 assertEquals(1, twoThirds.gcd()); 77 } 78 79 @SmallTest 80 public void testEquals() { 81 Rational r = new Rational(1, 2); 82 assertEquals(1, r.getNumerator()); 83 assertEquals(2, r.getDenominator()); 84 85 assertEquals(r, r); 86 assertFalse(r.equals(null)); 87 assertFalse(r.equals(new Object())); 88 89 Rational twoThirds = new Rational(2, 3); 90 assertFalse(r.equals(twoThirds)); 91 assertFalse(twoThirds.equals(r)); 92 93 Rational fourSixths = new Rational(4, 6); 94 assertEquals(twoThirds, fourSixths); 95 assertEquals(fourSixths, twoThirds); 96 97 Rational moreComplicated = new Rational(5*6*7*8*9, 1*2*3*4*5); 98 Rational moreComplicated2 = new Rational(5*6*7*8*9*78, 1*2*3*4*5*78); 99 assertEquals(moreComplicated, moreComplicated2); 100 assertEquals(moreComplicated2, moreComplicated); 101 102 // Ensure negatives are fine 103 twoThirds = new Rational(-2, 3); 104 fourSixths = new Rational(-4, 6); 105 assertEquals(twoThirds, fourSixths); 106 assertEquals(fourSixths, twoThirds); 107 108 moreComplicated = new Rational(-5*6*7*8*9, 1*2*3*4*5); 109 moreComplicated2 = new Rational(-5*6*7*8*9*78, 1*2*3*4*5*78); 110 assertEquals(moreComplicated, moreComplicated2); 111 assertEquals(moreComplicated2, moreComplicated); 112 113 } 114}