Assertions in design by contract: Difference between revisions

(→‎{{header|Java}}: added Java)
Line 138:
One could assert an invariant in quicksort such that following the split the maximum of the small group is less than the minimum of the large group:
<pre>assert (>./LEFT) < (<./RIGHT)</pre>
 
=={{header|Java}}==
The ''-ea'' or ''-enableassertions'' option must be passed to the VM when running the application for this to work.<br>
Example taken from [[Perceptron#Java|Perceptron task]].
<lang java>
(...)
int feedForward(double[] inputs) {
assert inputs.length == weights.length : "weights and input length mismatch";
 
double sum = 0;
for (int i = 0; i < weights.length; i++) {
sum += inputs[i] * weights[i];
}
return activate(sum);
}
(...)
</lang>
 
=={{header|Racket}}==
Anonymous user