xref: /netbsd-src/tests/include/sys/t_pslist.c (revision d6cbc02da6308d5df9fb5486d27f972e58b2c399)
1 /*	$NetBSD: t_pslist.c,v 1.2 2019/12/01 15:28:20 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * XXX This is a limited test to make sure the operations behave as
34  * described on a sequential machine.  It does nothing to test the
35  * pserialize-safety of any operations.  The following definitions must
36  * be replaced in any parallel tests.
37  */
38 
39 #define	atomic_load_relaxed(p)		(*(p))
40 #define	atomic_load_acquire(p)		(*(p))
41 #define	atomic_load_consume(p)		(*(p))
42 #define	atomic_store_relaxed(p,v)	(*(p) = (v))
43 #define	atomic_store_release(p,v)	(*(p) = (v))
44 
45 #include <sys/pslist.h>
46 
47 #include <atf-c.h>
48 
49 ATF_TC(misc);
ATF_TC_HEAD(misc,tc)50 ATF_TC_HEAD(misc, tc)
51 {
52 	atf_tc_set_md_var(tc, "descr", "pserialize-safe list tests");
53 }
ATF_TC_BODY(misc,tc)54 ATF_TC_BODY(misc, tc)
55 {
56 	struct pslist_head h = PSLIST_INITIALIZER;
57 	struct element {
58 		unsigned i;
59 		struct pslist_entry entry;
60 	} elements[] = {
61 		{ .i = 0, .entry = PSLIST_ENTRY_INITIALIZER },
62 		{ .i = 1 },
63 		{ .i = 2 },
64 		{ .i = 3 },
65 		{ .i = 4 },
66 		{ .i = 5 },
67 		{ .i = 6 },
68 		{ .i = 7 },
69 	};
70 	struct element *element;
71 	unsigned i;
72 
73 	/* Check PSLIST_INITIALIZER is destroyable.  */
74 	PSLIST_DESTROY(&h);
75 	PSLIST_INIT(&h);
76 
77 	/* Check PSLIST_ENTRY_INITIALIZER is destroyable.  */
78 	PSLIST_ENTRY_DESTROY(&elements[0], entry);
79 
80 	for (i = 0; i < __arraycount(elements); i++)
81 		PSLIST_ENTRY_INIT(&elements[i], entry);
82 
83 	PSLIST_WRITER_INSERT_HEAD(&h, &elements[4], entry);
84 	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[2], entry);
85 	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[3], entry);
86 	PSLIST_WRITER_INSERT_BEFORE(&elements[2], &elements[1], entry);
87 	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
88 	PSLIST_WRITER_INSERT_AFTER(&elements[4], &elements[5], entry);
89 	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[7], entry);
90 	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[6], entry);
91 
92 	PSLIST_WRITER_REMOVE(&elements[0], entry);
93 	ATF_CHECK(elements[0].entry.ple_next != NULL);
94 	PSLIST_ENTRY_DESTROY(&elements[0], entry);
95 
96 	PSLIST_WRITER_REMOVE(&elements[4], entry);
97 	ATF_CHECK(elements[4].entry.ple_next != NULL);
98 	PSLIST_ENTRY_DESTROY(&elements[4], entry);
99 
100 	PSLIST_ENTRY_INIT(&elements[0], entry);
101 	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
102 
103 	PSLIST_ENTRY_INIT(&elements[4], entry);
104 	PSLIST_WRITER_INSERT_AFTER(&elements[3], &elements[4], entry);
105 
106 	i = 0;
107 	PSLIST_WRITER_FOREACH(element, &h, struct element, entry) {
108 		ATF_CHECK_EQ(i, element->i);
109 		i++;
110 	}
111 	i = 0;
112 	PSLIST_READER_FOREACH(element, &h, struct element, entry) {
113 		ATF_CHECK_EQ(i, element->i);
114 		i++;
115 	}
116 
117 	while ((element = PSLIST_WRITER_FIRST(&h, struct element, entry))
118 	    != NULL) {
119 		PSLIST_WRITER_REMOVE(element, entry);
120 		PSLIST_ENTRY_DESTROY(element, entry);
121 	}
122 
123 	PSLIST_DESTROY(&h);
124 }
125 
ATF_TP_ADD_TCS(tp)126 ATF_TP_ADD_TCS(tp)
127 {
128 
129 	ATF_TP_ADD_TC(tp, misc);
130 
131 	return atf_no_error();
132 }
133