diff options
| author | millert <> | 2004-06-24 04:33:07 +0000 |
|---|---|---|
| committer | millert <> | 2004-06-24 04:33:07 +0000 |
| commit | 242acba8652a28aeb609072dfde10f2074651e07 (patch) | |
| tree | 316a4e929296cafb0db9c77c7f493658c6767734 | |
| parent | 1de6a9244da68c966004aea5b8472e3d2428a5c3 (diff) | |
| download | openbsd-242acba8652a28aeb609072dfde10f2074651e07.tar.gz openbsd-242acba8652a28aeb609072dfde10f2074651e07.tar.bz2 openbsd-242acba8652a28aeb609072dfde10f2074651e07.zip | |
hsearch regress from NetBSD via ray at cyth dot net
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libc/Makefile | 4 | ||||
| -rw-r--r-- | src/regress/lib/libc/hsearch/Makefile | 5 | ||||
| -rw-r--r-- | src/regress/lib/libc/hsearch/hsearchtest.c | 131 |
3 files changed, 138 insertions, 2 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 935ca659d7..3221470553 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.14 2004/04/30 17:15:12 espie Exp $ | 1 | # $OpenBSD: Makefile,v 1.15 2004/06/24 04:33:07 millert Exp $ |
| 2 | 2 | ||
| 3 | SUBDIR+= _setjmp alloca atexit db getaddrinfo longjmp malloc | 3 | SUBDIR+= _setjmp alloca atexit db getaddrinfo hsearch longjmp malloc |
| 4 | SUBDIR+= popen regex setjmp setjmp-signal sigreturn sigsetjmp | 4 | SUBDIR+= popen regex setjmp setjmp-signal sigreturn sigsetjmp |
| 5 | SUBDIR+= sprintf strerror time | 5 | SUBDIR+= sprintf strerror time |
| 6 | 6 | ||
diff --git a/src/regress/lib/libc/hsearch/Makefile b/src/regress/lib/libc/hsearch/Makefile new file mode 100644 index 0000000000..cddc8ac0c4 --- /dev/null +++ b/src/regress/lib/libc/hsearch/Makefile | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.1 2004/06/24 04:33:07 millert Exp $ | ||
| 2 | |||
| 3 | PROG= hsearchtest | ||
| 4 | |||
| 5 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/hsearch/hsearchtest.c b/src/regress/lib/libc/hsearch/hsearchtest.c new file mode 100644 index 0000000000..2972b54794 --- /dev/null +++ b/src/regress/lib/libc/hsearch/hsearchtest.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | /* $OpenBSD: hsearchtest.c,v 1.1 2004/06/24 04:33:07 millert 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 | #ifndef lint | ||
| 43 | static const char copyright[] = | ||
| 44 | "@(#) Copyright (c) 2001 Christopher G. Demetriou. All rights reserved.\n"; | ||
| 45 | #endif /* not lint */ | ||
| 46 | |||
| 47 | #ifndef lint | ||
| 48 | static const char rcsid[] = "$OpenBSD: hsearchtest.c,v 1.1 2004/06/24 04:33:07 millert Exp $"; | ||
| 49 | #endif /* not lint */ | ||
| 50 | |||
| 51 | #include <search.h> | ||
| 52 | #include <stdlib.h> | ||
| 53 | #include <stdio.h> | ||
| 54 | #include <string.h> | ||
| 55 | |||
| 56 | #define TEST(e) ((e) ? (void)0 : testfail(__FILE__, __LINE__, #e)) | ||
| 57 | |||
| 58 | static void | ||
| 59 | testfail(const char *file, unsigned long line, const char *expression) | ||
| 60 | { | ||
| 61 | |||
| 62 | fprintf(stderr, "TEST FAILED: %s: file %s, line %ld\n", | ||
| 63 | expression, file, line); | ||
| 64 | exit(1); | ||
| 65 | } | ||
| 66 | |||
| 67 | int | ||
| 68 | main(int argc, char *argv[]) | ||
| 69 | { | ||
| 70 | ENTRY e, *ep, *ep2; | ||
| 71 | int created_ok; | ||
| 72 | char ch[2]; | ||
| 73 | int i; | ||
| 74 | |||
| 75 | created_ok = hcreate(16); | ||
| 76 | TEST(created_ok); | ||
| 77 | |||
| 78 | /* ch[1] should be constant from here on down. */ | ||
| 79 | ch[1] = '\0'; | ||
| 80 | |||
| 81 | /* Basic insertions. Check enough that there'll be collisions. */ | ||
| 82 | for (i = 0; i < 26; i++) { | ||
| 83 | ch[0] = 'a' + i; | ||
| 84 | e.key = strdup(ch); /* ptr to provided key is kept! */ | ||
| 85 | TEST(e.key != NULL); | ||
| 86 | e.data = (void *)(long)i; | ||
| 87 | ep = hsearch(e, ENTER); | ||
| 88 | TEST(ep != NULL); | ||
| 89 | TEST(strcmp(ep->key, ch) == 0); | ||
| 90 | TEST((long)ep->data == i); | ||
| 91 | } | ||
| 92 | |||
| 93 | /* e.key should be constant from here on down. */ | ||
| 94 | e.key = ch; | ||
| 95 | |||
| 96 | /* Basic lookups. */ | ||
| 97 | for (i = 0; i < 26; i++) { | ||
| 98 | ch[0] = 'a' + i; | ||
| 99 | ep = hsearch(e, FIND); | ||
| 100 | TEST(ep != NULL); | ||
| 101 | TEST(strcmp(ep->key, ch) == 0); | ||
| 102 | TEST((long)ep->data == i); | ||
| 103 | } | ||
| 104 | |||
| 105 | /* Check duplicate entry. Should _not_ overwrite existing data. */ | ||
| 106 | ch[0] = 'a'; | ||
| 107 | e.data = (void *)(long)12345; | ||
| 108 | ep = hsearch(e, FIND); | ||
| 109 | TEST(ep != NULL); | ||
| 110 | TEST(strcmp(ep->key, ch) == 0); | ||
| 111 | TEST((long)ep->data == 0); | ||
| 112 | |||
| 113 | /* Check for something that's not there. */ | ||
| 114 | ch[0] = 'A'; | ||
| 115 | ep = hsearch(e, FIND); | ||
| 116 | TEST(ep == NULL); | ||
| 117 | |||
| 118 | /* Check two at once. */ | ||
| 119 | ch[0] = 'a'; | ||
| 120 | ep = hsearch(e, FIND); | ||
| 121 | ch[0] = 'b'; | ||
| 122 | ep2 = hsearch(e, FIND); | ||
| 123 | TEST(ep != NULL); | ||
| 124 | TEST(strcmp(ep->key, "a") == 0 && (long)ep->data == 0); | ||
| 125 | TEST(ep2 != NULL); | ||
| 126 | TEST(strcmp(ep2->key, "b") == 0 && (long)ep2->data == 1); | ||
| 127 | |||
| 128 | hdestroy(); | ||
| 129 | |||
| 130 | exit(0); | ||
| 131 | } | ||
