import java.util.Arrays; import java.util.Random; public class SymmetricStackTestSelf { public static void main(String[] a) { final boolean PRINT_RESULTS = true; final int TESTS = 20; final int ROUNDSPERTEST = 2000; // Stack muss Startgröße 1 haben SymmetricStack testStack = new SymmetricStack(); SymmetricStackSolution solStack = new SymmetricStackSolution(); if (testStack.getNumberOfElements() != 0) { System.out.println("Fehler " + 01 + ": Anzahl Elemente am Anfang ist '" + testStack.getNumberOfElements() + "' statt 0."); } if (!testStack.isEmpty()) { System.out.println("Fehler " + 02 + ": Stack muss anfangs leer sein, " + "wurde aber als nicht leer erkannt ('" + testStack.isEmpty() + "')."); } if (testStack.isFull()) { System.out.println("Fehler " + 03 + ": Stack kann/darf anfangs nicht " + "voll sein, wurde aber als voll erkannt ('" + testStack.isFull() + "')."); } if (testStack.getFirst() != -1 || testStack.getLast() != -1) { System.out.println("Fehler " + 04 + ": First und Last müssen anfangs " + "-1 sein, waren: first='" + testStack.getFirst() + "', last='" + testStack.getLast() + "'."); } if (testStack.getData().length <= 0) { System.out.println("Fehler " + 05 + ": Neuer Stack muss anfangs eine " + "Länge größer/gleich 1 haben, hatte aber '" + testStack.getData().length + "'."); } testEmptyRemove(testStack, true); testEmptyRemove(testStack, false); for (int j = 0; j < TESTS; j++) { Random R = new Random(); int r; boolean suc = true; for (int i = 0; i < ROUNDSPERTEST; i++) { if (PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } if (testStack.getData().length == 0) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("Stack hat Länge ..."); System.out.println("... testStack: " + testStack.getNumberOfElements()); System.out.println("... solutionStack: " + solStack.getNumberOfElements()); suc = false; break; } else if (testStack.isEmpty() != solStack.isEmpty()) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("isEmpty():"); System.out.println("- testStack: " + testStack.isEmpty()); System.out.println("- solutionStack: " + solStack.isEmpty()); suc = false; break; } else if (testStack.isFull() != solStack.isFull()) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("isFull():"); System.out.println("- testStack: " + testStack.isFull()); System.out.println("- solutionStack: " + solStack.isFull()); suc = false; break; } else if (testStack.getFirst() != solStack.getFirst()) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("getFirst():"); System.out.println("- testStack: " + testStack.getFirst()); System.out.println("- solutionStack: " + solStack.getFirst()); suc = false; break; } else if (testStack.getLast() != solStack.getLast()) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("getLast():"); System.out.println("- testStack: " + testStack.getLast()); System.out.println("- solutionStack: " + solStack.getLast()); suc = false; break; } else if (testStack.getNumberOfElements() != solStack.getNumberOfElements()) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("getNumberOfElements():"); System.out.println("- testStack: " + testStack.getNumberOfElements()); System.out.println("- solutionStack: " + solStack.getNumberOfElements()); suc = false; break; } else { int[] sData = testStack.getData(); int[] solData = solStack.getData(); boolean different = false; for (int k = 0; k < solStack.getNumberOfElements(); k++) { if (sData[k] != solData[k]) { different = true; break; } } if (different) { if (!PRINT_RESULTS) { System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); } System.out.println("getData():"); System.out.println("- testStack:\n" + "\t- first: " + testStack.getFirst() + "\t- last: " + testStack.getLast() + "\t- data: " + Arrays.toString(sData)); System.out.println("- solutionStack:\n" + "\t- first: " + solStack.getFirst() + "\t- last: " + solStack.getLast() + "\t- data: " + Arrays.toString(solData)); suc = false; break; } } switch (R.nextInt(6)) { case 0: r = R.nextInt(200) - 100; if (PRINT_RESULTS) { System.out.println("append(" + r + ")"); } solStack.append(r); testStack.append(r); break; case 1: r = R.nextInt(200) - 100; if (PRINT_RESULTS) { System.out.println("prepend(" + r + ")"); } solStack.prepend(r); testStack.prepend(r); break; case 2: if (PRINT_RESULTS) { System.out.println("decrease()"); } solStack.decrease(); testStack.decrease(); break; case 3: if (PRINT_RESULTS) { System.out.println("increase()"); } solStack.decrease(); testStack.decrease(); break; case 4: if (PRINT_RESULTS) { System.out.println("removeFirst()"); } solStack.removeFirst(); testStack.removeFirst(); break; case 5: if (PRINT_RESULTS) { System.out.println("removeLast()"); } solStack.removeLast(); testStack.removeLast(); break; } if (!solStack.toString().equals(testStack.toString())) { suc = false; break; } } if (!suc) { System.out.println("FAILED!"); System.out.println("testStack: " + testStack); System.out.println("solutionStack: " + solStack); System.out.println("\n\n"); break; // neue Runde neues Glück } } System.out.println("Finished!"); } private static void testEmptyRemove(SymmetricStack s, boolean removeFirst) { int getNumberOfElementsB = s.getNumberOfElements(); int getFirstB = s.getFirst(); int getLastB = s.getLast(); int getDataLengthB = s.getData().length; if (removeFirst) { s.removeFirst(); } else { s.removeLast(); } int getNumberOfElementsA = s.getNumberOfElements(); int getFirstA = s.getFirst(); int getLastA = s.getLast(); int getDataLengthA = s.getData().length; if (getNumberOfElementsB != getNumberOfElementsA || getFirstA != getFirstB || getLastA != getLastB || getDataLengthB != getDataLengthA) { System.out.println("Fehler " + 06 + ": " + (removeFirst ? "removeFirst" : "removeLast") + " hat auf " + "dem leerem Stack zu einer Veränderung geführt.\n" + "- Before: getNumberOfElements='" + getNumberOfElementsB + "', getFirst='" + getFirstB + "', " + "getLast='" + getLastB + "', getData.length='" + getDataLengthB + "'\n" + "- After: getNumberOfElements='" + getNumberOfElementsA + "', getFirst='" + getFirstA + "', " + "getLast='" + getLastA + "', getData.length='" + getDataLengthA + "'"); } } }