source: CIVL/examples/loop_invariants/loop_assigns_given/selectSort.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: 1.1 KB
Line 
1#include <assert.h>
2#include <string.h>
3#pragma CIVL ACSL
4$input int N;
5$assume(N > 0);
6int a[N];
7int pre_a[N];
8
9/*@ predicate permut(int * p, int * q, int l, int u);
10 @*/
11
12int main() {
13 int i, j;
14
15 $havoc(&a);
16 memcpy(pre_a, a, N * sizeof(int));
17 /*@ loop invariant 0 <= i && i <= N;
18 @ loop invariant \forall int t; N-i+1<=t && t <N ==>
19 @ a[t] <= a[t-1];
20 @ loop invariant \forall int t; 0 <= t && t < N-i && i > 0 ==>
21 @ a[t] >= a[N-i];
22 @ loop invariant permut(a, pre_a, 0, N);
23 @ loop assigns i, j, a[0 .. N-1];
24 @*/
25 for (i = 0; i < N; i++) {
26 int minIdx = 0;
27 /*@ loop invariant 0<=j && j <=N-i && 0<=minIdx &&
28 @ minIdx<N-i;
29 @ loop invariant \forall int t; 0<=t && t <j ==>
30 @ a[t] >= a[minIdx];
31 @ loop assigns minIdx, j;
32 @*/
33 for (j = 0; j < N-i; j++)
34 if (a[j] < a[minIdx])
35 minIdx = j;
36
37 int tmp = a[minIdx];
38
39 a[minIdx] = a[N-i-1];
40 a[N-i-1] = tmp;
41 }
42 $assert(permut(a, pre_a, 0, N) && $forall (int i : 1 .. N-1) a[i] <= a[i-1]);
43 return 0;
44}
Note: See TracBrowser for help on using the repository browser.