test_containers.c 33 KB

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