source: CIVL/examples/loop_invariants/loop_assigns_given/binarySearch.cvl@ 7320499

main test-branch
Last change on this file since 7320499 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 605 bytes
Line 
1#pragma CIVL ACSL
2
3$input int N;
4$assume(N > 0);
5$input int a[N];
6
7int binary_search(long t[], int n, long v) {
8 int l = 0, u = n-1;
9
10 /*@ loop invariant 0 <= l && u <= n-1;
11 @ loop invariant \forall integer k; 0 <= k < n &&
12 @ (t[k] == v ==> l <= k && k <= u);
13 @ loop assigns l, u;
14 @*/
15 while (l <= u) {
16 int m = (l + u) / 2;
17
18 $assert(l <= m <= u);
19 if (t[m] < v)
20 l = m + 1;
21 else if (t[m] > v)
22 u = m - 1;
23 else
24 return m;
25 }
26 return -1;
27}
28
29int main() {
30 int v = 0;
31 int ret = binary_search(a, N, v);
32}
Note: See TracBrowser for help on using the repository browser.