test_containers.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "or.h"
  7. #include "fp_pair.h"
  8. #include "test.h"
  9. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  10. * *<b>b</b>. */
  11. static int
  12. compare_strs_(const void **a, const void **b)
  13. {
  14. const char *s1 = *a, *s2 = *b;
  15. return strcmp(s1, s2);
  16. }
  17. /** Helper: return a tristate based on comparing the strings in <b>a</b> and
  18. * *<b>b</b>. */
  19. static int
  20. compare_strs_for_bsearch_(const void *a, const void **b)
  21. {
  22. const char *s1 = a, *s2 = *b;
  23. return strcmp(s1, s2);
  24. }
  25. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  26. * *<b>b</b>, excluding a's first character, and ignoring case. */
  27. static int
  28. compare_without_first_ch_(const void *a, const void **b)
  29. {
  30. const char *s1 = a, *s2 = *b;
  31. return strcasecmp(s1+1, s2);
  32. }
  33. /** Run unit tests for basic dynamic-sized array functionality. */
  34. static void
  35. test_container_smartlist_basic(void)
  36. {
  37. smartlist_t *sl;
  38. /* XXXX test sort_digests, uniq_strings, uniq_digests */
  39. /* Test smartlist add, del_keeporder, insert, get. */
  40. sl = smartlist_new();
  41. smartlist_add(sl, (void*)1);
  42. smartlist_add(sl, (void*)2);
  43. smartlist_add(sl, (void*)3);
  44. smartlist_add(sl, (void*)4);
  45. smartlist_del_keeporder(sl, 1);
  46. smartlist_insert(sl, 1, (void*)22);
  47. smartlist_insert(sl, 0, (void*)0);
  48. smartlist_insert(sl, 5, (void*)555);
  49. test_eq_ptr((void*)0, smartlist_get(sl,0));
  50. test_eq_ptr((void*)1, smartlist_get(sl,1));
  51. test_eq_ptr((void*)22, smartlist_get(sl,2));
  52. test_eq_ptr((void*)3, smartlist_get(sl,3));
  53. test_eq_ptr((void*)4, smartlist_get(sl,4));
  54. test_eq_ptr((void*)555, smartlist_get(sl,5));
  55. /* Try deleting in the middle. */
  56. smartlist_del(sl, 1);
  57. test_eq_ptr((void*)555, smartlist_get(sl, 1));
  58. /* Try deleting at the end. */
  59. smartlist_del(sl, 4);
  60. test_eq(4, smartlist_len(sl));
  61. /* test isin. */
  62. test_assert(smartlist_contains(sl, (void*)3));
  63. test_assert(!smartlist_contains(sl, (void*)99));
  64. done:
  65. smartlist_free(sl);
  66. }
  67. /** Run unit tests for smartlist-of-strings functionality. */
  68. static void
  69. test_container_smartlist_strings(void)
  70. {
  71. smartlist_t *sl = smartlist_new();
  72. char *cp=NULL, *cp_alloc=NULL;
  73. size_t sz;
  74. /* Test split and join */
  75. test_eq(0, smartlist_len(sl));
  76. smartlist_split_string(sl, "abc", ":", 0, 0);
  77. test_eq(1, smartlist_len(sl));
  78. test_streq("abc", smartlist_get(sl, 0));
  79. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  80. test_eq(4, smartlist_len(sl));
  81. test_streq("a", smartlist_get(sl, 1));
  82. test_streq("bc", smartlist_get(sl, 2));
  83. test_streq("", smartlist_get(sl, 3));
  84. cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
  85. test_streq(cp_alloc, "abcabc");
  86. tor_free(cp_alloc);
  87. cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
  88. test_streq(cp_alloc, "abc!a!bc!");
  89. tor_free(cp_alloc);
  90. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  91. test_streq(cp_alloc, "abcXYaXYbcXY");
  92. tor_free(cp_alloc);
  93. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  94. test_streq(cp_alloc, "abcXYaXYbcXYXY");
  95. tor_free(cp_alloc);
  96. cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
  97. test_streq(cp_alloc, "abcabc");
  98. tor_free(cp_alloc);
  99. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  100. test_eq(8, smartlist_len(sl));
  101. test_streq("", smartlist_get(sl, 4));
  102. test_streq("def", smartlist_get(sl, 5));
  103. test_streq(" ", smartlist_get(sl, 6));
  104. test_streq("ghijk", smartlist_get(sl, 7));
  105. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  106. smartlist_clear(sl);
  107. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  108. test_eq(3, smartlist_len(sl));
  109. test_streq("a", smartlist_get(sl,0));
  110. test_streq("bbd", smartlist_get(sl,1));
  111. test_streq("cdef", smartlist_get(sl,2));
  112. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  113. SPLIT_SKIP_SPACE, 0);
  114. test_eq(8, smartlist_len(sl));
  115. test_streq("z", smartlist_get(sl,3));
  116. test_streq("zhasd", smartlist_get(sl,4));
  117. test_streq("", smartlist_get(sl,5));
  118. test_streq("bnud", smartlist_get(sl,6));
  119. test_streq("", smartlist_get(sl,7));
  120. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  121. smartlist_clear(sl);
  122. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  123. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  124. test_eq(4, smartlist_len(sl));
  125. test_streq("ab", smartlist_get(sl,0));
  126. test_streq("c", smartlist_get(sl,1));
  127. test_streq("d", smartlist_get(sl,2));
  128. test_streq("ef", smartlist_get(sl,3));
  129. smartlist_split_string(sl, "ghi\tj", NULL,
  130. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  131. test_eq(6, smartlist_len(sl));
  132. test_streq("ghi", smartlist_get(sl,4));
  133. test_streq("j", smartlist_get(sl,5));
  134. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  135. smartlist_clear(sl);
  136. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  137. test_streq(cp_alloc, "");
  138. tor_free(cp_alloc);
  139. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  140. test_streq(cp_alloc, "XY");
  141. tor_free(cp_alloc);
  142. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  143. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  144. test_eq(3, smartlist_len(sl));
  145. test_streq("z", smartlist_get(sl, 0));
  146. test_streq("zhasd", smartlist_get(sl, 1));
  147. test_streq("bnud", smartlist_get(sl, 2));
  148. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  149. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  150. test_eq(5, smartlist_len(sl));
  151. test_streq("z", smartlist_get(sl, 3));
  152. test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4));
  153. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  154. smartlist_clear(sl);
  155. smartlist_split_string(sl, "abcd\n", "\n",
  156. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  157. test_eq(1, smartlist_len(sl));
  158. test_streq("abcd", smartlist_get(sl, 0));
  159. smartlist_split_string(sl, "efgh", "\n",
  160. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  161. test_eq(2, smartlist_len(sl));
  162. test_streq("efgh", smartlist_get(sl, 1));
  163. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  164. smartlist_clear(sl);
  165. /* Test swapping, shuffling, and sorting. */
  166. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  167. test_eq(7, smartlist_len(sl));
  168. smartlist_sort(sl, compare_strs_);
  169. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  170. test_streq(cp_alloc,"and,arma,by,nickm,onion,router,the");
  171. tor_free(cp_alloc);
  172. smartlist_swap(sl, 1, 5);
  173. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  174. test_streq(cp_alloc,"and,router,by,nickm,onion,arma,the");
  175. tor_free(cp_alloc);
  176. smartlist_shuffle(sl);
  177. test_eq(7, smartlist_len(sl));
  178. test_assert(smartlist_contains_string(sl, "and"));
  179. test_assert(smartlist_contains_string(sl, "router"));
  180. test_assert(smartlist_contains_string(sl, "by"));
  181. test_assert(smartlist_contains_string(sl, "nickm"));
  182. test_assert(smartlist_contains_string(sl, "onion"));
  183. test_assert(smartlist_contains_string(sl, "arma"));
  184. test_assert(smartlist_contains_string(sl, "the"));
  185. /* Test bsearch. */
  186. smartlist_sort(sl, compare_strs_);
  187. test_streq("nickm", smartlist_bsearch(sl, "zNicKM",
  188. compare_without_first_ch_));
  189. test_streq("and", smartlist_bsearch(sl, " AND", compare_without_first_ch_));
  190. test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", compare_without_first_ch_));
  191. /* Test bsearch_idx */
  192. {
  193. int f;
  194. smartlist_t *tmp = NULL;
  195. test_eq(0, smartlist_bsearch_idx(sl," aaa",compare_without_first_ch_,&f));
  196. test_eq(f, 0);
  197. test_eq(0, smartlist_bsearch_idx(sl," and",compare_without_first_ch_,&f));
  198. test_eq(f, 1);
  199. test_eq(1, smartlist_bsearch_idx(sl," arm",compare_without_first_ch_,&f));
  200. test_eq(f, 0);
  201. test_eq(1, smartlist_bsearch_idx(sl," arma",compare_without_first_ch_,&f));
  202. test_eq(f, 1);
  203. test_eq(2, smartlist_bsearch_idx(sl," armb",compare_without_first_ch_,&f));
  204. test_eq(f, 0);
  205. test_eq(7, smartlist_bsearch_idx(sl," zzzz",compare_without_first_ch_,&f));
  206. test_eq(f, 0);
  207. /* Test trivial cases for list of length 0 or 1 */
  208. tmp = smartlist_new();
  209. test_eq(0, smartlist_bsearch_idx(tmp, "foo",
  210. compare_strs_for_bsearch_, &f));
  211. test_eq(f, 0);
  212. smartlist_insert(tmp, 0, (void *)("bar"));
  213. test_eq(1, smartlist_bsearch_idx(tmp, "foo",
  214. compare_strs_for_bsearch_, &f));
  215. test_eq(f, 0);
  216. test_eq(0, smartlist_bsearch_idx(tmp, "aaa",
  217. compare_strs_for_bsearch_, &f));
  218. test_eq(f, 0);
  219. test_eq(0, smartlist_bsearch_idx(tmp, "bar",
  220. compare_strs_for_bsearch_, &f));
  221. test_eq(f, 1);
  222. /* ... and one for length 2 */
  223. smartlist_insert(tmp, 1, (void *)("foo"));
  224. test_eq(1, smartlist_bsearch_idx(tmp, "foo",
  225. compare_strs_for_bsearch_, &f));
  226. test_eq(f, 1);
  227. test_eq(2, smartlist_bsearch_idx(tmp, "goo",
  228. compare_strs_for_bsearch_, &f));
  229. test_eq(f, 0);
  230. smartlist_free(tmp);
  231. }
  232. /* Test reverse() and pop_last() */
  233. smartlist_reverse(sl);
  234. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  235. test_streq(cp_alloc,"the,router,onion,nickm,by,arma,and");
  236. tor_free(cp_alloc);
  237. cp_alloc = smartlist_pop_last(sl);
  238. test_streq(cp_alloc, "and");
  239. tor_free(cp_alloc);
  240. test_eq(smartlist_len(sl), 6);
  241. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  242. smartlist_clear(sl);
  243. cp_alloc = smartlist_pop_last(sl);
  244. test_eq_ptr(cp_alloc, NULL);
  245. /* Test uniq() */
  246. smartlist_split_string(sl,
  247. "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
  248. ",", 0, 0);
  249. smartlist_sort(sl, compare_strs_);
  250. smartlist_uniq(sl, compare_strs_, tor_free_);
  251. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  252. test_streq(cp_alloc, "50,a,canal,man,noon,panama,plan,radar");
  253. tor_free(cp_alloc);
  254. /* Test contains_string, contains_string_case and contains_int_as_string */
  255. test_assert(smartlist_contains_string(sl, "noon"));
  256. test_assert(!smartlist_contains_string(sl, "noonoon"));
  257. test_assert(smartlist_contains_string_case(sl, "nOOn"));
  258. test_assert(!smartlist_contains_string_case(sl, "nooNooN"));
  259. test_assert(smartlist_contains_int_as_string(sl, 50));
  260. test_assert(!smartlist_contains_int_as_string(sl, 60));
  261. /* Test smartlist_choose */
  262. {
  263. int i;
  264. int allsame = 1;
  265. int allin = 1;
  266. void *first = smartlist_choose(sl);
  267. test_assert(smartlist_contains(sl, first));
  268. for (i = 0; i < 100; ++i) {
  269. void *second = smartlist_choose(sl);
  270. if (second != first)
  271. allsame = 0;
  272. if (!smartlist_contains(sl, second))
  273. allin = 0;
  274. }
  275. test_assert(!allsame);
  276. test_assert(allin);
  277. }
  278. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  279. smartlist_clear(sl);
  280. /* Test string_remove and remove and join_strings2 */
  281. smartlist_split_string(sl,
  282. "Some say the Earth will end in ice and some in fire",
  283. " ", 0, 0);
  284. cp = smartlist_get(sl, 4);
  285. test_streq(cp, "will");
  286. smartlist_add(sl, cp);
  287. smartlist_remove(sl, cp);
  288. tor_free(cp);
  289. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  290. test_streq(cp_alloc, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  291. tor_free(cp_alloc);
  292. smartlist_string_remove(sl, "in");
  293. cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
  294. test_streq(cp_alloc, "Some+say+the+Earth+fire+end+some+ice+and");
  295. test_eq((int)sz, 40);
  296. done:
  297. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  298. smartlist_free(sl);
  299. tor_free(cp_alloc);
  300. }
  301. /** Run unit tests for smartlist set manipulation functions. */
  302. static void
  303. test_container_smartlist_overlap(void)
  304. {
  305. smartlist_t *sl = smartlist_new();
  306. smartlist_t *ints = smartlist_new();
  307. smartlist_t *odds = smartlist_new();
  308. smartlist_t *evens = smartlist_new();
  309. smartlist_t *primes = smartlist_new();
  310. int i;
  311. for (i=1; i < 10; i += 2)
  312. smartlist_add(odds, (void*)(uintptr_t)i);
  313. for (i=0; i < 10; i += 2)
  314. smartlist_add(evens, (void*)(uintptr_t)i);
  315. /* add_all */
  316. smartlist_add_all(ints, odds);
  317. smartlist_add_all(ints, evens);
  318. test_eq(smartlist_len(ints), 10);
  319. smartlist_add(primes, (void*)2);
  320. smartlist_add(primes, (void*)3);
  321. smartlist_add(primes, (void*)5);
  322. smartlist_add(primes, (void*)7);
  323. /* overlap */
  324. test_assert(smartlist_overlap(ints, odds));
  325. test_assert(smartlist_overlap(odds, primes));
  326. test_assert(smartlist_overlap(evens, primes));
  327. test_assert(!smartlist_overlap(odds, evens));
  328. /* intersect */
  329. smartlist_add_all(sl, odds);
  330. smartlist_intersect(sl, primes);
  331. test_eq(smartlist_len(sl), 3);
  332. test_assert(smartlist_contains(sl, (void*)3));
  333. test_assert(smartlist_contains(sl, (void*)5));
  334. test_assert(smartlist_contains(sl, (void*)7));
  335. /* subtract */
  336. smartlist_add_all(sl, primes);
  337. smartlist_subtract(sl, odds);
  338. test_eq(smartlist_len(sl), 1);
  339. test_assert(smartlist_contains(sl, (void*)2));
  340. done:
  341. smartlist_free(odds);
  342. smartlist_free(evens);
  343. smartlist_free(ints);
  344. smartlist_free(primes);
  345. smartlist_free(sl);
  346. }
  347. /** Run unit tests for smartlist-of-digests functions. */
  348. static void
  349. test_container_smartlist_digests(void)
  350. {
  351. smartlist_t *sl = smartlist_new();
  352. /* contains_digest */
  353. smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN));
  354. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  355. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  356. test_eq(0, smartlist_contains_digest(NULL, "AAAAAAAAAAAAAAAAAAAA"));
  357. test_assert(smartlist_contains_digest(sl, "AAAAAAAAAAAAAAAAAAAA"));
  358. test_assert(smartlist_contains_digest(sl, "\00090AAB2AAAAaasdAAAAA"));
  359. test_eq(0, smartlist_contains_digest(sl, "\00090AAB2AAABaasdAAAAA"));
  360. /* sort digests */
  361. smartlist_sort_digests(sl);
  362. test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  363. test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  364. test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  365. test_eq(3, smartlist_len(sl));
  366. /* uniq_digests */
  367. smartlist_uniq_digests(sl);
  368. test_eq(2, smartlist_len(sl));
  369. test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  370. test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  371. done:
  372. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  373. smartlist_free(sl);
  374. }
  375. /** Run unit tests for concatenate-a-smartlist-of-strings functions. */
  376. static void
  377. test_container_smartlist_join(void)
  378. {
  379. smartlist_t *sl = smartlist_new();
  380. smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
  381. *sl4 = smartlist_new();
  382. char *joined=NULL;
  383. /* unique, sorted. */
  384. smartlist_split_string(sl,
  385. "Abashments Ambush Anchorman Bacon Banks Borscht "
  386. "Bunks Inhumane Insurance Knish Know Manners "
  387. "Maraschinos Stamina Sunbonnets Unicorns Wombats",
  388. " ", 0, 0);
  389. /* non-unique, sorted. */
  390. smartlist_split_string(sl2,
  391. "Ambush Anchorman Anchorman Anemias Anemias Bacon "
  392. "Crossbowmen Inhumane Insurance Knish Know Manners "
  393. "Manners Maraschinos Wombats Wombats Work",
  394. " ", 0, 0);
  395. SMARTLIST_FOREACH_JOIN(sl, char *, cp1,
  396. sl2, char *, cp2,
  397. strcmp(cp1,cp2),
  398. smartlist_add(sl3, cp2)) {
  399. test_streq(cp1, cp2);
  400. smartlist_add(sl4, cp1);
  401. } SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
  402. SMARTLIST_FOREACH(sl3, const char *, cp,
  403. test_assert(smartlist_contains(sl2, cp) &&
  404. !smartlist_contains_string(sl, cp)));
  405. SMARTLIST_FOREACH(sl4, const char *, cp,
  406. test_assert(smartlist_contains(sl, cp) &&
  407. smartlist_contains_string(sl2, cp)));
  408. joined = smartlist_join_strings(sl3, ",", 0, NULL);
  409. test_streq(joined, "Anemias,Anemias,Crossbowmen,Work");
  410. tor_free(joined);
  411. joined = smartlist_join_strings(sl4, ",", 0, NULL);
  412. test_streq(joined, "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
  413. "Knish,Know,Manners,Manners,Maraschinos,Wombats,Wombats");
  414. tor_free(joined);
  415. done:
  416. smartlist_free(sl4);
  417. smartlist_free(sl3);
  418. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  419. smartlist_free(sl2);
  420. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  421. smartlist_free(sl);
  422. tor_free(joined);
  423. }
  424. static void
  425. test_container_smartlist_ints_eq(void *arg)
  426. {
  427. smartlist_t *sl1 = NULL, *sl2 = NULL;
  428. int x;
  429. (void)arg;
  430. tt_assert(smartlist_ints_eq(NULL, NULL));
  431. sl1 = smartlist_new();
  432. tt_assert(!smartlist_ints_eq(sl1, NULL));
  433. tt_assert(!smartlist_ints_eq(NULL, sl1));
  434. sl2 = smartlist_new();
  435. tt_assert(smartlist_ints_eq(sl1, sl2));
  436. x = 5;
  437. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  438. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  439. x = 90;
  440. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  441. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  442. tt_assert(smartlist_ints_eq(sl1, sl2));
  443. x = -50;
  444. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  445. tt_assert(! smartlist_ints_eq(sl1, sl2));
  446. tt_assert(! smartlist_ints_eq(sl2, sl1));
  447. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  448. tt_assert(smartlist_ints_eq(sl1, sl2));
  449. *(int*)smartlist_get(sl1, 1) = 101010;
  450. tt_assert(! smartlist_ints_eq(sl2, sl1));
  451. *(int*)smartlist_get(sl2, 1) = 101010;
  452. tt_assert(smartlist_ints_eq(sl1, sl2));
  453. done:
  454. if (sl1)
  455. SMARTLIST_FOREACH(sl1, int *, ip, tor_free(ip));
  456. if (sl2)
  457. SMARTLIST_FOREACH(sl2, int *, ip, tor_free(ip));
  458. smartlist_free(sl1);
  459. smartlist_free(sl2);
  460. }
  461. /** Run unit tests for bitarray code */
  462. static void
  463. test_container_bitarray(void)
  464. {
  465. bitarray_t *ba = NULL;
  466. int i, j, ok=1;
  467. ba = bitarray_init_zero(1);
  468. test_assert(ba);
  469. test_assert(! bitarray_is_set(ba, 0));
  470. bitarray_set(ba, 0);
  471. test_assert(bitarray_is_set(ba, 0));
  472. bitarray_clear(ba, 0);
  473. test_assert(! bitarray_is_set(ba, 0));
  474. bitarray_free(ba);
  475. ba = bitarray_init_zero(1023);
  476. for (i = 1; i < 64; ) {
  477. for (j = 0; j < 1023; ++j) {
  478. if (j % i)
  479. bitarray_set(ba, j);
  480. else
  481. bitarray_clear(ba, j);
  482. }
  483. for (j = 0; j < 1023; ++j) {
  484. if (!bool_eq(bitarray_is_set(ba, j), j%i))
  485. ok = 0;
  486. }
  487. test_assert(ok);
  488. if (i < 7)
  489. ++i;
  490. else if (i == 28)
  491. i = 32;
  492. else
  493. i += 7;
  494. }
  495. done:
  496. if (ba)
  497. bitarray_free(ba);
  498. }
  499. /** Run unit tests for digest set code (implemented as a hashtable or as a
  500. * bloom filter) */
  501. static void
  502. test_container_digestset(void)
  503. {
  504. smartlist_t *included = smartlist_new();
  505. char d[DIGEST_LEN];
  506. int i;
  507. int ok = 1;
  508. int false_positives = 0;
  509. digestset_t *set = NULL;
  510. for (i = 0; i < 1000; ++i) {
  511. crypto_rand(d, DIGEST_LEN);
  512. smartlist_add(included, tor_memdup(d, DIGEST_LEN));
  513. }
  514. set = digestset_new(1000);
  515. SMARTLIST_FOREACH(included, const char *, cp,
  516. if (digestset_contains(set, cp))
  517. ok = 0);
  518. test_assert(ok);
  519. SMARTLIST_FOREACH(included, const char *, cp,
  520. digestset_add(set, cp));
  521. SMARTLIST_FOREACH(included, const char *, cp,
  522. if (!digestset_contains(set, cp))
  523. ok = 0);
  524. test_assert(ok);
  525. for (i = 0; i < 1000; ++i) {
  526. crypto_rand(d, DIGEST_LEN);
  527. if (digestset_contains(set, d))
  528. ++false_positives;
  529. }
  530. test_assert(false_positives < 50); /* Should be far lower. */
  531. done:
  532. if (set)
  533. digestset_free(set);
  534. SMARTLIST_FOREACH(included, char *, cp, tor_free(cp));
  535. smartlist_free(included);
  536. }
  537. typedef struct pq_entry_t {
  538. const char *val;
  539. int idx;
  540. } pq_entry_t;
  541. /** Helper: return a tristate based on comparing two pq_entry_t values. */
  542. static int
  543. compare_strings_for_pqueue_(const void *p1, const void *p2)
  544. {
  545. const pq_entry_t *e1=p1, *e2=p2;
  546. return strcmp(e1->val, e2->val);
  547. }
  548. /** Run unit tests for heap-based priority queue functions. */
  549. static void
  550. test_container_pqueue(void)
  551. {
  552. smartlist_t *sl = smartlist_new();
  553. int (*cmp)(const void *, const void*);
  554. const int offset = STRUCT_OFFSET(pq_entry_t, idx);
  555. #define ENTRY(s) pq_entry_t s = { #s, -1 }
  556. ENTRY(cows);
  557. ENTRY(zebras);
  558. ENTRY(fish);
  559. ENTRY(frogs);
  560. ENTRY(apples);
  561. ENTRY(squid);
  562. ENTRY(daschunds);
  563. ENTRY(eggplants);
  564. ENTRY(weissbier);
  565. ENTRY(lobsters);
  566. ENTRY(roquefort);
  567. ENTRY(chinchillas);
  568. ENTRY(fireflies);
  569. #define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
  570. cmp = compare_strings_for_pqueue_;
  571. smartlist_pqueue_add(sl, cmp, offset, &cows);
  572. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  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, &daschunds);
  578. smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  579. smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  580. smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  581. smartlist_pqueue_add(sl, cmp, offset, &roquefort);
  582. OK();
  583. test_eq(smartlist_len(sl), 11);
  584. test_eq_ptr(smartlist_get(sl, 0), &apples);
  585. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
  586. test_eq(smartlist_len(sl), 10);
  587. OK();
  588. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
  589. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
  590. smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  591. OK();
  592. smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  593. OK();
  594. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
  595. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
  596. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
  597. OK();
  598. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  599. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  600. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
  601. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
  602. OK();
  603. test_eq(smartlist_len(sl), 3);
  604. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  605. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
  606. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
  607. test_eq(smartlist_len(sl), 0);
  608. OK();
  609. /* Now test remove. */
  610. smartlist_pqueue_add(sl, cmp, offset, &cows);
  611. smartlist_pqueue_add(sl, cmp, offset, &fish);
  612. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  613. smartlist_pqueue_add(sl, cmp, offset, &apples);
  614. smartlist_pqueue_add(sl, cmp, offset, &squid);
  615. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  616. test_eq(smartlist_len(sl), 6);
  617. OK();
  618. smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  619. test_eq(smartlist_len(sl), 5);
  620. OK();
  621. smartlist_pqueue_remove(sl, cmp, offset, &cows);
  622. test_eq(smartlist_len(sl), 4);
  623. OK();
  624. smartlist_pqueue_remove(sl, cmp, offset, &apples);
  625. test_eq(smartlist_len(sl), 3);
  626. OK();
  627. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  628. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  629. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  630. test_eq(smartlist_len(sl), 0);
  631. OK();
  632. #undef OK
  633. done:
  634. smartlist_free(sl);
  635. }
  636. /** Run unit tests for string-to-void* map functions */
  637. static void
  638. test_container_strmap(void)
  639. {
  640. strmap_t *map;
  641. strmap_iter_t *iter;
  642. const char *k;
  643. void *v;
  644. char *visited = NULL;
  645. smartlist_t *found_keys = NULL;
  646. map = strmap_new();
  647. test_assert(map);
  648. test_eq(strmap_size(map), 0);
  649. test_assert(strmap_isempty(map));
  650. v = strmap_set(map, "K1", (void*)99);
  651. test_eq_ptr(v, NULL);
  652. test_assert(!strmap_isempty(map));
  653. v = strmap_set(map, "K2", (void*)101);
  654. test_eq_ptr(v, NULL);
  655. v = strmap_set(map, "K1", (void*)100);
  656. test_eq_ptr(v, (void*)99);
  657. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  658. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  659. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  660. strmap_assert_ok(map);
  661. v = strmap_remove(map,"K2");
  662. strmap_assert_ok(map);
  663. test_eq_ptr(v, (void*)101);
  664. test_eq_ptr(strmap_get(map,"K2"), NULL);
  665. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  666. strmap_set(map, "K2", (void*)101);
  667. strmap_set(map, "K3", (void*)102);
  668. strmap_set(map, "K4", (void*)103);
  669. test_eq(strmap_size(map), 4);
  670. strmap_assert_ok(map);
  671. strmap_set(map, "K5", (void*)104);
  672. strmap_set(map, "K6", (void*)105);
  673. strmap_assert_ok(map);
  674. /* Test iterator. */
  675. iter = strmap_iter_init(map);
  676. found_keys = smartlist_new();
  677. while (!strmap_iter_done(iter)) {
  678. strmap_iter_get(iter,&k,&v);
  679. smartlist_add(found_keys, tor_strdup(k));
  680. test_eq_ptr(v, strmap_get(map, k));
  681. if (!strcmp(k, "K2")) {
  682. iter = strmap_iter_next_rmv(map,iter);
  683. } else {
  684. iter = strmap_iter_next(map,iter);
  685. }
  686. }
  687. /* Make sure we removed K2, but not the others. */
  688. test_eq_ptr(strmap_get(map, "K2"), NULL);
  689. test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  690. /* Make sure we visited everyone once */
  691. smartlist_sort_strings(found_keys);
  692. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  693. test_streq(visited, "K1:K2:K3:K4:K5:K6");
  694. strmap_assert_ok(map);
  695. /* Clean up after ourselves. */
  696. strmap_free(map, NULL);
  697. map = NULL;
  698. /* Now try some lc functions. */
  699. map = strmap_new();
  700. strmap_set_lc(map,"Ab.C", (void*)1);
  701. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  702. strmap_assert_ok(map);
  703. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  704. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  705. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  706. strmap_assert_ok(map);
  707. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  708. done:
  709. if (map)
  710. strmap_free(map,NULL);
  711. if (found_keys) {
  712. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  713. smartlist_free(found_keys);
  714. }
  715. tor_free(visited);
  716. }
  717. /** Run unit tests for getting the median of a list. */
  718. static void
  719. test_container_order_functions(void)
  720. {
  721. int lst[25], n = 0;
  722. // int a=12,b=24,c=25,d=60,e=77;
  723. #define median() median_int(lst, n)
  724. lst[n++] = 12;
  725. test_eq(12, median()); /* 12 */
  726. lst[n++] = 77;
  727. //smartlist_shuffle(sl);
  728. test_eq(12, median()); /* 12, 77 */
  729. lst[n++] = 77;
  730. //smartlist_shuffle(sl);
  731. test_eq(77, median()); /* 12, 77, 77 */
  732. lst[n++] = 24;
  733. test_eq(24, median()); /* 12,24,77,77 */
  734. lst[n++] = 60;
  735. lst[n++] = 12;
  736. lst[n++] = 25;
  737. //smartlist_shuffle(sl);
  738. test_eq(25, median()); /* 12,12,24,25,60,77,77 */
  739. #undef median
  740. done:
  741. ;
  742. }
  743. static void
  744. test_container_di_map(void *arg)
  745. {
  746. di_digest256_map_t *map = NULL;
  747. const uint8_t key1[] = "In view of the fact that it was ";
  748. const uint8_t key2[] = "superficially convincing, being ";
  749. const uint8_t key3[] = "properly enciphered in a one-tim";
  750. const uint8_t key4[] = "e cipher scheduled for use today";
  751. char *v1 = tor_strdup(", it came close to causing a disaster...");
  752. char *v2 = tor_strdup("I regret to have to advise you that the mission");
  753. char *v3 = tor_strdup("was actually initiated...");
  754. /* -- John Brunner, _The Shockwave Rider_ */
  755. (void)arg;
  756. /* Try searching on an empty map. */
  757. tt_ptr_op(NULL, ==, dimap_search(map, key1, NULL));
  758. tt_ptr_op(NULL, ==, dimap_search(map, key2, NULL));
  759. tt_ptr_op(v3, ==, dimap_search(map, key2, v3));
  760. dimap_free(map, NULL);
  761. map = NULL;
  762. /* Add a single entry. */
  763. dimap_add_entry(&map, key1, v1);
  764. tt_ptr_op(NULL, ==, dimap_search(map, key2, NULL));
  765. tt_ptr_op(v3, ==, dimap_search(map, key2, v3));
  766. tt_ptr_op(v1, ==, dimap_search(map, key1, NULL));
  767. /* Now try it with three entries in the map. */
  768. dimap_add_entry(&map, key2, v2);
  769. dimap_add_entry(&map, key3, v3);
  770. tt_ptr_op(v1, ==, dimap_search(map, key1, NULL));
  771. tt_ptr_op(v2, ==, dimap_search(map, key2, NULL));
  772. tt_ptr_op(v3, ==, dimap_search(map, key3, NULL));
  773. tt_ptr_op(NULL, ==, dimap_search(map, key4, NULL));
  774. tt_ptr_op(v1, ==, dimap_search(map, key4, v1));
  775. done:
  776. tor_free(v1);
  777. tor_free(v2);
  778. tor_free(v3);
  779. dimap_free(map, NULL);
  780. }
  781. /** Run unit tests for fp_pair-to-void* map functions */
  782. static void
  783. test_container_fp_pair_map(void)
  784. {
  785. fp_pair_map_t *map;
  786. fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  787. void *v;
  788. fp_pair_map_iter_t *iter;
  789. fp_pair_t k;
  790. map = fp_pair_map_new();
  791. test_assert(map);
  792. test_eq(fp_pair_map_size(map), 0);
  793. test_assert(fp_pair_map_isempty(map));
  794. memset(fp1.first, 0x11, DIGEST_LEN);
  795. memset(fp1.second, 0x12, DIGEST_LEN);
  796. memset(fp2.first, 0x21, DIGEST_LEN);
  797. memset(fp2.second, 0x22, DIGEST_LEN);
  798. memset(fp3.first, 0x31, DIGEST_LEN);
  799. memset(fp3.second, 0x32, DIGEST_LEN);
  800. memset(fp4.first, 0x41, DIGEST_LEN);
  801. memset(fp4.second, 0x42, DIGEST_LEN);
  802. memset(fp5.first, 0x51, DIGEST_LEN);
  803. memset(fp5.second, 0x52, DIGEST_LEN);
  804. memset(fp6.first, 0x61, DIGEST_LEN);
  805. memset(fp6.second, 0x62, DIGEST_LEN);
  806. v = fp_pair_map_set(map, &fp1, (void*)99);
  807. tt_ptr_op(v, ==, NULL);
  808. test_assert(!fp_pair_map_isempty(map));
  809. v = fp_pair_map_set(map, &fp2, (void*)101);
  810. tt_ptr_op(v, ==, NULL);
  811. v = fp_pair_map_set(map, &fp1, (void*)100);
  812. tt_ptr_op(v, ==, (void*)99);
  813. test_eq_ptr(fp_pair_map_get(map, &fp1), (void*)100);
  814. test_eq_ptr(fp_pair_map_get(map, &fp2), (void*)101);
  815. test_eq_ptr(fp_pair_map_get(map, &fp3), NULL);
  816. fp_pair_map_assert_ok(map);
  817. v = fp_pair_map_remove(map, &fp2);
  818. fp_pair_map_assert_ok(map);
  819. test_eq_ptr(v, (void*)101);
  820. test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  821. test_eq_ptr(fp_pair_map_remove(map, &fp2), NULL);
  822. fp_pair_map_set(map, &fp2, (void*)101);
  823. fp_pair_map_set(map, &fp3, (void*)102);
  824. fp_pair_map_set(map, &fp4, (void*)103);
  825. test_eq(fp_pair_map_size(map), 4);
  826. fp_pair_map_assert_ok(map);
  827. fp_pair_map_set(map, &fp5, (void*)104);
  828. fp_pair_map_set(map, &fp6, (void*)105);
  829. fp_pair_map_assert_ok(map);
  830. /* Test iterator. */
  831. iter = fp_pair_map_iter_init(map);
  832. while (!fp_pair_map_iter_done(iter)) {
  833. fp_pair_map_iter_get(iter, &k, &v);
  834. test_eq_ptr(v, fp_pair_map_get(map, &k));
  835. if (tor_memeq(&fp2, &k, sizeof(fp2))) {
  836. iter = fp_pair_map_iter_next_rmv(map, iter);
  837. } else {
  838. iter = fp_pair_map_iter_next(map, iter);
  839. }
  840. }
  841. /* Make sure we removed fp2, but not the others. */
  842. test_eq_ptr(fp_pair_map_get(map, &fp2), NULL);
  843. test_eq_ptr(fp_pair_map_get(map, &fp5), (void*)104);
  844. fp_pair_map_assert_ok(map);
  845. /* Clean up after ourselves. */
  846. fp_pair_map_free(map, NULL);
  847. map = NULL;
  848. done:
  849. if (map)
  850. fp_pair_map_free(map, NULL);
  851. }
  852. #define CONTAINER_LEGACY(name) \
  853. { #name, legacy_test_helper, 0, &legacy_setup, test_container_ ## name }
  854. #define CONTAINER(name, flags) \
  855. { #name, test_container_ ## name, (flags), NULL, NULL }
  856. struct testcase_t container_tests[] = {
  857. CONTAINER_LEGACY(smartlist_basic),
  858. CONTAINER_LEGACY(smartlist_strings),
  859. CONTAINER_LEGACY(smartlist_overlap),
  860. CONTAINER_LEGACY(smartlist_digests),
  861. CONTAINER_LEGACY(smartlist_join),
  862. CONTAINER(smartlist_ints_eq, 0),
  863. CONTAINER_LEGACY(bitarray),
  864. CONTAINER_LEGACY(digestset),
  865. CONTAINER_LEGACY(strmap),
  866. CONTAINER_LEGACY(pqueue),
  867. CONTAINER_LEGACY(order_functions),
  868. CONTAINER(di_map, 0),
  869. CONTAINER_LEGACY(fp_pair_map),
  870. END_OF_TESTCASES
  871. };