]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/lib/isc/tests/symtab_test.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / lib / isc / tests / symtab_test.c
1 /*
2  * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id$ */
18
19 /*! \file */
20
21 #include <config.h>
22
23 #include <atf-c.h>
24
25 #include <unistd.h>
26
27 #include <isc/symtab.h>
28 #include <isc/print.h>
29
30 #include "isctest.h"
31
32 static void
33 undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
34         UNUSED(arg);
35
36         ATF_REQUIRE_EQ(type, 1);
37         isc_mem_free(mctx, key);
38         isc_mem_free(mctx, value.as_pointer);
39 }
40
41 /*
42  * Individual unit tests
43  */
44
45 ATF_TC(symtab_grow);
46 ATF_TC_HEAD(symtab_grow, tc) {
47         atf_tc_set_md_var(tc, "descr", "symbol table growth");
48 }
49 ATF_TC_BODY(symtab_grow, tc) {
50         isc_result_t result;
51         isc_symtab_t *st = NULL;
52         isc_symvalue_t value;
53         isc_symexists_t policy = isc_symexists_reject;
54         int i;
55
56         UNUSED(tc);
57
58         result = isc_test_begin(NULL, ISC_TRUE);
59         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
60
61         result = isc_symtab_create(mctx, 3, undefine, NULL, ISC_FALSE, &st);
62         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
63         ATF_REQUIRE(st != NULL);
64
65         /* Nothing should be in the table yet */
66
67         /*
68          * Put 1024 entries in the table (this should necessate
69          * regrowing the hash table several times
70          */
71         for (i = 0; i < 1024; i++) {
72                 char str[16], *key;
73
74                 snprintf(str, sizeof(str), "%04x", i);
75                 key = isc_mem_strdup(mctx, str);
76                 ATF_REQUIRE(key != NULL);
77                 value.as_pointer = isc_mem_strdup(mctx, str);
78                 ATF_REQUIRE(value.as_pointer != NULL);
79                 result = isc_symtab_define(st, key, 1, value, policy);
80                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
81                 if (result != ISC_R_SUCCESS)
82                         undefine(key, 1, value, NULL);
83         }
84
85         /*
86          * Try to put them in again; this should fail
87          */
88         for (i = 0; i < 1024; i++) {
89                 char str[16], *key;
90
91                 snprintf(str, sizeof(str), "%04x", i);
92                 key = isc_mem_strdup(mctx, str);
93                 ATF_REQUIRE(key != NULL);
94                 value.as_pointer = isc_mem_strdup(mctx, str);
95                 ATF_REQUIRE(value.as_pointer != NULL);
96                 result = isc_symtab_define(st, key, 1, value, policy);
97                 ATF_CHECK_EQ(result, ISC_R_EXISTS);
98                 undefine(key, 1, value, NULL);
99         }
100
101         /*
102          * Retrieve them; this should succeed
103          */
104         for (i = 0; i < 1024; i++) {
105                 char str[16];
106
107                 snprintf(str, sizeof(str), "%04x", i);
108                 result = isc_symtab_lookup(st, str, 0, &value);
109                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
110                 ATF_CHECK_STREQ(str, value.as_pointer);
111         }
112
113         /*
114          * Undefine them
115          */
116         for (i = 0; i < 1024; i++) {
117                 char str[16];
118
119                 snprintf(str, sizeof(str), "%04x", i);
120                 result = isc_symtab_undefine(st, str, 1);
121                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
122         }
123
124         /*
125          * Retrieve them again; this should fail
126          */
127         for (i = 0; i < 1024; i++) {
128                 char str[16];
129
130                 snprintf(str, sizeof(str), "%04x", i);
131                 result = isc_symtab_lookup(st, str, 0, &value);
132                 ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
133         }
134
135         isc_symtab_destroy(&st);
136         isc_test_end();
137 }
138
139 /*
140  * Main
141  */
142 ATF_TP_ADD_TCS(tp) {
143         ATF_TP_ADD_TC(tp, symtab_grow);
144
145         return (atf_no_error());
146 }
147