// Program to test solutions to problem #9 on the cse142 final, autumn 2015. // Fill in your solution to splice, then compile and run the program. import java.util.*; public class FinalTest9 { public static int[] splice(int[] list, int from, int to) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static int[] splice2(int[] list, int from, int to) { int[] result = new int[list.length]; int index = 0; for (int i = to; i < list.length; i++) { result[index] = list[i]; index++; } for (int i = from; i < to; i++) { result[index] = list[i]; index++; } for (int i = 0; i < from; i++) { result[index] = list[i]; index++; } return result; } private static int testCount, failCount; public static final int ERRORS_MAX = 25; public static void main(String[] args) { failCount = 0; test(new int[] {0, 1, 2, 3, 4}); test(new int[] {0, 1, 2, 3, 4, 5}); test(new int[] {0, 1, 2, 3, 4, 5, 6}); test(new int[] {0, 1, 2, 3, 4, 5, 6, 7}); test(new int[] {13, 5, 4, 2, 9, 8, 42}); test(new int[] {13, 5, 4, 2, 9, 8}); test(new int[] {13, 5, 4, 2, 9}); test(new int[] {13, 5, 4, 2}); test(new int[] {13, 5, 4}); test(new int[] {13, 5}); test(new int[] {13}); test(new int[] {}); if (failCount == 0) { System.out.println("PASSED all " + testCount + " tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data) { int[] copy = Arrays.copyOf(data, data.length); for (int i = 0; i < data.length; i++) { for (int j = i; j <= data.length; j++) { testCount++; int[] result2 = splice2(data, i, j); int[] result1 = null; boolean fail = false; RuntimeException except = null; try { result1 = splice(data, i, j); } catch (RuntimeException e) { fail = true; except = e; } fail = fail || !Arrays.equals(result1, result2) || !Arrays.equals(data, copy); if (fail) { System.out.println("testing splice(" + Arrays.toString(copy) + ", " + i + ", " + j + ")"); System.out.println(parts(copy, i, j)); if (except != null) { StackTraceElement[] trace = except.getStackTrace(); int line = trace[0].getLineNumber(); System.out.println("Threw exception at line#" + line); } else { if (!Arrays.equals(result1, result2)) { System.out.println("correct: " + Arrays.toString(result2)); System.out.println("yours : " + Arrays.toString(result1)); } if (!Arrays.equals(data, copy)) { System.out.println("data was: " + Arrays.toString(copy)); System.out.println("data now: " + Arrays.toString(data)); data = Arrays.copyOf(copy, copy.length); } } System.out.println(); failCount++; if (failCount > ERRORS_MAX) { System.out.println("exceeds " + ERRORS_MAX + " errors"); System.out.println("with " + (testCount - failCount) + " correct"); System.exit(0); } } } } } public static String parts(int[] list, int from, int to) { return Arrays.toString(Arrays.copyOfRange(list, 0, from)) + " " + Arrays.toString(Arrays.copyOfRange(list, from, to)) + " " + Arrays.toString(Arrays.copyOfRange(list, to, list.length)); } }