test_containers.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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_pos(void *arg)
  452. {
  453. (void) arg;
  454. smartlist_t *sl = smartlist_new();
  455. smartlist_add(sl, tor_strdup("This"));
  456. smartlist_add(sl, tor_strdup("is"));
  457. smartlist_add(sl, tor_strdup("a"));
  458. smartlist_add(sl, tor_strdup("test"));
  459. smartlist_add(sl, tor_strdup("for"));
  460. smartlist_add(sl, tor_strdup("a"));
  461. smartlist_add(sl, tor_strdup("function"));
  462. /* Test string_pos */
  463. tt_int_op(smartlist_string_pos(NULL, "Fred"), ==, -1);
  464. tt_int_op(smartlist_string_pos(sl, "Fred"), ==, -1);
  465. tt_int_op(smartlist_string_pos(sl, "This"), ==, 0);
  466. tt_int_op(smartlist_string_pos(sl, "a"), ==, 2);
  467. tt_int_op(smartlist_string_pos(sl, "function"), ==, 6);
  468. /* Test pos */
  469. tt_int_op(smartlist_pos(NULL, "Fred"), ==, -1);
  470. tt_int_op(smartlist_pos(sl, "Fred"), ==, -1);
  471. tt_int_op(smartlist_pos(sl, "This"), ==, -1);
  472. tt_int_op(smartlist_pos(sl, "a"), ==, -1);
  473. tt_int_op(smartlist_pos(sl, "function"), ==, -1);
  474. tt_int_op(smartlist_pos(sl, smartlist_get(sl,0)), ==, 0);
  475. tt_int_op(smartlist_pos(sl, smartlist_get(sl,2)), ==, 2);
  476. tt_int_op(smartlist_pos(sl, smartlist_get(sl,5)), ==, 5);
  477. tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), ==, 6);
  478. done:
  479. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  480. smartlist_free(sl);
  481. }
  482. static void
  483. test_container_smartlist_ints_eq(void *arg)
  484. {
  485. smartlist_t *sl1 = NULL, *sl2 = NULL;
  486. int x;
  487. (void)arg;
  488. tt_assert(smartlist_ints_eq(NULL, NULL));
  489. sl1 = smartlist_new();
  490. tt_assert(!smartlist_ints_eq(sl1, NULL));
  491. tt_assert(!smartlist_ints_eq(NULL, sl1));
  492. sl2 = smartlist_new();
  493. tt_assert(smartlist_ints_eq(sl1, sl2));
  494. x = 5;
  495. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  496. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  497. x = 90;
  498. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  499. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  500. tt_assert(smartlist_ints_eq(sl1, sl2));
  501. x = -50;
  502. smartlist_add(sl1, tor_memdup(&x, sizeof(int)));
  503. tt_assert(! smartlist_ints_eq(sl1, sl2));
  504. tt_assert(! smartlist_ints_eq(sl2, sl1));
  505. smartlist_add(sl2, tor_memdup(&x, sizeof(int)));
  506. tt_assert(smartlist_ints_eq(sl1, sl2));
  507. *(int*)smartlist_get(sl1, 1) = 101010;
  508. tt_assert(! smartlist_ints_eq(sl2, sl1));
  509. *(int*)smartlist_get(sl2, 1) = 101010;
  510. tt_assert(smartlist_ints_eq(sl1, sl2));
  511. done:
  512. if (sl1)
  513. SMARTLIST_FOREACH(sl1, int *, ip, tor_free(ip));
  514. if (sl2)
  515. SMARTLIST_FOREACH(sl2, int *, ip, tor_free(ip));
  516. smartlist_free(sl1);
  517. smartlist_free(sl2);
  518. }
  519. /** Run unit tests for bitarray code */
  520. static void
  521. test_container_bitarray(void *arg)
  522. {
  523. bitarray_t *ba = NULL;
  524. int i, j, ok=1;
  525. (void)arg;
  526. ba = bitarray_init_zero(1);
  527. tt_assert(ba);
  528. tt_assert(! bitarray_is_set(ba, 0));
  529. bitarray_set(ba, 0);
  530. tt_assert(bitarray_is_set(ba, 0));
  531. bitarray_clear(ba, 0);
  532. tt_assert(! bitarray_is_set(ba, 0));
  533. bitarray_free(ba);
  534. ba = bitarray_init_zero(1023);
  535. for (i = 1; i < 64; ) {
  536. for (j = 0; j < 1023; ++j) {
  537. if (j % i)
  538. bitarray_set(ba, j);
  539. else
  540. bitarray_clear(ba, j);
  541. }
  542. for (j = 0; j < 1023; ++j) {
  543. if (!bool_eq(bitarray_is_set(ba, j), j%i))
  544. ok = 0;
  545. }
  546. tt_assert(ok);
  547. if (i < 7)
  548. ++i;
  549. else if (i == 28)
  550. i = 32;
  551. else
  552. i += 7;
  553. }
  554. done:
  555. if (ba)
  556. bitarray_free(ba);
  557. }
  558. /** Run unit tests for digest set code (implemented as a hashtable or as a
  559. * bloom filter) */
  560. static void
  561. test_container_digestset(void *arg)
  562. {
  563. smartlist_t *included = smartlist_new();
  564. char d[DIGEST_LEN];
  565. int i;
  566. int ok = 1;
  567. int false_positives = 0;
  568. digestset_t *set = NULL;
  569. (void)arg;
  570. for (i = 0; i < 1000; ++i) {
  571. crypto_rand(d, DIGEST_LEN);
  572. smartlist_add(included, tor_memdup(d, DIGEST_LEN));
  573. }
  574. set = digestset_new(1000);
  575. SMARTLIST_FOREACH(included, const char *, cp,
  576. if (digestset_contains(set, cp))
  577. ok = 0);
  578. tt_assert(ok);
  579. SMARTLIST_FOREACH(included, const char *, cp,
  580. digestset_add(set, cp));
  581. SMARTLIST_FOREACH(included, const char *, cp,
  582. if (!digestset_contains(set, cp))
  583. ok = 0);
  584. tt_assert(ok);
  585. for (i = 0; i < 1000; ++i) {
  586. crypto_rand(d, DIGEST_LEN);
  587. if (digestset_contains(set, d))
  588. ++false_positives;
  589. }
  590. tt_int_op(50, OP_GT, false_positives); /* Should be far lower. */
  591. done:
  592. if (set)
  593. digestset_free(set);
  594. SMARTLIST_FOREACH(included, char *, cp, tor_free(cp));
  595. smartlist_free(included);
  596. }
  597. typedef struct pq_entry_t {
  598. const char *val;
  599. int idx;
  600. } pq_entry_t;
  601. /** Helper: return a tristate based on comparing two pq_entry_t values. */
  602. static int
  603. compare_strings_for_pqueue_(const void *p1, const void *p2)
  604. {
  605. const pq_entry_t *e1=p1, *e2=p2;
  606. return strcmp(e1->val, e2->val);
  607. }
  608. /** Run unit tests for heap-based priority queue functions. */
  609. static void
  610. test_container_pqueue(void *arg)
  611. {
  612. smartlist_t *sl = smartlist_new();
  613. int (*cmp)(const void *, const void*);
  614. const int offset = STRUCT_OFFSET(pq_entry_t, idx);
  615. #define ENTRY(s) pq_entry_t s = { #s, -1 }
  616. ENTRY(cows);
  617. ENTRY(zebras);
  618. ENTRY(fish);
  619. ENTRY(frogs);
  620. ENTRY(apples);
  621. ENTRY(squid);
  622. ENTRY(daschunds);
  623. ENTRY(eggplants);
  624. ENTRY(weissbier);
  625. ENTRY(lobsters);
  626. ENTRY(roquefort);
  627. ENTRY(chinchillas);
  628. ENTRY(fireflies);
  629. #define OK() smartlist_pqueue_assert_ok(sl, cmp, offset)
  630. (void)arg;
  631. cmp = compare_strings_for_pqueue_;
  632. smartlist_pqueue_add(sl, cmp, offset, &cows);
  633. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  634. smartlist_pqueue_add(sl, cmp, offset, &fish);
  635. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  636. smartlist_pqueue_add(sl, cmp, offset, &apples);
  637. smartlist_pqueue_add(sl, cmp, offset, &squid);
  638. smartlist_pqueue_add(sl, cmp, offset, &daschunds);
  639. smartlist_pqueue_add(sl, cmp, offset, &eggplants);
  640. smartlist_pqueue_add(sl, cmp, offset, &weissbier);
  641. smartlist_pqueue_add(sl, cmp, offset, &lobsters);
  642. smartlist_pqueue_add(sl, cmp, offset, &roquefort);
  643. OK();
  644. tt_int_op(smartlist_len(sl),OP_EQ, 11);
  645. tt_ptr_op(smartlist_get(sl, 0),OP_EQ, &apples);
  646. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &apples);
  647. tt_int_op(smartlist_len(sl),OP_EQ, 10);
  648. OK();
  649. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &cows);
  650. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &daschunds);
  651. smartlist_pqueue_add(sl, cmp, offset, &chinchillas);
  652. OK();
  653. smartlist_pqueue_add(sl, cmp, offset, &fireflies);
  654. OK();
  655. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &chinchillas);
  656. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &eggplants);
  657. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &fireflies);
  658. OK();
  659. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &fish);
  660. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &frogs);
  661. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &lobsters);
  662. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &roquefort);
  663. OK();
  664. tt_int_op(smartlist_len(sl),OP_EQ, 3);
  665. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &squid);
  666. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &weissbier);
  667. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &zebras);
  668. tt_int_op(smartlist_len(sl),OP_EQ, 0);
  669. OK();
  670. /* Now test remove. */
  671. smartlist_pqueue_add(sl, cmp, offset, &cows);
  672. smartlist_pqueue_add(sl, cmp, offset, &fish);
  673. smartlist_pqueue_add(sl, cmp, offset, &frogs);
  674. smartlist_pqueue_add(sl, cmp, offset, &apples);
  675. smartlist_pqueue_add(sl, cmp, offset, &squid);
  676. smartlist_pqueue_add(sl, cmp, offset, &zebras);
  677. tt_int_op(smartlist_len(sl),OP_EQ, 6);
  678. OK();
  679. smartlist_pqueue_remove(sl, cmp, offset, &zebras);
  680. tt_int_op(smartlist_len(sl),OP_EQ, 5);
  681. OK();
  682. smartlist_pqueue_remove(sl, cmp, offset, &cows);
  683. tt_int_op(smartlist_len(sl),OP_EQ, 4);
  684. OK();
  685. smartlist_pqueue_remove(sl, cmp, offset, &apples);
  686. tt_int_op(smartlist_len(sl),OP_EQ, 3);
  687. OK();
  688. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &fish);
  689. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &frogs);
  690. tt_ptr_op(smartlist_pqueue_pop(sl, cmp, offset),OP_EQ, &squid);
  691. tt_int_op(smartlist_len(sl),OP_EQ, 0);
  692. OK();
  693. #undef OK
  694. done:
  695. smartlist_free(sl);
  696. }
  697. /** Run unit tests for string-to-void* map functions */
  698. static void
  699. test_container_strmap(void *arg)
  700. {
  701. strmap_t *map;
  702. strmap_iter_t *iter;
  703. const char *k;
  704. void *v;
  705. char *visited = NULL;
  706. smartlist_t *found_keys = NULL;
  707. char *v1 = tor_strdup("v1");
  708. char *v99 = tor_strdup("v99");
  709. char *v100 = tor_strdup("v100");
  710. char *v101 = tor_strdup("v101");
  711. char *v102 = tor_strdup("v102");
  712. char *v103 = tor_strdup("v103");
  713. char *v104 = tor_strdup("v104");
  714. char *v105 = tor_strdup("v105");
  715. (void)arg;
  716. map = strmap_new();
  717. tt_assert(map);
  718. tt_int_op(strmap_size(map),OP_EQ, 0);
  719. tt_assert(strmap_isempty(map));
  720. v = strmap_set(map, "K1", v99);
  721. tt_ptr_op(v,OP_EQ, NULL);
  722. tt_assert(!strmap_isempty(map));
  723. v = strmap_set(map, "K2", v101);
  724. tt_ptr_op(v,OP_EQ, NULL);
  725. v = strmap_set(map, "K1", v100);
  726. tt_ptr_op(v,OP_EQ, v99);
  727. tt_ptr_op(strmap_get(map,"K1"),OP_EQ, v100);
  728. tt_ptr_op(strmap_get(map,"K2"),OP_EQ, v101);
  729. tt_ptr_op(strmap_get(map,"K-not-there"),OP_EQ, NULL);
  730. strmap_assert_ok(map);
  731. v = strmap_remove(map,"K2");
  732. strmap_assert_ok(map);
  733. tt_ptr_op(v,OP_EQ, v101);
  734. tt_ptr_op(strmap_get(map,"K2"),OP_EQ, NULL);
  735. tt_ptr_op(strmap_remove(map,"K2"),OP_EQ, NULL);
  736. strmap_set(map, "K2", v101);
  737. strmap_set(map, "K3", v102);
  738. strmap_set(map, "K4", v103);
  739. tt_int_op(strmap_size(map),OP_EQ, 4);
  740. strmap_assert_ok(map);
  741. strmap_set(map, "K5", v104);
  742. strmap_set(map, "K6", v105);
  743. strmap_assert_ok(map);
  744. /* Test iterator. */
  745. iter = strmap_iter_init(map);
  746. found_keys = smartlist_new();
  747. while (!strmap_iter_done(iter)) {
  748. strmap_iter_get(iter,&k,&v);
  749. smartlist_add(found_keys, tor_strdup(k));
  750. tt_ptr_op(v,OP_EQ, strmap_get(map, k));
  751. if (!strcmp(k, "K2")) {
  752. iter = strmap_iter_next_rmv(map,iter);
  753. } else {
  754. iter = strmap_iter_next(map,iter);
  755. }
  756. }
  757. /* Make sure we removed K2, but not the others. */
  758. tt_ptr_op(strmap_get(map, "K2"),OP_EQ, NULL);
  759. tt_ptr_op(strmap_get(map, "K5"),OP_EQ, v104);
  760. /* Make sure we visited everyone once */
  761. smartlist_sort_strings(found_keys);
  762. visited = smartlist_join_strings(found_keys, ":", 0, NULL);
  763. tt_str_op(visited,OP_EQ, "K1:K2:K3:K4:K5:K6");
  764. strmap_assert_ok(map);
  765. /* Clean up after ourselves. */
  766. strmap_free(map, NULL);
  767. map = NULL;
  768. /* Now try some lc functions. */
  769. map = strmap_new();
  770. strmap_set_lc(map,"Ab.C", v1);
  771. tt_ptr_op(strmap_get(map,"ab.c"),OP_EQ, v1);
  772. strmap_assert_ok(map);
  773. tt_ptr_op(strmap_get_lc(map,"AB.C"),OP_EQ, v1);
  774. tt_ptr_op(strmap_get(map,"AB.C"),OP_EQ, NULL);
  775. tt_ptr_op(strmap_remove_lc(map,"aB.C"),OP_EQ, v1);
  776. strmap_assert_ok(map);
  777. tt_ptr_op(strmap_get_lc(map,"AB.C"),OP_EQ, NULL);
  778. done:
  779. if (map)
  780. strmap_free(map,NULL);
  781. if (found_keys) {
  782. SMARTLIST_FOREACH(found_keys, char *, cp, tor_free(cp));
  783. smartlist_free(found_keys);
  784. }
  785. tor_free(visited);
  786. tor_free(v1);
  787. tor_free(v99);
  788. tor_free(v100);
  789. tor_free(v101);
  790. tor_free(v102);
  791. tor_free(v103);
  792. tor_free(v104);
  793. tor_free(v105);
  794. }
  795. /** Run unit tests for getting the median of a list. */
  796. static void
  797. test_container_order_functions(void *arg)
  798. {
  799. int lst[25], n = 0;
  800. uint32_t lst_2[25];
  801. // int a=12,b=24,c=25,d=60,e=77;
  802. #define median() median_int(lst, n)
  803. (void)arg;
  804. lst[n++] = 12;
  805. tt_int_op(12,OP_EQ, median()); /* 12 */
  806. lst[n++] = 77;
  807. //smartlist_shuffle(sl);
  808. tt_int_op(12,OP_EQ, median()); /* 12, 77 */
  809. lst[n++] = 77;
  810. //smartlist_shuffle(sl);
  811. tt_int_op(77,OP_EQ, median()); /* 12, 77, 77 */
  812. lst[n++] = 24;
  813. tt_int_op(24,OP_EQ, median()); /* 12,24,77,77 */
  814. lst[n++] = 60;
  815. lst[n++] = 12;
  816. lst[n++] = 25;
  817. //smartlist_shuffle(sl);
  818. tt_int_op(25,OP_EQ, median()); /* 12,12,24,25,60,77,77 */
  819. #undef median
  820. #define third_quartile() third_quartile_uint32(lst_2, n)
  821. n = 0;
  822. lst_2[n++] = 1;
  823. tt_int_op(1,OP_EQ, third_quartile()); /* ~1~ */
  824. lst_2[n++] = 2;
  825. tt_int_op(2,OP_EQ, third_quartile()); /* 1, ~2~ */
  826. lst_2[n++] = 3;
  827. lst_2[n++] = 4;
  828. lst_2[n++] = 5;
  829. tt_int_op(4,OP_EQ, third_quartile()); /* 1, 2, 3, ~4~, 5 */
  830. lst_2[n++] = 6;
  831. lst_2[n++] = 7;
  832. lst_2[n++] = 8;
  833. lst_2[n++] = 9;
  834. tt_int_op(7,OP_EQ, third_quartile()); /* 1, 2, 3, 4, 5, 6, ~7~, 8, 9 */
  835. lst_2[n++] = 10;
  836. lst_2[n++] = 11;
  837. /* 1, 2, 3, 4, 5, 6, 7, 8, ~9~, 10, 11 */
  838. tt_int_op(9,OP_EQ, third_quartile());
  839. #undef third_quartile
  840. double dbls[] = { 1.0, 10.0, 100.0, 1e4, 1e5, 1e6 };
  841. tt_double_eq(1.0, median_double(dbls, 1));
  842. tt_double_eq(1.0, median_double(dbls, 2));
  843. tt_double_eq(10.0, median_double(dbls, 3));
  844. tt_double_eq(10.0, median_double(dbls, 4));
  845. tt_double_eq(100.0, median_double(dbls, 5));
  846. tt_double_eq(100.0, median_double(dbls, 6));
  847. time_t times[] = { 5, 10, 20, 25, 15 };
  848. tt_assert(5 == median_time(times, 1));
  849. tt_assert(5 == median_time(times, 2));
  850. tt_assert(10 == median_time(times, 3));
  851. tt_assert(10 == median_time(times, 4));
  852. tt_assert(15 == median_time(times, 5));
  853. int32_t int32s[] = { -5, -10, -50, 100 };
  854. tt_int_op(-5, ==, median_int32(int32s, 1));
  855. tt_int_op(-10, ==, median_int32(int32s, 2));
  856. tt_int_op(-10, ==, median_int32(int32s, 3));
  857. tt_int_op(-10, ==, median_int32(int32s, 4));
  858. long longs[] = { -30, 30, 100, -100, 7 };
  859. tt_int_op(7, ==, find_nth_long(longs, 5, 2));
  860. done:
  861. ;
  862. }
  863. static void
  864. test_container_di_map(void *arg)
  865. {
  866. di_digest256_map_t *map = NULL;
  867. const uint8_t key1[] = "In view of the fact that it was ";
  868. const uint8_t key2[] = "superficially convincing, being ";
  869. const uint8_t key3[] = "properly enciphered in a one-tim";
  870. const uint8_t key4[] = "e cipher scheduled for use today";
  871. char *v1 = tor_strdup(", it came close to causing a disaster...");
  872. char *v2 = tor_strdup("I regret to have to advise you that the mission");
  873. char *v3 = tor_strdup("was actually initiated...");
  874. /* -- John Brunner, _The Shockwave Rider_ */
  875. (void)arg;
  876. /* Try searching on an empty map. */
  877. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key1, NULL));
  878. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key2, NULL));
  879. tt_ptr_op(v3, OP_EQ, dimap_search(map, key2, v3));
  880. dimap_free(map, NULL);
  881. map = NULL;
  882. /* Add a single entry. */
  883. dimap_add_entry(&map, key1, v1);
  884. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key2, NULL));
  885. tt_ptr_op(v3, OP_EQ, dimap_search(map, key2, v3));
  886. tt_ptr_op(v1, OP_EQ, dimap_search(map, key1, NULL));
  887. /* Now try it with three entries in the map. */
  888. dimap_add_entry(&map, key2, v2);
  889. dimap_add_entry(&map, key3, v3);
  890. tt_ptr_op(v1, OP_EQ, dimap_search(map, key1, NULL));
  891. tt_ptr_op(v2, OP_EQ, dimap_search(map, key2, NULL));
  892. tt_ptr_op(v3, OP_EQ, dimap_search(map, key3, NULL));
  893. tt_ptr_op(NULL, OP_EQ, dimap_search(map, key4, NULL));
  894. tt_ptr_op(v1, OP_EQ, dimap_search(map, key4, v1));
  895. done:
  896. tor_free(v1);
  897. tor_free(v2);
  898. tor_free(v3);
  899. dimap_free(map, NULL);
  900. }
  901. /** Run unit tests for fp_pair-to-void* map functions */
  902. static void
  903. test_container_fp_pair_map(void *arg)
  904. {
  905. fp_pair_map_t *map;
  906. fp_pair_t fp1, fp2, fp3, fp4, fp5, fp6;
  907. void *v;
  908. fp_pair_map_iter_t *iter;
  909. fp_pair_t k;
  910. char *v99 = tor_strdup("99");
  911. char *v100 = tor_strdup("v100");
  912. char *v101 = tor_strdup("v101");
  913. char *v102 = tor_strdup("v102");
  914. char *v103 = tor_strdup("v103");
  915. char *v104 = tor_strdup("v104");
  916. char *v105 = tor_strdup("v105");
  917. (void)arg;
  918. map = fp_pair_map_new();
  919. tt_assert(map);
  920. tt_int_op(fp_pair_map_size(map),OP_EQ, 0);
  921. tt_assert(fp_pair_map_isempty(map));
  922. memset(fp1.first, 0x11, DIGEST_LEN);
  923. memset(fp1.second, 0x12, DIGEST_LEN);
  924. memset(fp2.first, 0x21, DIGEST_LEN);
  925. memset(fp2.second, 0x22, DIGEST_LEN);
  926. memset(fp3.first, 0x31, DIGEST_LEN);
  927. memset(fp3.second, 0x32, DIGEST_LEN);
  928. memset(fp4.first, 0x41, DIGEST_LEN);
  929. memset(fp4.second, 0x42, DIGEST_LEN);
  930. memset(fp5.first, 0x51, DIGEST_LEN);
  931. memset(fp5.second, 0x52, DIGEST_LEN);
  932. memset(fp6.first, 0x61, DIGEST_LEN);
  933. memset(fp6.second, 0x62, DIGEST_LEN);
  934. v = fp_pair_map_set(map, &fp1, v99);
  935. tt_ptr_op(v, OP_EQ, NULL);
  936. tt_assert(!fp_pair_map_isempty(map));
  937. v = fp_pair_map_set(map, &fp2, v101);
  938. tt_ptr_op(v, OP_EQ, NULL);
  939. v = fp_pair_map_set(map, &fp1, v100);
  940. tt_ptr_op(v, OP_EQ, v99);
  941. tt_ptr_op(fp_pair_map_get(map, &fp1),OP_EQ, v100);
  942. tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, v101);
  943. tt_ptr_op(fp_pair_map_get(map, &fp3),OP_EQ, NULL);
  944. fp_pair_map_assert_ok(map);
  945. v = fp_pair_map_remove(map, &fp2);
  946. fp_pair_map_assert_ok(map);
  947. tt_ptr_op(v,OP_EQ, v101);
  948. tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL);
  949. tt_ptr_op(fp_pair_map_remove(map, &fp2),OP_EQ, NULL);
  950. fp_pair_map_set(map, &fp2, v101);
  951. fp_pair_map_set(map, &fp3, v102);
  952. fp_pair_map_set(map, &fp4, v103);
  953. tt_int_op(fp_pair_map_size(map),OP_EQ, 4);
  954. fp_pair_map_assert_ok(map);
  955. fp_pair_map_set(map, &fp5, v104);
  956. fp_pair_map_set(map, &fp6, v105);
  957. fp_pair_map_assert_ok(map);
  958. /* Test iterator. */
  959. iter = fp_pair_map_iter_init(map);
  960. while (!fp_pair_map_iter_done(iter)) {
  961. fp_pair_map_iter_get(iter, &k, &v);
  962. tt_ptr_op(v,OP_EQ, fp_pair_map_get(map, &k));
  963. if (tor_memeq(&fp2, &k, sizeof(fp2))) {
  964. iter = fp_pair_map_iter_next_rmv(map, iter);
  965. } else {
  966. iter = fp_pair_map_iter_next(map, iter);
  967. }
  968. }
  969. /* Make sure we removed fp2, but not the others. */
  970. tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL);
  971. tt_ptr_op(fp_pair_map_get(map, &fp5),OP_EQ, v104);
  972. fp_pair_map_assert_ok(map);
  973. /* Clean up after ourselves. */
  974. fp_pair_map_free(map, NULL);
  975. map = NULL;
  976. done:
  977. if (map)
  978. fp_pair_map_free(map, NULL);
  979. tor_free(v99);
  980. tor_free(v100);
  981. tor_free(v101);
  982. tor_free(v102);
  983. tor_free(v103);
  984. tor_free(v104);
  985. tor_free(v105);
  986. }
  987. static void
  988. test_container_smartlist_most_frequent(void *arg)
  989. {
  990. (void) arg;
  991. smartlist_t *sl = smartlist_new();
  992. int count = -1;
  993. const char *cp;
  994. cp = smartlist_get_most_frequent_string_(sl, &count);
  995. tt_int_op(count, ==, 0);
  996. tt_ptr_op(cp, ==, NULL);
  997. /* String must be sorted before we call get_most_frequent */
  998. smartlist_split_string(sl, "abc:def:ghi", ":", 0, 0);
  999. cp = smartlist_get_most_frequent_string_(sl, &count);
  1000. tt_int_op(count, ==, 1);
  1001. tt_str_op(cp, ==, "ghi"); /* Ties broken in favor of later element */
  1002. smartlist_split_string(sl, "def:ghi", ":", 0, 0);
  1003. smartlist_sort_strings(sl);
  1004. cp = smartlist_get_most_frequent_string_(sl, &count);
  1005. tt_int_op(count, ==, 2);
  1006. tt_ptr_op(cp, !=, NULL);
  1007. tt_str_op(cp, ==, "ghi"); /* Ties broken in favor of later element */
  1008. smartlist_split_string(sl, "def:abc:qwop", ":", 0, 0);
  1009. smartlist_sort_strings(sl);
  1010. cp = smartlist_get_most_frequent_string_(sl, &count);
  1011. tt_int_op(count, ==, 3);
  1012. tt_ptr_op(cp, !=, NULL);
  1013. tt_str_op(cp, ==, "def"); /* No tie */
  1014. done:
  1015. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  1016. smartlist_free(sl);
  1017. }
  1018. static void
  1019. test_container_smartlist_sort_ptrs(void *arg)
  1020. {
  1021. (void)arg;
  1022. int array[10];
  1023. int *arrayptrs[11];
  1024. smartlist_t *sl = smartlist_new();
  1025. unsigned i=0, j;
  1026. for (j = 0; j < ARRAY_LENGTH(array); ++j) {
  1027. smartlist_add(sl, &array[j]);
  1028. arrayptrs[i++] = &array[j];
  1029. if (j == 5) {
  1030. smartlist_add(sl, &array[j]);
  1031. arrayptrs[i++] = &array[j];
  1032. }
  1033. }
  1034. for (i = 0; i < 10; ++i) {
  1035. smartlist_shuffle(sl);
  1036. smartlist_sort_pointers(sl);
  1037. for (j = 0; j < ARRAY_LENGTH(arrayptrs); ++j) {
  1038. tt_ptr_op(smartlist_get(sl, j), ==, arrayptrs[j]);
  1039. }
  1040. }
  1041. done:
  1042. smartlist_free(sl);
  1043. }
  1044. static void
  1045. test_container_smartlist_strings_eq(void *arg)
  1046. {
  1047. (void)arg;
  1048. smartlist_t *sl1 = smartlist_new();
  1049. smartlist_t *sl2 = smartlist_new();
  1050. #define EQ_SHOULD_SAY(s1,s2,val) \
  1051. do { \
  1052. SMARTLIST_FOREACH(sl1, char *, cp, tor_free(cp)); \
  1053. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp)); \
  1054. smartlist_clear(sl1); \
  1055. smartlist_clear(sl2); \
  1056. smartlist_split_string(sl1, (s1), ":", 0, 0); \
  1057. smartlist_split_string(sl2, (s2), ":", 0, 0); \
  1058. tt_int_op((val), OP_EQ, smartlist_strings_eq(sl1, sl2)); \
  1059. } while (0)
  1060. /* Both NULL, so equal */
  1061. tt_int_op(1, ==, smartlist_strings_eq(NULL, NULL));
  1062. /* One NULL, not equal. */
  1063. tt_int_op(0, ==, smartlist_strings_eq(NULL, sl1));
  1064. tt_int_op(0, ==, smartlist_strings_eq(sl1, NULL));
  1065. /* Both empty, both equal. */
  1066. EQ_SHOULD_SAY("", "", 1);
  1067. /* One empty, not equal */
  1068. EQ_SHOULD_SAY("", "ab", 0);
  1069. EQ_SHOULD_SAY("", "xy:z", 0);
  1070. EQ_SHOULD_SAY("abc", "", 0);
  1071. EQ_SHOULD_SAY("abc:cd", "", 0);
  1072. /* Different lengths, not equal. */
  1073. EQ_SHOULD_SAY("hello:world", "hello", 0);
  1074. EQ_SHOULD_SAY("hello", "hello:friends", 0);
  1075. /* Same lengths, not equal */
  1076. EQ_SHOULD_SAY("Hello:world", "goodbye:world", 0);
  1077. EQ_SHOULD_SAY("Hello:world", "Hello:stars", 0);
  1078. /* Actually equal */
  1079. EQ_SHOULD_SAY("ABC", "ABC", 1);
  1080. EQ_SHOULD_SAY(" ab : cd : e", " ab : cd : e", 1);
  1081. done:
  1082. SMARTLIST_FOREACH(sl1, char *, cp, tor_free(cp));
  1083. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  1084. smartlist_free(sl1);
  1085. smartlist_free(sl2);
  1086. }
  1087. #define CONTAINER_LEGACY(name) \
  1088. { #name, test_container_ ## name , 0, NULL, NULL }
  1089. #define CONTAINER(name, flags) \
  1090. { #name, test_container_ ## name, (flags), NULL, NULL }
  1091. struct testcase_t container_tests[] = {
  1092. CONTAINER_LEGACY(smartlist_basic),
  1093. CONTAINER_LEGACY(smartlist_strings),
  1094. CONTAINER_LEGACY(smartlist_overlap),
  1095. CONTAINER_LEGACY(smartlist_digests),
  1096. CONTAINER_LEGACY(smartlist_join),
  1097. CONTAINER_LEGACY(smartlist_pos),
  1098. CONTAINER(smartlist_ints_eq, 0),
  1099. CONTAINER_LEGACY(bitarray),
  1100. CONTAINER_LEGACY(digestset),
  1101. CONTAINER_LEGACY(strmap),
  1102. CONTAINER_LEGACY(pqueue),
  1103. CONTAINER_LEGACY(order_functions),
  1104. CONTAINER(di_map, 0),
  1105. CONTAINER_LEGACY(fp_pair_map),
  1106. CONTAINER(smartlist_most_frequent, 0),
  1107. CONTAINER(smartlist_sort_ptrs, 0),
  1108. CONTAINER(smartlist_strings_eq, 0),
  1109. END_OF_TESTCASES
  1110. };