Skip to content
Snippets Groups Projects
Commit 0ab8d09d authored by Julien Signoles's avatar Julien Signoles
Browse files

[E-ACSL] preparing CSRV'14

parent 8f835658
No related branches found
No related tags found
No related merge requests found
......@@ -7,4 +7,4 @@ echo compiling $1
gcc -std=c99 -pedantic -Wall -Wno-long-long -Wno-attributes -Wno-unused-but-set-variable -fno-builtin -o $1.out $SHARE/e-acsl/e_acsl.c $SHARE/e-acsl/memory_model/e_acsl_bittree.c $SHARE/e-acsl/memory_model/e_acsl_mmodel.c $1 -lgmp
echo executing $1
./$1.out
time ./$1.out
/* ************************************************************************** */
/* instructions */
/* ************************************************************************** */
/*
check that each call to [binary_search]:
o takes as argument:
- a [length] >= 0; and
- a fully-allocated and sorted array [a] of (at least) [length] elements
o returns:
- either an index [i] s.t. a[i] == key;
- or -1 if there is no such index
*/
/* ************************************************************************** */
#define LEN 2000000
int binary_search(int* a, int length, int key) {
int low = 0, high = length - 1;
while (low<=high) {
int mid = low + (high - low) / 2;
if (a[mid] == key) return mid;
if (a[mid] < key) { low = mid + 1; }
else { high = mid - 1; }
}
return -1;
}
int main(void) {
int t[LEN];
int res, i;
for(i = 0; i < LEN; i++)
t[i] = 2 * i + 1;
res = binary_search(t, LEN, 10101);
if (res != 5050) return 1;
res = binary_search(t, LEN, 10100);
if (res != -1) return 2;
t[LEN / 4] = t[0];
// an error must be detected on the next function call
res = binary_search(t, LEN, 101);
if (res != -1) return 3;
return 0;
}
/* ************************************************************************** */
/* instructions */
/* ************************************************************************** */
/*
check that each call to [binary_search]:
o takes as argument:
- a [length] >= 0; and
- a fully-allocated and sorted array [a] of (at least) [length] elements
o returns:
- either an index [i] s.t. a[i] == key;
- or -1 if there is no such index
*/
/* ************************************************************************** */
#define LEN 2000000
/*@ requires \valid(a+(0..length-1));
requires \forall integer i; 0 <= i < length - 1 ==> a[i] <= a[i+1];
requires length >=0;
behavior exists:
assumes \exists integer i; 0 <= i < length && a[i] == key;
ensures 0 <= \result < length && a[\result] == key;
behavior not_exists:
assumes \forall integer i; 0 <= i < length ==> a[i] != key;
ensures \result == -1;
*/
int binary_search(int* a, int length, int key) {
int low = 0, high = length - 1;
while (low<=high) {
int mid = low + (high - low) / 2;
if (a[mid] == key) return mid;
if (a[mid] < key) { low = mid + 1; }
else { high = mid - 1; }
}
return -1;
}
int main(void) {
int t[LEN];
int res, i;
for(i = 0; i < LEN; i++)
t[i] = 2 * i + 1;
res = binary_search(t, LEN, 10101);
if (res != 5050) return 1;
res = binary_search(t, LEN, 10100);
if (res != -1) return 2;
t[LEN / 4] = t[0];
// an error must be detected on the next function call
res = binary_search(t, LEN, 101);
if (res != -1) return 3;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment