1 #define MAIN_(x) A_CAST_2(x, _rbt)
13 static A_INLINE int_node
*int_entry(void const *node
)
15 return a_cast_r(int_node
*, a_cast_r(a_uptr
, node
) - a_offsetof(int_node
, node
)); // NOLINT
18 static int int_cmp(void const *lhs
, void const *rhs
)
20 return int_entry(lhs
)->data
- int_entry(rhs
)->data
;
23 static int intcmp(void const *lhs
, void const *rhs
)
25 return *a_int_(const *, lhs
) - *a_int_(const *, rhs
);
28 static int test(int argc
, char *argv
[])
33 unsigned int const n
= 0x1000;
35 a_str str
= A_STR_NUL
;
36 a_rbt root
= A_RBT_ROOT
;
37 int_node
*vec
= a_new(int_node
, A_NULL
, n
);
38 int *sorted
= a_new(int, A_NULL
, n
);
39 for (unsigned int i
= 0; i
< n
; ++i
)
41 a_str_putf(&str
, "%u", i
);
42 vec
[i
].data
= a_cast_s(int, a_hash_bkdr(a_str_ptr(&str
), 0));
44 sorted
[i
] = vec
[i
].data
;
45 a_rbt_insert(&root
, &vec
[i
].node
, int_cmp
);
48 debug("insert 0x%04X/0x%04X\n", i
, n
);
53 for (unsigned int i
= 0; i
< n
; ++i
)
55 TEST_BUG(a_rbt_search(&root
, &vec
[i
].node
, int_cmp
));
59 qsort(sorted
, n
, sizeof(int), intcmp
);
62 for (unsigned int i
= 0; i
< n
; ++i
)
66 a_rbt_foreach(cur
, &root
)
68 int_node
*it
= int_entry(cur
);
69 TEST_BUG(it
->data
== sorted
[x
]);
73 for (unsigned int i
= 0; i
< n
; ++i
)
75 TEST_BUG(vec
[i
].reached
);
80 for (unsigned int i
= 0; i
< n
; ++i
)
84 a_rbt_foreach_reverse(cur
, &root
)
86 int_node
*it
= int_entry(cur
);
87 TEST_BUG(it
->data
== sorted
[--x
]);
90 for (unsigned int i
= 0; i
< n
; ++i
)
92 TEST_BUG(vec
[i
].reached
);
96 for (unsigned int i
= 0; i
< n
; ++i
)
100 a_rbt_pre_foreach(cur
, &root
)
102 int_node
*it
= int_entry(cur
);
105 for (unsigned int i
= 0; i
< n
; ++i
)
107 TEST_BUG(vec
[i
].reached
);
110 for (unsigned int i
= 0; i
< n
; ++i
)
114 a_rbt_pre_foreach_reverse(cur
, &root
)
116 int_node
*it
= int_entry(cur
);
119 for (unsigned int i
= 0; i
< n
; ++i
)
121 TEST_BUG(vec
[i
].reached
);
124 for (unsigned int i
= 0; i
< n
; ++i
)
128 a_rbt_post_foreach(cur
, &root
)
130 int_node
*it
= int_entry(cur
);
133 for (unsigned int i
= 0; i
< n
; ++i
)
135 TEST_BUG(vec
[i
].reached
);
138 for (unsigned int i
= 0; i
< n
; ++i
)
142 a_rbt_post_foreach_reverse(cur
, &root
)
144 int_node
*it
= int_entry(cur
);
147 for (unsigned int i
= 0; i
< n
; ++i
)
149 TEST_BUG(vec
[i
].reached
);
152 for (unsigned int i
= 0; i
< n
; ++i
)
154 a_rbt_remove(&root
, &vec
[i
].node
);
157 debug("remove 0x%04X/0x%04X\n", i
, n
);
161 TEST_BUG(!a_rbt_search(&root
, &vec
->node
, int_cmp
));
163 for (unsigned int i
= 0; i
< n
; ++i
)
165 a_rbt_insert(&root
, &vec
[i
].node
, int_cmp
);
168 a_rbt_fortear(cur
, next
, &root
)
170 int_entry(cur
)->reached
= 1;
172 for (unsigned int i
= 0; i
< n
; ++i
)
174 TEST_BUG(vec
[i
].reached
);
182 int MAIN(int argc
, char *argv
[]) // NOLINT(misc-definitions-in-headers)
184 printf("%s\n", A_FUNC
);