2
test_dict.c - simple test for the dict module
3
This file is part of the nss-pam-ldapd library.
5
Copyright (C) 2007, 2008 Arthur de Jong
7
This library is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Lesser General Public
9
License as published by the Free Software Foundation; either
10
version 2.1 of the License, or (at your option) any later version.
12
This library is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
Lesser General Public License for more details.
17
You should have received a copy of the GNU Lesser General Public
18
License along with this library; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30
#include "common/dict.h"
31
#include "compat/attrs.h"
33
/* Simple test that adds a few key/value pairs to the dict and the does
35
static void test_simple(void)
40
static char *value1="value1";
41
static char *value2="value2";
42
static char *replace2="replace2";
45
/* store some entries */
46
dict_put(dict,"key1",value1);
47
dict_put(dict,"key2",value2);
48
dict_put(dict,"key3",dict);
49
dict_put(dict,"KEY2",replace2);
50
/* check dictionary contents */
51
val=dict_get(dict,"KeY1");
53
val=dict_get(dict,"kEy2");
54
assert(val==replace2);
55
val=dict_get(dict,"KeY3");
57
val=dict_get(dict,"key4");
60
dict_put(dict,"kEy3",NULL);
61
val=dict_get(dict,"keY3");
63
/* loop over dictionary contents */
64
dict_loop_first(dict);
65
while (dict_loop_next(dict,&key,&val)!=NULL)
67
assert(((val==value1)||(val==replace2)));
73
/* Test to insert a large number of elements in the dict. */
74
static void test_lotsofelements(void)
83
/* insert a number of entries */
86
r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
87
sprintf(buf,"test%04d",r);
88
dict_put(dict,buf,&buf);
90
/* remove a number of entries */
93
r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
94
sprintf(buf,"test%04d",r);
95
dict_put(dict,buf,NULL);
97
/* add some more entries */
100
r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
101
sprintf(buf,"test%04d",r);
102
dict_put(dict,buf,&buf);
104
/* loop over dictionary contents */
105
dict_loop_first(dict);
106
while (dict_loop_next(dict,&key,&val)!=NULL)
110
/* free dictionary */
114
/* Test to insert a large number of elements in the dict. */
115
static void test_readelements(const char *fname)
124
/* read file and insert all entries */
127
while (fgets(buf,sizeof(buf),fp)!=NULL)
130
buf[strlen(buf)-1]='\0';
131
dict_put(dict,buf,&buf);
134
/* loop over dictionary contents */
135
dict_loop_first(dict);
136
while (dict_loop_next(dict,&key,&val)!=NULL)
140
/* free dictionary */
144
static void test_countelements(int num)
153
/* insert a number of entries */
156
r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
157
sprintf(buf,"%04dx%04d",i,r);
158
dict_put(dict,buf,&buf);
160
/* loop over dictionary contents */
161
dict_loop_first(dict);
163
while (dict_loop_next(dict,&key,&val)!=NULL)
168
/* we should have num elements */
170
/* free dictionary */
174
/* the main program... */
175
int main(int UNUSED(argc),char UNUSED(*argv[]))
179
/* build the name of the file */
180
srcdir=getenv("srcdir");
182
strcpy(fname,"usernames.txt");
184
snprintf(fname,sizeof(fname),"%s/usernames.txt",srcdir);
185
fname[sizeof(fname)-1]='\0';
188
test_lotsofelements();
189
test_readelements(fname);
190
test_countelements(0);
191
test_countelements(1);
192
test_countelements(2);
193
test_countelements(3);
194
test_countelements(4);
195
test_countelements(10);
196
test_countelements(20);