test_containers.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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>, 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_new();
  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_new();
  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_new();
  272. smartlist_t *ints = smartlist_new();
  273. smartlist_t *odds = smartlist_new();
  274. smartlist_t *evens = smartlist_new();
  275. smartlist_t *primes = smartlist_new();
  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_new();
  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_new();
  346. smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
  347. *sl4 = smartlist_new();
  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_new();
  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. typedef struct pq_entry_t {
  467. const char *val;
  468. int idx;
  469. } pq_entry_t;
  470. /** Helper: return a tristate based on comparing two pq_entry_t values. */
  471. static int
  472. compare_strings_for_pqueue_(const void *p1, const void *p2)
  473. {
  474. const pq_entry_t *e1=p1, *e2=p2;
  475. return strcmp(e1->val, e2->val);
  476. }
  477. /** Run unit tests for heap-based priority queue functions. */
  478. static void
  479. test_container_pqueue(void)
  480. {
  481. smartlist_t *sl = smartlist_new();
  482. int (*cmp)(const void *, const void*);
  483. const int offset = STRUCT_OFFSET(pq_entry_t, idx);
  484. #define ENTRY(s) pq_entry_t s = { #s, -1 }
  485. ENTRY(cows);
  486. ENTRY(zebras);
  487. ENTRY(fish);
  488. ENTRY(frogs);
  489. ENTRY(apples);
  490. ENTRY(squid);
  491. ENTRY(daschunds);
  492. ENTRY(eggplants);
  493. ENTRY(weissbier);
  494. ENTRY(lobsters);
  495. ENTRY(roquefort);
  496. ENTRY(chinchillas);
  497. ENTRY(fireflies);
  498. #define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
  499. cmp = compare_strings_for_pqueue_;
  500. smartlist_pqueue_add(sl, cmp, offset, &cows);
  501. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  502. smartlist_pqueue_add(sl, cmp, offset, &fish);
  503. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  504. smartlist_pqueue_add(sl, cmp, offset, &apples);
  505. smartlist_pqueue_add(sl, cmp, offset, &squid);
  506. smartlist_pqueue_add(sl, cmp, offset, &daschunds);
  507. smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  508. smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  509. smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  510. smartlist_pqueue_add(sl, cmp, offset, &roquefort);
  511. OK();
  512. test_eq(smartlist_len(sl), 11);
  513. test_eq_ptr(smartlist_get(sl, 0), &apples);
  514. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &apples);
  515. test_eq(smartlist_len(sl), 10);
  516. OK();
  517. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &cows);
  518. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &daschunds);
  519. smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  520. OK();
  521. smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  522. OK();
  523. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &chinchillas);
  524. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &eggplants);
  525. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fireflies);
  526. OK();
  527. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  528. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  529. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &lobsters);
  530. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &roquefort);
  531. OK();
  532. test_eq(smartlist_len(sl), 3);
  533. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  534. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &weissbier);
  535. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &zebras);
  536. test_eq(smartlist_len(sl), 0);
  537. OK();
  538. /* Now test remove. */
  539. smartlist_pqueue_add(sl, cmp, offset, &cows);
  540. smartlist_pqueue_add(sl, cmp, offset, &fish);
  541. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  542. smartlist_pqueue_add(sl, cmp, offset, &apples);
  543. smartlist_pqueue_add(sl, cmp, offset, &squid);
  544. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  545. test_eq(smartlist_len(sl), 6);
  546. OK();
  547. smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  548. test_eq(smartlist_len(sl), 5);
  549. OK();
  550. smartlist_pqueue_remove(sl, cmp, offset, &cows);
  551. test_eq(smartlist_len(sl), 4);
  552. OK();
  553. smartlist_pqueue_remove(sl, cmp, offset, &apples);
  554. test_eq(smartlist_len(sl), 3);
  555. OK();
  556. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &fish);
  557. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &frogs);
  558. test_eq_ptr(smartlist_pqueue_pop(sl, cmp, offset), &squid);
  559. test_eq(smartlist_len(sl), 0);
  560. OK();
  561. #undef OK
  562. done:
  563. smartlist_free(sl);
  564. }
  565. /** Run unit tests for string-to-void* map functions */
  566. static void
  567. test_container_strmap(void)
  568. {
  569. strmap_t *map;
  570. strmap_iter_t *iter;
  571. const char *k;
  572. void *v;
  573. char *visited = NULL;
  574. smartlist_t *found_keys = NULL;
  575. map = strmap_new();
  576. test_assert(map);
  577. test_eq(strmap_size(map), 0);
  578. test_assert(strmap_isempty(map));
  579. v = strmap_set(map, "K1", (void*)99);
  580. test_eq(v, NULL);
  581. test_assert(!strmap_isempty(map));
  582. v = strmap_set(map, "K2", (void*)101);
  583. test_eq(v, NULL);
  584. v = strmap_set(map, "K1", (void*)100);
  585. test_eq(v, (void*)99);
  586. test_eq_ptr(strmap_get(map,"K1"), (void*)100);
  587. test_eq_ptr(strmap_get(map,"K2"), (void*)101);
  588. test_eq_ptr(strmap_get(map,"K-not-there"), NULL);
  589. strmap_assert_ok(map);
  590. v = strmap_remove(map,"K2");
  591. strmap_assert_ok(map);
  592. test_eq_ptr(v, (void*)101);
  593. test_eq_ptr(strmap_get(map,"K2"), NULL);
  594. test_eq_ptr(strmap_remove(map,"K2"), NULL);
  595. strmap_set(map, "K2", (void*)101);
  596. strmap_set(map, "K3", (void*)102);
  597. strmap_set(map, "K4", (void*)103);
  598. test_eq(strmap_size(map), 4);
  599. strmap_assert_ok(map);
  600. strmap_set(map, "K5", (void*)104);
  601. strmap_set(map, "K6", (void*)105);
  602. strmap_assert_ok(map);
  603. /* Test iterator. */
  604. iter = strmap_iter_init(map);
  605. found_keys = smartlist_new();
  606. while (!strmap_iter_done(iter)) {
  607. strmap_iter_get(iter,&k,&v);
  608. smartlist_add(found_keys, tor_strdup(k));
  609. test_eq_ptr(v, strmap_get(map, k));
  610. if (!strcmp(k, "K2")) {
  611. iter = strmap_iter_next_rmv(map,iter);
  612. } else {
  613. iter = strmap_iter_next(map,iter);
  614. }
  615. }
  616. /* Make sure we removed K2, but not the others. */
  617. test_eq_ptr(strmap_get(map, "K2"), NULL);
  618. test_eq_ptr(strmap_get(map, "K5"), (void*)104);
  619. /* Make sure we visited everyone once */
  620. smartlist_sort_strings(found_keys);
  621. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  622. test_streq(visited, "K1:K2:K3:K4:K5:K6");
  623. strmap_assert_ok(map);
  624. /* Clean up after ourselves. */
  625. strmap_free(map, NULL);
  626. map = NULL;
  627. /* Now try some lc functions. */
  628. map = strmap_new();
  629. strmap_set_lc(map,"Ab.C", (void*)1);
  630. test_eq_ptr(strmap_get(map,"ab.c"), (void*)1);
  631. strmap_assert_ok(map);
  632. test_eq_ptr(strmap_get_lc(map,"AB.C"), (void*)1);
  633. test_eq_ptr(strmap_get(map,"AB.C"), NULL);
  634. test_eq_ptr(strmap_remove_lc(map,"aB.C"), (void*)1);
  635. strmap_assert_ok(map);
  636. test_eq_ptr(strmap_get_lc(map,"AB.C"), NULL);
  637. done:
  638. if (map)
  639. strmap_free(map,NULL);
  640. if (found_keys) {
  641. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  642. smartlist_free(found_keys);
  643. }
  644. tor_free(visited);
  645. }
  646. /** Run unit tests for getting the median of a list. */
  647. static void
  648. test_container_order_functions(void)
  649. {
  650. int lst[25], n = 0;
  651. // int a=12,b=24,c=25,d=60,e=77;
  652. #define median() median_int(lst, n)
  653. lst[n++] = 12;
  654. test_eq(12, median()); /* 12 */
  655. lst[n++] = 77;
  656. //smartlist_shuffle(sl);
  657. test_eq(12, median()); /* 12, 77 */
  658. lst[n++] = 77;
  659. //smartlist_shuffle(sl);
  660. test_eq(77, median()); /* 12, 77, 77 */
  661. lst[n++] = 24;
  662. test_eq(24, median()); /* 12,24,77,77 */
  663. lst[n++] = 60;
  664. lst[n++] = 12;
  665. lst[n++] = 25;
  666. //smartlist_shuffle(sl);
  667. test_eq(25, median()); /* 12,12,24,25,60,77,77 */
  668. #undef median
  669. done:
  670. ;
  671. }
  672. #define CONTAINER_LEGACY(name) \
  673. { #name, legacy_test_helper, 0, &legacy_setup, test_container_ ## name }
  674. struct testcase_t container_tests[] = {
  675. CONTAINER_LEGACY(smartlist_basic),
  676. CONTAINER_LEGACY(smartlist_strings),
  677. CONTAINER_LEGACY(smartlist_overlap),
  678. CONTAINER_LEGACY(smartlist_digests),
  679. CONTAINER_LEGACY(smartlist_join),
  680. CONTAINER_LEGACY(bitarray),
  681. CONTAINER_LEGACY(digestset),
  682. CONTAINER_LEGACY(strmap),
  683. CONTAINER_LEGACY(pqueue),
  684. CONTAINER_LEGACY(order_functions),
  685. END_OF_TESTCASES
  686. };