test_containers.c 43 KB

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