test_containers.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "core/or/or.h"
  7. #include "lib/crypt_ops/crypto_rand.h"
  8. #include "feature/dircommon/fp_pair.h"
  9. #include "test/test.h"
  10. #include "lib/container/bitarray.h"
  11. #include "lib/container/order.h"
  12. #include "lib/crypt_ops/digestset.h"
  13. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  14. * *<b>b</b>. */
  15. static int
  16. compare_strs_(const void **a, const void **b)
  17. {
  18. const char *s1 = *a, *s2 = *b;
  19. return strcmp(s1, s2);
  20. }
  21. /** Helper: return a tristate based on comparing the strings in <b>a</b> and
  22. * *<b>b</b>. */
  23. static int
  24. compare_strs_for_bsearch_(const void *a, const void **b)
  25. {
  26. const char *s1 = a, *s2 = *b;
  27. return strcmp(s1, s2);
  28. }
  29. /** Helper: return a tristate based on comparing the strings in *<b>a</b> and
  30. * *<b>b</b>, excluding a's first character, and ignoring case. */
  31. static int
  32. cmp_without_first_(const void *a, const void **b)
  33. {
  34. const char *s1 = a, *s2 = *b;
  35. return strcasecmp(s1+1, s2);
  36. }
  37. /** Run unit tests for basic dynamic-sized array functionality. */
  38. static void
  39. test_container_smartlist_basic(void *arg)
  40. {
  41. smartlist_t *sl;
  42. char *v0 = tor_strdup("v0");
  43. char *v1 = tor_strdup("v1");
  44. char *v2 = tor_strdup("v2");
  45. char *v3 = tor_strdup("v3");
  46. char *v4 = tor_strdup("v4");
  47. char *v22 = tor_strdup("v22");
  48. char *v99 = tor_strdup("v99");
  49. char *v555 = tor_strdup("v555");
  50. /* XXXX test sort_digests, uniq_strings, uniq_digests */
  51. /* Test smartlist add, del_keeporder, insert, get. */
  52. (void)arg;
  53. sl = smartlist_new();
  54. smartlist_add(sl, v1);
  55. smartlist_add(sl, v2);
  56. smartlist_add(sl, v3);
  57. smartlist_add(sl, v4);
  58. smartlist_del_keeporder(sl, 1);
  59. smartlist_insert(sl, 1, v22);
  60. smartlist_insert(sl, 0, v0);
  61. smartlist_insert(sl, 5, v555);
  62. tt_ptr_op(v0,OP_EQ, smartlist_get(sl,0));
  63. tt_ptr_op(v1,OP_EQ, smartlist_get(sl,1));
  64. tt_ptr_op(v22,OP_EQ, smartlist_get(sl,2));
  65. tt_ptr_op(v3,OP_EQ, smartlist_get(sl,3));
  66. tt_ptr_op(v4,OP_EQ, smartlist_get(sl,4));
  67. tt_ptr_op(v555,OP_EQ, smartlist_get(sl,5));
  68. /* Try deleting in the middle. */
  69. smartlist_del(sl, 1);
  70. tt_ptr_op(v555,OP_EQ, smartlist_get(sl, 1));
  71. /* Try deleting at the end. */
  72. smartlist_del(sl, 4);
  73. tt_int_op(4,OP_EQ, smartlist_len(sl));
  74. /* test isin. */
  75. tt_assert(smartlist_contains(sl, v3));
  76. tt_assert(!smartlist_contains(sl, v99));
  77. done:
  78. smartlist_free(sl);
  79. tor_free(v0);
  80. tor_free(v1);
  81. tor_free(v2);
  82. tor_free(v3);
  83. tor_free(v4);
  84. tor_free(v22);
  85. tor_free(v99);
  86. tor_free(v555);
  87. }
  88. /** Test SMARTLIST_FOREACH_REVERSE_BEGIN loop macro */
  89. static void
  90. test_container_smartlist_foreach_reverse(void *arg)
  91. {
  92. smartlist_t *sl = smartlist_new();
  93. int i;
  94. (void) arg;
  95. /* Add integers to smartlist in increasing order */
  96. for (i=0;i<100;i++) {
  97. smartlist_add(sl, (void*)(uintptr_t)i);
  98. }
  99. /* Pop them out in reverse and test their value */
  100. SMARTLIST_FOREACH_REVERSE_BEGIN(sl, void*, k) {
  101. i--;
  102. tt_ptr_op(k, OP_EQ, (void*)(uintptr_t)i);
  103. } SMARTLIST_FOREACH_END(k);
  104. done:
  105. smartlist_free(sl);
  106. }
  107. /** Run unit tests for smartlist-of-strings functionality. */
  108. static void
  109. test_container_smartlist_strings(void *arg)
  110. {
  111. smartlist_t *sl = smartlist_new();
  112. char *cp=NULL, *cp_alloc=NULL;
  113. size_t sz;
  114. /* Test split and join */
  115. (void)arg;
  116. tt_int_op(0,OP_EQ, smartlist_len(sl));
  117. smartlist_split_string(sl, "abc", ":", 0, 0);
  118. tt_int_op(1,OP_EQ, smartlist_len(sl));
  119. tt_str_op("abc",OP_EQ, smartlist_get(sl, 0));
  120. smartlist_split_string(sl, "a::bc::", "::", 0, 0);
  121. tt_int_op(4,OP_EQ, smartlist_len(sl));
  122. tt_str_op("a",OP_EQ, smartlist_get(sl, 1));
  123. tt_str_op("bc",OP_EQ, smartlist_get(sl, 2));
  124. tt_str_op("",OP_EQ, smartlist_get(sl, 3));
  125. cp_alloc = smartlist_join_strings(sl, "", 0, NULL);
  126. tt_str_op(cp_alloc,OP_EQ, "abcabc");
  127. tor_free(cp_alloc);
  128. cp_alloc = smartlist_join_strings(sl, "!", 0, NULL);
  129. tt_str_op(cp_alloc,OP_EQ, "abc!a!bc!");
  130. tor_free(cp_alloc);
  131. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  132. tt_str_op(cp_alloc,OP_EQ, "abcXYaXYbcXY");
  133. tor_free(cp_alloc);
  134. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  135. tt_str_op(cp_alloc,OP_EQ, "abcXYaXYbcXYXY");
  136. tor_free(cp_alloc);
  137. cp_alloc = smartlist_join_strings(sl, "", 1, NULL);
  138. tt_str_op(cp_alloc,OP_EQ, "abcabc");
  139. tor_free(cp_alloc);
  140. smartlist_split_string(sl, "/def/ /ghijk", "/", 0, 0);
  141. tt_int_op(8,OP_EQ, smartlist_len(sl));
  142. tt_str_op("",OP_EQ, smartlist_get(sl, 4));
  143. tt_str_op("def",OP_EQ, smartlist_get(sl, 5));
  144. tt_str_op(" ",OP_EQ, smartlist_get(sl, 6));
  145. tt_str_op("ghijk",OP_EQ, smartlist_get(sl, 7));
  146. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  147. smartlist_clear(sl);
  148. smartlist_split_string(sl, "a,bbd,cdef", ",", SPLIT_SKIP_SPACE, 0);
  149. tt_int_op(3,OP_EQ, smartlist_len(sl));
  150. tt_str_op("a",OP_EQ, smartlist_get(sl,0));
  151. tt_str_op("bbd",OP_EQ, smartlist_get(sl,1));
  152. tt_str_op("cdef",OP_EQ, smartlist_get(sl,2));
  153. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  154. SPLIT_SKIP_SPACE, 0);
  155. tt_int_op(8,OP_EQ, smartlist_len(sl));
  156. tt_str_op("z",OP_EQ, smartlist_get(sl,3));
  157. tt_str_op("zhasd",OP_EQ, smartlist_get(sl,4));
  158. tt_str_op("",OP_EQ, smartlist_get(sl,5));
  159. tt_str_op("bnud",OP_EQ, smartlist_get(sl,6));
  160. tt_str_op("",OP_EQ, smartlist_get(sl,7));
  161. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  162. smartlist_clear(sl);
  163. smartlist_split_string(sl, " ab\tc \td ef ", NULL,
  164. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  165. tt_int_op(4,OP_EQ, smartlist_len(sl));
  166. tt_str_op("ab",OP_EQ, smartlist_get(sl,0));
  167. tt_str_op("c",OP_EQ, smartlist_get(sl,1));
  168. tt_str_op("d",OP_EQ, smartlist_get(sl,2));
  169. tt_str_op("ef",OP_EQ, smartlist_get(sl,3));
  170. smartlist_split_string(sl, "ghi\tj", NULL,
  171. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  172. tt_int_op(6,OP_EQ, smartlist_len(sl));
  173. tt_str_op("ghi",OP_EQ, smartlist_get(sl,4));
  174. tt_str_op("j",OP_EQ, smartlist_get(sl,5));
  175. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  176. smartlist_clear(sl);
  177. cp_alloc = smartlist_join_strings(sl, "XY", 0, NULL);
  178. tt_str_op(cp_alloc,OP_EQ, "");
  179. tor_free(cp_alloc);
  180. cp_alloc = smartlist_join_strings(sl, "XY", 1, NULL);
  181. tt_str_op(cp_alloc,OP_EQ, "XY");
  182. tor_free(cp_alloc);
  183. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  184. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  185. tt_int_op(3,OP_EQ, smartlist_len(sl));
  186. tt_str_op("z",OP_EQ, smartlist_get(sl, 0));
  187. tt_str_op("zhasd",OP_EQ, smartlist_get(sl, 1));
  188. tt_str_op("bnud",OP_EQ, smartlist_get(sl, 2));
  189. smartlist_split_string(sl, " z <> zhasd <> <> bnud<> ", "<>",
  190. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  191. tt_int_op(5,OP_EQ, smartlist_len(sl));
  192. tt_str_op("z",OP_EQ, smartlist_get(sl, 3));
  193. tt_str_op("zhasd <> <> bnud<>",OP_EQ, smartlist_get(sl, 4));
  194. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  195. smartlist_clear(sl);
  196. smartlist_split_string(sl, "abcd\n", "\n",
  197. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  198. tt_int_op(1,OP_EQ, smartlist_len(sl));
  199. tt_str_op("abcd",OP_EQ, smartlist_get(sl, 0));
  200. smartlist_split_string(sl, "efgh", "\n",
  201. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  202. tt_int_op(2,OP_EQ, smartlist_len(sl));
  203. tt_str_op("efgh",OP_EQ, smartlist_get(sl, 1));
  204. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  205. smartlist_clear(sl);
  206. /* Test swapping, shuffling, and sorting. */
  207. smartlist_split_string(sl, "the,onion,router,by,arma,and,nickm", ",", 0, 0);
  208. tt_int_op(7,OP_EQ, smartlist_len(sl));
  209. smartlist_sort(sl, compare_strs_);
  210. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  211. tt_str_op(cp_alloc,OP_EQ, "and,arma,by,nickm,onion,router,the");
  212. tor_free(cp_alloc);
  213. smartlist_swap(sl, 1, 5);
  214. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  215. tt_str_op(cp_alloc,OP_EQ, "and,router,by,nickm,onion,arma,the");
  216. tor_free(cp_alloc);
  217. smartlist_shuffle(sl);
  218. tt_int_op(7,OP_EQ, smartlist_len(sl));
  219. tt_assert(smartlist_contains_string(sl, "and"));
  220. tt_assert(smartlist_contains_string(sl, "router"));
  221. tt_assert(smartlist_contains_string(sl, "by"));
  222. tt_assert(smartlist_contains_string(sl, "nickm"));
  223. tt_assert(smartlist_contains_string(sl, "onion"));
  224. tt_assert(smartlist_contains_string(sl, "arma"));
  225. tt_assert(smartlist_contains_string(sl, "the"));
  226. /* Test bsearch. */
  227. smartlist_sort(sl, compare_strs_);
  228. tt_str_op("nickm",OP_EQ, smartlist_bsearch(sl, "zNicKM",
  229. cmp_without_first_));
  230. tt_str_op("and",OP_EQ,
  231. smartlist_bsearch(sl, " AND", cmp_without_first_));
  232. tt_ptr_op(NULL,OP_EQ, smartlist_bsearch(sl, " ANz", cmp_without_first_));
  233. /* Test bsearch_idx */
  234. {
  235. int f;
  236. smartlist_t *tmp = NULL;
  237. tt_int_op(0,OP_EQ,smartlist_bsearch_idx(sl," aaa",cmp_without_first_,&f));
  238. tt_int_op(f,OP_EQ, 0);
  239. tt_int_op(0,OP_EQ, smartlist_bsearch_idx(sl," and",cmp_without_first_,&f));
  240. tt_int_op(f,OP_EQ, 1);
  241. tt_int_op(1,OP_EQ, smartlist_bsearch_idx(sl," arm",cmp_without_first_,&f));
  242. tt_int_op(f,OP_EQ, 0);
  243. tt_int_op(1,OP_EQ,
  244. smartlist_bsearch_idx(sl," arma",cmp_without_first_,&f));
  245. tt_int_op(f,OP_EQ, 1);
  246. tt_int_op(2,OP_EQ,
  247. smartlist_bsearch_idx(sl," armb",cmp_without_first_,&f));
  248. tt_int_op(f,OP_EQ, 0);
  249. tt_int_op(7,OP_EQ,
  250. smartlist_bsearch_idx(sl," zzzz",cmp_without_first_,&f));
  251. tt_int_op(f,OP_EQ, 0);
  252. /* Test trivial cases for list of length 0 or 1 */
  253. tmp = smartlist_new();
  254. tt_int_op(0,OP_EQ, smartlist_bsearch_idx(tmp, "foo",
  255. compare_strs_for_bsearch_, &f));
  256. tt_int_op(f,OP_EQ, 0);
  257. smartlist_insert(tmp, 0, (void *)("bar"));
  258. tt_int_op(1,OP_EQ, smartlist_bsearch_idx(tmp, "foo",
  259. compare_strs_for_bsearch_, &f));
  260. tt_int_op(f,OP_EQ, 0);
  261. tt_int_op(0,OP_EQ, smartlist_bsearch_idx(tmp, "aaa",
  262. compare_strs_for_bsearch_, &f));
  263. tt_int_op(f,OP_EQ, 0);
  264. tt_int_op(0,OP_EQ, smartlist_bsearch_idx(tmp, "bar",
  265. compare_strs_for_bsearch_, &f));
  266. tt_int_op(f,OP_EQ, 1);
  267. /* ... and one for length 2 */
  268. smartlist_insert(tmp, 1, (void *)("foo"));
  269. tt_int_op(1,OP_EQ, smartlist_bsearch_idx(tmp, "foo",
  270. compare_strs_for_bsearch_, &f));
  271. tt_int_op(f,OP_EQ, 1);
  272. tt_int_op(2,OP_EQ, smartlist_bsearch_idx(tmp, "goo",
  273. compare_strs_for_bsearch_, &f));
  274. tt_int_op(f,OP_EQ, 0);
  275. smartlist_free(tmp);
  276. }
  277. /* Test reverse() and pop_last() */
  278. smartlist_reverse(sl);
  279. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  280. tt_str_op(cp_alloc,OP_EQ, "the,router,onion,nickm,by,arma,and");
  281. tor_free(cp_alloc);
  282. cp_alloc = smartlist_pop_last(sl);
  283. tt_str_op(cp_alloc,OP_EQ, "and");
  284. tor_free(cp_alloc);
  285. tt_int_op(smartlist_len(sl),OP_EQ, 6);
  286. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  287. smartlist_clear(sl);
  288. cp_alloc = smartlist_pop_last(sl);
  289. tt_ptr_op(cp_alloc,OP_EQ, NULL);
  290. /* Test uniq() */
  291. smartlist_split_string(sl,
  292. "50,noon,radar,a,man,a,plan,a,canal,panama,radar,noon,50",
  293. ",", 0, 0);
  294. smartlist_sort(sl, compare_strs_);
  295. smartlist_uniq(sl, compare_strs_, tor_free_);
  296. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  297. tt_str_op(cp_alloc,OP_EQ, "50,a,canal,man,noon,panama,plan,radar");
  298. tor_free(cp_alloc);
  299. /* Test contains_string, contains_string_case and contains_int_as_string */
  300. tt_assert(smartlist_contains_string(sl, "noon"));
  301. tt_assert(!smartlist_contains_string(sl, "noonoon"));
  302. tt_assert(smartlist_contains_string_case(sl, "nOOn"));
  303. tt_assert(!smartlist_contains_string_case(sl, "nooNooN"));
  304. tt_assert(smartlist_contains_int_as_string(sl, 50));
  305. tt_assert(!smartlist_contains_int_as_string(sl, 60));
  306. /* Test smartlist_choose */
  307. {
  308. int i;
  309. int allsame = 1;
  310. int allin = 1;
  311. void *first = smartlist_choose(sl);
  312. tt_assert(smartlist_contains(sl, first));
  313. for (i = 0; i < 100; ++i) {
  314. void *second = smartlist_choose(sl);
  315. if (second != first)
  316. allsame = 0;
  317. if (!smartlist_contains(sl, second))
  318. allin = 0;
  319. }
  320. tt_assert(!allsame);
  321. tt_assert(allin);
  322. }
  323. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  324. smartlist_clear(sl);
  325. /* Test string_remove and remove and join_strings2 */
  326. smartlist_split_string(sl,
  327. "Some say the Earth will end in ice and some in fire",
  328. " ", 0, 0);
  329. cp = smartlist_get(sl, 4);
  330. tt_str_op(cp,OP_EQ, "will");
  331. smartlist_add(sl, cp);
  332. smartlist_remove(sl, cp);
  333. tor_free(cp);
  334. cp_alloc = smartlist_join_strings(sl, ",", 0, NULL);
  335. tt_str_op(cp_alloc,OP_EQ, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
  336. tor_free(cp_alloc);
  337. smartlist_string_remove(sl, "in");
  338. cp_alloc = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
  339. tt_str_op(cp_alloc,OP_EQ, "Some+say+the+Earth+fire+end+some+ice+and");
  340. tt_int_op((int)sz,OP_EQ, 40);
  341. done:
  342. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  343. smartlist_free(sl);
  344. tor_free(cp_alloc);
  345. }
  346. /** Run unit tests for smartlist set manipulation functions. */
  347. static void
  348. test_container_smartlist_overlap(void *arg)
  349. {
  350. smartlist_t *sl = smartlist_new();
  351. smartlist_t *ints = smartlist_new();
  352. smartlist_t *odds = smartlist_new();
  353. smartlist_t *evens = smartlist_new();
  354. smartlist_t *primes = smartlist_new();
  355. int i;
  356. (void)arg;
  357. for (i=1; i < 10; i += 2)
  358. smartlist_add(odds, (void*)(uintptr_t)i);
  359. for (i=0; i < 10; i += 2)
  360. smartlist_add(evens, (void*)(uintptr_t)i);
  361. /* add_all */
  362. smartlist_add_all(ints, odds);
  363. smartlist_add_all(ints, evens);
  364. tt_int_op(smartlist_len(ints),OP_EQ, 10);
  365. smartlist_add(primes, (void*)2);
  366. smartlist_add(primes, (void*)3);
  367. smartlist_add(primes, (void*)5);
  368. smartlist_add(primes, (void*)7);
  369. /* overlap */
  370. tt_assert(smartlist_overlap(ints, odds));
  371. tt_assert(smartlist_overlap(odds, primes));
  372. tt_assert(smartlist_overlap(evens, primes));
  373. tt_assert(!smartlist_overlap(odds, evens));
  374. /* intersect */
  375. smartlist_add_all(sl, odds);
  376. smartlist_intersect(sl, primes);
  377. tt_int_op(smartlist_len(sl),OP_EQ, 3);
  378. tt_assert(smartlist_contains(sl, (void*)3));
  379. tt_assert(smartlist_contains(sl, (void*)5));
  380. tt_assert(smartlist_contains(sl, (void*)7));
  381. /* subtract */
  382. smartlist_add_all(sl, primes);
  383. smartlist_subtract(sl, odds);
  384. tt_int_op(smartlist_len(sl),OP_EQ, 1);
  385. tt_assert(smartlist_contains(sl, (void*)2));
  386. done:
  387. smartlist_free(odds);
  388. smartlist_free(evens);
  389. smartlist_free(ints);
  390. smartlist_free(primes);
  391. smartlist_free(sl);
  392. }
  393. /** Run unit tests for smartlist-of-digests functions. */
  394. static void
  395. test_container_smartlist_digests(void *arg)
  396. {
  397. smartlist_t *sl = smartlist_new();
  398. /* contains_digest */
  399. (void)arg;
  400. smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN));
  401. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  402. smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
  403. tt_int_op(0,OP_EQ, smartlist_contains_digest(NULL, "AAAAAAAAAAAAAAAAAAAA"));
  404. tt_assert(smartlist_contains_digest(sl, "AAAAAAAAAAAAAAAAAAAA"));
  405. tt_assert(smartlist_contains_digest(sl, "\00090AAB2AAAAaasdAAAAA"));
  406. tt_int_op(0,OP_EQ, smartlist_contains_digest(sl, "\00090AAB2AAABaasdAAAAA"));
  407. /* sort digests */
  408. smartlist_sort_digests(sl);
  409. tt_mem_op(smartlist_get(sl, 0),OP_EQ, "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  410. tt_mem_op(smartlist_get(sl, 1),OP_EQ, "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  411. tt_mem_op(smartlist_get(sl, 2),OP_EQ, "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  412. tt_int_op(3,OP_EQ, smartlist_len(sl));
  413. /* uniq_digests */
  414. smartlist_uniq_digests(sl);
  415. tt_int_op(2,OP_EQ, smartlist_len(sl));
  416. tt_mem_op(smartlist_get(sl, 0),OP_EQ, "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
  417. tt_mem_op(smartlist_get(sl, 1),OP_EQ, "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
  418. done:
  419. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  420. smartlist_free(sl);
  421. }
  422. /** Run unit tests for concatenate-a-smartlist-of-strings functions. */
  423. static void
  424. test_container_smartlist_join(void *arg)
  425. {
  426. smartlist_t *sl = smartlist_new();
  427. smartlist_t *sl2 = smartlist_new(), *sl3 = smartlist_new(),
  428. *sl4 = smartlist_new();
  429. char *joined=NULL;
  430. /* unique, sorted. */
  431. (void)arg;
  432. smartlist_split_string(sl,
  433. "Abashments Ambush Anchorman Bacon Banks Borscht "
  434. "Bunks Inhumane Insurance Knish Know Manners "
  435. "Maraschinos Stamina Sunbonnets Unicorns Wombats",
  436. " ", 0, 0);
  437. /* non-unique, sorted. */
  438. smartlist_split_string(sl2,
  439. "Ambush Anchorman Anchorman Anemias Anemias Bacon "
  440. "Crossbowmen Inhumane Insurance Knish Know Manners "
  441. "Manners Maraschinos Wombats Wombats Work",
  442. " ", 0, 0);
  443. SMARTLIST_FOREACH_JOIN(sl, char *, cp1,
  444. sl2, char *, cp2,
  445. strcmp(cp1,cp2),
  446. smartlist_add(sl3, cp2)) {
  447. tt_str_op(cp1,OP_EQ, cp2);
  448. smartlist_add(sl4, cp1);
  449. } SMARTLIST_FOREACH_JOIN_END(cp1, cp2);
  450. SMARTLIST_FOREACH(sl3, const char *, cp,
  451. tt_assert(smartlist_contains(sl2, cp) &&
  452. !smartlist_contains_string(sl, cp)));
  453. SMARTLIST_FOREACH(sl4, const char *, cp,
  454. tt_assert(smartlist_contains(sl, cp) &&
  455. smartlist_contains_string(sl2, cp)));
  456. joined = smartlist_join_strings(sl3, ",", 0, NULL);
  457. tt_str_op(joined,OP_EQ, "Anemias,Anemias,Crossbowmen,Work");
  458. tor_free(joined);
  459. joined = smartlist_join_strings(sl4, ",", 0, NULL);
  460. tt_str_op(joined,OP_EQ,
  461. "Ambush,Anchorman,Anchorman,Bacon,Inhumane,Insurance,"
  462. "Knish,Know,Manners,Manners,Maraschinos,Wombats,Wombats");
  463. tor_free(joined);
  464. done:
  465. smartlist_free(sl4);
  466. smartlist_free(sl3);
  467. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  468. smartlist_free(sl2);
  469. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  470. smartlist_free(sl);
  471. tor_free(joined);
  472. }
  473. static void
  474. test_container_smartlist_pos(void *arg)
  475. {
  476. (void) arg;
  477. smartlist_t *sl = smartlist_new();
  478. smartlist_add_strdup(sl, "This");
  479. smartlist_add_strdup(sl, "is");
  480. smartlist_add_strdup(sl, "a");
  481. smartlist_add_strdup(sl, "test");
  482. smartlist_add_strdup(sl, "for");
  483. smartlist_add_strdup(sl, "a");
  484. smartlist_add_strdup(sl, "function");
  485. /* Test string_pos */
  486. tt_int_op(smartlist_string_pos(NULL, "Fred"), OP_EQ, -1);
  487. tt_int_op(smartlist_string_pos(sl, "Fred"), OP_EQ, -1);
  488. tt_int_op(smartlist_string_pos(sl, "This"), OP_EQ, 0);
  489. tt_int_op(smartlist_string_pos(sl, "a"), OP_EQ, 2);
  490. tt_int_op(smartlist_string_pos(sl, "function"), OP_EQ, 6);
  491. /* Test pos */
  492. tt_int_op(smartlist_pos(NULL, "Fred"), OP_EQ, -1);
  493. tt_int_op(smartlist_pos(sl, "Fred"), OP_EQ, -1);
  494. tt_int_op(smartlist_pos(sl, "This"), OP_EQ, -1);
  495. tt_int_op(smartlist_pos(sl, "a"), OP_EQ, -1);
  496. tt_int_op(smartlist_pos(sl, "function"), OP_EQ, -1);
  497. tt_int_op(smartlist_pos(sl, smartlist_get(sl,0)), OP_EQ, 0);
  498. tt_int_op(smartlist_pos(sl, smartlist_get(sl,2)), OP_EQ, 2);
  499. tt_int_op(smartlist_pos(sl, smartlist_get(sl,5)), OP_EQ, 5);
  500. tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), OP_EQ, 6);
  501. done:
  502. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  503. smartlist_free(sl);
  504. }
  505. static void
  506. test_container_smartlist_ints_eq(void *arg)
  507. {
  508. smartlist_t *sl1 = NULL, *sl2 = NULL;
  509. int x;
  510. (void)arg;
  511. tt_assert(smartlist_ints_eq(NULL, NULL));
  512. sl1 = smartlist_new();
  513. tt_assert(!smartlist_ints_eq(sl1, NULL));
  514. tt_assert(!smartlist_ints_eq(NULL, sl1));
  515. sl2 = smartlist_new();
  516. tt_assert(smartlist_ints_eq(sl1, sl2));
  517. x = 5;
  518. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  519. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  520. x = 90;
  521. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  522. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  523. tt_assert(smartlist_ints_eq(sl1, sl2));
  524. x = -50;
  525. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  526. tt_assert(! smartlist_ints_eq(sl1, sl2));
  527. tt_assert(! smartlist_ints_eq(sl2, sl1));
  528. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  529. tt_assert(smartlist_ints_eq(sl1, sl2));
  530. *(int*)smartlist_get(sl1, 1) = 101010;
  531. tt_assert(! smartlist_ints_eq(sl2, sl1));
  532. *(int*)smartlist_get(sl2, 1) = 101010;
  533. tt_assert(smartlist_ints_eq(sl1, sl2));
  534. done:
  535. if (sl1)
  536. SMARTLIST_FOREACH(sl1, int *, ip, tor_free(ip));
  537. if (sl2)
  538. SMARTLIST_FOREACH(sl2, int *, ip, tor_free(ip));
  539. smartlist_free(sl1);
  540. smartlist_free(sl2);
  541. }
  542. static void
  543. test_container_smartlist_grow(void *arg)
  544. {
  545. (void)arg;
  546. smartlist_t *sl = smartlist_new();
  547. int i;
  548. const char *s[] = { "first", "2nd", "3rd" };
  549. /* case 1: starting from empty. */
  550. smartlist_grow(sl, 10);
  551. tt_int_op(10, OP_EQ, smartlist_len(sl));
  552. for (i = 0; i < 10; ++i) {
  553. tt_ptr_op(smartlist_get(sl, i), OP_EQ, NULL);
  554. }
  555. /* case 2: starting with a few elements, probably not reallocating. */
  556. smartlist_free(sl);
  557. sl = smartlist_new();
  558. smartlist_add(sl, (char*)s[0]);
  559. smartlist_add(sl, (char*)s[1]);
  560. smartlist_add(sl, (char*)s[2]);
  561. smartlist_grow(sl, 5);
  562. tt_int_op(5, OP_EQ, smartlist_len(sl));
  563. for (i = 0; i < 3; ++i) {
  564. tt_ptr_op(smartlist_get(sl, i), OP_EQ, s[i]);
  565. }
  566. tt_ptr_op(smartlist_get(sl, 3), OP_EQ, NULL);
  567. tt_ptr_op(smartlist_get(sl, 4), OP_EQ, NULL);
  568. /* case 3: starting with a few elements, but reallocating. */
  569. smartlist_free(sl);
  570. sl = smartlist_new();
  571. smartlist_add(sl, (char*)s[0]);
  572. smartlist_add(sl, (char*)s[1]);
  573. smartlist_add(sl, (char*)s[2]);
  574. smartlist_grow(sl, 100);
  575. tt_int_op(100, OP_EQ, smartlist_len(sl));
  576. for (i = 0; i < 3; ++i) {
  577. tt_ptr_op(smartlist_get(sl, i), OP_EQ, s[i]);
  578. }
  579. for (i = 3; i < 100; ++i) {
  580. tt_ptr_op(smartlist_get(sl, i), OP_EQ, NULL);
  581. }
  582. /* case 4: shrinking doesn't happen. */
  583. smartlist_free(sl);
  584. sl = smartlist_new();
  585. smartlist_add(sl, (char*)s[0]);
  586. smartlist_add(sl, (char*)s[1]);
  587. smartlist_add(sl, (char*)s[2]);
  588. smartlist_grow(sl, 1);
  589. tt_int_op(3, OP_EQ, smartlist_len(sl));
  590. for (i = 0; i < 3; ++i) {
  591. tt_ptr_op(smartlist_get(sl, i), OP_EQ, s[i]);
  592. }
  593. done:
  594. smartlist_free(sl);
  595. }
  596. /** Run unit tests for bitarray code */
  597. static void
  598. test_container_bitarray(void *arg)
  599. {
  600. bitarray_t *ba = NULL;
  601. int i, j, ok=1;
  602. (void)arg;
  603. ba = bitarray_init_zero(1);
  604. tt_assert(ba);
  605. tt_assert(! bitarray_is_set(ba, 0));
  606. bitarray_set(ba, 0);
  607. tt_assert(bitarray_is_set(ba, 0));
  608. bitarray_clear(ba, 0);
  609. tt_assert(! bitarray_is_set(ba, 0));
  610. bitarray_free(ba);
  611. ba = bitarray_init_zero(1023);
  612. for (i = 1; i < 64; ) {
  613. for (j = 0; j < 1023; ++j) {
  614. if (j % i)
  615. bitarray_set(ba, j);
  616. else
  617. bitarray_clear(ba, j);
  618. }
  619. for (j = 0; j < 1023; ++j) {
  620. if (!bool_eq(bitarray_is_set(ba, j), j%i))
  621. ok = 0;
  622. }
  623. tt_assert(ok);
  624. if (i < 7)
  625. ++i;
  626. else if (i == 28)
  627. i = 32;
  628. else
  629. i += 7;
  630. }
  631. done:
  632. if (ba)
  633. bitarray_free(ba);
  634. }
  635. /** Run unit tests for digest set code (implemented as a hashtable or as a
  636. * bloom filter) */
  637. static void
  638. test_container_digestset(void *arg)
  639. {
  640. smartlist_t *included = smartlist_new();
  641. char d[DIGEST_LEN];
  642. int i;
  643. int ok = 1;
  644. int false_positives = 0;
  645. digestset_t *set = NULL;
  646. (void)arg;
  647. for (i = 0; i < 1000; ++i) {
  648. crypto_rand(d, DIGEST_LEN);
  649. smartlist_add(included, tor_memdup(d, DIGEST_LEN));
  650. }
  651. set = digestset_new(1000);
  652. SMARTLIST_FOREACH(included, const char *, cp,
  653. if (digestset_probably_contains(set, cp))
  654. ok = 0);
  655. tt_assert(ok);
  656. SMARTLIST_FOREACH(included, const char *, cp,
  657. digestset_add(set, cp));
  658. SMARTLIST_FOREACH(included, const char *, cp,
  659. if (!digestset_probably_contains(set, cp))
  660. ok = 0);
  661. tt_assert(ok);
  662. for (i = 0; i < 1000; ++i) {
  663. crypto_rand(d, DIGEST_LEN);
  664. if (digestset_probably_contains(set, d))
  665. ++false_positives;
  666. }
  667. tt_int_op(50, OP_GT, false_positives); /* Should be far lower. */
  668. done:
  669. if (set)
  670. digestset_free(set);
  671. SMARTLIST_FOREACH(included, char *, cp, tor_free(cp));
  672. smartlist_free(included);
  673. }
  674. typedef struct pq_entry_t {
  675. const char *val;
  676. int idx;
  677. } pq_entry_t;
  678. /** Helper: return a tristate based on comparing two pq_entry_t values. */
  679. static int
  680. compare_strings_for_pqueue_(const void *p1, const void *p2)
  681. {
  682. const pq_entry_t *e1=p1, *e2=p2;
  683. return strcmp(e1->val, e2->val);
  684. }
  685. /** Run unit tests for heap-based priority queue functions. */
  686. static void
  687. test_container_pqueue(void *arg)
  688. {
  689. smartlist_t *sl = smartlist_new();
  690. int (*cmp)(const void *, const void*);
  691. const int offset = offsetof(pq_entry_t, idx);
  692. #define ENTRY(s) pq_entry_t s = { #s, -1 }
  693. ENTRY(cows);
  694. ENTRY(zebras);
  695. ENTRY(fish);
  696. ENTRY(frogs);
  697. ENTRY(apples);
  698. ENTRY(squid);
  699. ENTRY(daschunds);
  700. ENTRY(eggplants);
  701. ENTRY(weissbier);
  702. ENTRY(lobsters);
  703. ENTRY(roquefort);
  704. ENTRY(chinchillas);
  705. ENTRY(fireflies);
  706. #define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
  707. (void)arg;
  708. cmp = compare_strings_for_pqueue_;
  709. smartlist_pqueue_add(sl, cmp, offset, &cows);
  710. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  711. smartlist_pqueue_add(sl, cmp, offset, &fish);
  712. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  713. smartlist_pqueue_add(sl, cmp, offset, &apples);
  714. smartlist_pqueue_add(sl, cmp, offset, &squid);
  715. smartlist_pqueue_add(sl, cmp, offset, &daschunds);
  716. smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  717. smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  718. smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  719. smartlist_pqueue_add(sl, cmp, offset, &roquefort);
  720. OK();
  721. tt_int_op(smartlist_len(sl),OP_EQ, 11);
  722. tt_ptr_op(smartlist_get(sl, 0),OP_EQ, &apples);
  723. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &apples);
  724. tt_int_op(smartlist_len(sl),OP_EQ, 10);
  725. OK();
  726. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &cows);
  727. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &daschunds);
  728. smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  729. OK();
  730. smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  731. OK();
  732. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &chinchillas);
  733. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &eggplants);
  734. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &fireflies);
  735. OK();
  736. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &fish);
  737. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &frogs);
  738. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &lobsters);
  739. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &roquefort);
  740. OK();
  741. tt_int_op(smartlist_len(sl),OP_EQ, 3);
  742. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &squid);
  743. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &weissbier);
  744. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &zebras);
  745. tt_int_op(smartlist_len(sl),OP_EQ, 0);
  746. OK();
  747. /* Now test remove. */
  748. smartlist_pqueue_add(sl, cmp, offset, &cows);
  749. smartlist_pqueue_add(sl, cmp, offset, &fish);
  750. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  751. smartlist_pqueue_add(sl, cmp, offset, &apples);
  752. smartlist_pqueue_add(sl, cmp, offset, &squid);
  753. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  754. tt_int_op(smartlist_len(sl),OP_EQ, 6);
  755. OK();
  756. smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  757. tt_int_op(smartlist_len(sl),OP_EQ, 5);
  758. OK();
  759. smartlist_pqueue_remove(sl, cmp, offset, &cows);
  760. tt_int_op(smartlist_len(sl),OP_EQ, 4);
  761. OK();
  762. smartlist_pqueue_remove(sl, cmp, offset, &apples);
  763. tt_int_op(smartlist_len(sl),OP_EQ, 3);
  764. OK();
  765. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &fish);
  766. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &frogs);
  767. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &squid);
  768. tt_int_op(smartlist_len(sl),OP_EQ, 0);
  769. OK();
  770. #undef OK
  771. done:
  772. smartlist_free(sl);
  773. }
  774. /** Run unit tests for string-to-void* map functions */
  775. static void
  776. test_container_strmap(void *arg)
  777. {
  778. strmap_t *map;
  779. strmap_iter_t *iter;
  780. const char *k;
  781. void *v;
  782. char *visited = NULL;
  783. smartlist_t *found_keys = NULL;
  784. char *v1 = tor_strdup("v1");
  785. char *v99 = tor_strdup("v99");
  786. char *v100 = tor_strdup("v100");
  787. char *v101 = tor_strdup("v101");
  788. char *v102 = tor_strdup("v102");
  789. char *v103 = tor_strdup("v103");
  790. char *v104 = tor_strdup("v104");
  791. char *v105 = tor_strdup("v105");
  792. (void)arg;
  793. map = strmap_new();
  794. tt_assert(map);
  795. tt_int_op(strmap_size(map),OP_EQ, 0);
  796. tt_assert(strmap_isempty(map));
  797. v = strmap_set(map, "K1", v99);
  798. tt_ptr_op(v,OP_EQ, NULL);
  799. tt_assert(!strmap_isempty(map));
  800. v = strmap_set(map, "K2", v101);
  801. tt_ptr_op(v,OP_EQ, NULL);
  802. v = strmap_set(map, "K1", v100);
  803. tt_ptr_op(v,OP_EQ, v99);
  804. tt_ptr_op(strmap_get(map,"K1"),OP_EQ, v100);
  805. tt_ptr_op(strmap_get(map,"K2"),OP_EQ, v101);
  806. tt_ptr_op(strmap_get(map,"K-not-there"),OP_EQ, NULL);
  807. strmap_assert_ok(map);
  808. v = strmap_remove(map,"K2");
  809. strmap_assert_ok(map);
  810. tt_ptr_op(v,OP_EQ, v101);
  811. tt_ptr_op(strmap_get(map,"K2"),OP_EQ, NULL);
  812. tt_ptr_op(strmap_remove(map,"K2"),OP_EQ, NULL);
  813. strmap_set(map, "K2", v101);
  814. strmap_set(map, "K3", v102);
  815. strmap_set(map, "K4", v103);
  816. tt_int_op(strmap_size(map),OP_EQ, 4);
  817. strmap_assert_ok(map);
  818. strmap_set(map, "K5", v104);
  819. strmap_set(map, "K6", v105);
  820. strmap_assert_ok(map);
  821. /* Test iterator. */
  822. iter = strmap_iter_init(map);
  823. found_keys = smartlist_new();
  824. while (!strmap_iter_done(iter)) {
  825. strmap_iter_get(iter,&k,&v);
  826. smartlist_add_strdup(found_keys, k);
  827. tt_ptr_op(v,OP_EQ, strmap_get(map, k));
  828. if (!strcmp(k, "K2")) {
  829. iter = strmap_iter_next_rmv(map,iter);
  830. } else {
  831. iter = strmap_iter_next(map,iter);
  832. }
  833. }
  834. /* Make sure we removed K2, but not the others. */
  835. tt_ptr_op(strmap_get(map, "K2"),OP_EQ, NULL);
  836. tt_ptr_op(strmap_get(map, "K5"),OP_EQ, v104);
  837. /* Make sure we visited everyone once */
  838. smartlist_sort_strings(found_keys);
  839. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  840. tt_str_op(visited,OP_EQ, "K1:K2:K3:K4:K5:K6");
  841. strmap_assert_ok(map);
  842. /* Clean up after ourselves. */
  843. strmap_free(map, NULL);
  844. map = NULL;
  845. /* Now try some lc functions. */
  846. map = strmap_new();
  847. strmap_set_lc(map,"Ab.C", v1);
  848. tt_ptr_op(strmap_get(map,"ab.c"),OP_EQ, v1);
  849. strmap_assert_ok(map);
  850. tt_ptr_op(strmap_get_lc(map,"AB.C"),OP_EQ, v1);
  851. tt_ptr_op(strmap_get(map,"AB.C"),OP_EQ, NULL);
  852. tt_ptr_op(strmap_remove_lc(map,"aB.C"),OP_EQ, v1);
  853. strmap_assert_ok(map);
  854. tt_ptr_op(strmap_get_lc(map,"AB.C"),OP_EQ, NULL);
  855. done:
  856. if (map)
  857. strmap_free(map,NULL);
  858. if (found_keys) {
  859. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  860. smartlist_free(found_keys);
  861. }
  862. tor_free(visited);
  863. tor_free(v1);
  864. tor_free(v99);
  865. tor_free(v100);
  866. tor_free(v101);
  867. tor_free(v102);
  868. tor_free(v103);
  869. tor_free(v104);
  870. tor_free(v105);
  871. }
  872. static void
  873. test_container_smartlist_remove(void *arg)
  874. {
  875. (void) arg;
  876. int array[5];
  877. smartlist_t *sl = smartlist_new();
  878. int i,j;
  879. for (j=0; j < 2; ++j)
  880. for (i=0; i < 5; ++i)
  881. smartlist_add(sl, &array[i]);
  882. smartlist_remove(sl, &array[0]);
  883. smartlist_remove(sl, &array[3]);
  884. smartlist_remove(sl, &array[4]);
  885. tt_assert(! smartlist_contains(sl, &array[0]));
  886. tt_assert(smartlist_contains(sl, &array[1]));
  887. tt_assert(smartlist_contains(sl, &array[2]));
  888. tt_assert(! smartlist_contains(sl, &array[3]));
  889. tt_assert(! smartlist_contains(sl, &array[4]));
  890. tt_int_op(smartlist_len(sl), OP_EQ, 4);
  891. smartlist_clear(sl);
  892. for (j=0; j < 2; ++j)
  893. for (i=0; i < 5; ++i)
  894. smartlist_add(sl, &array[i]);
  895. smartlist_remove_keeporder(sl, &array[0]);
  896. smartlist_remove_keeporder(sl, &array[3]);
  897. smartlist_remove_keeporder(sl, &array[4]);
  898. tt_int_op(smartlist_len(sl), OP_EQ, 4);
  899. tt_ptr_op(smartlist_get(sl, 0), OP_EQ, &array[1]);
  900. tt_ptr_op(smartlist_get(sl, 1), OP_EQ, &array[2]);
  901. tt_ptr_op(smartlist_get(sl, 2), OP_EQ, &array[1]);
  902. tt_ptr_op(smartlist_get(sl, 3), OP_EQ, &array[2]);
  903. done:
  904. smartlist_free(sl);
  905. }
  906. /** Run unit tests for getting the median of a list. */
  907. static void
  908. test_container_order_functions(void *arg)
  909. {
  910. int lst[25], n = 0;
  911. uint32_t lst_2[25];
  912. // int a=12,b=24,c=25,d=60,e=77;
  913. #define median() median_int(lst, n)
  914. (void)arg;
  915. lst[n++] = 12;
  916. tt_int_op(12,OP_EQ, median()); /* 12 */
  917. lst[n++] = 77;
  918. //smartlist_shuffle(sl);
  919. tt_int_op(12,OP_EQ, median()); /* 12, 77 */
  920. lst[n++] = 77;
  921. //smartlist_shuffle(sl);
  922. tt_int_op(77,OP_EQ, median()); /* 12, 77, 77 */
  923. lst[n++] = 24;
  924. tt_int_op(24,OP_EQ, median()); /* 12,24,77,77 */
  925. lst[n++] = 60;
  926. lst[n++] = 12;
  927. lst[n++] = 25;
  928. //smartlist_shuffle(sl);
  929. tt_int_op(25,OP_EQ, median()); /* 12,12,24,25,60,77,77 */
  930. #undef median
  931. #define third_quartile() third_quartile_uint32(lst_2, n)
  932. n = 0;
  933. lst_2[n++] = 1;
  934. tt_int_op(1,OP_EQ, third_quartile()); /* ~1~ */
  935. lst_2[n++] = 2;
  936. tt_int_op(2,OP_EQ, third_quartile()); /* 1, ~2~ */
  937. lst_2[n++] = 3;
  938. lst_2[n++] = 4;
  939. lst_2[n++] = 5;
  940. tt_int_op(4,OP_EQ, third_quartile()); /* 1, 2, 3, ~4~, 5 */
  941. lst_2[n++] = 6;
  942. lst_2[n++] = 7;
  943. lst_2[n++] = 8;
  944. lst_2[n++] = 9;
  945. tt_int_op(7,OP_EQ, third_quartile()); /* 1, 2, 3, 4, 5, 6, ~7~, 8, 9 */
  946. lst_2[n++] = 10;
  947. lst_2[n++] = 11;
  948. /* 1, 2, 3, 4, 5, 6, 7, 8, ~9~, 10, 11 */
  949. tt_int_op(9,OP_EQ, third_quartile());
  950. #undef third_quartile
  951. double dbls[] = { 1.0, 10.0, 100.0, 1e4, 1e5, 1e6 };
  952. tt_double_eq(1.0, median_double(dbls, 1));
  953. tt_double_eq(1.0, median_double(dbls, 2));
  954. tt_double_eq(10.0, median_double(dbls, 3));
  955. tt_double_eq(10.0, median_double(dbls, 4));
  956. tt_double_eq(100.0, median_double(dbls, 5));
  957. tt_double_eq(100.0, median_double(dbls, 6));
  958. time_t times[] = { 5, 10, 20, 25, 15 };
  959. tt_assert(5 == median_time(times, 1));
  960. tt_assert(5 == median_time(times, 2));
  961. tt_assert(10 == median_time(times, 3));
  962. tt_assert(10 == median_time(times, 4));
  963. tt_assert(15 == median_time(times, 5));
  964. int32_t int32s[] = { -5, -10, -50, 100 };
  965. tt_int_op(-5, OP_EQ, median_int32(int32s, 1));
  966. tt_int_op(-10, OP_EQ, median_int32(int32s, 2));
  967. tt_int_op(-10, OP_EQ, median_int32(int32s, 3));
  968. tt_int_op(-10, OP_EQ, median_int32(int32s, 4));
  969. long longs[] = { -30, 30, 100, -100, 7 };
  970. tt_int_op(7, OP_EQ, find_nth_long(longs, 5, 2));
  971. done:
  972. ;
  973. }
  974. static void
  975. test_container_di_map(void *arg)
  976. {
  977. di_digest256_map_t *map = NULL;
  978. const uint8_t key1[] = "In view of the fact that it was ";
  979. const uint8_t key2[] = "superficially convincing, being ";
  980. const uint8_t key3[] = "properly enciphered in a one-tim";
  981. const uint8_t key4[] = "e cipher scheduled for use today";
  982. char *v1 = tor_strdup(", it came close to causing a disaster...");
  983. char *v2 = tor_strdup("I regret to have to advise you that the mission");
  984. char *v3 = tor_strdup("was actually initiated...");
  985. /* -- John Brunner, _The Shockwave Rider_ */
  986. (void)arg;
  987. /* Try searching on an empty map. */
  988. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key1, NULL));
  989. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key2, NULL));
  990. tt_ptr_op(v3, OP_EQ, dimap_search(map, key2, v3));
  991. dimap_free(map, NULL);
  992. map = NULL;
  993. /* Add a single entry. */
  994. dimap_add_entry(&map, key1, v1);
  995. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key2, NULL));
  996. tt_ptr_op(v3, OP_EQ, dimap_search(map, key2, v3));
  997. tt_ptr_op(v1, OP_EQ, dimap_search(map, key1, NULL));
  998. /* Now try it with three entries in the map. */
  999. dimap_add_entry(&map, key2, v2);
  1000. dimap_add_entry(&map, key3, v3);
  1001. tt_ptr_op(v1, OP_EQ, dimap_search(map, key1, NULL));
  1002. tt_ptr_op(v2, OP_EQ, dimap_search(map, key2, NULL));
  1003. tt_ptr_op(v3, OP_EQ, dimap_search(map, key3, NULL));
  1004. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key4, NULL));
  1005. tt_ptr_op(v1, OP_EQ, dimap_search(map, key4, v1));
  1006. done:
  1007. tor_free(v1);
  1008. tor_free(v2);
  1009. tor_free(v3);
  1010. dimap_free(map, NULL);
  1011. }
  1012. /** Run unit tests for fp_pair-to-void* map functions */
  1013. static void
  1014. test_container_fp_pair_map(void *arg)
  1015. {
  1016. fp_pair_map_t *map;
  1017. fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  1018. void *v;
  1019. fp_pair_map_iter_t *iter;
  1020. fp_pair_t k;
  1021. char *v99 = tor_strdup("99");
  1022. char *v100 = tor_strdup("v100");
  1023. char *v101 = tor_strdup("v101");
  1024. char *v102 = tor_strdup("v102");
  1025. char *v103 = tor_strdup("v103");
  1026. char *v104 = tor_strdup("v104");
  1027. char *v105 = tor_strdup("v105");
  1028. (void)arg;
  1029. map = fp_pair_map_new();
  1030. tt_assert(map);
  1031. tt_int_op(fp_pair_map_size(map),OP_EQ, 0);
  1032. tt_assert(fp_pair_map_isempty(map));
  1033. memset(fp1.first, 0x11, DIGEST_LEN);
  1034. memset(fp1.second, 0x12, DIGEST_LEN);
  1035. memset(fp2.first, 0x21, DIGEST_LEN);
  1036. memset(fp2.second, 0x22, DIGEST_LEN);
  1037. memset(fp3.first, 0x31, DIGEST_LEN);
  1038. memset(fp3.second, 0x32, DIGEST_LEN);
  1039. memset(fp4.first, 0x41, DIGEST_LEN);
  1040. memset(fp4.second, 0x42, DIGEST_LEN);
  1041. memset(fp5.first, 0x51, DIGEST_LEN);
  1042. memset(fp5.second, 0x52, DIGEST_LEN);
  1043. memset(fp6.first, 0x61, DIGEST_LEN);
  1044. memset(fp6.second, 0x62, DIGEST_LEN);
  1045. v = fp_pair_map_set(map, &fp1, v99);
  1046. tt_ptr_op(v, OP_EQ, NULL);
  1047. tt_assert(!fp_pair_map_isempty(map));
  1048. v = fp_pair_map_set(map, &fp2, v101);
  1049. tt_ptr_op(v, OP_EQ, NULL);
  1050. v = fp_pair_map_set(map, &fp1, v100);
  1051. tt_ptr_op(v, OP_EQ, v99);
  1052. tt_ptr_op(fp_pair_map_get(map, &fp1),OP_EQ, v100);
  1053. tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, v101);
  1054. tt_ptr_op(fp_pair_map_get(map, &fp3),OP_EQ, NULL);
  1055. fp_pair_map_assert_ok(map);
  1056. v = fp_pair_map_remove(map, &fp2);
  1057. fp_pair_map_assert_ok(map);
  1058. tt_ptr_op(v,OP_EQ, v101);
  1059. tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL);
  1060. tt_ptr_op(fp_pair_map_remove(map, &fp2),OP_EQ, NULL);
  1061. fp_pair_map_set(map, &fp2, v101);
  1062. fp_pair_map_set(map, &fp3, v102);
  1063. fp_pair_map_set(map, &fp4, v103);
  1064. tt_int_op(fp_pair_map_size(map),OP_EQ, 4);
  1065. fp_pair_map_assert_ok(map);
  1066. fp_pair_map_set(map, &fp5, v104);
  1067. fp_pair_map_set_by_digests(map, fp6.first, fp6.second, v105);
  1068. fp_pair_map_assert_ok(map);
  1069. /* Test iterator. */
  1070. iter = fp_pair_map_iter_init(map);
  1071. while (!fp_pair_map_iter_done(iter)) {
  1072. fp_pair_map_iter_get(iter, &k, &v);
  1073. tt_ptr_op(v,OP_EQ, fp_pair_map_get(map, &k));
  1074. if (tor_memeq(&fp2, &k, sizeof(fp2))) {
  1075. iter = fp_pair_map_iter_next_rmv(map, iter);
  1076. } else {
  1077. iter = fp_pair_map_iter_next(map, iter);
  1078. }
  1079. }
  1080. /* Make sure we removed fp2, but not the others. */
  1081. tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL);
  1082. tt_ptr_op(fp_pair_map_get_by_digests(map, fp5.first, fp5.second),
  1083. OP_EQ, v104);
  1084. fp_pair_map_assert_ok(map);
  1085. /* Clean up after ourselves. */
  1086. fp_pair_map_free(map, NULL);
  1087. map = NULL;
  1088. done:
  1089. if (map)
  1090. fp_pair_map_free(map, NULL);
  1091. tor_free(v99);
  1092. tor_free(v100);
  1093. tor_free(v101);
  1094. tor_free(v102);
  1095. tor_free(v103);
  1096. tor_free(v104);
  1097. tor_free(v105);
  1098. }
  1099. static void
  1100. test_container_smartlist_most_frequent(void *arg)
  1101. {
  1102. (void) arg;
  1103. smartlist_t *sl = smartlist_new();
  1104. int count = -1;
  1105. const char *cp;
  1106. cp = smartlist_get_most_frequent_string_(sl, &count);
  1107. tt_int_op(count, OP_EQ, 0);
  1108. tt_ptr_op(cp, OP_EQ, NULL);
  1109. /* String must be sorted before we call get_most_frequent */
  1110. smartlist_split_string(sl, "abc:def:ghi", ":", 0, 0);
  1111. cp = smartlist_get_most_frequent_string_(sl, &count);
  1112. tt_int_op(count, OP_EQ, 1);
  1113. tt_str_op(cp, OP_EQ, "ghi"); /* Ties broken in favor of later element */
  1114. smartlist_split_string(sl, "def:ghi", ":", 0, 0);
  1115. smartlist_sort_strings(sl);
  1116. cp = smartlist_get_most_frequent_string_(sl, &count);
  1117. tt_int_op(count, OP_EQ, 2);
  1118. tt_ptr_op(cp, OP_NE, NULL);
  1119. tt_str_op(cp, OP_EQ, "ghi"); /* Ties broken in favor of later element */
  1120. smartlist_split_string(sl, "def:abc:qwop", ":", 0, 0);
  1121. smartlist_sort_strings(sl);
  1122. cp = smartlist_get_most_frequent_string_(sl, &count);
  1123. tt_int_op(count, OP_EQ, 3);
  1124. tt_ptr_op(cp, OP_NE, NULL);
  1125. tt_str_op(cp, OP_EQ, "def"); /* No tie */
  1126. done:
  1127. SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
  1128. smartlist_free(sl);
  1129. }
  1130. static void
  1131. test_container_smartlist_sort_ptrs(void *arg)
  1132. {
  1133. (void)arg;
  1134. int array[10];
  1135. int *arrayptrs[11];
  1136. smartlist_t *sl = smartlist_new();
  1137. unsigned i=0, j;
  1138. for (j = 0; j < ARRAY_LENGTH(array); ++j) {
  1139. smartlist_add(sl, &array[j]);
  1140. arrayptrs[i++] = &array[j];
  1141. if (j == 5) {
  1142. smartlist_add(sl, &array[j]);
  1143. arrayptrs[i++] = &array[j];
  1144. }
  1145. }
  1146. for (i = 0; i < 10; ++i) {
  1147. smartlist_shuffle(sl);
  1148. smartlist_sort_pointers(sl);
  1149. for (j = 0; j < ARRAY_LENGTH(arrayptrs); ++j) {
  1150. tt_ptr_op(smartlist_get(sl, j), OP_EQ, arrayptrs[j]);
  1151. }
  1152. }
  1153. done:
  1154. smartlist_free(sl);
  1155. }
  1156. static void
  1157. test_container_smartlist_strings_eq(void *arg)
  1158. {
  1159. (void)arg;
  1160. smartlist_t *sl1 = smartlist_new();
  1161. smartlist_t *sl2 = smartlist_new();
  1162. #define EQ_SHOULD_SAY(s1,s2,val) \
  1163. do { \
  1164. SMARTLIST_FOREACH(sl1, char *, cp, tor_free(cp)); \
  1165. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp)); \
  1166. smartlist_clear(sl1); \
  1167. smartlist_clear(sl2); \
  1168. smartlist_split_string(sl1, (s1), ":", 0, 0); \
  1169. smartlist_split_string(sl2, (s2), ":", 0, 0); \
  1170. tt_int_op((val), OP_EQ, smartlist_strings_eq(sl1, sl2)); \
  1171. } while (0)
  1172. /* Both NULL, so equal */
  1173. tt_int_op(1, OP_EQ, smartlist_strings_eq(NULL, NULL));
  1174. /* One NULL, not equal. */
  1175. tt_int_op(0, OP_EQ, smartlist_strings_eq(NULL, sl1));
  1176. tt_int_op(0, OP_EQ, smartlist_strings_eq(sl1, NULL));
  1177. /* Both empty, both equal. */
  1178. EQ_SHOULD_SAY("", "", 1);
  1179. /* One empty, not equal */
  1180. EQ_SHOULD_SAY("", "ab", 0);
  1181. EQ_SHOULD_SAY("", "xy:z", 0);
  1182. EQ_SHOULD_SAY("abc", "", 0);
  1183. EQ_SHOULD_SAY("abc:cd", "", 0);
  1184. /* Different lengths, not equal. */
  1185. EQ_SHOULD_SAY("hello:world", "hello", 0);
  1186. EQ_SHOULD_SAY("hello", "hello:friends", 0);
  1187. /* Same lengths, not equal */
  1188. EQ_SHOULD_SAY("Hello:world", "goodbye:world", 0);
  1189. EQ_SHOULD_SAY("Hello:world", "Hello:stars", 0);
  1190. /* Actually equal */
  1191. EQ_SHOULD_SAY("ABC", "ABC", 1);
  1192. EQ_SHOULD_SAY(" ab : cd : e", " ab : cd : e", 1);
  1193. done:
  1194. SMARTLIST_FOREACH(sl1, char *, cp, tor_free(cp));
  1195. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  1196. smartlist_free(sl1);
  1197. smartlist_free(sl2);
  1198. }
  1199. #define CONTAINER_LEGACY(name) \
  1200. { #name, test_container_ ## name , 0, NULL, NULL }
  1201. #define CONTAINER(name, flags) \
  1202. { #name, test_container_ ## name, (flags), NULL, NULL }
  1203. struct testcase_t container_tests[] = {
  1204. CONTAINER_LEGACY(smartlist_basic),
  1205. CONTAINER_LEGACY(smartlist_strings),
  1206. CONTAINER_LEGACY(smartlist_foreach_reverse),
  1207. CONTAINER_LEGACY(smartlist_overlap),
  1208. CONTAINER_LEGACY(smartlist_digests),
  1209. CONTAINER_LEGACY(smartlist_join),
  1210. CONTAINER_LEGACY(smartlist_pos),
  1211. CONTAINER(smartlist_remove, 0),
  1212. CONTAINER(smartlist_ints_eq, 0),
  1213. CONTAINER(smartlist_grow, 0),
  1214. CONTAINER_LEGACY(bitarray),
  1215. CONTAINER_LEGACY(digestset),
  1216. CONTAINER_LEGACY(strmap),
  1217. CONTAINER_LEGACY(pqueue),
  1218. CONTAINER_LEGACY(order_functions),
  1219. CONTAINER(di_map, 0),
  1220. CONTAINER_LEGACY(fp_pair_map),
  1221. CONTAINER(smartlist_most_frequent, 0),
  1222. CONTAINER(smartlist_sort_ptrs, 0),
  1223. CONTAINER(smartlist_strings_eq, 0),
  1224. END_OF_TESTCASES
  1225. };