source: CIVL/examples/loop_invariants/loop_assigns_gen/binarySearch.cvl@ bb03188

main test-branch
Last change on this file since bb03188 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: 575 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 @*/
14 while (l <= u) {
15 int m = (l + u) / 2;
16
17 $assert(l <= m <= u);
18 if (t[m] < v)
19 l = m + 1;
20 else if (t[m] > v)
21 u = m - 1;
22 else
23 return m;
24 }
25 return -1;
26}
27
28int main() {
29 int v = 0;
30 int ret = binary_search(a, N, v);
31}
Note: See TracBrowser for help on using the repository browser.