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