| 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 | $assume(N > 4);
|
|---|
| 10 | $input int inputs1[3][N];
|
|---|
| 11 | $input int inputs2[N][3];
|
|---|
| 12 |
|
|---|
| 13 | $scope root = $here;
|
|---|
| 14 |
|
|---|
| 15 | void main(){
|
|---|
| 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
|
|---|
| 28 | p = (int *)$malloc(root, sizeof(int) * 4);
|
|---|
| 29 | pp = (int **)$malloc(root, sizeof(int *) * 4);
|
|---|
| 30 | for(int i=0; i<4; i++){
|
|---|
| 31 | p[i] = i;
|
|---|
| 32 | pp[i] = (int *)$malloc(root, sizeof(int) * 2);
|
|---|
| 33 | for(int j=0; j<2; j++)
|
|---|
| 34 | pp[i][j] = i * 4 + j;
|
|---|
| 35 | }
|
|---|
| 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++;
|
|---|
| 41 | buf = (int *)$malloc(root, sizeof(int) * 8);
|
|---|
| 42 |
|
|---|
| 43 | // send integer object "1"
|
|---|
| 44 | bun = $bundle_pack(&integerObj_send, intSize);
|
|---|
| 45 | $bundle_unpack(bun, &integerObj_recv);
|
|---|
| 46 | $assert(integerObj_recv == integerObj_send, "first assertion");
|
|---|
| 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);
|
|---|
| 51 | $assert((buf[0] == 0 && buf[1] == 1 && buf[2] == 2), "second assertion");
|
|---|
| 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);
|
|---|
| 56 | $assert((buf[0] == 4 && buf[1] == 5), "third assertion");
|
|---|
| 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++)
|
|---|
| 62 | $assert(*(&a4d[0][1][1][0] + i) == (23 + i), "forth assertion");
|
|---|
| 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++)
|
|---|
| 69 | $assert(*(&a4d[0][1][1][0] + i) == (23 + i), "fifth assertion");
|
|---|
| 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));
|
|---|
| 74 | for(int i=0; i<2; i++)
|
|---|
| 75 | $assert(*(buf + 1 + i) == inputs2[1][i+1], "sixth assertion");
|
|---|
| 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++)
|
|---|
| 81 | $assert(*(buf + 1 + i) == inputs1[0][i], "seventh assertion");
|
|---|
| 82 |
|
|---|
| 83 | $free(p);
|
|---|
| 84 | for(int i=0; i<4; i++)
|
|---|
| 85 | $free(pp[i]);
|
|---|
| 86 | $free(pp);
|
|---|
| 87 | $free(buf);
|
|---|
| 88 | }
|
|---|