2 * Copyright (c) 2015 EMC Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/queue.h>
37 ATF_TC_HEAD(slist_test, tc)
40 atf_tc_set_md_var(tc, "descr", "SLIST macro feature tests");
43 ATF_TC_BODY(slist_test, tc)
45 SLIST_HEAD(stailhead, entry) head = SLIST_HEAD_INITIALIZER(head);
47 SLIST_ENTRY(entry) entries;
54 printf("Ensuring SLIST_EMPTY works\n");
56 ATF_REQUIRE(SLIST_EMPTY(&head));
60 SLIST_FOREACH(np, &head, entries) {
63 ATF_REQUIRE_EQ(length, 0);
65 printf("Ensuring SLIST_INSERT_HEAD works\n");
67 n1 = malloc(sizeof(struct entry));
68 ATF_REQUIRE(n1 != NULL);
71 SLIST_INSERT_HEAD(&head, n1, entries);
73 printf("Ensuring SLIST_FIRST returns element 1\n");
74 ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
77 SLIST_FOREACH(np, &head, entries) {
78 ATF_REQUIRE_EQ_MSG(np->i, j,
79 "%d (entry counter) != %d (counter)", np->i, j);
83 ATF_REQUIRE_EQ(length, 1);
85 printf("Ensuring SLIST_INSERT_AFTER works\n");
87 n2 = malloc(sizeof(struct entry));
88 ATF_REQUIRE(n2 != NULL);
91 SLIST_INSERT_AFTER(n1, n2, entries);
93 n3 = malloc(sizeof(struct entry));
94 ATF_REQUIRE(n3 != NULL);
97 SLIST_INSERT_AFTER(n2, n3, entries);
100 SLIST_FOREACH(np, &head, entries) {
101 ATF_REQUIRE_EQ_MSG(np->i, j,
102 "%d (entry counter) != %d (counter)", np->i, j);
106 ATF_REQUIRE_EQ(length, 3);
108 printf("Ensuring SLIST_REMOVE_HEAD works\n");
110 printf("Ensuring SLIST_FIRST returns element 1\n");
111 ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
113 SLIST_REMOVE_HEAD(&head, entries);
115 printf("Ensuring SLIST_FIRST now returns element 2\n");
116 ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
118 j = 1; /* Starting point's 1 this time */
120 SLIST_FOREACH(np, &head, entries) {
121 ATF_REQUIRE_EQ_MSG(np->i, j,
122 "%d (entry counter) != %d (counter)", np->i, j);
126 ATF_REQUIRE_EQ(length, 2);
128 printf("Ensuring SLIST_REMOVE_AFTER works by removing the tail\n");
130 SLIST_REMOVE_AFTER(n2, entries);
132 j = 1; /* Starting point's 1 this time */
134 SLIST_FOREACH(np, &head, entries) {
135 ATF_REQUIRE_EQ_MSG(np->i, j,
136 "%d (entry counter) != %d (counter)", np->i, j);
140 ATF_REQUIRE_EQ(length, 1);
142 printf("Ensuring SLIST_FIRST returns element 2\n");
143 ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
148 ATF_TC_HEAD(stailq_test, tc)
151 atf_tc_set_md_var(tc, "descr", "STAILQ macro feature tests");
154 ATF_TC_BODY(stailq_test, tc)
156 STAILQ_HEAD(stailhead, entry) head = STAILQ_HEAD_INITIALIZER(head);
158 STAILQ_ENTRY(entry) entries;
160 } *n1, *n2, *n3, *np;
163 printf("Ensuring empty STAILQs are treated properly\n");
165 ATF_REQUIRE(STAILQ_EMPTY(&head));
169 STAILQ_FOREACH(np, &head, entries) {
172 ATF_REQUIRE_EQ(length, 0);
174 printf("Ensuring STAILQ_INSERT_HEAD works\n");
176 n1 = malloc(sizeof(struct entry));
177 ATF_REQUIRE(n1 != NULL);
180 STAILQ_INSERT_HEAD(&head, n1, entries);
183 STAILQ_FOREACH(np, &head, entries) {
184 ATF_REQUIRE_EQ_MSG(np->i, j,
185 "%d (entry counter) != %d (counter)", np->i, j);
189 ATF_REQUIRE_EQ(length, 1);
191 printf("Ensuring STAILQ_INSERT_TAIL works\n");
193 n2 = malloc(sizeof(struct entry));
194 ATF_REQUIRE(n2 != NULL);
197 STAILQ_INSERT_TAIL(&head, n2, entries);
199 n3 = malloc(sizeof(struct entry));
200 ATF_REQUIRE(n3 != NULL);
203 STAILQ_INSERT_TAIL(&head, n3, entries);
206 STAILQ_FOREACH(np, &head, entries) {
207 ATF_REQUIRE_EQ_MSG(np->i, j,
208 "%d (entry counter) != %d (counter)", np->i, j);
212 ATF_REQUIRE_EQ(length, 3);
214 printf("Ensuring STAILQ_REMOVE_HEAD works\n");
216 STAILQ_REMOVE_HEAD(&head, entries);
218 j = 1; /* Starting point's 1 this time */
220 STAILQ_FOREACH(np, &head, entries) {
221 ATF_REQUIRE_EQ_MSG(np->i, j,
222 "%d (entry counter) != %d (counter)", np->i, j);
226 ATF_REQUIRE_EQ(length, 2);
233 ATF_TP_ADD_TC(tp, slist_test);
234 ATF_TP_ADD_TC(tp, stailq_test);
236 return (atf_no_error());