From 9ad6f6d99c895cb32de602b9f731f4cb730066a6 Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Thu, 1 Oct 2015 16:07:56 -0700 Subject: Fix factorial(0) Bug: 24575798 Make factorial(0) return 1, as intended. Add a few factorial tests. Fix targetPackage spec so test is actually runnable again. Change-Id: Ibb8d827f0325999c6b5ed49a939e1532ddf55639 (cherry picked from commit 6951591806f46405564a6cb868a88d0daf6764d2) --- src/com/android/calculator2/BoundedRational.java | 3 +++ tests/AndroidManifest.xml | 2 +- tests/README.txt | 2 +- tests/src/com/android/calculator2/BRTest.java | 4 ++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/com/android/calculator2/BoundedRational.java b/src/com/android/calculator2/BoundedRational.java index 2b3d1ed..ebaa157 100644 --- a/src/com/android/calculator2/BoundedRational.java +++ b/src/com/android/calculator2/BoundedRational.java @@ -468,6 +468,9 @@ public class BoundedRational { } return prod1.multiply(prod2); } else { + if (n == 0) { + return BigInteger.ONE; + } BigInteger res = BigInteger.valueOf(n); for (long i = n - step; i > 1; i -= step) { res = res.multiply(BigInteger.valueOf(i)); diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index 62fe5c0..491603d 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -21,7 +21,7 @@ diff --git a/tests/README.txt b/tests/README.txt index ee64f0c..84ac795 100644 --- a/tests/README.txt +++ b/tests/README.txt @@ -4,7 +4,7 @@ Run on Android with 2) Install the calculator with adb install /out/target/product/generic/data/app/ExactCalculator/ExactCalculator.apk 3) adb install /out/target/product/generic/data/app/ExactCalculatorTests/ExactCalculatorTests.apk -4) adb shell am instrument -w com.android.exactcalculator.tests/android.test.InstrumentationTestRunner +4) adb shell am instrument -w com.android.calculator2.tests/android.test.InstrumentationTestRunner There are two kinds of tests: diff --git a/tests/src/com/android/calculator2/BRTest.java b/tests/src/com/android/calculator2/BRTest.java index 77d4bf0..89906ca 100644 --- a/tests/src/com/android/calculator2/BRTest.java +++ b/tests/src/com/android/calculator2/BRTest.java @@ -145,6 +145,10 @@ public class BRTest extends TestCase { "digitsRequired(-1/2)"); check(BoundedRational.digitsRequired(new BoundedRational(1,-2)) == 1, "digitsRequired(1/-2)"); + check(BoundedRational.fact(BoundedRational.ZERO).equals(BoundedRational.ONE), "0!"); + check(BoundedRational.fact(BoundedRational.ONE).equals(BoundedRational.ONE), "1!"); + check(BoundedRational.fact(BoundedRational.TWO).equals(BoundedRational.TWO), "2!"); + check(BoundedRational.fact(BR_15).equals(new BoundedRational(1307674368000L)), "15!"); // We check values that include all interesting degree values. BoundedRational r = BR_M390; while (!r.equals(BR_390)) { -- cgit v1.2.3