SELKIELogger  1.0.0
QueueTest.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "SELKIELoggerBase.h"
25 
47 int main(void) {
48  msgqueue QT = {0};
49 
50  if (!queue_init(&QT)) {
51  // LCOV_EXCL_START
52  fprintf(stderr, "Failed to initialise queue\n");
53  perror("queue_init");
54  return -1;
55  // LCOV_EXCL_STOP
56  }
57 
58  int count = queue_count(&QT);
59  fprintf(stdout, "Empty queue initialised: %d messages in queue\n", count);
60  if (count != 0) {
61  // LCOV_EXCL_START
62  fprintf(stderr, "Incorrect item count for empty queue (expected 0, got %d)\n",
63  count);
64  queue_destroy(&QT);
65  return -1;
66  // LCOV_EXCL_STOP
67  }
68 
69  fprintf(stdout, "Push initial message...\n");
70  if (!queue_push(&QT, msg_new_float(1, 5, 13.0))) {
71  // LCOV_EXCL_START
72  fprintf(stderr, "Unable to push item to queue\n");
73  perror("queue_push");
74  return -1;
75  // LCOV_EXCL_STOP
76  }
77 
78  count = queue_count(&QT);
79  if (count != 1) {
80  // LCOV_EXCL_START
81  fprintf(stderr, "Incorrect item count (expected 1, got %d)\n", count);
82  return -1;
83  // LCOV_EXCL_STOP
84  }
85 
86  msg_t *out = queue_pop(&QT);
87  if (out == NULL || out->data.value != 13.0) {
88  // LCOV_EXCL_START
89  fprintf(stderr, "Failed to retrieve initial test message\n.");
90  perror("queue_pop");
91  return -1;
92  // LCOV_EXCL_STOP
93  }
94  msg_destroy(out);
95  free(out);
96  out = NULL;
97 
98  count = queue_count(&QT);
99  if (count != 0) {
100  // LCOV_EXCL_START
101  fprintf(stderr, "Incorrect item count for empty queue (expected 0, got %d)\n",
102  count);
103  queue_destroy(&QT);
104  return -1;
105  // LCOV_EXCL_STOP
106  }
107 
108  fprintf(stdout, "Push sequence of messages...\n");
109  if (!queue_push(&QT, msg_new_float(1, 5, 1.0))) {
110  // LCOV_EXCL_START
111  fprintf(stderr, "Unable to push item to queue\n");
112  perror("queue_push");
113  return -1;
114  // LCOV_EXCL_STOP
115  }
116  if (!queue_push(&QT, msg_new_float(1, 5, 2.0))) {
117  // LCOV_EXCL_START
118  fprintf(stderr, "Unable to push item to queue\n");
119  perror("queue_push");
120  return -1;
121  // LCOV_EXCL_STOP
122  }
123  if (!queue_push(&QT, msg_new_float(1, 5, 3.0))) {
124  // LCOV_EXCL_START
125  fprintf(stderr, "Unable to push item to queue\n");
126  perror("queue_push");
127  return -1;
128  // LCOV_EXCL_STOP
129  }
130  if (!queue_push(&QT, msg_new_float(1, 5, 4.0))) {
131  // LCOV_EXCL_START
132  fprintf(stderr, "Unable to push item to queue\n");
133  perror("queue_push");
134  return -1;
135  // LCOV_EXCL_STOP
136  }
137  if (!queue_push(&QT, msg_new_float(1, 5, 5.0))) {
138  // LCOV_EXCL_START
139  fprintf(stderr, "Unable to push item to queue\n");
140  perror("queue_push");
141  return -1;
142  // LCOV_EXCL_STOP
143  }
144 
145  count = queue_count(&QT);
146  if (count != 5) {
147  // LCOV_EXCL_START
148  fprintf(stderr, "Incorrect item count (expected 5, got %d)\n", count);
149  return -1;
150  // LCOV_EXCL_STOP
151  }
152 
153  while (count--) {
154  msg_t *item = queue_pop(&QT);
155  if (item->data.value != (5 - count)) {
156  // LCOV_EXCL_START
157  fprintf(stderr, "Messages out of order? (Expected value %d, got %.0f)\n",
158  (5 - count), item->data.value);
159  queue_destroy(&QT);
160  return -1;
161  // LCOV_EXCL_STOP
162  }
163  msg_destroy(item);
164  free(item);
165  }
166 
167  count = queue_count(&QT);
168  if (count != 0) {
169  // LCOV_EXCL_START
170  fprintf(stderr, "Incorrect item count (expected 0, got %d)\n", count);
171  return -1;
172  // LCOV_EXCL_STOP
173  }
174 
175  if (!queue_push(&QT, msg_new_string(1, 5, 20, "Test Message - 1234"))) {
176  // LCOV_EXCL_START
177  fprintf(stderr, "Unable to push string message to queue");
178  return -1;
179  // LCOV_EXCL_STOP
180  }
181 
182  out = queue_pop(&QT);
183  fprintf(stdout, "Logged message: %s\n", out->data.string.data);
184  msg_destroy(out);
185  free(out);
186 
187  // Destroy the now-empty queue structure
188  queue_destroy(&QT);
189 
190  // Reinitialise the queue
191  if (!queue_init(&QT)) {
192  // LCOV_EXCL_START
193  fprintf(stderr, "Failed to initialise queue\n");
194  perror("queue_init");
195  return -1;
196  // LCOV_EXCL_STOP
197  }
198 
199  if (!queue_push(&QT, msg_new_string(1, 6, 20, "Test Message - 5678"))) {
200  // LCOV_EXCL_START
201  fprintf(stderr, "Unable to push string message to queue");
202  return -1;
203  // LCOV_EXCL_STOP
204  }
205 
206  if (!queue_push(&QT, msg_new_string(1, 7, 20, "Test Message - ABCD"))) {
207  // LCOV_EXCL_START
208  fprintf(stderr, "Unable to push string message to queue");
209  return -1;
210  // LCOV_EXCL_STOP
211  }
212  // Now destroy a queue with multiple items still present
213  queue_destroy(&QT);
214 
215  count = queue_count(&QT);
216  if (count != -1) {
217  // LCOV_EXCL_START
218  fprintf(stderr,
219  "Incorrect item count for destroyed queue (expected -1 [Error], "
220  "got %d)\n",
221  count);
222  return -1;
223  // LCOV_EXCL_STOP
224  }
225 
226  queue_init(&QT);
227  if (queue_pop(&QT) != NULL) {
228  // LCOV_EXCL_START
229  fprintf(stderr, "Gained an item from a destroyed queue!\n");
230  return -1;
231  // LCOV_EXCL_STOP
232  }
233  queue_destroy(&QT);
234 
235  return 0;
236 }
int main(void)
Definition: QueueTest.c:47
msg_t * msg_new_string(const uint8_t source, const uint8_t type, const size_t len, const char *str)
Create a new message with a single string embedded.
Definition: messages.c:84
void msg_destroy(msg_t *msg)
Destroy a message.
Definition: messages.c:349
msg_t * msg_new_float(const uint8_t source, const uint8_t type, const float val)
Create new message with a single numeric value.
Definition: messages.c:38
int queue_count(const msgqueue *queue)
Iterate over queue and return current number of items.
Definition: queue.c:219
bool queue_push(msgqueue *queue, msg_t *msg)
Add a message to the tail of the queue.
Definition: queue.c:103
msg_t * queue_pop(msgqueue *queue)
Remove topmost item from the queue and return it, if queue is not empty.
Definition: queue.c:186
void queue_destroy(msgqueue *queue)
Invalidate queue and destroy all contents.
Definition: queue.c:64
bool queue_init(msgqueue *queue)
Ensure queue structure is set to known good values and marked valid.
Definition: queue.c:35
Queuable message.
Definition: messages.h:71
msg_data_t data
Embedded data.
Definition: messages.h:76
Represent a simple FIFO message queue.
Definition: queue.h:51
char * data
Character array, should be null terminated.
Definition: strarray.h:39
string string
Single character array with length.
Definition: messages.h:49
float value
Generic numerical data.
Definition: messages.h:46