1 /*
2 test_dict.c - simple test for the dict module
3 This file is part of the nss-pam-ldapd library.
4
5 Copyright (C) 2007, 2008, 2009, 2010, 2012 Arthur de Jong
6
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.
11
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.
16
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
20 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <stdlib.h>
29
30 #include "common/dict.h"
31 #include "compat/attrs.h"
32
33 /* Simple test that adds a few key/value pairs to the dict and the does
34 most operations. */
test_simple(void)35 static void test_simple(void)
36 {
37 DICT *dict;
38 void *val;
39 static char *value1 = "value1";
40 static char *value2 = "value2";
41 static char *replace2 = "replace2";
42 const char **keys;
43 int i;
44 /* initialize */
45 dict = dict_new();
46 /* store some entries */
47 dict_put(dict, "key1", value1);
48 dict_put(dict, "key2", value2);
49 dict_put(dict, "key3", dict);
50 dict_put(dict, "key2", replace2);
51 /* check dictionary contents */
52 val = dict_get(dict, "key1");
53 assert(val == value1);
54 val = dict_get(dict, "key2");
55 assert(val == replace2);
56 val = dict_get(dict, "key3");
57 assert(val == dict);
58 val = dict_get(dict, "key4");
59 assert(val == NULL);
60 val = dict_get(dict, "KEY1");
61 assert(val == NULL);
62 /* remove a key */
63 dict_put(dict, "key3", NULL);
64 val = dict_get(dict, "key3");
65 assert(val == NULL);
66 /* loop over dictionary contents */
67 keys = dict_keys(dict);
68 for (i = 0; keys[i] != NULL; i++)
69 {
70 val = dict_get(dict, keys[i]);
71 assert(((val == value1) || (val == replace2)));
72 }
73 /* free stuff */
74 dict_free(dict);
75 free(keys);
76 }
77
78 /* Test to insert a large number of elements in the dict. */
test_lotsofelements(void)79 static void test_lotsofelements(void)
80 {
81 DICT *dict;
82 char buf[80];
83 int i, r;
84 void *val;
85 const char **keys;
86 /* initialize */
87 dict = dict_new();
88 /* insert a number of entries */
89 for (i = 0; i < 1024; i++)
90 {
91 r = 1 + (int)(10000.0 * (rand() / (RAND_MAX + 1.0)));
92 sprintf(buf, "test%04d", r);
93 dict_put(dict, buf, &buf);
94 }
95 /* remove a number of entries */
96 for (i = 0; i < 100; i++)
97 {
98 r = 1 + (int)(10000.0 * (rand() / (RAND_MAX + 1.0)));
99 sprintf(buf, "test%04d", r);
100 dict_put(dict, buf, NULL);
101 }
102 /* add some more entries */
103 for (i = 0; i < 1024; i++)
104 {
105 r = 1 + (int)(10000.0 * (rand() / (RAND_MAX + 1.0)));
106 sprintf(buf, "test%04d", r);
107 dict_put(dict, buf, &buf);
108 }
109 /* loop over dictionary contents */
110 keys = dict_keys(dict);
111 for (i = 0; keys[i] != NULL; i++)
112 {
113 val = dict_get(dict, keys[i]);
114 assert(val == buf);
115 }
116 /* free stuff */
117 dict_free(dict);
118 free(keys);
119 }
120
121 /* Test to insert a large number of elements in the dict. */
test_readelements(const char * fname)122 static void test_readelements(const char *fname)
123 {
124 DICT *dict;
125 char buf[80];
126 FILE *fp;
127 void *val;
128 const char **keys;
129 int i;
130 /* initialize */
131 dict = dict_new();
132 /* read file and insert all entries */
133 fp = fopen(fname, "r");
134 assert(fp != NULL);
135 while (fgets(buf, sizeof(buf), fp) != NULL)
136 {
137 /* strip newline */
138 buf[strlen(buf) - 1] = '\0';
139 dict_put(dict, buf, &buf);
140 }
141 fclose(fp);
142 /* loop over dictionary contents */
143 keys = dict_keys(dict);
144 for (i = 0; keys[i] != NULL; i++)
145 {
146 val = dict_get(dict, keys[i]);
147 assert(val == buf);
148 }
149 /* free stuff */
150 dict_free(dict);
151 free(keys);
152 }
153
test_countelements(int num)154 static void test_countelements(int num)
155 {
156 DICT *dict;
157 char buf[80];
158 int i, r;
159 const char **keys;
160 /* initialize */
161 dict = dict_new();
162 /* insert a number of entries */
163 for (i = 0; i < num; i++)
164 {
165 r = 1 + (int)(10000.0 * (rand() / (RAND_MAX + 1.0)));
166 sprintf(buf, "%04dx%04d", i, r);
167 dict_put(dict, buf, &buf);
168 }
169 /* loop over dictionary contents */
170 keys = dict_keys(dict);
171 for (i = 0; keys[i] != NULL; i++)
172 /* nothing */ ;
173 /* we should have num elements */
174 assert(i == num);
175 /* free stuff */
176 dict_free(dict);
177 free(keys);
178 }
179
180 /* the main program... */
main(int UNUSED (argc),char UNUSED (* argv[]))181 int main(int UNUSED(argc), char UNUSED(*argv[]))
182 {
183 char *srcdir;
184 char fname[100];
185 /* build the name of the file */
186 srcdir = getenv("srcdir");
187 if (srcdir == NULL)
188 strcpy(fname, "usernames.txt");
189 else
190 snprintf(fname, sizeof(fname), "%s/usernames.txt", srcdir);
191 fname[sizeof(fname) - 1] = '\0';
192 /* run the tests */
193 test_simple();
194 test_lotsofelements();
195 test_readelements(fname);
196 test_countelements(0);
197 test_countelements(1);
198 test_countelements(2);
199 test_countelements(3);
200 test_countelements(4);
201 test_countelements(10);
202 test_countelements(20);
203 return 0;
204 }
205