]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/tests/fwp_msgtest/fwp_msgtest.c
Compilation fixes in tests
[frescor/fwp.git] / fwp / tests / fwp_msgtest / fwp_msgtest.c
1 #include "fwp_msgq.h"
2 #include "fwp_msgb.h"
3 #include "fwp_msg.h"
4 #include "fwp_contract.h"
5
6 #include <semaphore.h>
7 #include <stdio.h>
8
9 int main(int argc, char** argv)
10 {
11         struct fwp_msgq msgq;
12         struct fwp_msgb *msgb;
13         struct fwp_contract cont_a, cont_b, cont_c, cont_d;
14         int nparams;
15
16         nparams = argc;
17         fwp_msgq_init(&msgq);   
18         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
19         
20         /* prepare and enqueue the first message buffer */
21         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_contract));
22         if (!msgb) {
23                 printf("Could not allocate msgb for message A\n");
24                 return -1;
25         }else 
26                 printf("Message buffer for message A allocated\n");
27
28         cont_a.budget = 50;
29         cont_a.period_usec = 80;
30         fwp_msg_contract_in(msgb->data, &cont_a);
31
32         printf("Enqueue message A. ");
33         if (!fwp_msgq_enqueue(&msgq, msgb))
34                 printf("OK.\n");
35         else {
36                 printf("Failed! \n");
37                 return -1;
38         }
39         
40         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
41
42         /* prepare and enqueue the second message buffer */
43         msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_contract));
44         if (!msgb) {
45                 printf("Could not allocate msgb for message B\n");
46                 return -1;
47         }else 
48                 printf("Message buffer for message B allocated\n");
49
50         cont_b.budget = 30;
51         cont_b.period_usec = 100;
52         fwp_msg_contract_in(msgb->data, &cont_b);
53
54         printf("Enqueue message B. ");
55         if (!fwp_msgq_enqueue(&msgq, msgb))
56                 printf("OK.\n");
57         else {
58                 printf("Failed! \n");
59                 return -1;
60         }
61         
62         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
63         
64         /* dequeue the first message and compare with original contract*/
65         printf("Dequeue message A. ");
66         msgb = fwp_msgq_dequeue(&msgq);
67         if (!msgb) {
68                 printf("Failed! \n");
69                 return -1;
70         }else
71                 printf("OK.\n");
72
73         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
74         
75         fwp_msg_contract_out(msgb->data, &cont_c);
76         fwp_msgb_free(msgb);
77         
78         if ((cont_a.budget != cont_c.budget)||
79             (cont_a.period_usec != cont_c.period_usec)) {
80                 printf("Contracts do not match for message A.\n");
81                 return -1;
82         }
83         printf("Contracts match for message A.\n");
84         
85
86         /* dequeue the second message and compare with original contract*/
87         printf("Dequeue message B. ");
88         msgb = fwp_msgq_dequeue(&msgq);
89         if (!msgb) {
90                 printf("Failed! \n");
91                 return -1;
92         }else
93                 printf("OK.\n");
94
95         printf("in=%d out=%d pending=%d \n",msgq.in,msgq.out,msgq.nr_pending);
96         
97         fwp_msg_contract_out(msgb->data, &cont_d);
98         fwp_msgb_free(msgb);
99         
100         if ((cont_b.budget != cont_d.budget)||
101             (cont_b.period_usec != cont_d.period_usec)) {
102                 printf("Contracts do not match for message B.\n");
103                 return -1;
104         }
105         printf("Contracts match for message B.\n");
106         msgb = fwp_msgq_dequeue(&msgq);
107
108         if (msgb) {
109                 printf("Message should be empty but is is not\n");
110                 return -1;
111         }
112                 
113         printf("%s PASSED!\n", argv[0]);
114         return 0;
115 }