1*4adfb19eSrillig /* $NetBSD: t_list.c,v 1.3 2024/07/10 20:52:32 rillig Exp $ */
2d77818a2Spgoyette
3d77818a2Spgoyette /*-
4d77818a2Spgoyette * Copyright (c) 2017 The NetBSD Foundation, Inc.
5d77818a2Spgoyette * All rights reserved.
6d77818a2Spgoyette *
7d77818a2Spgoyette * This code is derived from software contributed to The NetBSD Foundation
8d77818a2Spgoyette * by Paul Goyette
9d77818a2Spgoyette *
10d77818a2Spgoyette * Redistribution and use in source and binary forms, with or without
11d77818a2Spgoyette * modification, are permitted provided that the following conditions
12d77818a2Spgoyette * are met:
13d77818a2Spgoyette * 1. Redistributions of source code must retain the above copyright
14d77818a2Spgoyette * notice, this list of conditions and the following disclaimer.
15d77818a2Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
16d77818a2Spgoyette * notice, this list of conditions and the following disclaimer in the
17d77818a2Spgoyette * documentation and/or other materials provided with the distribution.
18d77818a2Spgoyette *
19d77818a2Spgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d77818a2Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d77818a2Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d77818a2Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d77818a2Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d77818a2Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d77818a2Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d77818a2Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d77818a2Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d77818a2Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d77818a2Spgoyette * POSSIBILITY OF SUCH DAMAGE.
30d77818a2Spgoyette */
31d77818a2Spgoyette
32d77818a2Spgoyette #include <stdlib.h>
33d77818a2Spgoyette #include <string.h>
34d77818a2Spgoyette
35d77818a2Spgoyette #include <sys/queue.h>
36d77818a2Spgoyette
37d77818a2Spgoyette #include <atf-c.h>
38d77818a2Spgoyette
39d77818a2Spgoyette /*
40d77818a2Spgoyette * XXX This is a limited test to make sure the operations behave as
41d77818a2Spgoyette * described on a sequential machine. It does nothing to test the
42d77818a2Spgoyette * pserialize-safety of any operations.
43d77818a2Spgoyette */
44d77818a2Spgoyette
45d77818a2Spgoyette ATF_TC(list_move);
ATF_TC_HEAD(list_move,tc)46d77818a2Spgoyette ATF_TC_HEAD(list_move, tc)
47d77818a2Spgoyette {
48d77818a2Spgoyette atf_tc_set_md_var(tc, "descr", "LIST_MOVE verification");
49d77818a2Spgoyette }
ATF_TC_BODY(list_move,tc)50d77818a2Spgoyette ATF_TC_BODY(list_move, tc)
51d77818a2Spgoyette {
52d77818a2Spgoyette LIST_HEAD(listhead, entry) old_head, new_head, old_copy;
53d77818a2Spgoyette struct entry {
54d77818a2Spgoyette LIST_ENTRY(entry) entries;
55d77818a2Spgoyette uint64_t value;
56d77818a2Spgoyette } *n1, *n2, *n3;
57d77818a2Spgoyette
58d77818a2Spgoyette LIST_INIT(&old_head);
59d77818a2Spgoyette
60d77818a2Spgoyette n1 = malloc(sizeof(struct entry));
61d77818a2Spgoyette n1->value = 1;
62d77818a2Spgoyette LIST_INSERT_HEAD(&old_head, n1, entries);
63d77818a2Spgoyette
64d77818a2Spgoyette n2 = malloc(sizeof(struct entry));
65d77818a2Spgoyette n2->value = 2;
66d77818a2Spgoyette LIST_INSERT_HEAD(&old_head, n2, entries);
67d77818a2Spgoyette
68a3959c1fSpgoyette LIST_MOVE(&old_head, &new_head, entries);
69d77818a2Spgoyette
70d77818a2Spgoyette memcpy(&old_copy, &old_head, sizeof(old_head));
71d77818a2Spgoyette
72d77818a2Spgoyette n3 = LIST_FIRST(&new_head);
73*4adfb19eSrillig ATF_CHECK_MSG(n3->value == 2, "Unexpected value for LIST_FIRST");
74d77818a2Spgoyette
75d77818a2Spgoyette LIST_REMOVE(n3, entries);
76d77818a2Spgoyette ATF_CHECK_MSG(memcmp(&old_copy, &old_head, sizeof(old_head)) == 0,
77d77818a2Spgoyette "Unexpected modification of old_head during LIST_REMOVE");
78d77818a2Spgoyette
79d77818a2Spgoyette LIST_REMOVE(LIST_FIRST(&new_head), entries);
80d77818a2Spgoyette ATF_CHECK_MSG(LIST_EMPTY(&new_head), "New list not empty!");
81d77818a2Spgoyette }
82d77818a2Spgoyette
ATF_TP_ADD_TCS(tp)83d77818a2Spgoyette ATF_TP_ADD_TCS(tp)
84d77818a2Spgoyette {
85d77818a2Spgoyette
86d77818a2Spgoyette ATF_TP_ADD_TC(tp, list_move);
87d77818a2Spgoyette
88d77818a2Spgoyette return atf_no_error();
89d77818a2Spgoyette }
90