1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2023 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions #if DEVELOPMENT || DEBUG
30*bbb1b6f9SApple OSS Distributions
31*bbb1b6f9SApple OSS Distributions #include <kern/startup.h>
32*bbb1b6f9SApple OSS Distributions #include <kern/zalloc.h>
33*bbb1b6f9SApple OSS Distributions #include <sys/proc_ro.h>
34*bbb1b6f9SApple OSS Distributions #include <sys/vm.h>
35*bbb1b6f9SApple OSS Distributions
36*bbb1b6f9SApple OSS Distributions #include <tests/ktest.h>
37*bbb1b6f9SApple OSS Distributions
38*bbb1b6f9SApple OSS Distributions #include <libkern/tree.h>
39*bbb1b6f9SApple OSS Distributions
40*bbb1b6f9SApple OSS Distributions /*
41*bbb1b6f9SApple OSS Distributions * RB tree node that we use for testing.
42*bbb1b6f9SApple OSS Distributions * The nodes are compared using the numeric `rbt_id'.
43*bbb1b6f9SApple OSS Distributions */
44*bbb1b6f9SApple OSS Distributions struct rbt_test_node {
45*bbb1b6f9SApple OSS Distributions RB_ENTRY(rbt_test_node) link;
46*bbb1b6f9SApple OSS Distributions unsigned int rbt_id; /* comparison id */
47*bbb1b6f9SApple OSS Distributions unsigned int rbt_flags;
48*bbb1b6f9SApple OSS Distributions #define RBT_FLAG_IN_USE 1
49*bbb1b6f9SApple OSS Distributions #define RBT_MASK_IN_USE 1
50*bbb1b6f9SApple OSS Distributions };
51*bbb1b6f9SApple OSS Distributions
52*bbb1b6f9SApple OSS Distributions typedef struct rbt_test_node * __single rbt_test_node_t;
53*bbb1b6f9SApple OSS Distributions
54*bbb1b6f9SApple OSS Distributions /*
55*bbb1b6f9SApple OSS Distributions * Comparison function for rbt test nodes.
56*bbb1b6f9SApple OSS Distributions */
57*bbb1b6f9SApple OSS Distributions static int
rbt_cmp(struct rbt_test_node * a,struct rbt_test_node * b)58*bbb1b6f9SApple OSS Distributions rbt_cmp(struct rbt_test_node *a, struct rbt_test_node *b)
59*bbb1b6f9SApple OSS Distributions {
60*bbb1b6f9SApple OSS Distributions if (!a && !b) {
61*bbb1b6f9SApple OSS Distributions return 0;
62*bbb1b6f9SApple OSS Distributions } else if (a && !b) {
63*bbb1b6f9SApple OSS Distributions return -1;
64*bbb1b6f9SApple OSS Distributions } else if (!a && b) {
65*bbb1b6f9SApple OSS Distributions return 1;
66*bbb1b6f9SApple OSS Distributions } else if (a->rbt_id == b->rbt_id) {
67*bbb1b6f9SApple OSS Distributions return 0;
68*bbb1b6f9SApple OSS Distributions } else if (a->rbt_id < b->rbt_id) {
69*bbb1b6f9SApple OSS Distributions return -1;
70*bbb1b6f9SApple OSS Distributions } else {
71*bbb1b6f9SApple OSS Distributions return 1;
72*bbb1b6f9SApple OSS Distributions }
73*bbb1b6f9SApple OSS Distributions }
74*bbb1b6f9SApple OSS Distributions
75*bbb1b6f9SApple OSS Distributions /*
76*bbb1b6f9SApple OSS Distributions * Define a red-black tree type we are going to test.
77*bbb1b6f9SApple OSS Distributions */
78*bbb1b6f9SApple OSS Distributions RB_HEAD(_rb_test_tree, rbt_test_node);
79*bbb1b6f9SApple OSS Distributions RB_PROTOTYPE(_rb_test_tree, rbt_test_node, link, rbt_cmp)
80*bbb1b6f9SApple OSS Distributions RB_GENERATE(_rb_test_tree, rbt_test_node, link, rbt_cmp)
81*bbb1b6f9SApple OSS Distributions
82*bbb1b6f9SApple OSS Distributions /*
83*bbb1b6f9SApple OSS Distributions * Array of test nodes that we are going to use.
84*bbb1b6f9SApple OSS Distributions */
85*bbb1b6f9SApple OSS Distributions #define RBT_TEST_NODE_COUNT 7
86*bbb1b6f9SApple OSS Distributions static struct rbt_test_node test_nodes[RBT_TEST_NODE_COUNT];
87*bbb1b6f9SApple OSS Distributions static int test_node_ids[RBT_TEST_NODE_COUNT] = {88, 66, 44, 22, 0, 77, 55 };
88*bbb1b6f9SApple OSS Distributions
89*bbb1b6f9SApple OSS Distributions
90*bbb1b6f9SApple OSS Distributions static size_t
rb_tree_insert_nodes(struct _rb_test_tree * tree,size_t count)91*bbb1b6f9SApple OSS Distributions rb_tree_insert_nodes(struct _rb_test_tree *tree, size_t count)
92*bbb1b6f9SApple OSS Distributions {
93*bbb1b6f9SApple OSS Distributions unsigned int idx = 0;
94*bbb1b6f9SApple OSS Distributions if (RBT_TEST_NODE_COUNT < count) {
95*bbb1b6f9SApple OSS Distributions count = RBT_TEST_NODE_COUNT;
96*bbb1b6f9SApple OSS Distributions }
97*bbb1b6f9SApple OSS Distributions
98*bbb1b6f9SApple OSS Distributions for (idx = 0; idx < count; idx++) {
99*bbb1b6f9SApple OSS Distributions rbt_test_node_t node = &test_nodes[idx];
100*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ_INT(node->rbt_flags & RBT_MASK_IN_USE, 0, "Trying to insert a tree node that is already in use");
101*bbb1b6f9SApple OSS Distributions node->rbt_id = test_node_ids[idx];
102*bbb1b6f9SApple OSS Distributions RB_INSERT(_rb_test_tree, tree, node);
103*bbb1b6f9SApple OSS Distributions node->rbt_flags |= RBT_FLAG_IN_USE;
104*bbb1b6f9SApple OSS Distributions }
105*bbb1b6f9SApple OSS Distributions return count;
106*bbb1b6f9SApple OSS Distributions }
107*bbb1b6f9SApple OSS Distributions
108*bbb1b6f9SApple OSS Distributions static size_t
rb_tree_remove_nodes(struct _rb_test_tree * tree,size_t count)109*bbb1b6f9SApple OSS Distributions rb_tree_remove_nodes(struct _rb_test_tree *tree, size_t count)
110*bbb1b6f9SApple OSS Distributions {
111*bbb1b6f9SApple OSS Distributions unsigned int idx = 0;
112*bbb1b6f9SApple OSS Distributions if (RBT_TEST_NODE_COUNT < count) {
113*bbb1b6f9SApple OSS Distributions count = RBT_TEST_NODE_COUNT;
114*bbb1b6f9SApple OSS Distributions }
115*bbb1b6f9SApple OSS Distributions
116*bbb1b6f9SApple OSS Distributions for (idx = 0; idx < count; idx++) {
117*bbb1b6f9SApple OSS Distributions rbt_test_node_t node = &test_nodes[idx];
118*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ_INT(node->rbt_flags & RBT_MASK_IN_USE, 1, "Trying to remove a tree node that is not in use");
119*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ_INT(node->rbt_id, test_node_ids[idx], "The node id does not match the node index");
120*bbb1b6f9SApple OSS Distributions RB_REMOVE(_rb_test_tree, tree, node);
121*bbb1b6f9SApple OSS Distributions node->rbt_flags &= ~RBT_FLAG_IN_USE;
122*bbb1b6f9SApple OSS Distributions }
123*bbb1b6f9SApple OSS Distributions return count;
124*bbb1b6f9SApple OSS Distributions }
125*bbb1b6f9SApple OSS Distributions
126*bbb1b6f9SApple OSS Distributions static int
rb_tree_test_run(__unused int64_t in,int64_t * out)127*bbb1b6f9SApple OSS Distributions rb_tree_test_run(__unused int64_t in, int64_t *out)
128*bbb1b6f9SApple OSS Distributions {
129*bbb1b6f9SApple OSS Distributions struct _rb_test_tree test_tree;
130*bbb1b6f9SApple OSS Distributions RB_INIT(&test_tree);
131*bbb1b6f9SApple OSS Distributions
132*bbb1b6f9SApple OSS Distributions rb_tree_insert_nodes(&test_tree, 7);
133*bbb1b6f9SApple OSS Distributions rb_tree_remove_nodes(&test_tree, 7);
134*bbb1b6f9SApple OSS Distributions
135*bbb1b6f9SApple OSS Distributions *out = 0;
136*bbb1b6f9SApple OSS Distributions return 0;
137*bbb1b6f9SApple OSS Distributions }
138*bbb1b6f9SApple OSS Distributions
139*bbb1b6f9SApple OSS Distributions SYSCTL_TEST_REGISTER(rb_tree_test, rb_tree_test_run);
140*bbb1b6f9SApple OSS Distributions
141*bbb1b6f9SApple OSS Distributions #endif /* DEVELOPMENT || DEBUG */
142