symtab_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011-2013  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <atf-c.h>
00024 
00025 #include <unistd.h>
00026 
00027 #include <isc/symtab.h>
00028 #include <isc/print.h>
00029 
00030 #include "isctest.h"
00031 
00032 static void
00033 undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
00034         UNUSED(arg);
00035 
00036         ATF_REQUIRE_EQ(type, 1);
00037         isc_mem_free(mctx, key);
00038         isc_mem_free(mctx, value.as_pointer);
00039 }
00040 
00041 /*
00042  * Individual unit tests
00043  */
00044 
00045 ATF_TC(symtab_grow);
00046 ATF_TC_HEAD(symtab_grow, tc) {
00047         atf_tc_set_md_var(tc, "descr", "symbol table growth");
00048 }
00049 ATF_TC_BODY(symtab_grow, tc) {
00050         isc_result_t result;
00051         isc_symtab_t *st = NULL;
00052         isc_symvalue_t value;
00053         isc_symexists_t policy = isc_symexists_reject;
00054         int i;
00055 
00056         UNUSED(tc);
00057 
00058         result = isc_test_begin(NULL, ISC_TRUE);
00059         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00060 
00061         result = isc_symtab_create(mctx, 3, undefine, NULL, ISC_FALSE, &st);
00062         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00063         ATF_REQUIRE(st != NULL);
00064 
00065         /* Nothing should be in the table yet */
00066 
00067         /*
00068          * Put 1024 entries in the table (this should necessate
00069          * regrowing the hash table several times
00070          */
00071         for (i = 0; i < 1024; i++) {
00072                 char str[16], *key;
00073 
00074                 snprintf(str, sizeof(str), "%04x", i);
00075                 key = isc_mem_strdup(mctx, str);
00076                 ATF_REQUIRE(key != NULL);
00077                 value.as_pointer = isc_mem_strdup(mctx, str);
00078                 ATF_REQUIRE(value.as_pointer != NULL);
00079                 result = isc_symtab_define(st, key, 1, value, policy);
00080                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00081                 if (result != ISC_R_SUCCESS)
00082                         undefine(key, 1, value, NULL);
00083         }
00084 
00085         /*
00086          * Try to put them in again; this should fail
00087          */
00088         for (i = 0; i < 1024; i++) {
00089                 char str[16], *key;
00090 
00091                 snprintf(str, sizeof(str), "%04x", i);
00092                 key = isc_mem_strdup(mctx, str);
00093                 ATF_REQUIRE(key != NULL);
00094                 value.as_pointer = isc_mem_strdup(mctx, str);
00095                 ATF_REQUIRE(value.as_pointer != NULL);
00096                 result = isc_symtab_define(st, key, 1, value, policy);
00097                 ATF_CHECK_EQ(result, ISC_R_EXISTS);
00098                 undefine(key, 1, value, NULL);
00099         }
00100 
00101         /*
00102          * Retrieve them; this should succeed
00103          */
00104         for (i = 0; i < 1024; i++) {
00105                 char str[16];
00106 
00107                 snprintf(str, sizeof(str), "%04x", i);
00108                 result = isc_symtab_lookup(st, str, 0, &value);
00109                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00110                 ATF_CHECK_STREQ(str, (char *)value.as_pointer);
00111         }
00112 
00113         /*
00114          * Undefine them
00115          */
00116         for (i = 0; i < 1024; i++) {
00117                 char str[16];
00118 
00119                 snprintf(str, sizeof(str), "%04x", i);
00120                 result = isc_symtab_undefine(st, str, 1);
00121                 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
00122         }
00123 
00124         /*
00125          * Retrieve them again; this should fail
00126          */
00127         for (i = 0; i < 1024; i++) {
00128                 char str[16];
00129 
00130                 snprintf(str, sizeof(str), "%04x", i);
00131                 result = isc_symtab_lookup(st, str, 0, &value);
00132                 ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
00133         }
00134 
00135         isc_symtab_destroy(&st);
00136         isc_test_end();
00137 }
00138 
00139 /*
00140  * Main
00141  */
00142 ATF_TP_ADD_TCS(tp) {
00143         ATF_TP_ADD_TC(tp, symtab_grow);
00144 
00145         return (atf_no_error());
00146 }
00147 

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha