Bump to iniparser 4.1
[platform/upstream/iniparser.git] / test / test_dictionary.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "CuTest.h"
5
6 /* We need to directly insert the .c file in order to test the */
7 /* static functions as well */
8 #include "dictionary.c"
9
10 void Test_xstrdup(CuTest *tc)
11 {
12     size_t i;
13     char *dup_str;
14     const char *strings[] = {
15         "",
16         "test",
17         " "
18     };
19     char *string_very_long;
20
21     /* NULL test */
22     CuAssertPtrEquals(tc, NULL, xstrdup(NULL));
23
24     for (i = 0 ; i < sizeof(strings) / sizeof(char *) ; ++i) {
25         dup_str = xstrdup(strings[i]);
26         CuAssertStrEquals(tc, strings[i], dup_str);
27         free(dup_str);
28     }
29
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);
36
37     free(string_very_long);
38     free(dup_str);
39 }
40
41 void Test_dictionary_grow(CuTest *tc)
42 {
43     unsigned i;
44     dictionary *dic;
45
46     dic = dictionary_new(DICTMINSZ);
47     CuAssertPtrNotNull(tc, dic);
48     CuAssertIntEquals(tc, 0, dic->n);
49     CuAssertIntEquals(tc, DICTMINSZ, dic->size);
50
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);
55     }
56 }
57
58 void Test_dictionary_hash(CuTest *tc)
59 {
60     /* NULL test */
61     CuAssertIntEquals(tc, 0, dictionary_hash(NULL));
62 }
63
64 void Test_dictionary_growing(CuTest *tc)
65 {
66     int i, j;
67     char sec_name[32];
68     char key_name[64];
69     dictionary *dic;
70
71     dic = dictionary_new(DICTMINSZ);
72     CuAssertPtrNotNull(tc, dic);
73     CuAssertIntEquals(tc, 0, dic->n);
74
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);
83         }
84     }
85
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);
92         }
93         dictionary_unset(dic, sec_name);
94         CuAssertIntEquals(tc, (i - 1) * (11), dic->n);
95     }
96
97     dictionary_del(dic);
98 }
99
100 static char *get_dump(dictionary *d)
101 {
102     FILE *fd;
103     char *dump_buff;
104     int dump_size;
105
106     /* Dump the dictionary in temporary file */
107     fd = tmpfile();
108     if (fd == NULL)
109         return NULL;
110     dictionary_dump(d, fd);
111
112     /* Retrieve the dump file */
113     dump_size = ftell(fd);
114     if (dump_size == -1) {
115         fclose(fd);
116         return NULL;
117     }
118     rewind(fd);
119
120     dump_buff = (char*) calloc(1, dump_size + 1);
121     if (dump_buff == NULL) {
122         fclose(fd);
123         return NULL;
124     }
125     if (fread(dump_buff, 1, dump_size, fd) != (size_t)dump_size) {
126         fclose(fd);
127         return NULL;
128     }
129
130     fclose(fd);
131     return dump_buff;
132 }
133
134 void Test_dictionary_unset(CuTest *tc)
135 {
136     int i, j;
137     char sec_name[32];
138     char key_name[64];
139     dictionary *dic1;
140     dictionary *dic2;
141     char *dic1_dump;
142     char *dic2_dump;
143
144     /* try dummy unsets */
145     dictionary_unset(NULL, NULL);
146     dictionary_unset(NULL, key_name);
147
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");
157         }
158     }
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");
167         }
168     }
169
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);
174     free(dic1_dump);
175     free(dic2_dump);
176
177     /* Those tests should not change the dictionary */
178     dictionary_unset(dic2, NULL);
179     dictionary_unset(dic2, "bad_key");
180
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);
185     free(dic1_dump);
186     free(dic2_dump);
187 }
188
189 void Test_dictionary_dump(CuTest *tc)
190 {
191     int i, j;
192     char sec_name[32];
193     char key_name[64];
194     dictionary *dic;
195     char *dump_buff;
196     const char dump_real[] = "\
197                 sec1\t[]\n\
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\
202                 sec2\t[]\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\
207 ";
208
209     dic = dictionary_new(DICTMINSZ);
210     CuAssertPtrNotNull(tc, dic);
211
212     /* Try dummy values */
213     dictionary_dump(NULL, NULL);
214     dictionary_dump(dic, NULL);
215
216     /* Try with empty dictionary first */
217     dump_buff = get_dump(dic);
218     CuAssertStrEquals(tc, "empty dictionary\n", dump_buff);
219     free(dump_buff);
220
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");
228         }
229     }
230
231     /* Check the dump file */
232     dump_buff = get_dump(dic);
233     CuAssertStrEquals(tc, dump_real, dump_buff);
234     free(dump_buff);
235
236     dictionary_del(dic);
237 }