source: CIVL/examples/languageFeatures/functionPointer.cvl@ b51463d

1.23 2.0 main test-branch
Last change on this file since b51463d was 5caec96, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

added assert1.cvl.

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

  • Property mode set to 100644
File size: 1.7 KB
Line 
1/* This program demonstrates the use of function pointer, checking
2 * of the equality of $proc and $scope type expressions, and the
3 * use of system functions $proc_defined/$scope_defined to check if
4 * a $proc/$scope expression is defined.
5 * Command line example:
6 * civl verify functionPointer.cvl
7 * or
8 * civl verify functionPointer.cvl -enablePrintf=false
9 * (if you do not like to see the message printed by those printf function calls.)
10 */
11
12#include <civlc.h>
13#include <stdio.h>
14$proc procs[4];
15$scope scopes[4];
16
17int min(int a, int b) {
18 if (a < b)
19 return a;
20 else
21 return b;
22}
23
24int fDouble(int (*f)(int, int), int a, int b) {
25 int result;
26
27 result = f(a, b);
28 return result * 2;
29}
30
31// f is a function pointer type argument
32$proc proc_create(void (*f)(int), int x){
33 $proc p = $spawn f(x);
34
35 return p;
36}
37
38void foo(int id) {
39 printf("I'm spawned with id %d.\n", id);
40 $assert(procs[id] == $self);
41 scopes[id] = $here;
42 if($scope_defined(scopes[id]))
43 printf("I own scope with id %d.\n", scopes[id]);
44 else
45 printf("Error: my scope is gone!\n");
46}
47
48void main(){
49 // min here is a function pointer
50 int k = fDouble(min, 5, 8);
51
52 for(int i = 0; i < 4; i++){
53 procs[i] = proc_create(foo, i);
54 }
55
56 for(int i = 0; i < 4; i++){
57 $wait(procs[i]);
58 printf("Process %d terminates.\n", i);
59 if($proc_defined(procs[i]))
60 printf("Process %d is not removed!\n", i);
61 else
62 printf("Process %d now has invalid reference: %d\n", i, procs[i]);
63 }
64
65 for(int i = 0; i < 4; i++){
66 _Bool defined = $proc_defined(procs[i]);
67
68 $assert(!defined);
69 defined = $scope_defined(scopes[i]);
70 $assert(!defined);
71 }
72}
Note: See TracBrowser for help on using the repository browser.