view src/queue_test.c @ 5:e117c4d93602

Added list - a doubly linked list. Fixed memory leaks in some of the test drivers.
author Eris Caffee <discordia@eldalin.com>
date Fri, 21 Sep 2012 18:42:49 -0500
parents 392ce56806f9
children 68f85ffc6029
line source
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "queue.h"
6 #define MAX 10
8 int main(int argc, char**argv)
9 {
10 int i, j;
11 int *ip;
12 struct queue * q;
14 q = queue_new(MAX);
15 if (NULL == q)
16 {
17 perror("queue_new");
18 exit(EXIT_FAILURE);
19 }
21 // Fill the queue
22 for (i=0; i < MAX; i++)
23 {
24 ip = malloc(sizeof(int));
25 *ip = i;
26 if (NULL == queue_push(q, (void *) ip))
27 perror("queue_push");
28 }
30 // remove 5 items
31 printf("queue size is %zd\n", queue_size(q));
32 for (j=0; j < 5 && NULL != (ip = queue_pop(q)); j++)
33 {
34 printf("%d\n", *ip);
35 free(ip);
36 }
37 printf("queue size is %zd\n", queue_size(q));
39 // Overfill the queue
40 for (i=MAX; i < 2*MAX; i++)
41 {
42 ip = malloc(sizeof(int));
43 *ip = i;
44 if (NULL == queue_push(q, (void *) ip))
45 {
46 perror("queue_push");
47 free(ip);
48 }
49 }
51 // Empty the queue
52 printf("queue size is %zd\n", queue_size(q));
53 while (NULL != (ip = queue_pop(q)))
54 {
55 printf("%d\n", *ip);
56 free(ip);
57 }
58 printf("queue size is %zd\n", queue_size(q));
60 queue_delete(q);
62 exit(EXIT_SUCCESS);
63 }