Hallo,
ich habe zu meinem Programm der catalanschen Zahlen folgenden Code geschrieben:
Der Test gibt mir folgenden Fehlercode aus:
There were 1 failures:
1) Test checkInvalidState returned:
java.lang.AssertionError: Invalid state should result in a IllegalArgumentException but it seems you are giving me something else
?at org.junit.Assert.fail(Assert.java:88)
?at CatalanRecursionTest.checkInvalidState(CatalanRecursionTest.java:130)
?at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
?at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
?at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
?at java.lang.reflect.Method.invoke(Method.java:498)
?at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
?at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
?at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
?at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.util.concurrent.FutureTask.run(FutureTask.java:266)
?at java.lang.Thread.run(Thread.java:748)
Kann sich von euch jemand vorstellen, warum? Die Exceptions werden eigentlich immer zu Anfang jedes Methodenaufrufes abgefragt. Meine einzige Idee wäre, dass die Bedingung für "i is invalid" nicht richtig ist, was meint ihr?
ich habe zu meinem Programm der catalanschen Zahlen folgenden Code geschrieben:
Java:
package programming.set8.catalan;
public class RatherExcellentRecursiveCatalanNumbersCalculationHelper {
private int totalAmountOfCalls = 0;
private int maximumDepth = 0;
private boolean sequenceChecker = true;
private int[] arrayTwo = new int[0];
private int counter = 0;
/**
* Calculate Catalan number C(n) and collect statistics along the way.
*
* @param n
* which Catalan number to calculate.
* @return the calculated Catalan number.
* @throws IllegalArgumentException
* if n < 0.
*/
public int catalan(int n) {
if (n < 0) {
throw new IllegalArgumentException("n must be positive.");
}
this.sequenceChecker = false;
this.totalAmountOfCalls += 1;
if (n >= maximumDepth) {
maximumDepth = n + 1;
int[] array = new int[maximumDepth];
this.arrayTwo = array;
}
arrayTwo[n] += 1;
int res = 0;
if (n < 1) {
return 1;
} else {
for (int k = 0; k <= n - 1; k++) {
res += catalan(k) * catalan(n - k - 1);
}
}
return res;
}
/**
* Returns the total number of Catalan numbers computed to compute the one
* requested by the user.
*
* @return number of calculated Catalan numbers.
* @throws IllegalStateException
* if this method is called before {@link #catalan(int)} was called.
*/
public int getCalculatedCatalanNumbersCount() {
if (sequenceChecker) {
throw new IllegalStateException("can´t be called before catalan(int) method.");
}
getCounter();
return counter;
}
/**
* Returns the number of times the given Catalan number was computed to compute
* the one requested by the user.
*
* @param i
* the Catalan number whose computation statistics to return.
* @return how often C(i) was computed.
* @throws IllegalStateException
* if this method is called before {@link #catalan(int)} was called.
* @throws IllegalArgumentException
* if the index i is invalid.
*/
public int getCalculatedCatalanNumberCount(int i) {
if (i >= maximumDepth || i < 0) {
throw new IllegalArgumentException("i must be > 0 and <= n.");
}
if (sequenceChecker) {
throw new IllegalStateException("can´t be called before catalan(int) method.");
}
return arrayTwo[i];
}
/**
* Returns the maximum stack depth encountered while computing the requested
* Catalan number.
*
* @return maximum stack depth.
*/
public int getMaximumStackDepth() {
return maximumDepth;
}
private int getCounter() {
this.counter = totalAmountOfCalls;
totalAmountOfCalls = 0;
return counter;
}
}
Der Test gibt mir folgenden Fehlercode aus:
There were 1 failures:
1) Test checkInvalidState returned:
java.lang.AssertionError: Invalid state should result in a IllegalArgumentException but it seems you are giving me something else
?at org.junit.Assert.fail(Assert.java:88)
?at CatalanRecursionTest.checkInvalidState(CatalanRecursionTest.java:130)
?at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
?at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
?at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
?at java.lang.reflect.Method.invoke(Method.java:498)
?at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
?at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
?at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
?at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
?at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
?at java.util.concurrent.FutureTask.run(FutureTask.java:266)
?at java.lang.Thread.run(Thread.java:748)
Kann sich von euch jemand vorstellen, warum? Die Exceptions werden eigentlich immer zu Anfang jedes Methodenaufrufes abgefragt. Meine einzige Idee wäre, dass die Bedingung für "i is invalid" nicht richtig ist, was meint ihr?