test_containers.c 33 KB

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