test_dictionary.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include <unity.h>
  2. /* We need to directly insert the .c file in order to test the */
  3. /* static functions as well */
  4. #include "dictionary.c"
  5. void setUp(void)
  6. {
  7. }
  8. void tearDown(void)
  9. {
  10. }
  11. void test_xstrdup(void)
  12. {
  13. size_t i;
  14. char *dup_str;
  15. const char *strings[] = {
  16. "",
  17. "test",
  18. " "
  19. };
  20. char *string_very_long;
  21. /* NULL test */
  22. TEST_ASSERT_NULL(xstrdup(NULL));
  23. for (i = 0 ; i < sizeof(strings) / sizeof(char *) ; ++i) {
  24. dup_str = xstrdup(strings[i]);
  25. TEST_ASSERT_EQUAL_STRING(strings[i], dup_str);
  26. free(dup_str);
  27. }
  28. /* test a overflowing string */
  29. string_very_long = (char*) malloc(10 * 1024);
  30. memset(string_very_long, '#', 10 * 1024);
  31. string_very_long[10 * 1024 - 1] = '\0';
  32. dup_str = xstrdup(string_very_long);
  33. TEST_ASSERT_EQUAL_STRING(string_very_long, dup_str);
  34. free(string_very_long);
  35. free(dup_str);
  36. }
  37. void test_dictionary_grow(void)
  38. {
  39. unsigned i;
  40. dictionary *dic;
  41. dic = dictionary_new(DICTMINSZ);
  42. TEST_ASSERT_NOT_NULL(dic);
  43. TEST_ASSERT_EQUAL(0, dic->n);
  44. TEST_ASSERT_EQUAL(DICTMINSZ, dic->size);
  45. for (i = 1 ; i < 10 ; ++i) {
  46. TEST_ASSERT_EQUAL(0, dictionary_grow(dic));
  47. TEST_ASSERT_EQUAL(0, dic->n);
  48. TEST_ASSERT_EQUAL((1 << i) * DICTMINSZ, dic->size);
  49. }
  50. dictionary_del(dic);
  51. }
  52. void test_dictionary_hash(void)
  53. {
  54. /* NULL test */
  55. TEST_ASSERT_EQUAL(0, dictionary_hash(NULL));
  56. }
  57. void test_dictionary_growing(void)
  58. {
  59. int i, j;
  60. char sec_name[32];
  61. char key_name[64];
  62. dictionary *dic;
  63. dic = dictionary_new(DICTMINSZ);
  64. TEST_ASSERT_NOT_NULL(dic);
  65. TEST_ASSERT_EQUAL(0, dic->n);
  66. /* Makes the dictionary grow */
  67. for (i = 1 ; i < 101; ++i) {
  68. sprintf(sec_name, "sec%d", i);
  69. TEST_ASSERT_EQUAL(0, dictionary_set(dic, sec_name, ""));
  70. for (j = 1 ; j < 11; ++j) {
  71. sprintf(key_name, "%s:key%d", sec_name, j);
  72. TEST_ASSERT_EQUAL(0, dictionary_set(dic, key_name, "dummy_value"));
  73. TEST_ASSERT_EQUAL(i + (i - 1) * 10 + j, dic->n);
  74. }
  75. }
  76. /* Shrink the dictionary */
  77. for (i = 100 ; i > 0; --i) {
  78. sprintf(sec_name, "sec%d", i);
  79. for (j = 10 ; j > 0; --j) {
  80. sprintf(key_name, "%s:key%d", sec_name, j);
  81. dictionary_unset(dic, key_name);
  82. }
  83. dictionary_unset(dic, sec_name);
  84. TEST_ASSERT_EQUAL((i - 1) * (11), dic->n);
  85. }
  86. dictionary_del(dic);
  87. }
  88. static char *get_dump(dictionary *d)
  89. {
  90. FILE *fd;
  91. char *dump_buff;
  92. int dump_size;
  93. /* Dump the dictionary in temporary file */
  94. fd = tmpfile();
  95. if (fd == NULL)
  96. return NULL;
  97. dictionary_dump(d, fd);
  98. /* Retrieve the dump file */
  99. dump_size = ftell(fd);
  100. if (dump_size == -1) {
  101. fclose(fd);
  102. return NULL;
  103. }
  104. rewind(fd);
  105. dump_buff = (char*) calloc(1, dump_size + 1);
  106. if (dump_buff == NULL) {
  107. fclose(fd);
  108. return NULL;
  109. }
  110. if (fread(dump_buff, 1, dump_size, fd) != (size_t)dump_size) {
  111. fclose(fd);
  112. return NULL;
  113. }
  114. fclose(fd);
  115. return dump_buff;
  116. }
  117. void test_dictionary_unset(void)
  118. {
  119. int i, j;
  120. char sec_name[32];
  121. char key_name[64];
  122. dictionary *dic1;
  123. dictionary *dic2;
  124. char *dic1_dump;
  125. char *dic2_dump;
  126. /* try dummy unsets */
  127. dictionary_unset(NULL, NULL);
  128. dictionary_unset(NULL, key_name);
  129. /* Generate two similar dictionaries */
  130. dic1 = dictionary_new(DICTMINSZ);
  131. TEST_ASSERT_NOT_NULL(dic1);
  132. for (i = 1 ; i < 10; ++i) {
  133. sprintf(sec_name, "sec%d", i);
  134. dictionary_set(dic1, sec_name, "");
  135. for (j = 1 ; j < 10; ++j) {
  136. sprintf(key_name, "%s:key%d", sec_name, j);
  137. dictionary_set(dic1, key_name, "dummy_value");
  138. }
  139. }
  140. dic2 = dictionary_new(DICTMINSZ);
  141. TEST_ASSERT_NOT_NULL(dic2);
  142. for (i = 1 ; i < 10; ++i) {
  143. sprintf(sec_name, "sec%d", i);
  144. dictionary_set(dic2, sec_name, "");
  145. for (j = 1 ; j < 10; ++j) {
  146. sprintf(key_name, "%s:key%d", sec_name, j);
  147. dictionary_set(dic2, key_name, "dummy_value");
  148. }
  149. }
  150. /* Make sure the dictionaries are the same */
  151. dic1_dump = get_dump(dic1);
  152. dic2_dump = get_dump(dic2);
  153. TEST_ASSERT_EQUAL_STRING(dic1_dump, dic2_dump);
  154. free(dic1_dump);
  155. free(dic2_dump);
  156. /* Those tests should not change the dictionary */
  157. dictionary_unset(dic2, NULL);
  158. dictionary_unset(dic2, "bad_key");
  159. /* dic1 and dic2 must still be the same */
  160. dic1_dump = get_dump(dic1);
  161. dic2_dump = get_dump(dic2);
  162. TEST_ASSERT_EQUAL_STRING(dic1_dump, dic2_dump);
  163. free(dic1_dump);
  164. free(dic2_dump);
  165. dictionary_del(dic1);
  166. dictionary_del(dic2);
  167. }
  168. void test_dictionary_dump(void)
  169. {
  170. int i, j;
  171. char sec_name[32];
  172. char key_name[64];
  173. dictionary *dic;
  174. char *dump_buff;
  175. const char dump_real[] = "\
  176. sec1\t[]\n\
  177. sec1:key1\t[dummy_value]\n\
  178. sec1:key2\t[dummy_value]\n\
  179. sec1:key3\t[dummy_value]\n\
  180. sec1:key4\t[dummy_value]\n\
  181. sec2\t[]\n\
  182. sec2:key1\t[dummy_value]\n\
  183. sec2:key2\t[dummy_value]\n\
  184. sec2:key3\t[dummy_value]\n\
  185. sec2:key4\t[dummy_value]\n\
  186. ";
  187. dic = dictionary_new(DICTMINSZ);
  188. TEST_ASSERT_NOT_NULL(dic);
  189. /* Try dummy values */
  190. dictionary_dump(NULL, NULL);
  191. dictionary_dump(dic, NULL);
  192. /* Try with empty dictionary first */
  193. dump_buff = get_dump(dic);
  194. TEST_ASSERT_EQUAL_STRING("empty dictionary\n", dump_buff);
  195. free(dump_buff);
  196. /* Populate the dictionary */
  197. for (i = 1 ; i < 3; ++i) {
  198. sprintf(sec_name, "sec%d", i);
  199. dictionary_set(dic, sec_name, "");
  200. for (j = 1 ; j < 5; ++j) {
  201. sprintf(key_name, "%s:key%d", sec_name, j);
  202. dictionary_set(dic, key_name, "dummy_value");
  203. }
  204. }
  205. /* Check the dump file */
  206. dump_buff = get_dump(dic);
  207. TEST_ASSERT_EQUAL_STRING(dump_real, dump_buff);
  208. free(dump_buff);
  209. dictionary_del(dic);
  210. }
  211. void test_dictionary_get(void)
  212. {
  213. dictionary *dic;
  214. int i, j;
  215. char sec_name[32];
  216. char key_name[64];
  217. /*NULL test*/
  218. TEST_ASSERT_NULL(dictionary_get(NULL, NULL, NULL));
  219. TEST_ASSERT_NULL(dictionary_get(NULL, "string", NULL));
  220. /*Check the def return element*/
  221. dic = dictionary_new(DICTMINSZ);
  222. TEST_ASSERT_NOT_NULL(dic);
  223. TEST_ASSERT_NULL(dictionary_get(dic, "dummy", NULL));
  224. TEST_ASSERT_EQUAL_STRING("def", dictionary_get(dic, NULL, "def"));
  225. TEST_ASSERT_EQUAL_STRING("def", dictionary_get(dic, "dummy", "def"));
  226. /*Populate the dictionary*/
  227. for (i = 1; i < 3; ++i)
  228. {
  229. sprintf(sec_name, "sec%d", i);
  230. dictionary_set(dic, sec_name, "");
  231. for (j = 1; j < 5; ++j)
  232. {
  233. sprintf(key_name, "%s:key%d", sec_name, j);
  234. dictionary_set(dic, key_name, "dummy_value");
  235. TEST_ASSERT_EQUAL_STRING("dummy_value",
  236. dictionary_get(dic, key_name, "string"));
  237. }
  238. }
  239. /*Test get dictionary section value*/
  240. TEST_ASSERT_EQUAL_STRING("",
  241. dictionary_get(dic, "sec1", NULL));
  242. TEST_ASSERT_EQUAL_STRING("",
  243. dictionary_get(dic, "sec1", "def"));
  244. /*delete and set a key in a dictionary*/
  245. dictionary_unset(dic, "sec1:key4");
  246. TEST_ASSERT_EQUAL_STRING("def",
  247. dictionary_get(dic, "sec1:key4", "def"));
  248. dictionary_set(dic, "sec1:key4", "dummy_value");
  249. TEST_ASSERT_EQUAL_STRING("dummy_value",
  250. dictionary_get(dic, "sec1:key4", "def"));
  251. dictionary_del(dic);
  252. }