// Program to test solutions to problem #10 on the cse143x final, autumn 2017. // Fill in your solution to duplicatePairs, then compile and run the program import java.util.*; class LinkedIntList { public void duplicatePairs() { } private ListNode front; // first value in the list // this is the sample solution public void duplicatePairs2() { ListNode curr = front; while (curr != null && curr.next != null) { // create duplicate pair ListNode second = new ListNode(curr.next.data, curr.next.next); ListNode first = new ListNode(curr.data, second); // insert into list curr.next.next = first; // jump to next node curr = curr.next.next.next.next; } } // post: constructs an empty list public LinkedIntList() { front = null; } public boolean isEmpty() { return front == null; } // post: returns the current number of elements in the list public int size() { int count = 0; ListNode current = front; while (current != null) { current = current.next; count++; } return count; } // pre : 0 <= index < size() // post: returns the integer at the given index in the list public int get(int index) { ListNode current = front; for (int i = 0; i < index; i++) current = current.next; return current.data; } // post: creates a comma-separated, bracketed version of the list public String toString() { if (front == null) return "[]"; else { String result = "[" + front.data; ListNode current = front.next; while (current != null) { result += ", " + current.data; current = current.next; } result += "]"; return result; } } // post : returns the position of the first occurence of the given // value (-1 if not found) public int indexOf(int value) { int index = 0; ListNode current = front; while (current != null) { if (current.data == value) return index; index++; current = current.next; } return -1; } // post: appends the given value to the end of the list public void add(int value) { if (front == null) front = new ListNode(value); else { ListNode current = front; while (current.next != null) current = current.next; current.next = new ListNode(value); } } // pre: 0 <= index <= size() // post: inserts the given value at the given index, shifting subsequent // values right public void add(int index, int value) { if (index == 0) front = new ListNode(value, front); else { ListNode current = front; for (int i = 0; i < index - 1; i++) current = current.next; current.next = new ListNode(value, current.next); } } // pre : 0 <= index < size() // post: removes value at the given index, shifting subsequent values left public void remove(int index) { if (index == 0) front = front.next; else { ListNode current = front; for (int i = 0; i < index - 1; i++) current = current.next; current.next = current.next.next; } } } public class FinalTest10 { private static class TestResult { public final boolean pass; public final int setOtherToNullCount; public final int numTests; public TestResult(boolean pass, int setOther, int numTests) { this.pass = pass; this.setOtherToNullCount = setOther; this.numTests = numTests; } } private static int[][] emptyList = {{}}; private static int[][] oneElement = {{3}, {9}, {-12}}; private static int[][] twoElements = { {18, 5}, {22, 7}, {-3, 0}, {9, 9} }; private static int[][] fiveElements = { {2, 4, 6, 0, 1}, {1, 2, 3, 4, 5}, {-6, -2, -15, 0, 0}, }; private static int[][] sixElements = { {4, 8, 15, 16, 23, 42}, {-5, -4, -3, -2, -1, 0}, {-7, -7, -7, -7, -7, -7} }; private static int[][] sevenElements = { {8, 6, 7, 5, 3, 0, 9}, {18, 4, 27, 9, 54, 5, 63}, {0, -9, 3, -12, 0, 0, 8} }; private static int[][] twelveElements = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {-6, -11, -2, -1, -9, -8, -3, -1, -6, -4, -7, 0} }; private static int[][] nineteenElements = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, {-12, -6, 3, -5, 9, -10, 5, 14, 5, -10, -1, 0, -15, 7, -12, 12, 0, 5, -12}, {14, 12,-11, -10, -4, -1, 15, -4, -10, -15, -9, 14, -3, -4, 5, 10, -3, 1, -1} }; private static int count = 0; // test counter for arrays private static int totalFail = 0; private static int otherNull = 0; public static void main(String[] args) { // int[][][][] cases = {case1, case2, case3, case4, case5, case6, case7, case8}; int[][][] cases = {emptyList, oneElement, twoElements, fiveElements, sixElements, sevenElements}; System.out.println("TEST FOR NULL list1 CASE"); // TestResult nullRun = test(emptyList); // boolean nullPass = nullRun.pass; // TestResult otherRun = test(nullOther); // boolean otherPass = otherRun.pass; // boolean otherPass = true; int numSetNull = 0; int numTests = 0; int numPass = 0; for (int i = 0; i < cases.length; i++) { System.out.println("TEST FOR CASE " + (i + 1)); TestResult run = test(cases[i]); if (run.pass) { numPass++; } numTests += run.numTests; numSetNull += run.setOtherToNullCount; System.out.println(); } System.out.println(); // System.out.println("Set null in " + numSetNull + " / " + numTests + " cases"); // System.out.println("Works when front == null? " + (nullPass ? "yup" : "nope")); // System.out.println("Works when other.front == null? " + (otherPass ? "yup" : "nope")); System.out.println("Score: " + numPass + " / " + cases.length); /*for (int i = 1; i <= cases.length * 2; i++) { System.out.println("case #" + i); test(i); } if (totalFail > 0) { System.out.println("failed " + totalFail + " of " + count + " tests"); } else { System.out.println("passed all tests"); } if (otherNull != 0) { System.out.println(); int nullCases = count - 1; System.out.println("other.front not set to null in " + otherNull + " of " + nullCases + " cases"); }*/ } public static TestResult test(int[][] cases) { boolean pass = true; int setCount = 0; for (int i = 0; i < cases.length; i++) { TestResult run = test(cases[i]); setCount += run.setOtherToNullCount; if (!run.pass) { pass = false; } } return new TestResult(pass, setCount, cases.length); } // public static void test(int number) { // int index = (number - 1) / 2; // int[] data1 = cases[index][(number - 1) % 2]; // int[] data2 = cases[index][number % 2]; // test(data1, data2); // } public static TestResult test(int[] data1) { // list1/list2 for testing sample, list3/list4 for their code try { LinkedIntList list1 = new LinkedIntList(); // LinkedIntList list2 = new LinkedIntList(); LinkedIntList list3 = new LinkedIntList(); // LinkedIntList list4 = new LinkedIntList(); for (int n : data1) { list1.add(n); list3.add(n); } // for (int n : data2) { // list2.add(n); // list4.add(n); // } boolean fail = false; boolean setOther = true; System.out.println("list1 = " + list1); // System.out.println("list2 = " + list2); list1.duplicatePairs2(); System.out.println("expected = " + list1); boolean threw = false; try { list3.duplicatePairs(); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); threw = true; } String str1 = list1.toString(); String str3 = list3.toString(); if (!fail && !str1.equals(str3)) { fail = true; System.out.println("actual = " + str3); } if (fail || threw) { System.out.println("failed"); totalFail++; } else { System.out.println("passed"); } // if (!list4.isEmpty()) { // System.out.println("list4 = " + list4); // System.out.println("other.front not set to null"); // otherNull++; // setOther = false; // } System.out.println(); count++; // increment test counter return new TestResult(!fail, setOther ? 1 : 0, 1); } catch (Exception e) { System.out.println("Exception!"); e.printStackTrace(); return new TestResult(false, 0, 1); } } } class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } }