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