xref: /freebsd-13.1/lib/libc/tests/sys/queue_test.c (revision f443c488)
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/cdefs.h>
28*f443c488SEnji Cooper __FBSDID("$FreeBSD$");
29*f443c488SEnji Cooper 
30*f443c488SEnji Cooper #include <sys/queue.h>
31*f443c488SEnji Cooper #include <stdio.h>
32*f443c488SEnji Cooper #include <stdlib.h>
33*f443c488SEnji Cooper 
34*f443c488SEnji Cooper #include <atf-c.h>
35*f443c488SEnji Cooper 
36*f443c488SEnji Cooper ATF_TC(slist_test);
ATF_TC_HEAD(slist_test,tc)37*f443c488SEnji Cooper ATF_TC_HEAD(slist_test, tc)
38*f443c488SEnji Cooper {
39*f443c488SEnji Cooper 
40*f443c488SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "SLIST macro feature tests");
41*f443c488SEnji Cooper }
42*f443c488SEnji Cooper 
ATF_TC_BODY(slist_test,tc)43*f443c488SEnji Cooper ATF_TC_BODY(slist_test, tc)
44*f443c488SEnji Cooper {
45*f443c488SEnji Cooper 	SLIST_HEAD(stailhead, entry) head = SLIST_HEAD_INITIALIZER(head);
46*f443c488SEnji Cooper 	struct entry {
47*f443c488SEnji Cooper 		SLIST_ENTRY(entry) entries;
48*f443c488SEnji Cooper 		int i;
49*f443c488SEnji Cooper 	} *n1, *n2, *n3, *np;
50*f443c488SEnji Cooper 	int i, j, length;
51*f443c488SEnji Cooper 
52*f443c488SEnji Cooper 	SLIST_INIT(&head);
53*f443c488SEnji Cooper 
54*f443c488SEnji Cooper 	printf("Ensuring SLIST_EMPTY works\n");
55*f443c488SEnji Cooper 
56*f443c488SEnji Cooper 	ATF_REQUIRE(SLIST_EMPTY(&head));
57*f443c488SEnji Cooper 
58*f443c488SEnji Cooper 	i = length = 0;
59*f443c488SEnji Cooper 
60*f443c488SEnji Cooper 	SLIST_FOREACH(np, &head, entries) {
61*f443c488SEnji Cooper 		length++;
62*f443c488SEnji Cooper 	}
63*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 0);
64*f443c488SEnji Cooper 
65*f443c488SEnji Cooper 	printf("Ensuring SLIST_INSERT_HEAD works\n");
66*f443c488SEnji Cooper 
67*f443c488SEnji Cooper 	n1 = malloc(sizeof(struct entry));
68*f443c488SEnji Cooper 	ATF_REQUIRE(n1 != NULL);
69*f443c488SEnji Cooper 	n1->i = i++;
70*f443c488SEnji Cooper 
71*f443c488SEnji Cooper 	SLIST_INSERT_HEAD(&head, n1, entries);
72*f443c488SEnji Cooper 
73*f443c488SEnji Cooper 	printf("Ensuring SLIST_FIRST returns element 1\n");
74*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
75*f443c488SEnji Cooper 
76*f443c488SEnji Cooper 	j = length = 0;
77*f443c488SEnji Cooper 	SLIST_FOREACH(np, &head, entries) {
78*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
79*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
80*f443c488SEnji Cooper 		j++;
81*f443c488SEnji Cooper 		length++;
82*f443c488SEnji Cooper 	}
83*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 1);
84*f443c488SEnji Cooper 
85*f443c488SEnji Cooper 	printf("Ensuring SLIST_INSERT_AFTER works\n");
86*f443c488SEnji Cooper 
87*f443c488SEnji Cooper 	n2 = malloc(sizeof(struct entry));
88*f443c488SEnji Cooper 	ATF_REQUIRE(n2 != NULL);
89*f443c488SEnji Cooper 	n2->i = i++;
90*f443c488SEnji Cooper 
91*f443c488SEnji Cooper 	SLIST_INSERT_AFTER(n1, n2, entries);
92*f443c488SEnji Cooper 
93*f443c488SEnji Cooper 	n3 = malloc(sizeof(struct entry));
94*f443c488SEnji Cooper 	ATF_REQUIRE(n3 != NULL);
95*f443c488SEnji Cooper 	n3->i = i++;
96*f443c488SEnji Cooper 
97*f443c488SEnji Cooper 	SLIST_INSERT_AFTER(n2, n3, entries);
98*f443c488SEnji Cooper 
99*f443c488SEnji Cooper 	j = length = 0;
100*f443c488SEnji Cooper 	SLIST_FOREACH(np, &head, entries) {
101*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
102*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
103*f443c488SEnji Cooper 		j++;
104*f443c488SEnji Cooper 		length++;
105*f443c488SEnji Cooper 	}
106*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 3);
107*f443c488SEnji Cooper 
108*f443c488SEnji Cooper 	printf("Ensuring SLIST_REMOVE_HEAD works\n");
109*f443c488SEnji Cooper 
110*f443c488SEnji Cooper 	printf("Ensuring SLIST_FIRST returns element 1\n");
111*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
112*f443c488SEnji Cooper 
113*f443c488SEnji Cooper 	SLIST_REMOVE_HEAD(&head, entries);
114*f443c488SEnji Cooper 
115*f443c488SEnji Cooper 	printf("Ensuring SLIST_FIRST now returns element 2\n");
116*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
117*f443c488SEnji Cooper 
118*f443c488SEnji Cooper 	j = 1; /* Starting point's 1 this time */
119*f443c488SEnji Cooper 	length = 0;
120*f443c488SEnji Cooper 	SLIST_FOREACH(np, &head, entries) {
121*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
122*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
123*f443c488SEnji Cooper 		j++;
124*f443c488SEnji Cooper 		length++;
125*f443c488SEnji Cooper 	}
126*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 2);
127*f443c488SEnji Cooper 
128*f443c488SEnji Cooper 	printf("Ensuring SLIST_REMOVE_AFTER works by removing the tail\n");
129*f443c488SEnji Cooper 
130*f443c488SEnji Cooper 	SLIST_REMOVE_AFTER(n2, entries);
131*f443c488SEnji Cooper 
132*f443c488SEnji Cooper 	j = 1; /* Starting point's 1 this time */
133*f443c488SEnji Cooper 	length = 0;
134*f443c488SEnji Cooper 	SLIST_FOREACH(np, &head, entries) {
135*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
136*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
137*f443c488SEnji Cooper 		j++;
138*f443c488SEnji Cooper 		length++;
139*f443c488SEnji Cooper 	}
140*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 1);
141*f443c488SEnji Cooper 
142*f443c488SEnji Cooper 	printf("Ensuring SLIST_FIRST returns element 2\n");
143*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
144*f443c488SEnji Cooper 
145*f443c488SEnji Cooper }
146*f443c488SEnji Cooper 
147*f443c488SEnji Cooper ATF_TC(stailq_test);
ATF_TC_HEAD(stailq_test,tc)148*f443c488SEnji Cooper ATF_TC_HEAD(stailq_test, tc)
149*f443c488SEnji Cooper {
150*f443c488SEnji Cooper 
151*f443c488SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "STAILQ macro feature tests");
152*f443c488SEnji Cooper }
153*f443c488SEnji Cooper 
ATF_TC_BODY(stailq_test,tc)154*f443c488SEnji Cooper ATF_TC_BODY(stailq_test, tc)
155*f443c488SEnji Cooper {
156*f443c488SEnji Cooper 	STAILQ_HEAD(stailhead, entry) head = STAILQ_HEAD_INITIALIZER(head);
157*f443c488SEnji Cooper 	struct entry {
158*f443c488SEnji Cooper 		STAILQ_ENTRY(entry) entries;
159*f443c488SEnji Cooper 		int i;
160*f443c488SEnji Cooper 	} *n1, *n2, *n3, *np;
161*f443c488SEnji Cooper 	int i, j, length;
162*f443c488SEnji Cooper 
163*f443c488SEnji Cooper 	printf("Ensuring empty STAILQs are treated properly\n");
164*f443c488SEnji Cooper 	STAILQ_INIT(&head);
165*f443c488SEnji Cooper 	ATF_REQUIRE(STAILQ_EMPTY(&head));
166*f443c488SEnji Cooper 
167*f443c488SEnji Cooper 	i = length = 0;
168*f443c488SEnji Cooper 
169*f443c488SEnji Cooper 	STAILQ_FOREACH(np, &head, entries) {
170*f443c488SEnji Cooper 		length++;
171*f443c488SEnji Cooper 	}
172*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 0);
173*f443c488SEnji Cooper 
174*f443c488SEnji Cooper 	printf("Ensuring STAILQ_INSERT_HEAD works\n");
175*f443c488SEnji Cooper 
176*f443c488SEnji Cooper 	n1 = malloc(sizeof(struct entry));
177*f443c488SEnji Cooper 	ATF_REQUIRE(n1 != NULL);
178*f443c488SEnji Cooper 	n1->i = i++;
179*f443c488SEnji Cooper 
180*f443c488SEnji Cooper 	STAILQ_INSERT_HEAD(&head, n1, entries);
181*f443c488SEnji Cooper 
182*f443c488SEnji Cooper 	j = length = 0;
183*f443c488SEnji Cooper 	STAILQ_FOREACH(np, &head, entries) {
184*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
185*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
186*f443c488SEnji Cooper 		j++;
187*f443c488SEnji Cooper 		length++;
188*f443c488SEnji Cooper 	}
189*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 1);
190*f443c488SEnji Cooper 
191*f443c488SEnji Cooper 	printf("Ensuring STAILQ_INSERT_TAIL works\n");
192*f443c488SEnji Cooper 
193*f443c488SEnji Cooper 	n2 = malloc(sizeof(struct entry));
194*f443c488SEnji Cooper 	ATF_REQUIRE(n2 != NULL);
195*f443c488SEnji Cooper 	n2->i = i++;
196*f443c488SEnji Cooper 
197*f443c488SEnji Cooper 	STAILQ_INSERT_TAIL(&head, n2, entries);
198*f443c488SEnji Cooper 
199*f443c488SEnji Cooper 	n3 = malloc(sizeof(struct entry));
200*f443c488SEnji Cooper 	ATF_REQUIRE(n3 != NULL);
201*f443c488SEnji Cooper 	n3->i = i++;
202*f443c488SEnji Cooper 
203*f443c488SEnji Cooper 	STAILQ_INSERT_TAIL(&head, n3, entries);
204*f443c488SEnji Cooper 
205*f443c488SEnji Cooper 	j = length = 0;
206*f443c488SEnji Cooper 	STAILQ_FOREACH(np, &head, entries) {
207*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
208*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
209*f443c488SEnji Cooper 		j++;
210*f443c488SEnji Cooper 		length++;
211*f443c488SEnji Cooper 	}
212*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 3);
213*f443c488SEnji Cooper 
214*f443c488SEnji Cooper 	printf("Ensuring STAILQ_REMOVE_HEAD works\n");
215*f443c488SEnji Cooper 
216*f443c488SEnji Cooper 	STAILQ_REMOVE_HEAD(&head, entries);
217*f443c488SEnji Cooper 
218*f443c488SEnji Cooper 	j = 1; /* Starting point's 1 this time */
219*f443c488SEnji Cooper 	length = 0;
220*f443c488SEnji Cooper 	STAILQ_FOREACH(np, &head, entries) {
221*f443c488SEnji Cooper 		ATF_REQUIRE_EQ_MSG(np->i, j,
222*f443c488SEnji Cooper 		    "%d (entry counter) != %d (counter)", np->i, j);
223*f443c488SEnji Cooper 		j++;
224*f443c488SEnji Cooper 		length++;
225*f443c488SEnji Cooper 	}
226*f443c488SEnji Cooper 	ATF_REQUIRE_EQ(length, 2);
227*f443c488SEnji Cooper 
228*f443c488SEnji Cooper }
229*f443c488SEnji Cooper 
ATF_TP_ADD_TCS(tp)230*f443c488SEnji Cooper ATF_TP_ADD_TCS(tp)
231*f443c488SEnji Cooper {
232*f443c488SEnji Cooper 
233*f443c488SEnji Cooper 	ATF_TP_ADD_TC(tp, slist_test);
234*f443c488SEnji Cooper 	ATF_TP_ADD_TC(tp, stailq_test);
235*f443c488SEnji Cooper 
236*f443c488SEnji Cooper 	return (atf_no_error());
237*f443c488SEnji Cooper }
238