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