1290909Sngie/*-
2290909Sngie * Copyright (c) 2015 EMC Corp.
3290909Sngie * All rights reserved.
4290909Sngie *
5290909Sngie * Redistribution and use in source and binary forms, with or without
6290909Sngie * modification, are permitted provided that the following conditions
7290909Sngie * are met:
8290909Sngie * 1. Redistributions of source code must retain the above copyright
9290909Sngie *    notice, this list of conditions and the following disclaimer.
10290909Sngie * 2. Redistributions in binary form must reproduce the above copyright
11290909Sngie *    notice, this list of conditions and the following disclaimer in the
12290909Sngie *    documentation and/or other materials provided with the distribution.
13290909Sngie *
14290909Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15290909Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16290909Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17290909Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18290909Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19290909Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20290909Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21290909Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22290909Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23290909Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24290909Sngie * SUCH DAMAGE.
25290909Sngie */
26290909Sngie
27290909Sngie#include <sys/cdefs.h>
28290909Sngie__FBSDID("$FreeBSD$");
29290909Sngie
30290909Sngie#include <sys/queue.h>
31290909Sngie#include <stdio.h>
32290909Sngie#include <stdlib.h>
33290909Sngie
34290909Sngie#include <atf-c.h>
35290909Sngie
36290909SngieATF_TC(slist_test);
37290909SngieATF_TC_HEAD(slist_test, tc)
38290909Sngie{
39290909Sngie
40290909Sngie	atf_tc_set_md_var(tc, "descr", "SLIST macro feature tests");
41290909Sngie}
42290909Sngie
43290909SngieATF_TC_BODY(slist_test, tc)
44290909Sngie{
45290909Sngie	SLIST_HEAD(stailhead, entry) head = SLIST_HEAD_INITIALIZER(head);
46290909Sngie	struct entry {
47290909Sngie		SLIST_ENTRY(entry) entries;
48290909Sngie		int i;
49290909Sngie	} *n1, *n2, *n3, *np;
50290909Sngie	int i, j, length;
51290909Sngie
52290909Sngie	SLIST_INIT(&head);
53290909Sngie
54290909Sngie	printf("Ensuring SLIST_EMPTY works\n");
55290909Sngie
56290909Sngie	ATF_REQUIRE(SLIST_EMPTY(&head));
57290909Sngie
58290909Sngie	i = length = 0;
59290909Sngie
60290909Sngie	SLIST_FOREACH(np, &head, entries) {
61290909Sngie		length++;
62290909Sngie	}
63290909Sngie	ATF_REQUIRE_EQ(length, 0);
64290909Sngie
65290909Sngie	printf("Ensuring SLIST_INSERT_HEAD works\n");
66290909Sngie
67290909Sngie	n1 = malloc(sizeof(struct entry));
68290909Sngie	ATF_REQUIRE(n1 != NULL);
69290909Sngie	n1->i = i++;
70290909Sngie
71290909Sngie	SLIST_INSERT_HEAD(&head, n1, entries);
72290909Sngie
73290909Sngie	printf("Ensuring SLIST_FIRST returns element 1\n");
74290909Sngie	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
75290909Sngie
76290909Sngie	j = length = 0;
77290909Sngie	SLIST_FOREACH(np, &head, entries) {
78290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
79290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
80290909Sngie		j++;
81290909Sngie		length++;
82290909Sngie	}
83290909Sngie	ATF_REQUIRE_EQ(length, 1);
84290909Sngie
85290909Sngie	printf("Ensuring SLIST_INSERT_AFTER works\n");
86290909Sngie
87290909Sngie	n2 = malloc(sizeof(struct entry));
88290909Sngie	ATF_REQUIRE(n2 != NULL);
89290909Sngie	n2->i = i++;
90290909Sngie
91290909Sngie	SLIST_INSERT_AFTER(n1, n2, entries);
92290909Sngie
93290909Sngie	n3 = malloc(sizeof(struct entry));
94290909Sngie	ATF_REQUIRE(n3 != NULL);
95290909Sngie	n3->i = i++;
96290909Sngie
97290909Sngie	SLIST_INSERT_AFTER(n2, n3, entries);
98290909Sngie
99290909Sngie	j = length = 0;
100290909Sngie	SLIST_FOREACH(np, &head, entries) {
101290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
102290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
103290909Sngie		j++;
104290909Sngie		length++;
105290909Sngie	}
106290909Sngie	ATF_REQUIRE_EQ(length, 3);
107290909Sngie
108290909Sngie	printf("Ensuring SLIST_REMOVE_HEAD works\n");
109290909Sngie
110290909Sngie	printf("Ensuring SLIST_FIRST returns element 1\n");
111290909Sngie	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n1);
112290909Sngie
113290909Sngie	SLIST_REMOVE_HEAD(&head, entries);
114290909Sngie
115290909Sngie	printf("Ensuring SLIST_FIRST now returns element 2\n");
116290909Sngie	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
117290909Sngie
118290909Sngie	j = 1; /* Starting point's 1 this time */
119290909Sngie	length = 0;
120290909Sngie	SLIST_FOREACH(np, &head, entries) {
121290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
122290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
123290909Sngie		j++;
124290909Sngie		length++;
125290909Sngie	}
126290909Sngie	ATF_REQUIRE_EQ(length, 2);
127290909Sngie
128290909Sngie	printf("Ensuring SLIST_REMOVE_AFTER works by removing the tail\n");
129290909Sngie
130290909Sngie	SLIST_REMOVE_AFTER(n2, entries);
131290909Sngie
132290909Sngie	j = 1; /* Starting point's 1 this time */
133290909Sngie	length = 0;
134290909Sngie	SLIST_FOREACH(np, &head, entries) {
135290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
136290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
137290909Sngie		j++;
138290909Sngie		length++;
139290909Sngie	}
140290909Sngie	ATF_REQUIRE_EQ(length, 1);
141290909Sngie
142290909Sngie	printf("Ensuring SLIST_FIRST returns element 2\n");
143290909Sngie	ATF_REQUIRE_EQ(SLIST_FIRST(&head), n2);
144290909Sngie
145290909Sngie}
146290909Sngie
147290909SngieATF_TC(stailq_test);
148290909SngieATF_TC_HEAD(stailq_test, tc)
149290909Sngie{
150290909Sngie
151290909Sngie	atf_tc_set_md_var(tc, "descr", "STAILQ macro feature tests");
152290909Sngie}
153290909Sngie
154290909SngieATF_TC_BODY(stailq_test, tc)
155290909Sngie{
156290909Sngie	STAILQ_HEAD(stailhead, entry) head = STAILQ_HEAD_INITIALIZER(head);
157290909Sngie	struct entry {
158290909Sngie		STAILQ_ENTRY(entry) entries;
159290909Sngie		int i;
160290909Sngie	} *n1, *n2, *n3, *np;
161290909Sngie	int i, j, length;
162290909Sngie
163290909Sngie	printf("Ensuring empty STAILQs are treated properly\n");
164290909Sngie	STAILQ_INIT(&head);
165290909Sngie	ATF_REQUIRE(STAILQ_EMPTY(&head));
166290909Sngie
167290909Sngie	i = length = 0;
168290909Sngie
169290909Sngie	STAILQ_FOREACH(np, &head, entries) {
170290909Sngie		length++;
171290909Sngie	}
172290909Sngie	ATF_REQUIRE_EQ(length, 0);
173290909Sngie
174290909Sngie	printf("Ensuring STAILQ_INSERT_HEAD works\n");
175290909Sngie
176290909Sngie	n1 = malloc(sizeof(struct entry));
177290909Sngie	ATF_REQUIRE(n1 != NULL);
178290909Sngie	n1->i = i++;
179290909Sngie
180290909Sngie	STAILQ_INSERT_HEAD(&head, n1, entries);
181290909Sngie
182290909Sngie	j = length = 0;
183290909Sngie	STAILQ_FOREACH(np, &head, entries) {
184290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
185290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
186290909Sngie		j++;
187290909Sngie		length++;
188290909Sngie	}
189290909Sngie	ATF_REQUIRE_EQ(length, 1);
190290909Sngie
191290909Sngie	printf("Ensuring STAILQ_INSERT_TAIL works\n");
192290909Sngie
193290909Sngie	n2 = malloc(sizeof(struct entry));
194290909Sngie	ATF_REQUIRE(n2 != NULL);
195290909Sngie	n2->i = i++;
196290909Sngie
197290909Sngie	STAILQ_INSERT_TAIL(&head, n2, entries);
198290909Sngie
199290909Sngie	n3 = malloc(sizeof(struct entry));
200290909Sngie	ATF_REQUIRE(n3 != NULL);
201290909Sngie	n3->i = i++;
202290909Sngie
203290909Sngie	STAILQ_INSERT_TAIL(&head, n3, entries);
204290909Sngie
205290909Sngie	j = length = 0;
206290909Sngie	STAILQ_FOREACH(np, &head, entries) {
207290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
208290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
209290909Sngie		j++;
210290909Sngie		length++;
211290909Sngie	}
212290909Sngie	ATF_REQUIRE_EQ(length, 3);
213290909Sngie
214290909Sngie	printf("Ensuring STAILQ_REMOVE_HEAD works\n");
215290909Sngie
216290909Sngie	STAILQ_REMOVE_HEAD(&head, entries);
217290909Sngie
218290909Sngie	j = 1; /* Starting point's 1 this time */
219290909Sngie	length = 0;
220290909Sngie	STAILQ_FOREACH(np, &head, entries) {
221290909Sngie		ATF_REQUIRE_EQ_MSG(np->i, j,
222290909Sngie		    "%d (entry counter) != %d (counter)", np->i, j);
223290909Sngie		j++;
224290909Sngie		length++;
225290909Sngie	}
226290909Sngie	ATF_REQUIRE_EQ(length, 2);
227290909Sngie
228290909Sngie}
229290909Sngie
230290909SngieATF_TP_ADD_TCS(tp)
231290909Sngie{
232290909Sngie
233290909Sngie	ATF_TP_ADD_TC(tp, slist_test);
234290909Sngie	ATF_TP_ADD_TC(tp, stailq_test);
235290909Sngie
236290909Sngie	return (atf_no_error());
237290909Sngie}
238