source: CIVL/examples/loop_invariants/loop_assigns_gen/max2.cvl

main
Last change on this file 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: 774 bytes
RevLine 
[2fa0abd]1/*
2 * COST Verification Competition. vladimir@cost-ic0701.org
3 *
4 * Challenge 1: Maximum in an array
5 *
6 * Given: A non-empty integer array a.
7 *
8 * Verify that the index returned by the method max() given below points to
9 * an element maximal in the array.
10 */
11
12#pragma CIVL ACSL
13
14$input int n;
15$assume(n > 0);
16$input int a[n];
17
18int max(int *a, int len) {
19 int x = 0;
20 int y = len-1;
21
22 /*@ loop invariant 0 <= x && x <= y && y < len ;
23 @ loop invariant \forall int i; (0 <= i && i < x || y < i && i < len)
24 @ ==> a[i] <= (a[x] > a[y] ? a[x] : a[y]);
25 @*/
26 while (x != y) {
27 if (a[x] <= a[y]) x++;
28 else y--;
29 }
30 return x;
31}
32
33int main() {
34 int ret = max(a, n);
35
36 $assert($forall (int i : 0 .. (n-1)) a[i] <= a[ret]);
37}
Note: See TracBrowser for help on using the repository browser.