1/*	$NetBSD: t_pslist.c,v 1.1 2016/04/09 04:39:47 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#include <sys/pslist.h>
33
34#include <atf-c.h>
35
36/*
37 * XXX This is a limited test to make sure the operations behave as
38 * described on a sequential machine.  It does nothing to test the
39 * pserialize-safety of any operations.
40 */
41
42ATF_TC(misc);
43ATF_TC_HEAD(misc, tc)
44{
45	atf_tc_set_md_var(tc, "descr", "pserialize-safe list tests");
46}
47ATF_TC_BODY(misc, tc)
48{
49	struct pslist_head h = PSLIST_INITIALIZER;
50	struct element {
51		unsigned i;
52		struct pslist_entry entry;
53	} elements[] = {
54		{ .i = 0, .entry = PSLIST_ENTRY_INITIALIZER },
55		{ .i = 1 },
56		{ .i = 2 },
57		{ .i = 3 },
58		{ .i = 4 },
59		{ .i = 5 },
60		{ .i = 6 },
61		{ .i = 7 },
62	};
63	struct element *element;
64	unsigned i;
65
66	/* Check PSLIST_INITIALIZER is destroyable.  */
67	PSLIST_DESTROY(&h);
68	PSLIST_INIT(&h);
69
70	/* Check PSLIST_ENTRY_INITIALIZER is destroyable.  */
71	PSLIST_ENTRY_DESTROY(&elements[0], entry);
72
73	for (i = 0; i < __arraycount(elements); i++)
74		PSLIST_ENTRY_INIT(&elements[i], entry);
75
76	PSLIST_WRITER_INSERT_HEAD(&h, &elements[4], entry);
77	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[2], entry);
78	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[3], entry);
79	PSLIST_WRITER_INSERT_BEFORE(&elements[2], &elements[1], entry);
80	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
81	PSLIST_WRITER_INSERT_AFTER(&elements[4], &elements[5], entry);
82	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[7], entry);
83	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[6], entry);
84
85	PSLIST_WRITER_REMOVE(&elements[0], entry);
86	ATF_CHECK(elements[0].entry.ple_next != NULL);
87	PSLIST_ENTRY_DESTROY(&elements[0], entry);
88
89	PSLIST_WRITER_REMOVE(&elements[4], entry);
90	ATF_CHECK(elements[4].entry.ple_next != NULL);
91	PSLIST_ENTRY_DESTROY(&elements[4], entry);
92
93	PSLIST_ENTRY_INIT(&elements[0], entry);
94	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
95
96	PSLIST_ENTRY_INIT(&elements[4], entry);
97	PSLIST_WRITER_INSERT_AFTER(&elements[3], &elements[4], entry);
98
99	i = 0;
100	PSLIST_WRITER_FOREACH(element, &h, struct element, entry) {
101		ATF_CHECK_EQ(i, element->i);
102		i++;
103	}
104	i = 0;
105	PSLIST_READER_FOREACH(element, &h, struct element, entry) {
106		ATF_CHECK_EQ(i, element->i);
107		i++;
108	}
109
110	while ((element = PSLIST_WRITER_FIRST(&h, struct element, entry))
111	    != NULL) {
112		PSLIST_WRITER_REMOVE(element, entry);
113		PSLIST_ENTRY_DESTROY(element, entry);
114	}
115
116	PSLIST_DESTROY(&h);
117}
118
119ATF_TP_ADD_TCS(tp)
120{
121
122	ATF_TP_ADD_TC(tp, misc);
123
124	return atf_no_error();
125}
126