static isc_boolean_t logged(char *key, int value) { isc_result_t result; if (symtab == NULL) return (ISC_FALSE); result = isc_symtab_lookup(symtab, key, value, NULL); if (result == ISC_R_SUCCESS) return (ISC_TRUE); return (ISC_FALSE); }
ATF_TC_BODY(symtab_grow, tc) { isc_result_t result; isc_symtab_t *st = NULL; isc_symvalue_t value; isc_symexists_t policy = isc_symexists_reject; int i; UNUSED(tc); result = isc_test_begin(NULL, ISC_TRUE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); result = isc_symtab_create(mctx, 3, undefine, NULL, ISC_FALSE, &st); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE(st != NULL); /* Nothing should be in the table yet */ /* * Put 1024 entries in the table (this should necessate * regrowing the hash table several times */ for (i = 0; i < 1024; i++) { char str[16], *key; snprintf(str, sizeof(str), "%04x", i); key = isc_mem_strdup(mctx, str); ATF_REQUIRE(key != NULL); value.as_pointer = isc_mem_strdup(mctx, str); ATF_REQUIRE(value.as_pointer != NULL); result = isc_symtab_define(st, key, 1, value, policy); ATF_CHECK_EQ(result, ISC_R_SUCCESS); if (result != ISC_R_SUCCESS) undefine(key, 1, value, NULL); } /* * Try to put them in again; this should fail */ for (i = 0; i < 1024; i++) { char str[16], *key; snprintf(str, sizeof(str), "%04x", i); key = isc_mem_strdup(mctx, str); ATF_REQUIRE(key != NULL); value.as_pointer = isc_mem_strdup(mctx, str); ATF_REQUIRE(value.as_pointer != NULL); result = isc_symtab_define(st, key, 1, value, policy); ATF_CHECK_EQ(result, ISC_R_EXISTS); undefine(key, 1, value, NULL); } /* * Retrieve them; this should succeed */ for (i = 0; i < 1024; i++) { char str[16]; snprintf(str, sizeof(str), "%04x", i); result = isc_symtab_lookup(st, str, 0, &value); ATF_CHECK_EQ(result, ISC_R_SUCCESS); ATF_CHECK_STREQ(str, value.as_pointer); } /* * Undefine them */ for (i = 0; i < 1024; i++) { char str[16]; snprintf(str, sizeof(str), "%04x", i); result = isc_symtab_undefine(st, str, 1); ATF_CHECK_EQ(result, ISC_R_SUCCESS); } /* * Retrieve them again; this should fail */ for (i = 0; i < 1024; i++) { char str[16]; snprintf(str, sizeof(str), "%04x", i); result = isc_symtab_lookup(st, str, 0, &value); ATF_CHECK_EQ(result, ISC_R_NOTFOUND); } isc_symtab_destroy(&st); isc_test_end(); }
int main(int argc, char *argv[]) { char s[1000], *cp, *key; size_t len; isc_result_t result; isc_symvalue_t value; int trace = 0; int c; isc_symexists_t exists_policy = isc_symexists_reject; isc_boolean_t case_sensitive = ISC_FALSE; while ((c = isc_commandline_parse(argc, argv, "tarc")) != -1) { switch (c) { case 't': trace = 1; break; case 'a': exists_policy = isc_symexists_add; break; case 'r': exists_policy = isc_symexists_replace; break; case 'c': case_sensitive = ISC_TRUE; break; } } RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_symtab_create(mctx, 691, undefine_action, NULL, case_sensitive, &st) == ISC_R_SUCCESS); while (fgets(s, sizeof(s), stdin) != NULL) { len = strlen(s); if (len > 0U && s[len - 1] == '\n') { s[len - 1] = '\0'; len--; } cp = s; if (cp[0] == '!') { cp++; result = isc_symtab_undefine(st, cp, 1); if (trace || result != ISC_R_SUCCESS) printf("undefine('%s'): %s\n", cp, isc_result_totext(result)); } else { key = cp; while (*cp != '\0' && *cp != ' ' && *cp != '\t') cp++; if (*cp == '\0') { result = isc_symtab_lookup(st, key, 0, &value); if (trace || result != ISC_R_SUCCESS) { printf("lookup('%s'): %s", key, isc_result_totext(result)); if (result == ISC_R_SUCCESS) { cp = value.as_pointer; printf(", value == '%s'", cp); } printf("\n"); } } else { *cp++ = '\0'; key = isc_mem_strdup(mctx, key); value.as_pointer = isc_mem_strdup(mctx, cp); result = isc_symtab_define(st, key, 1, value, exists_policy); if (trace || result != ISC_R_SUCCESS) { printf("define('%s', '%s'): %s\n", key, cp, isc_result_totext(result)); if (result != ISC_R_SUCCESS) undefine_action(key, 1, value, NULL); } } } } isc_symtab_destroy(&st); isc_mem_stats(mctx, stdout); isc_mem_destroy(&mctx); return (0); }