test_containers.c 33 KB

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