6 /* We need to directly insert the .c file in order to test the */
7 /* static functions as well */
8 #include "dictionary.c"
10 void Test_xstrdup(CuTest *tc)
14 const char *strings[] = {
19 char *string_very_long;
22 CuAssertPtrEquals(tc, NULL, xstrdup(NULL));
24 for (i = 0 ; i < sizeof(strings) / sizeof(char *) ; ++i) {
25 dup_str = xstrdup(strings[i]);
26 CuAssertStrEquals(tc, strings[i], dup_str);
30 /* test a overflowing string */
31 string_very_long = (char*) malloc(10 * 1024);
32 memset(string_very_long, '#', 10 * 1024);
33 string_very_long[10 * 1024 - 1] = '\0';
34 dup_str = xstrdup(string_very_long);
35 CuAssertStrEquals(tc, string_very_long, dup_str);
37 free(string_very_long);
41 void Test_dictionary_grow(CuTest *tc)
46 dic = dictionary_new(DICTMINSZ);
47 CuAssertPtrNotNull(tc, dic);
48 CuAssertIntEquals(tc, 0, dic->n);
49 CuAssertIntEquals(tc, DICTMINSZ, dic->size);
51 for (i = 1 ; i < 10 ; ++i) {
52 CuAssertIntEquals(tc, 0, dictionary_grow(dic));
53 CuAssertIntEquals(tc, 0, dic->n);
54 CuAssertIntEquals(tc, (1 << i) * DICTMINSZ, dic->size);
58 void Test_dictionary_hash(CuTest *tc)
61 CuAssertIntEquals(tc, 0, dictionary_hash(NULL));
64 void Test_dictionary_growing(CuTest *tc)
71 dic = dictionary_new(DICTMINSZ);
72 CuAssertPtrNotNull(tc, dic);
73 CuAssertIntEquals(tc, 0, dic->n);
75 /* Makes the dictionary grow */
76 for (i = 1 ; i < 101; ++i) {
77 sprintf(sec_name, "sec%d", i);
78 CuAssertIntEquals(tc, 0, dictionary_set(dic, sec_name, ""));
79 for (j = 1 ; j < 11; ++j) {
80 sprintf(key_name, "%s:key%d", sec_name, j);
81 CuAssertIntEquals(tc, 0, dictionary_set(dic, key_name, "dummy_value"));
82 CuAssertIntEquals(tc, i + (i - 1) * 10 + j, dic->n);
86 /* Shrink the dictionary */
87 for (i = 100 ; i > 0; --i) {
88 sprintf(sec_name, "sec%d", i);
89 for (j = 10 ; j > 0; --j) {
90 sprintf(key_name, "%s:key%d", sec_name, j);
91 dictionary_unset(dic, key_name);
93 dictionary_unset(dic, sec_name);
94 CuAssertIntEquals(tc, (i - 1) * (11), dic->n);
100 static char *get_dump(dictionary *d)
106 /* Dump the dictionary in temporary file */
110 dictionary_dump(d, fd);
112 /* Retrieve the dump file */
113 dump_size = ftell(fd);
114 if (dump_size == -1) {
120 dump_buff = (char*) calloc(1, dump_size + 1);
121 if (dump_buff == NULL) {
125 if (fread(dump_buff, 1, dump_size, fd) != (size_t)dump_size) {
134 void Test_dictionary_unset(CuTest *tc)
144 /* try dummy unsets */
145 dictionary_unset(NULL, NULL);
146 dictionary_unset(NULL, key_name);
148 /* Generate two similar dictionaries */
149 dic1 = dictionary_new(DICTMINSZ);
150 CuAssertPtrNotNull(tc, dic1);
151 for (i = 1 ; i < 10; ++i) {
152 sprintf(sec_name, "sec%d", i);
153 dictionary_set(dic1, sec_name, "");
154 for (j = 1 ; j < 10; ++j) {
155 sprintf(key_name, "%s:key%d", sec_name, j);
156 dictionary_set(dic1, key_name, "dummy_value");
159 dic2 = dictionary_new(DICTMINSZ);
160 CuAssertPtrNotNull(tc, dic2);
161 for (i = 1 ; i < 10; ++i) {
162 sprintf(sec_name, "sec%d", i);
163 dictionary_set(dic2, sec_name, "");
164 for (j = 1 ; j < 10; ++j) {
165 sprintf(key_name, "%s:key%d", sec_name, j);
166 dictionary_set(dic2, key_name, "dummy_value");
170 /* Make sure the dictionaries are the same */
171 dic1_dump = get_dump(dic1);
172 dic2_dump = get_dump(dic2);
173 CuAssertStrEquals(tc, dic1_dump, dic2_dump);
177 /* Those tests should not change the dictionary */
178 dictionary_unset(dic2, NULL);
179 dictionary_unset(dic2, "bad_key");
181 /* dic1 and dic2 must still be the same */
182 dic1_dump = get_dump(dic1);
183 dic2_dump = get_dump(dic2);
184 CuAssertStrEquals(tc, dic1_dump, dic2_dump);
189 void Test_dictionary_dump(CuTest *tc)
196 const char dump_real[] = "\
198 sec1:key1\t[dummy_value]\n\
199 sec1:key2\t[dummy_value]\n\
200 sec1:key3\t[dummy_value]\n\
201 sec1:key4\t[dummy_value]\n\
203 sec2:key1\t[dummy_value]\n\
204 sec2:key2\t[dummy_value]\n\
205 sec2:key3\t[dummy_value]\n\
206 sec2:key4\t[dummy_value]\n\
209 dic = dictionary_new(DICTMINSZ);
210 CuAssertPtrNotNull(tc, dic);
212 /* Try dummy values */
213 dictionary_dump(NULL, NULL);
214 dictionary_dump(dic, NULL);
216 /* Try with empty dictionary first */
217 dump_buff = get_dump(dic);
218 CuAssertStrEquals(tc, "empty dictionary\n", dump_buff);
221 /* Populate the dictionary */
222 for (i = 1 ; i < 3; ++i) {
223 sprintf(sec_name, "sec%d", i);
224 dictionary_set(dic, sec_name, "");
225 for (j = 1 ; j < 5; ++j) {
226 sprintf(key_name, "%s:key%d", sec_name, j);
227 dictionary_set(dic, key_name, "dummy_value");
231 /* Check the dump file */
232 dump_buff = get_dump(dic);
233 CuAssertStrEquals(tc, dump_real, dump_buff);