test_containers.c 23 KB

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