test_containers.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "or.h"
  7. #include "test.h"
  8. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  9. * *<b>b</b>. */
  10. static int
  11. compare_strs_(const void **a, const void **b)
  12. {
  13. const char *s1 = *a, *s2 = *b;
  14. return strcmp(s1, s2);
  15. }
  16. /** Helper: return a tristate based on comparing the strings in <b>a</b> and
  17. * *<b>b</b>. */
  18. static int
  19. compare_strs_for_bsearch_(const void *a, const void **b)
  20. {
  21. const char *s1 = a, *s2 = *b;
  22. return strcmp(s1, s2);
  23. }
  24. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  25. * *<b>b</b>, excluding a's first character, and ignoring case. */
  26. static int
  27. compare_without_first_ch_(const void *a, const void **b)
  28. {
  29. const char *s1 = a, *s2 = *b;
  30. return strcasecmp(s1+1, s2);
  31. }
  32. /** Run unit tests for basic dynamic-sized array functionality. */
  33. static void
  34. test_container_smartlist_basic(void)
  35. {
  36. smartlist_t *sl;
  37. /* XXXX test sort_digests, uniq_strings, uniq_digests */
  38. /* Test smartlist add, del_keeporder, insert, get. */
  39. sl = smartlist_new();
  40. smartlist_add(sl, (void*)1);
  41. smartlist_add(sl, (void*)2);
  42. smartlist_add(sl, (void*)3);
  43. smartlist_add(sl, (void*)4);
  44. smartlist_del_keeporder(sl, 1);
  45. smartlist_insert(sl, 1, (void*)22);
  46. smartlist_insert(sl, 0, (void*)0);
  47. smartlist_insert(sl, 5, (void*)555);
  48. test_eq_ptr((void*)0, smartlist_get(sl,0));
  49. test_eq_ptr((void*)1, smartlist_get(sl,1));
  50. test_eq_ptr((void*)22, smartlist_get(sl,2));
  51. test_eq_ptr((void*)3, smartlist_get(sl,3));
  52. test_eq_ptr((void*)4, smartlist_get(sl,4));
  53. test_eq_ptr((void*)555, smartlist_get(sl,5));
  54. /* Try deleting in the middle. */
  55. smartlist_del(sl, 1);
  56. test_eq_ptr((void*)555, smartlist_get(sl, 1));
  57. /* Try deleting at the end. */
  58. smartlist_del(sl, 4);
  59. test_eq(4, smartlist_len(sl));
  60. /* test isin. */
  61. test_assert(smartlist_isin(sl, (void*)3));
  62. test_assert(!smartlist_isin(sl, (void*)99));
  63. done:
  64. smartlist_free(sl);
  65. }
  66. /** Run unit tests for smartlist-of-strings functionality. */
  67. static void
  68. test_container_smartlist_strings(void)
  69. {
  70. smartlist_t *sl = smartlist_new();
  71. char *cp=NULL, *cp_alloc=NULL;
  72. size_t sz;
  73. /* Test split and join */
  74. test_eq(0, smartlist_len(sl));
  75. smartlist_split_string(sl, "abc", ":", 0, 0);
  76. test_eq(1, smartlist_len(sl));
  77. test_streq("abc", smartlist_get(sl, 0));
  78. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  79. test_eq(4, smartlist_len(sl));
  80. test_streq("a", smartlist_get(sl, 1));
  81. test_streq("bc", smartlist_get(sl, 2));
  82. test_streq("", smartlist_get(sl, 3));
  83. cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
  84. test_streq(cp_alloc, "abcabc");
  85. tor_free(cp_alloc);
  86. cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
  87. test_streq(cp_alloc, "abc!a!bc!");
  88. tor_free(cp_alloc);
  89. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  90. test_streq(cp_alloc, "abcXYaXYbcXY");
  91. tor_free(cp_alloc);
  92. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  93. test_streq(cp_alloc, "abcXYaXYbcXYXY");
  94. tor_free(cp_alloc);
  95. cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
  96. test_streq(cp_alloc, "abcabc");
  97. tor_free(cp_alloc);
  98. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  99. test_eq(8, smartlist_len(sl));
  100. test_streq("", smartlist_get(sl, 4));
  101. test_streq("def", smartlist_get(sl, 5));
  102. test_streq(" ", smartlist_get(sl, 6));
  103. test_streq("ghijk", smartlist_get(sl, 7));
  104. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  105. smartlist_clear(sl);
  106. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  107. test_eq(3, smartlist_len(sl));
  108. test_streq("a", smartlist_get(sl,0));
  109. test_streq("bbd", smartlist_get(sl,1));
  110. test_streq("cdef", smartlist_get(sl,2));
  111. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  112. SPLIT_SKIP_SPACE, 0);
  113. test_eq(8, smartlist_len(sl));
  114. test_streq("z", smartlist_get(sl,3));
  115. test_streq("zhasd", smartlist_get(sl,4));
  116. test_streq("", smartlist_get(sl,5));
  117. test_streq("bnud", smartlist_get(sl,6));
  118. test_streq("", smartlist_get(sl,7));
  119. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  120. smartlist_clear(sl);
  121. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  122. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  123. test_eq(4, smartlist_len(sl));
  124. test_streq("ab", smartlist_get(sl,0));
  125. test_streq("c", smartlist_get(sl,1));
  126. test_streq("d", smartlist_get(sl,2));
  127. test_streq("ef", smartlist_get(sl,3));
  128. smartlist_split_string(sl, "ghi\tj", NULL,
  129. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  130. test_eq(6, smartlist_len(sl));
  131. test_streq("ghi", smartlist_get(sl,4));
  132. test_streq("j", smartlist_get(sl,5));
  133. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  134. smartlist_clear(sl);
  135. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  136. test_streq(cp_alloc, "");
  137. tor_free(cp_alloc);
  138. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  139. test_streq(cp_alloc, "XY");
  140. tor_free(cp_alloc);
  141. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  142. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  143. test_eq(3, smartlist_len(sl));
  144. test_streq("z", smartlist_get(sl, 0));
  145. test_streq("zhasd", smartlist_get(sl, 1));
  146. test_streq("bnud", smartlist_get(sl, 2));
  147. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  148. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  149. test_eq(5, smartlist_len(sl));
  150. test_streq("z", smartlist_get(sl, 3));
  151. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  152. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  153. smartlist_clear(sl);
  154. smartlist_split_string(sl, "abcd\n", "\n",
  155. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  156. test_eq(1, smartlist_len(sl));
  157. test_streq("abcd", smartlist_get(sl, 0));
  158. smartlist_split_string(sl, "efgh", "\n",
  159. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  160. test_eq(2, smartlist_len(sl));
  161. test_streq("efgh", smartlist_get(sl, 1));
  162. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  163. smartlist_clear(sl);
  164. /* Test swapping, shuffling, and sorting. */
  165. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  166. test_eq(7, smartlist_len(sl));
  167. smartlist_sort(sl, compare_strs_);
  168. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  169. test_streq(cp_alloc,"and,arma,by,nickm,onion,router,the");
  170. tor_free(cp_alloc);
  171. smartlist_swap(sl, 1, 5);
  172. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  173. test_streq(cp_alloc,"and,router,by,nickm,onion,arma,the");
  174. tor_free(cp_alloc);
  175. smartlist_shuffle(sl);
  176. test_eq(7, smartlist_len(sl));
  177. test_assert(smartlist_string_isin(sl, "and"));
  178. test_assert(smartlist_string_isin(sl, "router"));
  179. test_assert(smartlist_string_isin(sl, "by"));
  180. test_assert(smartlist_string_isin(sl, "nickm"));
  181. test_assert(smartlist_string_isin(sl, "onion"));
  182. test_assert(smartlist_string_isin(sl, "arma"));
  183. test_assert(smartlist_string_isin(sl, "the"));
  184. /* Test bsearch. */
  185. smartlist_sort(sl, compare_strs_);
  186. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  187. compare_without_first_ch_));
  188. test_streq("and", smartlist_bsearch(sl, " AND", compare_without_first_ch_));
  189. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", compare_without_first_ch_));
  190. /* Test bsearch_idx */
  191. {
  192. int f;
  193. smartlist_t *tmp = NULL;
  194. test_eq(0, smartlist_bsearch_idx(sl," aaa",compare_without_first_ch_,&f));
  195. test_eq(f, 0);
  196. test_eq(0, smartlist_bsearch_idx(sl," and",compare_without_first_ch_,&f));
  197. test_eq(f, 1);
  198. test_eq(1, smartlist_bsearch_idx(sl," arm",compare_without_first_ch_,&f));
  199. test_eq(f, 0);
  200. test_eq(1, smartlist_bsearch_idx(sl," arma",compare_without_first_ch_,&f));
  201. test_eq(f, 1);
  202. test_eq(2, smartlist_bsearch_idx(sl," armb",compare_without_first_ch_,&f));
  203. test_eq(f, 0);
  204. test_eq(7, smartlist_bsearch_idx(sl," zzzz",compare_without_first_ch_,&f));
  205. test_eq(f, 0);
  206. /* Test trivial cases for list of length 0 or 1 */
  207. tmp = smartlist_new();
  208. test_eq(0, smartlist_bsearch_idx(tmp, "foo",
  209. compare_strs_for_bsearch_, &f));
  210. test_eq(f, 0);
  211. smartlist_insert(tmp, 0, (void *)("bar"));
  212. test_eq(1, smartlist_bsearch_idx(tmp, "foo",
  213. compare_strs_for_bsearch_, &f));
  214. test_eq(f, 0);
  215. test_eq(0, smartlist_bsearch_idx(tmp, "aaa",
  216. compare_strs_for_bsearch_, &f));
  217. test_eq(f, 0);
  218. test_eq(0, smartlist_bsearch_idx(tmp, "bar",
  219. compare_strs_for_bsearch_, &f));
  220. test_eq(f, 1);
  221. /* ... and one for length 2 */
  222. smartlist_insert(tmp, 1, (void *)("foo"));
  223. test_eq(1, smartlist_bsearch_idx(tmp, "foo",
  224. compare_strs_for_bsearch_, &f));
  225. test_eq(f, 1);
  226. test_eq(2, smartlist_bsearch_idx(tmp, "goo",
  227. compare_strs_for_bsearch_, &f));
  228. test_eq(f, 0);
  229. smartlist_free(tmp);
  230. }
  231. /* Test reverse() and pop_last() */
  232. smartlist_reverse(sl);
  233. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  234. test_streq(cp_alloc,"the,router,onion,nickm,by,arma,and");
  235. tor_free(cp_alloc);
  236. cp_alloc = smartlist_pop_last(sl);
  237. test_streq(cp_alloc, "and");
  238. tor_free(cp_alloc);
  239. test_eq(smartlist_len(sl), 6);
  240. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  241. smartlist_clear(sl);
  242. cp_alloc = smartlist_pop_last(sl);
  243. test_eq_ptr(cp_alloc, NULL);
  244. /* Test uniq() */
  245. smartlist_split_string(sl,
  246. "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
  247. ",", 0, 0);
  248. smartlist_sort(sl, compare_strs_);
  249. smartlist_uniq(sl, compare_strs_, tor_free_);
  250. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  251. test_streq(cp_alloc, "50,a,canal,man,noon,panama,plan,radar");
  252. tor_free(cp_alloc);
  253. /* Test string_isin and isin_case and num_isin */
  254. test_assert(smartlist_string_isin(sl, "noon"));
  255. test_assert(!smartlist_string_isin(sl, "noonoon"));
  256. test_assert(smartlist_string_isin_case(sl, "nOOn"));
  257. test_assert(!smartlist_string_isin_case(sl, "nooNooN"));
  258. test_assert(smartlist_string_num_isin(sl, 50));
  259. test_assert(!smartlist_string_num_isin(sl, 60));
  260. /* Test smartlist_choose */
  261. {
  262. int i;
  263. int allsame = 1;
  264. int allin = 1;
  265. void *first = smartlist_choose(sl);
  266. test_assert(smartlist_isin(sl, first));
  267. for (i = 0; i < 100; ++i) {
  268. void *second = smartlist_choose(sl);
  269. if (second != first)
  270. allsame = 0;
  271. if (!smartlist_isin(sl, second))
  272. allin = 0;
  273. }
  274. test_assert(!allsame);
  275. test_assert(allin);
  276. }
  277. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  278. smartlist_clear(sl);
  279. /* Test string_remove and remove and join_strings2 */
  280. smartlist_split_string(sl,
  281. "Some say the Earth will end in ice and some in fire",
  282. " ", 0, 0);
  283. cp = smartlist_get(sl, 4);
  284. test_streq(cp, "will");
  285. smartlist_add(sl, cp);
  286. smartlist_remove(sl, cp);
  287. tor_free(cp);
  288. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  289. test_streq(cp_alloc, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  290. tor_free(cp_alloc);
  291. smartlist_string_remove(sl, "in");
  292. cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
  293. test_streq(cp_alloc, "Some+say+the+Earth+fire+end+some+ice+and");
  294. test_eq((int)sz, 40);
  295. done:
  296. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  297. smartlist_free(sl);
  298. tor_free(cp_alloc);
  299. }
  300. /** Run unit tests for smartlist set manipulation functions. */
  301. static void
  302. test_container_smartlist_overlap(void)
  303. {
  304. smartlist_t *sl = smartlist_new();
  305. smartlist_t *ints = smartlist_new();
  306. smartlist_t *odds = smartlist_new();
  307. smartlist_t *evens = smartlist_new();
  308. smartlist_t *primes = smartlist_new();
  309. int i;
  310. for (i=1; i < 10; i += 2)
  311. smartlist_add(odds, (void*)(uintptr_t)i);
  312. for (i=0; i < 10; i += 2)
  313. smartlist_add(evens, (void*)(uintptr_t)i);
  314. /* add_all */
  315. smartlist_add_all(ints, odds);
  316. smartlist_add_all(ints, evens);
  317. test_eq(smartlist_len(ints), 10);
  318. smartlist_add(primes, (void*)2);
  319. smartlist_add(primes, (void*)3);
  320. smartlist_add(primes, (void*)5);
  321. smartlist_add(primes, (void*)7);
  322. /* overlap */
  323. test_assert(smartlist_overlap(ints, odds));
  324. test_assert(smartlist_overlap(odds, primes));
  325. test_assert(smartlist_overlap(evens, primes));
  326. test_assert(!smartlist_overlap(odds, evens));
  327. /* intersect */
  328. smartlist_add_all(sl, odds);
  329. smartlist_intersect(sl, primes);
  330. test_eq(smartlist_len(sl), 3);
  331. test_assert(smartlist_isin(sl, (void*)3));
  332. test_assert(smartlist_isin(sl, (void*)5));
  333. test_assert(smartlist_isin(sl, (void*)7));
  334. /* subtract */
  335. smartlist_add_all(sl, primes);
  336. smartlist_subtract(sl, odds);
  337. test_eq(smartlist_len(sl), 1);
  338. test_assert(smartlist_isin(sl, (void*)2));
  339. done:
  340. smartlist_free(odds);
  341. smartlist_free(evens);
  342. smartlist_free(ints);
  343. smartlist_free(primes);
  344. smartlist_free(sl);
  345. }
  346. /** Run unit tests for smartlist-of-digests functions. */
  347. static void
  348. test_container_smartlist_digests(void)
  349. {
  350. smartlist_t *sl = smartlist_new();
  351. /* digest_isin. */
  352. smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN));
  353. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  354. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  355. test_eq(0, smartlist_digest_isin(NULL, "AAAAAAAAAAAAAAAAAAAA"));
  356. test_assert(smartlist_digest_isin(sl, "AAAAAAAAAAAAAAAAAAAA"));
  357. test_assert(smartlist_digest_isin(sl, "\00090AAB2AAAAaasdAAAAA"));
  358. test_eq(0, smartlist_digest_isin(sl, "\00090AAB2AAABaasdAAAAA"));
  359. /* sort digests */
  360. smartlist_sort_digests(sl);
  361. test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  362. test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  363. test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  364. test_eq(3, smartlist_len(sl));
  365. /* uniq_digests */
  366. smartlist_uniq_digests(sl);
  367. test_eq(2, smartlist_len(sl));
  368. test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  369. test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  370. done:
  371. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  372. smartlist_free(sl);
  373. }
  374. /** Run unit tests for concatenate-a-smartlist-of-strings functions. */
  375. static void
  376. test_container_smartlist_join(void)
  377. {
  378. smartlist_t *sl = smartlist_new();
  379. smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
  380. *sl4 = smartlist_new();
  381. char *joined=NULL;
  382. /* unique, sorted. */
  383. smartlist_split_string(sl,
  384. "Abashments Ambush Anchorman Bacon Banks Borscht "
  385. "Bunks Inhumane Insurance Knish Know Manners "
  386. "Maraschinos Stamina Sunbonnets Unicorns Wombats",
  387. " ", 0, 0);
  388. /* non-unique, sorted. */
  389. smartlist_split_string(sl2,
  390. "Ambush Anchorman Anchorman Anemias Anemias Bacon "
  391. "Crossbowmen Inhumane Insurance Knish Know Manners "
  392. "Manners Maraschinos Wombats Wombats Work",
  393. " ", 0, 0);
  394. SMARTLIST_FOREACH_JOIN(sl, char *, cp1,
  395. sl2, char *, cp2,
  396. strcmp(cp1,cp2),
  397. smartlist_add(sl3, cp2)) {
  398. test_streq(cp1, cp2);
  399. smartlist_add(sl4, cp1);
  400. } SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
  401. SMARTLIST_FOREACH(sl3, const char *, cp,
  402. test_assert(smartlist_isin(sl2, cp) &&
  403. !smartlist_string_isin(sl, cp)));
  404. SMARTLIST_FOREACH(sl4, const char *, cp,
  405. test_assert(smartlist_isin(sl, cp) &&
  406. smartlist_string_isin(sl2, cp)));
  407. joined = smartlist_join_strings(sl3, ",", 0, NULL);
  408. test_streq(joined, "Anemias,Anemias,Crossbowmen,Work");
  409. tor_free(joined);
  410. joined = smartlist_join_strings(sl4, ",", 0, NULL);
  411. test_streq(joined, "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
  412. "Knish,Know,Manners,Manners,Maraschinos,Wombats,Wombats");
  413. tor_free(joined);
  414. done:
  415. smartlist_free(sl4);
  416. smartlist_free(sl3);
  417. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  418. smartlist_free(sl2);
  419. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  420. smartlist_free(sl);
  421. tor_free(joined);
  422. }
  423. /** Run unit tests for bitarray code */
  424. static void
  425. test_container_bitarray(void)
  426. {
  427. bitarray_t *ba = NULL;
  428. int i, j, ok=1;
  429. ba = bitarray_init_zero(1);
  430. test_assert(ba);
  431. test_assert(! bitarray_is_set(ba, 0));
  432. bitarray_set(ba, 0);
  433. test_assert(bitarray_is_set(ba, 0));
  434. bitarray_clear(ba, 0);
  435. test_assert(! bitarray_is_set(ba, 0));
  436. bitarray_free(ba);
  437. ba = bitarray_init_zero(1023);
  438. for (i = 1; i < 64; ) {
  439. for (j = 0; j < 1023; ++j) {
  440. if (j % i)
  441. bitarray_set(ba, j);
  442. else
  443. bitarray_clear(ba, j);
  444. }
  445. for (j = 0; j < 1023; ++j) {
  446. if (!bool_eq(bitarray_is_set(ba, j), j%i))
  447. ok = 0;
  448. }
  449. test_assert(ok);
  450. if (i < 7)
  451. ++i;
  452. else if (i == 28)
  453. i = 32;
  454. else
  455. i += 7;
  456. }
  457. done:
  458. if (ba)
  459. bitarray_free(ba);
  460. }
  461. /** Run unit tests for digest set code (implemented as a hashtable or as a
  462. * bloom filter) */
  463. static void
  464. test_container_digestset(void)
  465. {
  466. smartlist_t *included = smartlist_new();
  467. char d[DIGEST_LEN];
  468. int i;
  469. int ok = 1;
  470. int false_positives = 0;
  471. digestset_t *set = NULL;
  472. for (i = 0; i < 1000; ++i) {
  473. crypto_rand(d, DIGEST_LEN);
  474. smartlist_add(included, tor_memdup(d, DIGEST_LEN));
  475. }
  476. set = digestset_new(1000);
  477. SMARTLIST_FOREACH(included, const char *, cp,
  478. if (digestset_isin(set, cp))
  479. ok = 0);
  480. test_assert(ok);
  481. SMARTLIST_FOREACH(included, const char *, cp,
  482. digestset_add(set, cp));
  483. SMARTLIST_FOREACH(included, const char *, cp,
  484. if (!digestset_isin(set, cp))
  485. ok = 0);
  486. test_assert(ok);
  487. for (i = 0; i < 1000; ++i) {
  488. crypto_rand(d, DIGEST_LEN);
  489. if (digestset_isin(set, d))
  490. ++false_positives;
  491. }
  492. test_assert(false_positives < 50); /* Should be far lower. */
  493. done:
  494. if (set)
  495. digestset_free(set);
  496. SMARTLIST_FOREACH(included, char *, cp, tor_free(cp));
  497. smartlist_free(included);
  498. }
  499. typedef struct pq_entry_t {
  500. const char *val;
  501. int idx;
  502. } pq_entry_t;
  503. /** Helper: return a tristate based on comparing two pq_entry_t values. */
  504. static int
  505. compare_strings_for_pqueue_(const void *p1, const void *p2)
  506. {
  507. const pq_entry_t *e1=p1, *e2=p2;
  508. return strcmp(e1->val, e2->val);
  509. }
  510. /** Run unit tests for heap-based priority queue functions. */
  511. static void
  512. test_container_pqueue(void)
  513. {
  514. smartlist_t *sl = smartlist_new();
  515. int (*cmp)(const void *, const void*);
  516. const int offset = STRUCT_OFFSET(pq_entry_t, idx);
  517. #define ENTRY(s) pq_entry_t s = { #s, -1 }
  518. ENTRY(cows);
  519. ENTRY(zebras);
  520. ENTRY(fish);
  521. ENTRY(frogs);
  522. ENTRY(apples);
  523. ENTRY(squid);
  524. ENTRY(daschunds);
  525. ENTRY(eggplants);
  526. ENTRY(weissbier);
  527. ENTRY(lobsters);
  528. ENTRY(roquefort);
  529. ENTRY(chinchillas);
  530. ENTRY(fireflies);
  531. #define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
  532. cmp = compare_strings_for_pqueue_;
  533. smartlist_pqueue_add(sl, cmp, offset, &cows);
  534. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  535. smartlist_pqueue_add(sl, cmp, offset, &fish);
  536. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  537. smartlist_pqueue_add(sl, cmp, offset, &apples);
  538. smartlist_pqueue_add(sl, cmp, offset, &squid);
  539. smartlist_pqueue_add(sl, cmp, offset, &daschunds);
  540. smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  541. smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  542. smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  543. smartlist_pqueue_add(sl, cmp, offset, &roquefort);
  544. OK();
  545. test_eq(smartlist_len(sl), 11);
  546. test_eq_ptr(smartlist_get(sl, 0), &apples);
  547. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
  548. test_eq(smartlist_len(sl), 10);
  549. OK();
  550. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
  551. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
  552. smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  553. OK();
  554. smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  555. OK();
  556. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
  557. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
  558. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
  559. OK();
  560. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  561. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  562. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
  563. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
  564. OK();
  565. test_eq(smartlist_len(sl), 3);
  566. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  567. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
  568. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
  569. test_eq(smartlist_len(sl), 0);
  570. OK();
  571. /* Now test remove. */
  572. smartlist_pqueue_add(sl, cmp, offset, &cows);
  573. smartlist_pqueue_add(sl, cmp, offset, &fish);
  574. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  575. smartlist_pqueue_add(sl, cmp, offset, &apples);
  576. smartlist_pqueue_add(sl, cmp, offset, &squid);
  577. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  578. test_eq(smartlist_len(sl), 6);
  579. OK();
  580. smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  581. test_eq(smartlist_len(sl), 5);
  582. OK();
  583. smartlist_pqueue_remove(sl, cmp, offset, &cows);
  584. test_eq(smartlist_len(sl), 4);
  585. OK();
  586. smartlist_pqueue_remove(sl, cmp, offset, &apples);
  587. test_eq(smartlist_len(sl), 3);
  588. OK();
  589. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  590. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  591. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  592. test_eq(smartlist_len(sl), 0);
  593. OK();
  594. #undef OK
  595. done:
  596. smartlist_free(sl);
  597. }
  598. /** Run unit tests for string-to-void* map functions */
  599. static void
  600. test_container_strmap(void)
  601. {
  602. strmap_t *map;
  603. strmap_iter_t *iter;
  604. const char *k;
  605. void *v;
  606. char *visited = NULL;
  607. smartlist_t *found_keys = NULL;
  608. map = strmap_new();
  609. test_assert(map);
  610. test_eq(strmap_size(map), 0);
  611. test_assert(strmap_isempty(map));
  612. v = strmap_set(map, "K1", (void*)99);
  613. test_eq_ptr(v, NULL);
  614. test_assert(!strmap_isempty(map));
  615. v = strmap_set(map, "K2", (void*)101);
  616. test_eq_ptr(v, NULL);
  617. v = strmap_set(map, "K1", (void*)100);
  618. test_eq_ptr(v, (void*)99);
  619. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  620. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  621. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  622. strmap_assert_ok(map);
  623. v = strmap_remove(map,"K2");
  624. strmap_assert_ok(map);
  625. test_eq_ptr(v, (void*)101);
  626. test_eq_ptr(strmap_get(map,"K2"), NULL);
  627. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  628. strmap_set(map, "K2", (void*)101);
  629. strmap_set(map, "K3", (void*)102);
  630. strmap_set(map, "K4", (void*)103);
  631. test_eq(strmap_size(map), 4);
  632. strmap_assert_ok(map);
  633. strmap_set(map, "K5", (void*)104);
  634. strmap_set(map, "K6", (void*)105);
  635. strmap_assert_ok(map);
  636. /* Test iterator. */
  637. iter = strmap_iter_init(map);
  638. found_keys = smartlist_new();
  639. while (!strmap_iter_done(iter)) {
  640. strmap_iter_get(iter,&k,&v);
  641. smartlist_add(found_keys, tor_strdup(k));
  642. test_eq_ptr(v, strmap_get(map, k));
  643. if (!strcmp(k, "K2")) {
  644. iter = strmap_iter_next_rmv(map,iter);
  645. } else {
  646. iter = strmap_iter_next(map,iter);
  647. }
  648. }
  649. /* Make sure we removed K2, but not the others. */
  650. test_eq_ptr(strmap_get(map, "K2"), NULL);
  651. test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  652. /* Make sure we visited everyone once */
  653. smartlist_sort_strings(found_keys);
  654. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  655. test_streq(visited, "K1:K2:K3:K4:K5:K6");
  656. strmap_assert_ok(map);
  657. /* Clean up after ourselves. */
  658. strmap_free(map, NULL);
  659. map = NULL;
  660. /* Now try some lc functions. */
  661. map = strmap_new();
  662. strmap_set_lc(map,"Ab.C", (void*)1);
  663. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  664. strmap_assert_ok(map);
  665. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  666. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  667. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  668. strmap_assert_ok(map);
  669. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  670. done:
  671. if (map)
  672. strmap_free(map,NULL);
  673. if (found_keys) {
  674. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  675. smartlist_free(found_keys);
  676. }
  677. tor_free(visited);
  678. }
  679. /** Run unit tests for getting the median of a list. */
  680. static void
  681. test_container_order_functions(void)
  682. {
  683. int lst[25], n = 0;
  684. // int a=12,b=24,c=25,d=60,e=77;
  685. #define median() median_int(lst, n)
  686. lst[n++] = 12;
  687. test_eq(12, median()); /* 12 */
  688. lst[n++] = 77;
  689. //smartlist_shuffle(sl);
  690. test_eq(12, median()); /* 12, 77 */
  691. lst[n++] = 77;
  692. //smartlist_shuffle(sl);
  693. test_eq(77, median()); /* 12, 77, 77 */
  694. lst[n++] = 24;
  695. test_eq(24, median()); /* 12,24,77,77 */
  696. lst[n++] = 60;
  697. lst[n++] = 12;
  698. lst[n++] = 25;
  699. //smartlist_shuffle(sl);
  700. test_eq(25, median()); /* 12,12,24,25,60,77,77 */
  701. #undef median
  702. done:
  703. ;
  704. }
  705. #define CONTAINER_LEGACY(name) \
  706. { #name, legacy_test_helper, 0, &legacy_setup, test_container_ ## name }
  707. struct testcase_t container_tests[] = {
  708. CONTAINER_LEGACY(smartlist_basic),
  709. CONTAINER_LEGACY(smartlist_strings),
  710. CONTAINER_LEGACY(smartlist_overlap),
  711. CONTAINER_LEGACY(smartlist_digests),
  712. CONTAINER_LEGACY(smartlist_join),
  713. CONTAINER_LEGACY(bitarray),
  714. CONTAINER_LEGACY(digestset),
  715. CONTAINER_LEGACY(strmap),
  716. CONTAINER_LEGACY(pqueue),
  717. CONTAINER_LEGACY(order_functions),
  718. END_OF_TESTCASES
  719. };