test_containers.c 40 KB

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