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

main test-branch
Last change on this file since e2570cd 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: 2.6 KB
RevLine 
[a7065e4]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 */
[9803bc1]5#include <civlc.cvh>
[d109d6b]6#include <bundle.cvh>
[a7065e4]7
8$input int N;
[f3282f0]9$assume(N > 4);
[a7065e4]10$input int inputs1[3][N];
11$input int inputs2[N][3];
[d66b03b]12
13$scope root = $here;
14
[a7065e4]15void main(){
[d109d6b]16 int integerObj_send = 1;
17 int integerObj_recv;
18 int * p;
19 int ** pp;
20 int * buf;
21 int a3d[3][4][5];
22 int a4d[2][2][2][2];
23 $bundle bun;
24 int counter = 0;
25 int intSize = sizeof(int);
26
27 //Initialization of message data
[d66b03b]28 p = (int *)$malloc(root, sizeof(int) * 4);
29 pp = (int **)$malloc(root, sizeof(int *) * 4);
[d109d6b]30 for(int i=0; i<4; i++){
31 p[i] = i;
[d66b03b]32 pp[i] = (int *)$malloc(root, sizeof(int) * 2);
[d109d6b]33 for(int j=0; j<2; j++)
34 pp[i][j] = i * 4 + j;
[a7065e4]35 }
[d109d6b]36 //p -> p[4]; pp -> pp[4][2];
37 for(int i=0; i<3; i++)
38 for(int j=0; j<4; j++)
39 for(int k=0; k<5; k++)
40 a3d[i][j][k] = counter++;
[d66b03b]41 buf = (int *)$malloc(root, sizeof(int) * 8);
[d109d6b]42
43 // send integer object "1"
44 bun = $bundle_pack(&integerObj_send, intSize);
45 $bundle_unpack(bun, &integerObj_recv);
[3ff27cf]46 $assert(integerObj_recv == integerObj_send, "first assertion");
[d109d6b]47
48 // send p[0], p[1] and p[2] with values "0" ,"1" and "2"
49 bun = $bundle_pack(p, 3 * intSize);
50 $bundle_unpack(bun, buf);
[3ff27cf]51 $assert((buf[0] == 0 && buf[1] == 1 && buf[2] == 2), "second assertion");
[d109d6b]52
53 // send pp[1][0], pp[1][1] with values "4" and "5"
54 bun = $bundle_pack(pp[1], 2 * intSize);
55 $bundle_unpack(bun, buf);
[3ff27cf]56 $assert((buf[0] == 4 && buf[1] == 5), "third assertion");
[d109d6b]57
58 // send "23...30"
59 bun = $bundle_pack(&a3d[1][0][3], 8 * intSize);
60 $bundle_unpack(bun, &a4d[0][1][1]);
61 for(int i=0; i<8; i++)
[3ff27cf]62 $assert(*(&a4d[0][1][1][0] + i) == (23 + i), "forth assertion");
[d109d6b]63
64 // send "23...30" again
65 bun = $bundle_pack(&a3d[1][0][3], 8 * intSize);
66 // pointer type is different from the previous one
67 $bundle_unpack(bun, a4d[0][1][1]);
68 for(int i=0; i<8; i++)
[3ff27cf]69 $assert(*(&a4d[0][1][1][0] + i) == (23 + i), "fifth assertion");
[d109d6b]70
71 // send inputs2[1][1] and inputs2[1][2]
72 bun = $bundle_pack(&inputs2[1][1], 2 * intSize);
73 $bundle_unpack(bun, (buf+1));
[a7065e4]74 for(int i=0; i<2; i++)
[3ff27cf]75 $assert(*(buf + 1 + i) == inputs2[1][i+1], "sixth assertion");
[d109d6b]76
77 // send inputs1[0][0]...inputs1[0][3]
78 bun = $bundle_pack(inputs1[0], 4 * intSize);
79 $bundle_unpack(bun, (buf+1));
80 for(int i=0; i<4; i++)
[3ff27cf]81 $assert(*(buf + 1 + i) == inputs1[0][i], "seventh assertion");
[d109d6b]82
83 $free(p);
84 for(int i=0; i<4; i++)
85 $free(pp[i]);
86 $free(pp);
87 $free(buf);
[a7065e4]88}
Note: See TracBrowser for help on using the repository browser.