/**** * * Test the recursive sum method. Use an iterative sum method as the testing * oracle. (Hmm, does this say something about how much we trust interative * solutions in Java compared to recursive solutions?) * */ public class RecursiveSumAlternativeTest { public static void main(String[] args) { RecursiveSumAlternative r = new RecursiveSumAlternative(); Integer a0[] = {}; Integer a1[] = {1}; Integer a2[] = {1,2}; Integer a3[] = {1,2,3}; Integer a4[] = {1,2,3,4,-5,6,7,9,10}; test("sum({})", r.sum(a0), sumOracle(a0)); test("sum({1})", r.sum(a1), sumOracle(a1)); test("sum({1,2})", r.sum(a2), sumOracle(a2)); test("sum({1,2,3})", r.sum(a3), sumOracle(a3)); test("sum({1,2,3,4,-5,6,7,9,10})", r.sum(a4), sumOracle(a4)); } private static int sumOracle(Integer[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } /** * Test a method call given a string value of what the call looks like, the * actual result of the call, and the expected result of the call. If the * actual and expected results are not equal, print a message to that * effect, otherwise do nothing. */ private static void test(String call, int actual, int expected) { if (actual != expected) { System.out.println( call + " returned " + actual + ", expected " + expected); } } }