xref: /openbsd/regress/lib/libc/hsearch/hsearchtest.c (revision 891d7ab6)
1 /*	$OpenBSD: hsearchtest.c,v 1.2 2009/10/27 23:59:32 deraadt Exp $	*/
2 /*	$NetBSD: hsearchtest.c,v 1.5 2003/07/26 19:38:46 salo Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 Christopher G. Demetriou
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *          This product includes software developed for the
19  *          NetBSD Project.  See http://www.NetBSD.org/ for
20  *          information about NetBSD.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
36  */
37 
38 /*
39  * Test program for hsearch() et al.
40  */
41 
42 #include <search.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 #define	TEST(e)	((e) ? (void)0 : testfail(__FILE__, __LINE__, #e))
48 
49 static void
50 testfail(const char *file, unsigned long line, const char *expression)
51 {
52 
53 	fprintf(stderr, "TEST FAILED: %s: file %s, line %ld\n",
54 	    expression, file, line);
55 	exit(1);
56 }
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	ENTRY e, *ep, *ep2;
62 	int created_ok;
63 	char ch[2];
64 	int i;
65 
66 	created_ok = hcreate(16);
67 	TEST(created_ok);
68 
69 	/* ch[1] should be constant from here on down. */
70 	ch[1] = '\0';
71 
72 	/* Basic insertions.  Check enough that there'll be collisions. */
73 	for (i = 0; i < 26; i++) {
74 		ch[0] = 'a' + i;
75 		e.key = strdup(ch);	/* ptr to provided key is kept! */
76 		TEST(e.key != NULL);
77 		e.data = (void *)(long)i;
78 		ep = hsearch(e, ENTER);
79 		TEST(ep != NULL);
80 		TEST(strcmp(ep->key, ch) == 0);
81 		TEST((long)ep->data == i);
82 	}
83 
84 	/* e.key should be constant from here on down. */
85 	e.key = ch;
86 
87 	/* Basic lookups. */
88 	for (i = 0; i < 26; i++) {
89 		ch[0] = 'a' + i;
90 		ep = hsearch(e, FIND);
91 		TEST(ep != NULL);
92 		TEST(strcmp(ep->key, ch) == 0);
93 		TEST((long)ep->data == i);
94 	}
95 
96 	/* Check duplicate entry.  Should _not_ overwrite existing data.  */
97 	ch[0] = 'a';
98 	e.data = (void *)(long)12345;
99 	ep = hsearch(e, FIND);
100 	TEST(ep != NULL);
101 	TEST(strcmp(ep->key, ch) == 0);
102 	TEST((long)ep->data == 0);
103 
104 	/* Check for something that's not there. */
105 	ch[0] = 'A';
106 	ep = hsearch(e, FIND);
107 	TEST(ep == NULL);
108 
109 	/* Check two at once. */
110 	ch[0] = 'a';
111 	ep = hsearch(e, FIND);
112 	ch[0] = 'b';
113 	ep2 = hsearch(e, FIND);
114 	TEST(ep != NULL);
115 	TEST(strcmp(ep->key, "a") == 0 && (long)ep->data == 0);
116 	TEST(ep2 != NULL);
117 	TEST(strcmp(ep2->key, "b") == 0 && (long)ep2->data == 1);
118 
119 	hdestroy();
120 
121 	exit(0);
122 }
123