view src/list_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
children abdba37f67a2
line source
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "list.h"
6 #define MAX 10
8 int main(int argc, char** argv)
9 {
10 struct list * l = list_new();
11 if (NULL == l)
12 {
13 perror("list_new");
14 exit(EXIT_FAILURE);
15 }
18 /////////////////////////////////////
19 // Push and pop from back
20 for (int i = 0; i < MAX ; i++)
21 {
22 int * ip = malloc(sizeof(int));
23 if (NULL == ip)
24 {
25 perror("malloc");
26 exit(EXIT_FAILURE);
27 }
29 *ip = i;
30 if (NULL == list_push_back(l, ip))
31 {
32 perror("list_push_back");
33 exit(EXIT_FAILURE);
34 }
35 }
37 printf("size %zd\n", list_size(l));
38 while (0 != list_size(l))
39 {
40 int * ip = list_pop_back(l);
41 printf("%d\n", *ip);
42 free(ip);
43 }
45 /////////////////////////////////////
46 // Push and pop from front
47 for (int i = 0; i < MAX ; i++)
48 {
49 int * ip = malloc(sizeof(int));
50 if (NULL == ip)
51 {
52 perror("malloc");
53 exit(EXIT_FAILURE);
54 }
56 *ip = i;
57 if (NULL == list_push_front(l, ip))
58 {
59 perror("list_push_front");
60 exit(EXIT_FAILURE);
61 }
62 }
64 printf("size %zd\n", list_size(l));
65 while (0 != list_size(l))
66 {
67 int * ip = list_pop_front(l);
68 printf("%d\n", *ip);
69 free(ip);
70 }
73 /////////////////////////////////////
74 // Push back and pop back
75 for (int i = 0; i < MAX ; i++)
76 {
77 int * ip = malloc(sizeof(int));
78 if (NULL == ip)
79 {
80 perror("malloc");
81 exit(EXIT_FAILURE);
82 }
84 *ip = i;
85 if (NULL == list_push_back(l, ip))
86 {
87 perror("list_push_back");
88 exit(EXIT_FAILURE);
89 }
90 }
92 printf("size %zd\n", list_size(l));
93 while (0 != list_size(l))
94 {
95 int * ip = list_pop_front(l);
96 printf("%d\n", *ip);
97 free(ip);
98 }
100 /////////////////////////////////////
101 // push back, walk front to back
102 for (int i = 0; i < MAX ; i++)
103 {
104 int * ip = malloc(sizeof(int));
105 if (NULL == ip)
106 {
107 perror("malloc");
108 exit(EXIT_FAILURE);
109 }
111 *ip = i;
112 if (NULL == list_push_back(l, ip))
113 {
114 perror("list_push_back");
115 exit(EXIT_FAILURE);
116 }
117 }
119 printf("size %zd\n", list_size(l));
121 struct list_iterator lit;
122 int * ip;
124 printf("Walking front to back\n");
125 for (ip = list_begin(l, &lit); NULL != ip; ip = list_next( &lit))
126 printf("%d\n", *ip);
128 printf("Inserting 15 after 5\n");
129 for (ip = list_begin(l, &lit); NULL != ip; ip = list_next( &lit))
130 {
131 if (5 == *ip)
132 {
133 int * newip = malloc(sizeof(int));
134 if (NULL == newip)
135 {
136 perror("malloc");
137 exit(EXIT_FAILURE);
138 }
139 *newip = 15;
140 if (NULL == list_insert_after(&lit, newip))
141 {
142 perror("malloc");
143 exit(EXIT_FAILURE);
144 }
145 break;
146 }
147 }
149 printf("Inserting 20 before 8\n");
150 for (ip = list_begin(l, &lit); NULL != ip; ip = list_next( &lit))
151 {
152 if (8 == *ip)
153 {
154 int * newip = malloc(sizeof(int));
155 if (NULL == newip)
156 {
157 perror("malloc");
158 exit(EXIT_FAILURE);
159 }
160 *newip = 20;
161 if (NULL == list_insert_before(&lit, newip))
162 {
163 perror("malloc");
164 exit(EXIT_FAILURE);
165 }
166 break;
167 }
168 }
170 printf("size %zd\n", list_size(l));
172 printf("Walking back to front\n");
173 for (ip = list_end(l, &lit); NULL != ip ; ip = list_prev(&lit))
174 printf("%d\n", *ip);
177 printf("Erasing front to back\n");
178 list_begin(l, &lit);
179 while (NULL != (ip = list_value(&lit)))
180 {
181 printf("%d\n", *ip);
182 list_erase(&lit);
183 free(ip);
184 }
185 printf("size %zd\n", list_size(l));
187 list_delete(l);
188 exit(EXIT_SUCCESS);
190 }