lib/gssapi/krb5: implement GSS_C_CHANNEL_BOUND_FLAG for gss_init_sec_context()
[heimdal.git] / lib / roken / tsearch-test.c
blobb9d99635a8e4789c0ca51d351ff3932b04f2bd61
1 /*
2 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3 * the AT&T man page says.
5 * The node_t structure is for internal use only, lint doesn't grok it.
7 * Written by reading the System V Interface Definition, not the code.
9 * Totally public domain.
12 #include <config.h>
14 #include "roken.h"
15 #include "search.h"
17 struct node {
18 char *string;
19 int order;
22 extern void *rk_tdelete(const void *, void **,
23 int (*)(const void *, const void *));
24 extern void *rk_tfind(const void *, void * const *,
25 int (*)(const void *, const void *));
26 extern void *rk_tsearch(const void *, void **, int (*)(const void *, const void *));
27 extern void rk_twalk(const void *, void (*)(const void *, VISIT, int));
29 void *rootnode = NULL;
30 int numerr = 0;
33 * This routine compares two nodes, based on an
34 * alphabetical ordering of the string field.
36 int
37 node_compare(const void *node1, const void *node2)
39 return strcmp(((const struct node *) node1)->string,
40 ((const struct node *) node2)->string);
43 static int walkorder = -1;
45 void
46 list_node(const void *ptr, VISIT order, int level)
48 const struct node *p = *(const struct node **) ptr;
50 if (order == postorder || order == leaf) {
51 walkorder++;
52 if (p->order != walkorder) {
53 warnx("sort failed: expected %d next, got %d\n", walkorder,
54 p->order);
55 numerr++;
60 int
61 main(int argc, char **argv)
63 int numtest = 1;
64 struct node *t, *p, tests[] = {
65 { "", 0 },
66 { "ab", 3 },
67 { "abc", 4 },
68 { "abcdefg", 8 },
69 { "abcd", 5 },
70 { "a", 2 },
71 { "abcdef", 7 },
72 { "abcde", 6 },
73 { "=", 1 },
74 { NULL }
77 for(t = tests; t->string; t++) {
78 /* Better not be there */
79 p = (struct node *)rk_tfind((void *)t, (void **)&rootnode,
80 node_compare);
82 if (p) {
83 warnx("erroneous list: found %d\n", p->order);
84 numerr++;
87 /* Put node into the tree. */
88 p = (struct node *) rk_tsearch((void *)t, (void **)&rootnode,
89 node_compare);
91 if (!p) {
92 warnx("erroneous list: missing %d\n", t->order);
93 numerr++;
97 rk_twalk(rootnode, list_node);
99 for(t = tests; t->string; t++) {
100 /* Better be there */
101 p = (struct node *) rk_tfind((void *)t, (void **)&rootnode,
102 node_compare);
104 if (!p) {
105 warnx("erroneous list: missing %d\n", t->order);
106 numerr++;
109 /* pull out node */
110 (void) rk_tdelete((void *)t, (void **)&rootnode,
111 node_compare);
113 /* Better not be there */
114 p = (struct node *) rk_tfind((void *)t, (void **)&rootnode,
115 node_compare);
117 if (p) {
118 warnx("erroneous list: found %d\n", p->order);
119 numerr++;
124 return numerr;