test_containers.c 35 KB

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