source: CIVL/examples/languageFeatures/bundleTest.cvl@ ecd2468

1.23 2.0 main test-branch
Last change on this file since ecd2468 was 3ff27cf, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

updated examples since $assert/$assume has been changed to functions; fixed the model builder for the new side-effect remover.

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

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/* Testing for bundle pack and unpack which invloves a flexible way of
2 reading and writing arrays. This example may still not cover all possible
3 cases. Anyone can add more send and receive pairs for different cases.
4 */
5#include <civlc.cvh>
6#include <bundle.cvh>
7
8$input int N;
9$input int inputs1[3][N];
10$input int inputs2[N][3];
11$assume(N > 4);
12void main(){
13 int integerObj_send = 1;
14 int integerObj_recv;
15 int * p;
16 int ** pp;
17 int * buf;
18 int a3d[3][4][5];
19 int a4d[2][2][2][2];
20 $bundle bun;
21 int counter = 0;
22 int intSize = sizeof(int);
23
24 //Initialization of message data
25 p = (int *)$malloc($root, sizeof(int) * 4);
26 pp = (int **)$malloc($root, sizeof(int *) * 4);
27 for(int i=0; i<4; i++){
28 p[i] = i;
29 pp[i] = (int *)$malloc($root, sizeof(int) * 2);
30 for(int j=0; j<2; j++)
31 pp[i][j] = i * 4 + j;
32 }
33 //p -> p[4]; pp -> pp[4][2];
34 for(int i=0; i<3; i++)
35 for(int j=0; j<4; j++)
36 for(int k=0; k<5; k++)
37 a3d[i][j][k] = counter++;
38 buf = (int *)$malloc($root, sizeof(int) * 8);
39
40 // send integer object "1"
41 bun = $bundle_pack(&integerObj_send, intSize);
42 $bundle_unpack(bun, &integerObj_recv);
43 $assert(integerObj_recv == integerObj_send, "first assertion");
44
45 // send p[0], p[1] and p[2] with values "0" ,"1" and "2"
46 bun = $bundle_pack(p, 3 * intSize);
47 $bundle_unpack(bun, buf);
48 $assert((buf[0] == 0 && buf[1] == 1 && buf[2] == 2), "second assertion");
49
50 // send pp[1][0], pp[1][1] with values "4" and "5"
51 bun = $bundle_pack(pp[1], 2 * intSize);
52 $bundle_unpack(bun, buf);
53 $assert((buf[0] == 4 && buf[1] == 5), "third assertion");
54
55 // send "23...30"
56 bun = $bundle_pack(&a3d[1][0][3], 8 * intSize);
57 $bundle_unpack(bun, &a4d[0][1][1]);
58 for(int i=0; i<8; i++)
59 $assert(*(&a4d[0][1][1][0] + i) == (23 + i), "forth assertion");
60
61 // send "23...30" again
62 bun = $bundle_pack(&a3d[1][0][3], 8 * intSize);
63 // pointer type is different from the previous one
64 $bundle_unpack(bun, a4d[0][1][1]);
65 for(int i=0; i<8; i++)
66 $assert(*(&a4d[0][1][1][0] + i) == (23 + i), "fifth assertion");
67
68 // send inputs2[1][1] and inputs2[1][2]
69 bun = $bundle_pack(&inputs2[1][1], 2 * intSize);
70 $bundle_unpack(bun, (buf+1));
71 for(int i=0; i<2; i++)
72 $assert(*(buf + 1 + i) == inputs2[1][i+1], "sixth assertion");
73
74 // send inputs1[0][0]...inputs1[0][3]
75 bun = $bundle_pack(inputs1[0], 4 * intSize);
76 $bundle_unpack(bun, (buf+1));
77 for(int i=0; i<4; i++)
78 $assert(*(buf + 1 + i) == inputs1[0][i], "seventh assertion");
79
80 $free(p);
81 for(int i=0; i<4; i++)
82 $free(pp[i]);
83 $free(pp);
84 $free(buf);
85}
Note: See TracBrowser for help on using the repository browser.