xref: /freebsd/sys/netpfil/ipfw/test/test_dn_heap.c (revision fdafd315)
13b3a8eb9SGleb Smirnoff /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 1998-2002,2010 Luigi Rizzo, Universita` di Pisa
53b3a8eb9SGleb Smirnoff  * All rights reserved
63b3a8eb9SGleb Smirnoff  *
73b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
83b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
93b3a8eb9SGleb Smirnoff  * are met:
103b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
113b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
123b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
133b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
143b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
153b3a8eb9SGleb Smirnoff  *
163b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173b3a8eb9SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193b3a8eb9SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203b3a8eb9SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213b3a8eb9SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223b3a8eb9SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233b3a8eb9SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253b3a8eb9SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263b3a8eb9SGleb Smirnoff  * SUCH DAMAGE.
273b3a8eb9SGleb Smirnoff  */
283b3a8eb9SGleb Smirnoff 
293b3a8eb9SGleb Smirnoff /*
303b3a8eb9SGleb Smirnoff  * Userland code for testing binary heaps and hash tables
313b3a8eb9SGleb Smirnoff  */
323b3a8eb9SGleb Smirnoff 
333b3a8eb9SGleb Smirnoff #include <sys/param.h>
343b3a8eb9SGleb Smirnoff 
353b3a8eb9SGleb Smirnoff #include <stdio.h>
363b3a8eb9SGleb Smirnoff #include <strings.h>
373b3a8eb9SGleb Smirnoff #include <stdlib.h>
383b3a8eb9SGleb Smirnoff 
393b3a8eb9SGleb Smirnoff #include  "dn_heap.h"
403b3a8eb9SGleb Smirnoff #define log(x, arg...)	fprintf(stderr, ## arg)
413b3a8eb9SGleb Smirnoff #define panic(x...)	fprintf(stderr, ## x), exit(1)
423b3a8eb9SGleb Smirnoff 
433b3a8eb9SGleb Smirnoff #include <string.h>
443b3a8eb9SGleb Smirnoff 
453b3a8eb9SGleb Smirnoff struct x {
463b3a8eb9SGleb Smirnoff 	struct x *ht_link;
473b3a8eb9SGleb Smirnoff 	char buf[0];
483b3a8eb9SGleb Smirnoff };
493b3a8eb9SGleb Smirnoff 
hf(uintptr_t key,int flags,void * arg)503b3a8eb9SGleb Smirnoff uint32_t hf(uintptr_t key, int flags, void *arg)
513b3a8eb9SGleb Smirnoff {
523b3a8eb9SGleb Smirnoff 	return (flags & DNHT_KEY_IS_OBJ) ?
533b3a8eb9SGleb Smirnoff 		((struct x *)key)->buf[0] : *(char *)key;
543b3a8eb9SGleb Smirnoff }
553b3a8eb9SGleb Smirnoff 
matchf(void * obj,uintptr_t key,int flags,void * arg)563b3a8eb9SGleb Smirnoff int matchf(void *obj, uintptr_t key, int flags, void *arg)
573b3a8eb9SGleb Smirnoff {
583b3a8eb9SGleb Smirnoff 	char *s = (flags & DNHT_KEY_IS_OBJ) ?
593b3a8eb9SGleb Smirnoff 		((struct x *)key)->buf : (char *)key;
603b3a8eb9SGleb Smirnoff 	return (strcmp(((struct x *)obj)->buf, s) == 0);
613b3a8eb9SGleb Smirnoff }
623b3a8eb9SGleb Smirnoff 
newfn(uintptr_t key,int flags,void * arg)633b3a8eb9SGleb Smirnoff void *newfn(uintptr_t key, int flags, void *arg)
643b3a8eb9SGleb Smirnoff {
653b3a8eb9SGleb Smirnoff 	char *s = (char *)key;
663b3a8eb9SGleb Smirnoff 	struct x *p = malloc(sizeof(*p) + 1 + strlen(s));
673b3a8eb9SGleb Smirnoff 	if (p)
683b3a8eb9SGleb Smirnoff 		strcpy(p->buf, s);
693b3a8eb9SGleb Smirnoff 	return p;
703b3a8eb9SGleb Smirnoff }
713b3a8eb9SGleb Smirnoff 
723b3a8eb9SGleb Smirnoff char *strings[] = {
733b3a8eb9SGleb Smirnoff 	"undici", "unico", "doppio", "devoto",
743b3a8eb9SGleb Smirnoff 	"uno", "due", "tre", "quattro", "cinque", "sei",
753b3a8eb9SGleb Smirnoff 	"uno", "due", "tre", "quattro", "cinque", "sei",
763b3a8eb9SGleb Smirnoff 	NULL,
773b3a8eb9SGleb Smirnoff };
783b3a8eb9SGleb Smirnoff 
doprint(void * _x,void * arg)793b3a8eb9SGleb Smirnoff int doprint(void *_x, void *arg)
803b3a8eb9SGleb Smirnoff {
813b3a8eb9SGleb Smirnoff 	struct x *x = _x;
823b3a8eb9SGleb Smirnoff 	printf("found element <%s>\n", x->buf);
833b3a8eb9SGleb Smirnoff 	return (int)arg;
843b3a8eb9SGleb Smirnoff }
853b3a8eb9SGleb Smirnoff 
863b3a8eb9SGleb Smirnoff static void
test_hash()873b3a8eb9SGleb Smirnoff test_hash()
883b3a8eb9SGleb Smirnoff {
893b3a8eb9SGleb Smirnoff 	char **p;
903b3a8eb9SGleb Smirnoff 	struct dn_ht *h;
913b3a8eb9SGleb Smirnoff 	uintptr_t x = 0;
923b3a8eb9SGleb Smirnoff 	uintptr_t x1 = 0;
933b3a8eb9SGleb Smirnoff 
943b3a8eb9SGleb Smirnoff 	/* first, find and allocate */
953b3a8eb9SGleb Smirnoff 	h = dn_ht_init(NULL, 10, 0, hf, matchf, newfn);
963b3a8eb9SGleb Smirnoff 
973b3a8eb9SGleb Smirnoff 	for (p = strings; *p; p++) {
983b3a8eb9SGleb Smirnoff 		dn_ht_find(h, (uintptr_t)*p, DNHT_INSERT, NULL);
993b3a8eb9SGleb Smirnoff 	}
1003b3a8eb9SGleb Smirnoff 	dn_ht_scan(h, doprint, 0);
1013b3a8eb9SGleb Smirnoff 	printf("/* second -- find without allocate */\n");
1023b3a8eb9SGleb Smirnoff 	h = dn_ht_init(NULL, 10, 0, hf, matchf, NULL);
1033b3a8eb9SGleb Smirnoff 	for (p = strings; *p; p++) {
1043b3a8eb9SGleb Smirnoff 		void **y = newfn((uintptr_t)*p, 0, NULL);
1053b3a8eb9SGleb Smirnoff 		if (x == 0)
1063b3a8eb9SGleb Smirnoff 			x = (uintptr_t)y;
1073b3a8eb9SGleb Smirnoff 		else {
1083b3a8eb9SGleb Smirnoff 			if (x1 == 0)
1093b3a8eb9SGleb Smirnoff 				x1 = (uintptr_t)*p;
1103b3a8eb9SGleb Smirnoff 		}
1113b3a8eb9SGleb Smirnoff 		dn_ht_find(h, (uintptr_t)y, DNHT_INSERT | DNHT_KEY_IS_OBJ, NULL);
1123b3a8eb9SGleb Smirnoff 	}
1133b3a8eb9SGleb Smirnoff 	dn_ht_scan(h, doprint, 0);
1143b3a8eb9SGleb Smirnoff 	printf("remove %p gives %p\n", (void *)x,
1153b3a8eb9SGleb Smirnoff 		dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL));
1163b3a8eb9SGleb Smirnoff 	printf("remove %p gives %p\n", (void *)x,
1173b3a8eb9SGleb Smirnoff 		dn_ht_find(h, x, DNHT_KEY_IS_OBJ | DNHT_REMOVE, NULL));
1183b3a8eb9SGleb Smirnoff 	printf("remove %p gives %p\n", (void *)x,
1193b3a8eb9SGleb Smirnoff 		dn_ht_find(h, x1, DNHT_REMOVE, NULL));
1203b3a8eb9SGleb Smirnoff 	printf("remove %p gives %p\n", (void *)x,
1213b3a8eb9SGleb Smirnoff 		dn_ht_find(h, x1, DNHT_REMOVE, NULL));
1223b3a8eb9SGleb Smirnoff 	dn_ht_scan(h, doprint, 0);
1233b3a8eb9SGleb Smirnoff }
1243b3a8eb9SGleb Smirnoff 
1253b3a8eb9SGleb Smirnoff int
main(int argc,char * argv[])1263b3a8eb9SGleb Smirnoff main(int argc, char *argv[])
1273b3a8eb9SGleb Smirnoff {
1283b3a8eb9SGleb Smirnoff 	struct dn_heap h;
1293b3a8eb9SGleb Smirnoff 	int i, n, n2, n3;
1303b3a8eb9SGleb Smirnoff 
1313b3a8eb9SGleb Smirnoff 	test_hash();
1323b3a8eb9SGleb Smirnoff 	return 0;
1333b3a8eb9SGleb Smirnoff 
1343b3a8eb9SGleb Smirnoff 	/* n = elements, n2 = cycles */
1353b3a8eb9SGleb Smirnoff 	n = (argc > 1) ? atoi(argv[1]) : 0;
1363b3a8eb9SGleb Smirnoff 	if (n <= 0 || n > 1000000)
1373b3a8eb9SGleb Smirnoff 		n = 100;
1383b3a8eb9SGleb Smirnoff 	n2 = (argc > 2) ? atoi(argv[2]) : 0;
1393b3a8eb9SGleb Smirnoff 	if (n2 <= 0)
1403b3a8eb9SGleb Smirnoff 		n = 1000000;
1413b3a8eb9SGleb Smirnoff 	n3 = (argc > 3) ? atoi(argv[3]) : 0;
1423b3a8eb9SGleb Smirnoff 	bzero(&h, sizeof(h));
1433b3a8eb9SGleb Smirnoff 	heap_init(&h, n, -1);
1443b3a8eb9SGleb Smirnoff 	while (n2-- > 0) {
1453b3a8eb9SGleb Smirnoff 		uint64_t prevk = 0;
1463b3a8eb9SGleb Smirnoff 		for (i=0; i < n; i++)
1473b3a8eb9SGleb Smirnoff 			heap_insert(&h, n3 ? n-i: random(), (void *)(100+i));
1483b3a8eb9SGleb Smirnoff 
1493b3a8eb9SGleb Smirnoff 		for (i=0; h.elements > 0; i++) {
1503b3a8eb9SGleb Smirnoff 			uint64_t k = h.p[0].key;
1513b3a8eb9SGleb Smirnoff 			if (k < prevk)
1523b3a8eb9SGleb Smirnoff 				panic("wrong sequence\n");
1533b3a8eb9SGleb Smirnoff 			prevk = k;
1543b3a8eb9SGleb Smirnoff 			if (0)
1553b3a8eb9SGleb Smirnoff 			printf("%d key %llu, val %p\n",
1563b3a8eb9SGleb Smirnoff 				i, h.p[0].key, h.p[0].object);
1573b3a8eb9SGleb Smirnoff 			heap_extract(&h, NULL);
1583b3a8eb9SGleb Smirnoff 		}
1593b3a8eb9SGleb Smirnoff 	}
1603b3a8eb9SGleb Smirnoff 	return 0;
1613b3a8eb9SGleb Smirnoff }
162