xref: /freebsd/lib/libc/tests/sys/queue_test.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * Copyright (c) 2015 EMC Corp.
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/queue.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #include <atf-c.h>
33 
34 ATF_TC(slist_test);
35 ATF_TC_HEAD(slist_test, tc)
36 {
37 
38 	atf_tc_set_md_var(tc, "descr", "SLIST macro feature tests");
39 }
40 
41 ATF_TC_BODY(slist_test, tc)
42 {
43 	SLIST_HEAD(stailhead, entry) head = SLIST_HEAD_INITIALIZER(head);
44 	struct entry {
45 		SLIST_ENTRY(entry) entries;
46 		int i;
47 	} *n1, *n2, *n3, *np;
48 	int i, j, length;
49 
50 	SLIST_INIT(&head);
51 
52 	printf("Ensuring SLIST_EMPTY works\n");
53 
54 	ATF_REQUIRE(SLIST_EMPTY(&head));
55 
56 	i = length = 0;
57 
58 	SLIST_FOREACH(np, &head, entries) {
59 		length++;
60 	}
61 	ATF_REQUIRE_EQ(length, 0);
62 
63 	printf("Ensuring SLIST_INSERT_HEAD works\n");
64 
65 	n1 = malloc(sizeof(struct entry));
66 	ATF_REQUIRE(n1 != NULL);
67 	n1->i = i++;
68 
69 	SLIST_INSERT_HEAD(&head, n1, entries);
70 
71 	printf("Ensuring SLIST_FIRST returns element 1\n");
72 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
73 
74 	j = length = 0;
75 	SLIST_FOREACH(np, &head, entries) {
76 		ATF_REQUIRE_EQ_MSG(np->i, j,
77 		    "%d (entry counter) != %d (counter)", np->i, j);
78 		j++;
79 		length++;
80 	}
81 	ATF_REQUIRE_EQ(length, 1);
82 
83 	printf("Ensuring SLIST_INSERT_AFTER works\n");
84 
85 	n2 = malloc(sizeof(struct entry));
86 	ATF_REQUIRE(n2 != NULL);
87 	n2->i = i++;
88 
89 	SLIST_INSERT_AFTER(n1, n2, entries);
90 
91 	n3 = malloc(sizeof(struct entry));
92 	ATF_REQUIRE(n3 != NULL);
93 	n3->i = i++;
94 
95 	SLIST_INSERT_AFTER(n2, n3, entries);
96 
97 	j = length = 0;
98 	SLIST_FOREACH(np, &head, entries) {
99 		ATF_REQUIRE_EQ_MSG(np->i, j,
100 		    "%d (entry counter) != %d (counter)", np->i, j);
101 		j++;
102 		length++;
103 	}
104 	ATF_REQUIRE_EQ(length, 3);
105 
106 	printf("Ensuring SLIST_REMOVE_HEAD works\n");
107 
108 	printf("Ensuring SLIST_FIRST returns element 1\n");
109 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
110 
111 	SLIST_REMOVE_HEAD(&head, entries);
112 
113 	printf("Ensuring SLIST_FIRST now returns element 2\n");
114 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
115 
116 	j = 1; /* Starting point's 1 this time */
117 	length = 0;
118 	SLIST_FOREACH(np, &head, entries) {
119 		ATF_REQUIRE_EQ_MSG(np->i, j,
120 		    "%d (entry counter) != %d (counter)", np->i, j);
121 		j++;
122 		length++;
123 	}
124 	ATF_REQUIRE_EQ(length, 2);
125 
126 	printf("Ensuring SLIST_REMOVE_AFTER works by removing the tail\n");
127 
128 	SLIST_REMOVE_AFTER(n2, entries);
129 
130 	j = 1; /* Starting point's 1 this time */
131 	length = 0;
132 	SLIST_FOREACH(np, &head, entries) {
133 		ATF_REQUIRE_EQ_MSG(np->i, j,
134 		    "%d (entry counter) != %d (counter)", np->i, j);
135 		j++;
136 		length++;
137 	}
138 	ATF_REQUIRE_EQ(length, 1);
139 
140 	printf("Ensuring SLIST_FIRST returns element 2\n");
141 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
142 
143 }
144 
145 ATF_TC(stailq_test);
146 ATF_TC_HEAD(stailq_test, tc)
147 {
148 
149 	atf_tc_set_md_var(tc, "descr", "STAILQ macro feature tests");
150 }
151 
152 ATF_TC_BODY(stailq_test, tc)
153 {
154 	STAILQ_HEAD(stailhead, entry) head = STAILQ_HEAD_INITIALIZER(head);
155 	struct entry {
156 		STAILQ_ENTRY(entry) entries;
157 		int i;
158 	} *n1, *n2, *n3, *np;
159 	int i, j, length;
160 
161 	printf("Ensuring empty STAILQs are treated properly\n");
162 	STAILQ_INIT(&head);
163 	ATF_REQUIRE(STAILQ_EMPTY(&head));
164 
165 	i = length = 0;
166 
167 	STAILQ_FOREACH(np, &head, entries) {
168 		length++;
169 	}
170 	ATF_REQUIRE_EQ(length, 0);
171 
172 	printf("Ensuring STAILQ_INSERT_HEAD works\n");
173 
174 	n1 = malloc(sizeof(struct entry));
175 	ATF_REQUIRE(n1 != NULL);
176 	n1->i = i++;
177 
178 	STAILQ_INSERT_HEAD(&head, n1, entries);
179 
180 	j = length = 0;
181 	STAILQ_FOREACH(np, &head, entries) {
182 		ATF_REQUIRE_EQ_MSG(np->i, j,
183 		    "%d (entry counter) != %d (counter)", np->i, j);
184 		j++;
185 		length++;
186 	}
187 	ATF_REQUIRE_EQ(length, 1);
188 
189 	printf("Ensuring STAILQ_INSERT_TAIL works\n");
190 
191 	n2 = malloc(sizeof(struct entry));
192 	ATF_REQUIRE(n2 != NULL);
193 	n2->i = i++;
194 
195 	STAILQ_INSERT_TAIL(&head, n2, entries);
196 
197 	n3 = malloc(sizeof(struct entry));
198 	ATF_REQUIRE(n3 != NULL);
199 	n3->i = i++;
200 
201 	STAILQ_INSERT_TAIL(&head, n3, entries);
202 
203 	j = length = 0;
204 	STAILQ_FOREACH(np, &head, entries) {
205 		ATF_REQUIRE_EQ_MSG(np->i, j,
206 		    "%d (entry counter) != %d (counter)", np->i, j);
207 		j++;
208 		length++;
209 	}
210 	ATF_REQUIRE_EQ(length, 3);
211 
212 	printf("Ensuring STAILQ_REMOVE_HEAD works\n");
213 
214 	STAILQ_REMOVE_HEAD(&head, entries);
215 
216 	j = 1; /* Starting point's 1 this time */
217 	length = 0;
218 	STAILQ_FOREACH(np, &head, entries) {
219 		ATF_REQUIRE_EQ_MSG(np->i, j,
220 		    "%d (entry counter) != %d (counter)", np->i, j);
221 		j++;
222 		length++;
223 	}
224 	ATF_REQUIRE_EQ(length, 2);
225 
226 }
227 
228 ATF_TP_ADD_TCS(tp)
229 {
230 
231 	ATF_TP_ADD_TC(tp, slist_test);
232 	ATF_TP_ADD_TC(tp, stailq_test);
233 
234 	return (atf_no_error());
235 }
236