// Program to test solutions to problem #7 on the cse142 final, winter 2019. // Fill in your solution to split, then compile and run the program import java.util.*; public class FinalTest7 { public static void split(ArrayList list) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void split2(ArrayList list) { for (int i = 0; i < list.size(); i += 2) { int n = list.remove(i); list.add(i, n / 2 + n % 2); list.add(i + 1, n / 2); } } private static int testCount, failCount; public static final int ERRORS_MAX = 10; public static void main(String[] args) { failCount = 0; for (int i = 0; i <= 15; i++) { test(i); } if (failCount == 0) { System.out.println("PASSED all " + testCount + " tests"); } } public static void test(int i) { int[] data = new int[i]; test(data, 0); } public static void test(int[] data, int index) { if (index == data.length) { testCount++; ArrayList data1 = new ArrayList(); ArrayList data2 = new ArrayList(); for (int n : data) { data1.add(n); data2.add(n); } boolean fail = false; RuntimeException except = null; try { split(data1); } catch (RuntimeException e) { fail = true; except = e; } split2(data2); fail = fail || !data1.equals(data2); if (fail) { if (except != null) { StackTraceElement[] trace = except.getStackTrace(); int line = trace[5].getLineNumber(); System.out.println("FAIL for " + Arrays.toString(data) + " with exception at line#" + line); } else { System.out.println("FAIL for " + Arrays.toString(data)); } System.out.println("correct: " + data2); System.out.println("yours : " + data1); System.out.println(); failCount++; if (failCount > ERRORS_MAX) { System.out.println("exceeds " + ERRORS_MAX + " errors"); System.out.println("with " + testCount + " correct"); System.exit(0); } } } else { data[index] = 10 + index; test(data, index + 1); data[index] = 20 + index; test(data, index + 1); } } }