2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Userland code for testing binary heaps and hash tables
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
43 #define log(x, arg...) fprintf(stderr, ## arg)
44 #define panic(x...) fprintf(stderr, ## x), exit(1)
53 uint32_t hf(uintptr_t key, int flags, void *arg)
55 return (flags & DNHT_KEY_IS_OBJ) ?
56 ((struct x *)key)->buf[0] : *(char *)key;
59 int matchf(void *obj, uintptr_t key, int flags, void *arg)
61 char *s = (flags & DNHT_KEY_IS_OBJ) ?
62 ((struct x *)key)->buf : (char *)key;
63 return (strcmp(((struct x *)obj)->buf, s) == 0);
66 void *newfn(uintptr_t key, int flags, void *arg)
68 char *s = (char *)key;
69 struct x *p = malloc(sizeof(*p) + 1 + strlen(s));
76 "undici", "unico", "doppio", "devoto",
77 "uno", "due", "tre", "quattro", "cinque", "sei",
78 "uno", "due", "tre", "quattro", "cinque", "sei",
82 int doprint(void *_x, void *arg)
85 printf("found element <%s>\n", x->buf);
97 /* first, find and allocate */
98 h = dn_ht_init(NULL, 10, 0, hf, matchf, newfn);
100 for (p = strings; *p; p++) {
101 dn_ht_find(h, (uintptr_t)*p, DNHT_INSERT, NULL);
103 dn_ht_scan(h, doprint, 0);
104 printf("/* second -- find without allocate */\n");
105 h = dn_ht_init(NULL, 10, 0, hf, matchf, NULL);
106 for (p = strings; *p; p++) {
107 void **y = newfn((uintptr_t)*p, 0, NULL);
114 dn_ht_find(h, (uintptr_t)y, DNHT_INSERT | DNHT_KEY_IS_OBJ, NULL);
116 dn_ht_scan(h, doprint, 0);
117 printf("remove %p gives %p\n", (void *)x,
118 dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL));
119 printf("remove %p gives %p\n", (void *)x,
120 dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL));
121 printf("remove %p gives %p\n", (void *)x,
122 dn_ht_find(h, x1, DNHT_REMOVE, NULL));
123 printf("remove %p gives %p\n", (void *)x,
124 dn_ht_find(h, x1, DNHT_REMOVE, NULL));
125 dn_ht_scan(h, doprint, 0);
129 main(int argc, char *argv[])
137 /* n = elements, n2 = cycles */
138 n = (argc > 1) ? atoi(argv[1]) : 0;
139 if (n <= 0 || n > 1000000)
141 n2 = (argc > 2) ? atoi(argv[2]) : 0;
144 n3 = (argc > 3) ? atoi(argv[3]) : 0;
145 bzero(&h, sizeof(h));
146 heap_init(&h, n, -1);
149 for (i=0; i < n; i++)
150 heap_insert(&h, n3 ? n-i: random(), (void *)(100+i));
152 for (i=0; h.elements > 0; i++) {
153 uint64_t k = h.p[0].key;
155 panic("wrong sequence\n");
158 printf("%d key %llu, val %p\n",
159 i, h.p[0].key, h.p[0].object);
160 heap_extract(&h, NULL);