5c04b376594b96e4cce2628ef4aed89829c2dbae
[trust_router.git] / common / tests / mq_test.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <assert.h>
38
39 #include <tr_mq.h>
40
41 static void notify_cb(TR_MQ *mq, void *arg)
42 {
43   char *s=(char *)arg;
44
45   printf("MQ %s no longer empty.\n", s);
46 }
47
48 int main(void)
49 {
50   TR_MQ *mq=NULL;
51   TR_MQ_MSG *msg=NULL;
52   TR_MQ_MSG *msg1=NULL;
53   TR_MQ_MSG *msg2=NULL;
54   TR_MQ_MSG *msg3=NULL;
55   TR_MQ_MSG *msg4=NULL;
56   char *mq_name="1";
57
58   mq=tr_mq_new(NULL);
59   mq->notify_cb=notify_cb;
60   mq->notify_cb_arg=mq_name;
61
62   msg1=tr_mq_msg_new(NULL,"Message 1", TR_MQ_PRIO_NORMAL);
63   assert(asprintf((char **)&(msg1->p), "First message.\n")!=-1);
64   msg1->p_free=free;
65   tr_mq_add(mq, msg1);
66   assert(mq->head==msg1);
67   assert(mq->tail==msg1);
68   assert(msg1->next==NULL);
69
70   msg2=tr_mq_msg_new(NULL, "Message 2", TR_MQ_PRIO_NORMAL);
71   assert(asprintf((char **)&(msg2->p), "Second message.\n")!=-1);
72   msg2->p_free=free;
73   tr_mq_add(mq, msg2);
74   assert(mq->head==msg1);
75   assert(msg1->next==msg2);
76   assert(mq->tail==msg2);
77   assert(msg2->next==NULL);
78
79   msg=tr_mq_pop(mq, NULL);
80   assert(msg==msg1);
81   assert(mq->head==msg2);
82   assert(mq->tail==msg2);
83   assert(msg2->next==NULL);
84   if ((msg!=NULL) && (msg->p!=NULL)) {
85     printf("%s", (char *)msg->p);
86     tr_mq_msg_free(msg);
87   } else
88     printf("no message to pop\n");
89   
90   msg3=tr_mq_msg_new(NULL, "Message 3", TR_MQ_PRIO_NORMAL);
91   assert(asprintf((char **)&(msg3->p), "%s", "Third message.\n")!=-1);
92   msg3->p_free=free;
93   tr_mq_add(mq, msg3);
94   assert(mq->head==msg2);
95   assert(mq->tail==msg3);
96   assert(msg2->next==msg3);
97   assert(msg3->next==NULL);
98
99   msg=tr_mq_pop(mq, NULL);
100   assert(msg==msg2);
101   assert(mq->head==msg3);
102   assert(mq->tail==msg3);
103   assert(msg3->next==NULL);
104   if ((msg!=NULL) && (msg->p!=NULL)) {
105     printf("%s",(char *)msg->p);
106     tr_mq_msg_free(msg);
107   } else
108     printf("no message to pop\n");
109   
110   msg=tr_mq_pop(mq, NULL);
111   assert(msg==msg3);
112   assert(mq->head==NULL);
113   assert(mq->tail==NULL);
114   if ((msg!=NULL) && (msg->p!=NULL)) {
115     printf("%s",(char *)msg->p);
116     tr_mq_msg_free(msg);
117   } else
118     printf("no message to pop\n");
119   
120   msg=tr_mq_pop(mq, NULL);
121   assert(msg==NULL);
122   assert(mq->head==NULL);
123   assert(mq->tail==NULL);
124   if ((msg!=NULL) && (msg->p!=NULL)) {
125     printf("%s",(char *)msg->p);
126     tr_mq_msg_free(msg);
127   } else
128     printf("no message to pop\n");
129
130   msg4=tr_mq_msg_new(NULL, "Message 4", TR_MQ_PRIO_NORMAL);
131   assert(asprintf((char **)&(msg4->p), "%s", "Fourth message.\n")!=-1);
132   msg4->p_free=free;
133   tr_mq_add(mq, msg4);
134   assert(mq->head==msg4);
135   assert(mq->tail==msg4);
136   assert(msg4->next==NULL);
137
138   msg=tr_mq_pop(mq, NULL);
139   assert(msg==msg4);
140   assert(mq->head==NULL);
141   assert(mq->tail==NULL);
142   if ((msg!=NULL) && (msg->p!=NULL)) {
143     printf("%s",(char *)msg->p);
144     tr_mq_msg_free(msg);
145   } else
146     printf("no message to pop\n");
147   
148   msg=tr_mq_pop(mq, NULL);
149   assert(msg==NULL);
150   assert(mq->head==NULL);
151   assert(mq->tail==NULL);
152   if ((msg!=NULL) && (msg->p!=NULL)) {
153     printf("%s",(char *)msg->p);
154     tr_mq_msg_free(msg);
155   } else
156     printf("no message to pop\n");
157
158   tr_mq_free(mq);
159
160   printf("success\n");
161   return 0;
162 }