Path: blob/main/sys/netpfil/ipfw/test/test_dn_heap.c
39507 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa4* All rights reserved5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* Userland code for testing binary heaps and hash tables30*/3132#include <sys/param.h>3334#include <stdio.h>35#include <strings.h>36#include <stdlib.h>3738#include "dn_heap.h"39#define log(x, arg...) fprintf(stderr, ## arg)40#define panic(x...) fprintf(stderr, ## x), exit(1)4142#include <string.h>4344struct x {45struct x *ht_link;46char buf[0];47};4849uint32_t hf(uintptr_t key, int flags, void *arg)50{51return (flags & DNHT_KEY_IS_OBJ) ?52((struct x *)key)->buf[0] : *(char *)key;53}5455int matchf(void *obj, uintptr_t key, int flags, void *arg)56{57char *s = (flags & DNHT_KEY_IS_OBJ) ?58((struct x *)key)->buf : (char *)key;59return (strcmp(((struct x *)obj)->buf, s) == 0);60}6162void *newfn(uintptr_t key, int flags, void *arg)63{64char *s = (char *)key;65struct x *p = malloc(sizeof(*p) + 1 + strlen(s));66if (p)67strcpy(p->buf, s);68return p;69}7071char *strings[] = {72"undici", "unico", "doppio", "devoto",73"uno", "due", "tre", "quattro", "cinque", "sei",74"uno", "due", "tre", "quattro", "cinque", "sei",75NULL,76};7778int doprint(void *_x, void *arg)79{80struct x *x = _x;81printf("found element <%s>\n", x->buf);82return (int)arg;83}8485static void86test_hash()87{88char **p;89struct dn_ht *h;90uintptr_t x = 0;91uintptr_t x1 = 0;9293/* first, find and allocate */94h = dn_ht_init(NULL, 10, 0, hf, matchf, newfn);9596for (p = strings; *p; p++) {97dn_ht_find(h, (uintptr_t)*p, DNHT_INSERT, NULL);98}99dn_ht_scan(h, doprint, 0);100printf("/* second -- find without allocate */\n");101h = dn_ht_init(NULL, 10, 0, hf, matchf, NULL);102for (p = strings; *p; p++) {103void **y = newfn((uintptr_t)*p, 0, NULL);104if (x == 0)105x = (uintptr_t)y;106else {107if (x1 == 0)108x1 = (uintptr_t)*p;109}110dn_ht_find(h, (uintptr_t)y, DNHT_INSERT | DNHT_KEY_IS_OBJ, NULL);111}112dn_ht_scan(h, doprint, 0);113printf("remove %p gives %p\n", (void *)x,114dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL));115printf("remove %p gives %p\n", (void *)x,116dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL));117printf("remove %p gives %p\n", (void *)x,118dn_ht_find(h, x1, DNHT_REMOVE, NULL));119printf("remove %p gives %p\n", (void *)x,120dn_ht_find(h, x1, DNHT_REMOVE, NULL));121dn_ht_scan(h, doprint, 0);122}123124int125main(int argc, char *argv[])126{127struct dn_heap h;128int i, n, n2, n3;129130test_hash();131return 0;132133/* n = elements, n2 = cycles */134n = (argc > 1) ? atoi(argv[1]) : 0;135if (n <= 0 || n > 1000000)136n = 100;137n2 = (argc > 2) ? atoi(argv[2]) : 0;138if (n2 <= 0)139n = 1000000;140n3 = (argc > 3) ? atoi(argv[3]) : 0;141bzero(&h, sizeof(h));142heap_init(&h, n, -1);143while (n2-- > 0) {144uint64_t prevk = 0;145for (i=0; i < n; i++)146heap_insert(&h, n3 ? n-i: random(), (void *)(100+i));147148for (i=0; h.elements > 0; i++) {149uint64_t k = h.p[0].key;150if (k < prevk)151panic("wrong sequence\n");152prevk = k;153if (0)154printf("%d key %llu, val %p\n",155i, h.p[0].key, h.p[0].object);156heap_extract(&h, NULL);157}158}159return 0;160}161162163