test_containers.c 40 KB

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