package edu.rice.hj.example.comp322.assignments.hw2; import edu.rice.hj.api.HjMetrics; import edu.rice.hj.runtime.config.HjSystemProperty; import edu.rice.hj.runtime.metrics.AbstractMetricsManager; import static edu.rice.hj.Module1.abstractMetrics; import static edu.rice.hj.Module1.launchHabaneroApp; /** * This class contains a test harness for the SumReduction client. It assumes availability of a completed * GeneralizedReduce class to be implemented by you. *

* This is a test class for your homework and should not be modified. * * @author Vivek Sarkar (vsarkar@rice.edu) */ public class TestSumReduction { /** *

main.

* * @param args an array of {@link String} objects. */ public static void main(final String[] args) { int n; if (args.length == 1) { n = Integer.parseInt(args[0]); } else { throw new RuntimeException("Usage: run TestSumReduction n"); } final Integer[] inputArray = new Integer[n]; for (int i = 0; i < n; i++) { inputArray[i] = i; } final Integer correctSum = calculateCorrectSum(inputArray); HjSystemProperty.abstractMetrics.set(true); launchHabaneroApp(() -> { reduction(inputArray, correctSum); }); final HjMetrics actualMetrics = abstractMetrics(); AbstractMetricsManager.dumpStatistics(actualMetrics); } /** * Calculate the sum of the input array using array reduction. * * @param inputArray The input array * @param correctSum a {@link Integer} object. */ public static void reduction( final Integer[] inputArray, final Integer correctSum) { final GeneralizedReduceApp app = new SumReduction(); final GeneralizedReduce red = new GeneralizedReduce<>(app); final Integer output = red.run(inputArray); System.out.println("Correct output = " + correctSum); System.out.println("Reduced output = " + output); if (correctSum.equals(output)) { System.out.println("Reduction SUCCEEDED"); } else { System.out.println("Reduction FAILED"); } } /** * Sum the elements in the array. * * @param inputArray An array of integers * @return The sum of all elements in the array */ public static Integer calculateCorrectSum(final Integer[] inputArray) { int sum = 0; for (final Integer loopItem : inputArray) { sum += loopItem; } return sum; } }