1275970Scy/*
2275970Scy * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3275970Scy *
4275970Scy * Permission to use, copy, modify, and/or distribute this software for any
5275970Scy * purpose with or without fee is hereby granted, provided that the above
6275970Scy * copyright notice and this permission notice appear in all copies.
7275970Scy *
8275970Scy * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9275970Scy * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10275970Scy * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11275970Scy * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12275970Scy * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13275970Scy * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14275970Scy * PERFORMANCE OF THIS SOFTWARE.
15275970Scy */
16275970Scy
17275970Scy/* $Id$ */
18275970Scy
19275970Scy/*! \file */
20275970Scy
21275970Scy#include <config.h>
22275970Scy
23275970Scy#include <atf-c.h>
24275970Scy
25275970Scy#include <unistd.h>
26275970Scy
27275970Scy#include <isc/symtab.h>
28275970Scy#include <isc/print.h>
29275970Scy
30275970Scy#include "isctest.h"
31275970Scy
32275970Scystatic void
33275970Scyundefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
34275970Scy	UNUSED(arg);
35275970Scy
36275970Scy	ATF_REQUIRE_EQ(type, 1);
37275970Scy	isc_mem_free(mctx, key);
38275970Scy	isc_mem_free(mctx, value.as_pointer);
39275970Scy}
40275970Scy
41275970Scy/*
42275970Scy * Individual unit tests
43275970Scy */
44275970Scy
45275970ScyATF_TC(symtab_grow);
46275970ScyATF_TC_HEAD(symtab_grow, tc) {
47275970Scy	atf_tc_set_md_var(tc, "descr", "symbol table growth");
48275970Scy}
49275970ScyATF_TC_BODY(symtab_grow, tc) {
50275970Scy	isc_result_t result;
51275970Scy	isc_symtab_t *st = NULL;
52275970Scy	isc_symvalue_t value;
53275970Scy	isc_symexists_t policy = isc_symexists_reject;
54275970Scy	int i;
55275970Scy
56275970Scy	UNUSED(tc);
57275970Scy
58275970Scy	result = isc_test_begin(NULL, ISC_TRUE);
59275970Scy	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
60275970Scy
61275970Scy	result = isc_symtab_create(mctx, 3, undefine, NULL, ISC_FALSE, &st);
62275970Scy	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
63275970Scy	ATF_REQUIRE(st != NULL);
64275970Scy
65275970Scy	/* Nothing should be in the table yet */
66275970Scy
67275970Scy	/*
68275970Scy	 * Put 1024 entries in the table (this should necessate
69275970Scy	 * regrowing the hash table several times
70275970Scy	 */
71275970Scy	for (i = 0; i < 1024; i++) {
72275970Scy		char str[16], *key;
73275970Scy
74275970Scy		snprintf(str, sizeof(str), "%04x", i);
75275970Scy		key = isc_mem_strdup(mctx, str);
76275970Scy		ATF_REQUIRE(key != NULL);
77275970Scy		value.as_pointer = isc_mem_strdup(mctx, str);
78275970Scy		ATF_REQUIRE(value.as_pointer != NULL);
79275970Scy		result = isc_symtab_define(st, key, 1, value, policy);
80275970Scy		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
81275970Scy		if (result != ISC_R_SUCCESS)
82275970Scy			undefine(key, 1, value, NULL);
83275970Scy	}
84275970Scy
85275970Scy	/*
86275970Scy	 * Try to put them in again; this should fail
87275970Scy	 */
88275970Scy	for (i = 0; i < 1024; i++) {
89275970Scy		char str[16], *key;
90275970Scy
91275970Scy		snprintf(str, sizeof(str), "%04x", i);
92275970Scy		key = isc_mem_strdup(mctx, str);
93275970Scy		ATF_REQUIRE(key != NULL);
94275970Scy		value.as_pointer = isc_mem_strdup(mctx, str);
95275970Scy		ATF_REQUIRE(value.as_pointer != NULL);
96275970Scy		result = isc_symtab_define(st, key, 1, value, policy);
97275970Scy		ATF_CHECK_EQ(result, ISC_R_EXISTS);
98275970Scy		undefine(key, 1, value, NULL);
99275970Scy	}
100275970Scy
101275970Scy	/*
102275970Scy	 * Retrieve them; this should succeed
103275970Scy	 */
104275970Scy	for (i = 0; i < 1024; i++) {
105275970Scy		char str[16];
106275970Scy
107275970Scy		snprintf(str, sizeof(str), "%04x", i);
108275970Scy		result = isc_symtab_lookup(st, str, 0, &value);
109275970Scy		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
110275970Scy		ATF_CHECK_STREQ(str, value.as_pointer);
111275970Scy	}
112275970Scy
113275970Scy	/*
114275970Scy	 * Undefine them
115275970Scy	 */
116275970Scy	for (i = 0; i < 1024; i++) {
117275970Scy		char str[16];
118275970Scy
119275970Scy		snprintf(str, sizeof(str), "%04x", i);
120275970Scy		result = isc_symtab_undefine(st, str, 1);
121275970Scy		ATF_CHECK_EQ(result, ISC_R_SUCCESS);
122275970Scy	}
123275970Scy
124275970Scy	/*
125275970Scy	 * Retrieve them again; this should fail
126275970Scy	 */
127275970Scy	for (i = 0; i < 1024; i++) {
128275970Scy		char str[16];
129275970Scy
130275970Scy		snprintf(str, sizeof(str), "%04x", i);
131275970Scy		result = isc_symtab_lookup(st, str, 0, &value);
132275970Scy		ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
133275970Scy	}
134275970Scy
135275970Scy	isc_symtab_destroy(&st);
136275970Scy	isc_test_end();
137275970Scy}
138275970Scy
139275970Scy/*
140275970Scy * Main
141275970Scy */
142275970ScyATF_TP_ADD_TCS(tp) {
143275970Scy	ATF_TP_ADD_TC(tp, symtab_grow);
144275970Scy
145275970Scy	return (atf_no_error());
146275970Scy}
147275970Scy
148