int search(int x, int[] A, int n)
//@requires n == \length(A);
/*@ensures \result == -1
|| (0<=\result&&\result<n && A[\result]) == x;
@*/
{
for (int i = 0; i < n; i++)
//@loop_invariant 0 <= i;
{
if (A[i]==x) return i;
}
return -1;
}
int search(int x, int[] A, int n)
//@requires n == \length(A);
/*@ensures \result == -1 && !is_in(x, A, 0, n)
|| (0<=\result&&\result<n && A[\result]) == x;
@*/
{
for (int i = 0; i < n; i++)
//@loop_invariant 0 <= i;
{
if (A[i]==x) return i;
}
return -1;
}
Proof:
Correctness:
Correctness:
A code that satisfy correctness by editing array
so adding a deep copy? - no, we don't need to write 100% contract
so we need testing
return may not be the first index
Edge cases
first element in an array
last element of an array
empty array
1 element array
Mishandled:
TODO: why?
Table of Content