test_astobj2.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief astobj2 test module
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend>TEST_FRAMEWORK</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/test.h"
  32. #include "asterisk/astobj2.h"
  33. /* Uncomment the following line to dump the container contents during tests. */
  34. //#define TEST_CONTAINER_DEBUG_DUMP 1
  35. enum test_container_type {
  36. TEST_CONTAINER_LIST,
  37. TEST_CONTAINER_HASH,
  38. TEST_CONTAINER_RBTREE,
  39. };
  40. /*!
  41. * \internal
  42. * \brief Convert the container type enum to string.
  43. * \since 12.0.0
  44. *
  45. * \param type Container type value to convert to string.
  46. *
  47. * \return String value of container type.
  48. */
  49. static const char *test_container2str(enum test_container_type type)
  50. {
  51. const char *c_type;
  52. c_type = "Unknown";
  53. switch (type) {
  54. case TEST_CONTAINER_LIST:
  55. c_type = "List";
  56. break;
  57. case TEST_CONTAINER_HASH:
  58. c_type = "Hash";
  59. break;
  60. case TEST_CONTAINER_RBTREE:
  61. c_type = "RBTree";
  62. break;
  63. }
  64. return c_type;
  65. }
  66. struct test_obj {
  67. /*! What to decrement when object is destroyed. */
  68. int *destructor_count;
  69. /*! Container object key */
  70. int i;
  71. /*! Identifier for duplicate object key tests. */
  72. int dup_number;
  73. };
  74. /*! Partial search key +/- matching range. */
  75. int partial_key_match_range;
  76. static void test_obj_destructor(void *v_obj)
  77. {
  78. struct test_obj *obj = (struct test_obj *) v_obj;
  79. if (obj->destructor_count) {
  80. --*obj->destructor_count;
  81. }
  82. }
  83. static int increment_cb(void *obj, void *arg, int flag)
  84. {
  85. int *i = (int *) arg;
  86. *i = *i + 1;
  87. return 0;
  88. }
  89. static int all_but_one_cb(void *obj, void *arg, int flag)
  90. {
  91. struct test_obj *cmp_obj = (struct test_obj *) obj;
  92. return (cmp_obj->i) ? CMP_MATCH : 0;
  93. }
  94. static int multiple_cb(void *obj, void *arg, int flag)
  95. {
  96. int *i = (int *) arg;
  97. struct test_obj *cmp_obj = (struct test_obj *) obj;
  98. return (cmp_obj->i < *i) ? CMP_MATCH : 0;
  99. }
  100. static int test_cmp_cb(void *obj, void *arg, int flags)
  101. {
  102. struct test_obj *cmp_obj = (struct test_obj *) obj;
  103. if (flags & OBJ_KEY) {
  104. int *i = (int *) arg;
  105. return (cmp_obj->i == *i) ? CMP_MATCH : 0;
  106. } else if (flags & OBJ_PARTIAL_KEY) {
  107. int *i = (int *) arg;
  108. return (*i - partial_key_match_range <= cmp_obj->i
  109. && cmp_obj->i <= *i + partial_key_match_range) ? CMP_MATCH : 0;
  110. } else {
  111. struct test_obj *arg_obj = (struct test_obj *) arg;
  112. return (cmp_obj->i == arg_obj->i) ? CMP_MATCH : 0;
  113. }
  114. }
  115. static int test_hash_cb(const void *obj, const int flags)
  116. {
  117. if (flags & OBJ_KEY) {
  118. const int *i = obj;
  119. return *i;
  120. } else if (flags & OBJ_PARTIAL_KEY) {
  121. /* This is absolutely wrong to be called with this flag value. */
  122. abort();
  123. /* Just in case abort() doesn't work or something else super silly */
  124. *((int *) 0) = 0;
  125. return 0;
  126. } else {
  127. const struct test_obj *hash_obj = obj;
  128. return hash_obj->i;
  129. }
  130. }
  131. static int test_sort_cb(const void *obj_left, const void *obj_right, int flags)
  132. {
  133. const struct test_obj *test_left = obj_left;
  134. if (flags & OBJ_KEY) {
  135. const int *i = obj_right;
  136. return test_left->i - *i;
  137. } else if (flags & OBJ_PARTIAL_KEY) {
  138. int *i = (int *) obj_right;
  139. if (*i - partial_key_match_range <= test_left->i
  140. && test_left->i <= *i + partial_key_match_range) {
  141. return 0;
  142. }
  143. return test_left->i - *i;
  144. } else {
  145. const struct test_obj *test_right = obj_right;
  146. return test_left->i - test_right->i;
  147. }
  148. }
  149. #if defined(TEST_CONTAINER_DEBUG_DUMP)
  150. /*!
  151. * \internal
  152. * \brief Print test object key.
  153. * \since 12.0.0
  154. *
  155. * \param v_obj A pointer to the object we want the key printed.
  156. * \param where User data needed by prnt to determine where to put output.
  157. * \param prnt Print output callback function to use.
  158. */
  159. static void test_prnt_obj(void *v_obj, void *where, ao2_prnt_fn *prnt)
  160. {
  161. struct test_obj *obj = v_obj;
  162. if (!obj) {
  163. return;
  164. }
  165. prnt(where, "%6d-%d", obj->i, obj->dup_number);
  166. }
  167. #endif /* defined(TEST_CONTAINER_DEBUG_DUMP) */
  168. /*!
  169. * \internal
  170. * \brief Test container cloning.
  171. * \since 12.0.0
  172. *
  173. * \param res Passed in enum ast_test_result_state.
  174. * \param orig Container to clone.
  175. * \param test Test output controller.
  176. *
  177. * \return enum ast_test_result_state
  178. */
  179. static int test_container_clone(int res, struct ao2_container *orig, struct ast_test *test)
  180. {
  181. struct ao2_container *clone;
  182. struct test_obj *obj;
  183. struct test_obj *obj2;
  184. struct ao2_iterator iter;
  185. clone = ao2_container_clone(orig, 0);
  186. if (!clone) {
  187. ast_test_status_update(test, "ao2_container_clone failed.\n");
  188. return AST_TEST_FAIL;
  189. }
  190. if (ao2_container_check(clone, 0)) {
  191. ast_test_status_update(test, "container integrity check failed\n");
  192. res = AST_TEST_FAIL;
  193. } else if (ao2_container_count(orig) != ao2_container_count(clone)) {
  194. ast_test_status_update(test, "Cloned container does not have the same number of objects.\n");
  195. res = AST_TEST_FAIL;
  196. } else {
  197. iter = ao2_iterator_init(orig, 0);
  198. for (; (obj = ao2_t_iterator_next(&iter, "test orig")); ao2_t_ref(obj, -1, "test orig")) {
  199. /*
  200. * Unlink the matching object from the cloned container to make
  201. * the next search faster. This is a big speed optimization!
  202. */
  203. obj2 = ao2_t_callback(clone, OBJ_POINTER | OBJ_UNLINK, ao2_match_by_addr, obj,
  204. "test clone");
  205. if (obj2) {
  206. ao2_t_ref(obj2, -1, "test clone");
  207. continue;
  208. }
  209. ast_test_status_update(test,
  210. "Orig container has an object %p not in the clone container.\n", obj);
  211. res = AST_TEST_FAIL;
  212. }
  213. ao2_iterator_destroy(&iter);
  214. if (ao2_container_count(clone)) {
  215. ast_test_status_update(test, "Cloned container still has objects.\n");
  216. res = AST_TEST_FAIL;
  217. }
  218. if (ao2_container_check(clone, 0)) {
  219. ast_test_status_update(test, "container integrity check failed\n");
  220. res = AST_TEST_FAIL;
  221. }
  222. }
  223. ao2_t_ref(clone, -1, "bye clone");
  224. return res;
  225. }
  226. /*!
  227. * \internal
  228. * \brief Test ao2_find with no flags.
  229. * \since 12.0.0
  230. *
  231. * \param res Passed in enum ast_test_result_state.
  232. * \param look_in Container to search.
  233. * \param limit Container contains objects 0 - (limit - 1).
  234. * \param test Test output controller.
  235. *
  236. * \return enum ast_test_result_state
  237. */
  238. static int test_ao2_find_w_no_flags(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
  239. {
  240. int i;
  241. int num;
  242. struct test_obj tmp_obj = { 0, };
  243. struct test_obj *obj;
  244. for (num = 100; num--;) {
  245. i = ast_random() % limit; /* find a random object */
  246. tmp_obj.i = i;
  247. obj = ao2_find(look_in, &tmp_obj, 0);
  248. if (!obj) {
  249. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with no flags failed.\n", i);
  250. res = AST_TEST_FAIL;
  251. } else {
  252. if (obj->i != i) {
  253. ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
  254. res = AST_TEST_FAIL;
  255. }
  256. ao2_t_ref(obj, -1, "test");
  257. }
  258. }
  259. return res;
  260. }
  261. /*!
  262. * \internal
  263. * \brief Test ao2_find with OBJ_POINTER.
  264. * \since 12.0.0
  265. *
  266. * \param res Passed in enum ast_test_result_state.
  267. * \param look_in Container to search.
  268. * \param limit Container contains objects 0 - (limit - 1).
  269. * \param test Test output controller.
  270. *
  271. * \return enum ast_test_result_state
  272. */
  273. static int test_ao2_find_w_OBJ_POINTER(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
  274. {
  275. int i;
  276. int num;
  277. struct test_obj tmp_obj = { 0, };
  278. struct test_obj *obj;
  279. for (num = 75; num--;) {
  280. i = ast_random() % limit; /* find a random object */
  281. tmp_obj.i = i;
  282. obj = ao2_find(look_in, &tmp_obj, OBJ_POINTER);
  283. if (!obj) {
  284. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_POINTER flag failed.\n", i);
  285. res = AST_TEST_FAIL;
  286. } else {
  287. if (obj->i != i) {
  288. ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
  289. res = AST_TEST_FAIL;
  290. }
  291. ao2_t_ref(obj, -1, "test");
  292. }
  293. }
  294. return res;
  295. }
  296. /*!
  297. * \internal
  298. * \brief Test ao2_find with OBJ_KEY.
  299. * \since 12.0.0
  300. *
  301. * \param res Passed in enum ast_test_result_state.
  302. * \param look_in Container to search.
  303. * \param limit Container contains objects 0 - (limit - 1).
  304. * \param test Test output controller.
  305. *
  306. * \return enum ast_test_result_state
  307. */
  308. static int test_ao2_find_w_OBJ_KEY(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
  309. {
  310. int i;
  311. int num;
  312. struct test_obj *obj;
  313. for (num = 75; num--;) {
  314. i = ast_random() % limit; /* find a random object */
  315. obj = ao2_find(look_in, &i, OBJ_KEY);
  316. if (!obj) {
  317. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_KEY flag failed.\n", i);
  318. res = AST_TEST_FAIL;
  319. } else {
  320. if (obj->i != i) {
  321. ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
  322. res = AST_TEST_FAIL;
  323. }
  324. ao2_t_ref(obj, -1, "test");
  325. }
  326. }
  327. return res;
  328. }
  329. /*!
  330. * \internal
  331. * \brief Test ao2_find with OBJ_PARTIAL_KEY.
  332. * \since 12.0.0
  333. *
  334. * \param res Passed in enum ast_test_result_state.
  335. * \param look_in Container to search.
  336. * \param limit Container contains objects 0 - (limit - 1).
  337. * \param test Test output controller.
  338. *
  339. * \return enum ast_test_result_state
  340. */
  341. static int test_ao2_find_w_OBJ_PARTIAL_KEY(int res, struct ao2_container *look_in, int limit, struct ast_test *test)
  342. {
  343. int i;
  344. int num;
  345. struct test_obj *obj;
  346. /* Set partial match to find exactly. */
  347. partial_key_match_range = 0;
  348. for (num = 100; num--;) {
  349. i = ast_random() % limit; /* find a random object */
  350. obj = ao2_find(look_in, &i, OBJ_PARTIAL_KEY);
  351. if (!obj) {
  352. ast_test_status_update(test, "COULD NOT FIND:%d, ao2_find() with OBJ_PARTIAL_KEY flag failed.\n", i);
  353. res = AST_TEST_FAIL;
  354. } else {
  355. if (obj->i != i) {
  356. ast_test_status_update(test, "object %d does not match %d\n", obj->i, i);
  357. res = AST_TEST_FAIL;
  358. }
  359. ao2_t_ref(obj, -1, "test");
  360. }
  361. }
  362. return res;
  363. }
  364. static int astobj2_test_1_helper(int tst_num, enum test_container_type type, int use_sort, unsigned int lim, struct ast_test *test)
  365. {
  366. const char *c_type;
  367. struct ao2_container *c1;
  368. struct ao2_container *c2;
  369. struct ao2_iterator it;
  370. struct ao2_iterator *mult_it;
  371. struct test_obj *obj;
  372. int n_buckets = 0;
  373. int increment = 0;
  374. int destructor_count = 0;
  375. int num;
  376. int res = AST_TEST_PASS;
  377. c_type = test_container2str(type);
  378. ast_test_status_update(test, "Test %d, %s containers (%s).\n",
  379. tst_num, c_type, use_sort ? "sorted" : "non-sorted");
  380. c1 = NULL;
  381. switch (type) {
  382. case TEST_CONTAINER_LIST:
  383. /* Lists just have one bucket. */
  384. n_buckets = 1;
  385. c1 = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  386. use_sort ? test_sort_cb : NULL, test_cmp_cb, "test");
  387. break;
  388. case TEST_CONTAINER_HASH:
  389. n_buckets = (ast_random() % ((lim / 4) + 1)) + 1;
  390. c1 = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, n_buckets,
  391. test_hash_cb, use_sort ? test_sort_cb : NULL, test_cmp_cb, "test");
  392. break;
  393. case TEST_CONTAINER_RBTREE:
  394. /* RBTrees just have one bucket. */
  395. n_buckets = 1;
  396. c1 = ao2_t_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  397. test_sort_cb, test_cmp_cb, "test");
  398. break;
  399. }
  400. c2 = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL, "test");
  401. if (!c1 || !c2) {
  402. ast_test_status_update(test, "ao2_container_alloc failed.\n");
  403. res = AST_TEST_FAIL;
  404. goto cleanup;
  405. }
  406. /* Create objects and link into container */
  407. for (num = 0; num < lim; ++num) {
  408. if (!(obj = ao2_t_alloc(sizeof(struct test_obj), test_obj_destructor, "making zombies"))) {
  409. ast_test_status_update(test, "ao2_alloc failed.\n");
  410. res = AST_TEST_FAIL;
  411. goto cleanup;
  412. }
  413. ++destructor_count;
  414. obj->destructor_count = &destructor_count;
  415. obj->i = num;
  416. ao2_link(c1, obj);
  417. ao2_t_ref(obj, -1, "test");
  418. if (ao2_container_check(c1, 0)) {
  419. ast_test_status_update(test, "container integrity check failed linking obj num:%d\n", num);
  420. res = AST_TEST_FAIL;
  421. goto cleanup;
  422. }
  423. if (ao2_container_count(c1) != num + 1) {
  424. ast_test_status_update(test, "container did not link correctly\n");
  425. res = AST_TEST_FAIL;
  426. }
  427. }
  428. ast_test_status_update(test, "%s container created: buckets: %d, items: %u\n",
  429. c_type, n_buckets, lim);
  430. /* Testing ao2_container_clone */
  431. res = test_container_clone(res, c1, test);
  432. /* Testing ao2_find with no flags */
  433. res = test_ao2_find_w_no_flags(res, c1, lim, test);
  434. /* Testing ao2_find with OBJ_POINTER */
  435. res = test_ao2_find_w_OBJ_POINTER(res, c1, lim, test);
  436. /* Testing ao2_find with OBJ_KEY */
  437. res = test_ao2_find_w_OBJ_KEY(res, c1, lim, test);
  438. /* Testing ao2_find with OBJ_PARTIAL_KEY */
  439. res = test_ao2_find_w_OBJ_PARTIAL_KEY(res, c1, lim, test);
  440. /* Test Callback with no flags. */
  441. increment = 0;
  442. ao2_t_callback(c1, 0, increment_cb, &increment, "test callback");
  443. if (increment != lim) {
  444. ast_test_status_update(test, "callback with no flags failed. Increment is %d\n", increment);
  445. res = AST_TEST_FAIL;
  446. }
  447. /* Test Callback with OBJ_NODATA. This should do nothing different than with no flags here. */
  448. increment = 0;
  449. ao2_t_callback(c1, OBJ_NODATA, increment_cb, &increment, "test callback");
  450. if (increment != lim) {
  451. ast_test_status_update(test, "callback with OBJ_NODATA failed. Increment is %d\n", increment);
  452. res = AST_TEST_FAIL;
  453. }
  454. /* Test OBJ_MULTIPLE with OBJ_UNLINK, add items back afterwards */
  455. num = lim < 25 ? lim : 25;
  456. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK, multiple_cb, &num, "test multiple"))) {
  457. ast_test_status_update(test, "OBJ_MULTIPLE with OBJ_UNLINK test failed.\n");
  458. res = AST_TEST_FAIL;
  459. } else {
  460. /* make sure num items unlinked is as expected */
  461. if ((lim - ao2_container_count(c1)) != num) {
  462. ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK test failed, did not unlink correct number of objects.\n");
  463. res = AST_TEST_FAIL;
  464. }
  465. if (ao2_container_check(c1, 0)) {
  466. ast_test_status_update(test, "container integrity check failed\n");
  467. res = AST_TEST_FAIL;
  468. goto cleanup;
  469. }
  470. /* link what was unlinked back into c1 */
  471. while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
  472. ao2_t_link(c1, obj, "test");
  473. ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
  474. }
  475. ao2_iterator_destroy(mult_it);
  476. if (ao2_container_check(c1, 0)) {
  477. ast_test_status_update(test, "container integrity check failed\n");
  478. res = AST_TEST_FAIL;
  479. goto cleanup;
  480. }
  481. }
  482. /* Test OBJ_MULTIPLE without unlink and iterate the returned container */
  483. num = 5;
  484. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
  485. ast_test_status_update(test, "OBJ_MULTIPLE without OBJ_UNLINK test failed.\n");
  486. res = AST_TEST_FAIL;
  487. } else {
  488. while ((obj = ao2_t_iterator_next(mult_it, "test"))) {
  489. ao2_t_ref(obj, -1, "test"); /* remove ref from iterator */
  490. }
  491. ao2_iterator_destroy(mult_it);
  492. }
  493. /* Test OBJ_MULTIPLE without unlink and no iterating */
  494. num = 5;
  495. if (!(mult_it = ao2_t_callback(c1, OBJ_MULTIPLE, multiple_cb, &num, "test multiple"))) {
  496. ast_test_status_update(test, "OBJ_MULTIPLE with no OBJ_UNLINK and no iterating failed.\n");
  497. res = AST_TEST_FAIL;
  498. } else {
  499. ao2_iterator_destroy(mult_it);
  500. }
  501. /* Is the container count what we expect after all the finds and unlinks? */
  502. if (ao2_container_count(c1) != lim) {
  503. ast_test_status_update(test, "container count does not match what is expected after ao2_find tests.\n");
  504. res = AST_TEST_FAIL;
  505. }
  506. /* Testing iterator. Unlink a single object and break. do not add item back */
  507. it = ao2_iterator_init(c1, 0);
  508. num = ast_random() % lim; /* remove a random object */
  509. if (!num) {
  510. /*
  511. * Well we cannot remove object zero because of test with
  512. * all_but_one_cb later.
  513. */
  514. num = 1;
  515. }
  516. while ((obj = ao2_t_iterator_next(&it, "test"))) {
  517. if (obj->i == num) {
  518. ao2_t_unlink(c1, obj, "test");
  519. ao2_t_ref(obj, -1, "test");
  520. break;
  521. }
  522. ao2_t_ref(obj, -1, "test");
  523. }
  524. ao2_iterator_destroy(&it);
  525. /* Is the container count what we expect after removing a single item? */
  526. if (ao2_container_count(c1) != (lim - 1)) {
  527. ast_test_status_update(test, "unlink during iterator failed. Number %d was not removed.\n", num);
  528. res = AST_TEST_FAIL;
  529. }
  530. if (ao2_container_check(c1, 0)) {
  531. ast_test_status_update(test, "container integrity check failed\n");
  532. res = AST_TEST_FAIL;
  533. goto cleanup;
  534. }
  535. /* Test unlink all with OBJ_MULTIPLE, leave a single object for the container to destroy */
  536. ao2_t_callback(c1, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, all_but_one_cb, NULL, "test multiple");
  537. /* check to make sure all test_obj destructors were called except for 1 */
  538. if (destructor_count != 1) {
  539. ast_test_status_update(test, "OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA failed. destructor count %d\n", destructor_count);
  540. res = AST_TEST_FAIL;
  541. }
  542. if (ao2_container_check(c1, 0)) {
  543. ast_test_status_update(test, "container integrity check failed\n");
  544. res = AST_TEST_FAIL;
  545. }
  546. #if defined(TEST_CONTAINER_DEBUG_DUMP)
  547. ao2_container_dump(c1, 0, "test_1 c1", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
  548. ao2_container_stats(c1, 0, "test_1 c1", (void *) test, (ao2_prnt_fn *) ast_test_debug);
  549. #endif /* defined(TEST_CONTAINER_DEBUG_DUMP) */
  550. cleanup:
  551. /* destroy containers */
  552. if (c1) {
  553. ao2_t_ref(c1, -1, "bye c1");
  554. }
  555. if (c2) {
  556. ao2_t_ref(c2, -1, "bye c2");
  557. }
  558. if (destructor_count > 0) {
  559. ast_test_status_update(test, "all destructors were not called, destructor count is %d\n", destructor_count);
  560. res = AST_TEST_FAIL;
  561. } else if (destructor_count < 0) {
  562. ast_test_status_update(test, "Destructor was called too many times, destructor count is %d\n", destructor_count);
  563. res = AST_TEST_FAIL;
  564. }
  565. return res;
  566. }
  567. AST_TEST_DEFINE(astobj2_test_1)
  568. {
  569. int res = AST_TEST_PASS;
  570. switch (cmd) {
  571. case TEST_INIT:
  572. info->name = "astobj2_test1";
  573. info->category = "/main/astobj2/";
  574. info->summary = "Test ao2 objects, containers, callbacks, and iterators";
  575. info->description =
  576. "Builds ao2_containers with various item numbers, bucket sizes, cmp and hash "
  577. "functions. Runs a series of tests to manipulate the container using callbacks "
  578. "and iterators. Verifies expected behavior.";
  579. return AST_TEST_NOT_RUN;
  580. case TEST_EXECUTE:
  581. break;
  582. }
  583. /* Test number, container_type, use_sort, number of objects. */
  584. if ((res = astobj2_test_1_helper(1, TEST_CONTAINER_LIST, 0, 50, test)) == AST_TEST_FAIL) {
  585. return res;
  586. }
  587. if ((res = astobj2_test_1_helper(2, TEST_CONTAINER_LIST, 1, 50, test)) == AST_TEST_FAIL) {
  588. return res;
  589. }
  590. if ((res = astobj2_test_1_helper(3, TEST_CONTAINER_HASH, 0, 1000, test)) == AST_TEST_FAIL) {
  591. return res;
  592. }
  593. if ((res = astobj2_test_1_helper(4, TEST_CONTAINER_HASH, 1, 1000, test)) == AST_TEST_FAIL) {
  594. return res;
  595. }
  596. if ((res = astobj2_test_1_helper(4, TEST_CONTAINER_RBTREE, 1, 1000, test)) == AST_TEST_FAIL) {
  597. return res;
  598. }
  599. return res;
  600. }
  601. AST_TEST_DEFINE(astobj2_test_2)
  602. {
  603. int res = AST_TEST_PASS;
  604. struct ao2_container *c;
  605. struct ao2_iterator i;
  606. struct test_obj *obj;
  607. int num;
  608. static const int NUM_OBJS = 5;
  609. int destructor_count = NUM_OBJS;
  610. struct test_obj tmp_obj = { 0, };
  611. switch (cmd) {
  612. case TEST_INIT:
  613. info->name = "astobj2_test2";
  614. info->category = "/main/astobj2/";
  615. info->summary = "Test a certain scenario using ao2 iterators";
  616. info->description =
  617. "This test is aimed at testing for a specific regression that occurred. "
  618. "Add some objects into a container. Mix finds and iteration and make "
  619. "sure that the iterator still sees all objects.";
  620. return AST_TEST_NOT_RUN;
  621. case TEST_EXECUTE:
  622. break;
  623. }
  624. c = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, test_cmp_cb);
  625. if (!c) {
  626. ast_test_status_update(test, "ao2_container_alloc_list failed.\n");
  627. res = AST_TEST_FAIL;
  628. goto cleanup;
  629. }
  630. for (num = 1; num <= NUM_OBJS; num++) {
  631. if (!(obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor))) {
  632. ast_test_status_update(test, "ao2_alloc failed.\n");
  633. res = AST_TEST_FAIL;
  634. goto cleanup;
  635. }
  636. obj->destructor_count = &destructor_count;
  637. obj->i = num;
  638. ao2_link(c, obj);
  639. ao2_ref(obj, -1);
  640. if (ao2_container_count(c) != num) {
  641. ast_test_status_update(test, "container did not link correctly\n");
  642. res = AST_TEST_FAIL;
  643. }
  644. }
  645. if (ao2_container_check(c, 0)) {
  646. ast_test_status_update(test, "container integrity check failed\n");
  647. res = AST_TEST_FAIL;
  648. goto cleanup;
  649. }
  650. /*
  651. * Iteration take 1. Just make sure we see all NUM_OBJS objects.
  652. */
  653. num = 0;
  654. i = ao2_iterator_init(c, 0);
  655. while ((obj = ao2_iterator_next(&i))) {
  656. num++;
  657. ao2_ref(obj, -1);
  658. }
  659. ao2_iterator_destroy(&i);
  660. if (num != NUM_OBJS) {
  661. ast_test_status_update(test, "iterate take 1, expected '%d', only saw '%d' objects\n",
  662. NUM_OBJS, num);
  663. res = AST_TEST_FAIL;
  664. }
  665. /*
  666. * Iteration take 2. Do a find for the last object, then iterate and make
  667. * sure we find all NUM_OBJS objects.
  668. */
  669. tmp_obj.i = NUM_OBJS;
  670. obj = ao2_find(c, &tmp_obj, OBJ_POINTER);
  671. if (!obj) {
  672. ast_test_status_update(test, "ao2_find() failed.\n");
  673. res = AST_TEST_FAIL;
  674. } else {
  675. ao2_ref(obj, -1);
  676. }
  677. num = 0;
  678. i = ao2_iterator_init(c, 0);
  679. while ((obj = ao2_iterator_next(&i))) {
  680. num++;
  681. ao2_ref(obj, -1);
  682. }
  683. ao2_iterator_destroy(&i);
  684. if (num != NUM_OBJS) {
  685. ast_test_status_update(test, "iterate take 2, expected '%d', only saw '%d' objects\n",
  686. NUM_OBJS, num);
  687. res = AST_TEST_FAIL;
  688. }
  689. /*
  690. * Iteration take 3. Do a find for an object while in the middle
  691. * of iterating;
  692. */
  693. num = 0;
  694. i = ao2_iterator_init(c, 0);
  695. while ((obj = ao2_iterator_next(&i))) {
  696. if (num == 1) {
  697. struct test_obj *obj2;
  698. tmp_obj.i = NUM_OBJS - 1;
  699. obj2 = ao2_find(c, &tmp_obj, OBJ_POINTER);
  700. if (!obj2) {
  701. ast_test_status_update(test, "ao2_find() failed.\n");
  702. res = AST_TEST_FAIL;
  703. } else {
  704. ao2_ref(obj2, -1);
  705. }
  706. }
  707. num++;
  708. ao2_ref(obj, -1);
  709. }
  710. ao2_iterator_destroy(&i);
  711. if (num != NUM_OBJS) {
  712. ast_test_status_update(test, "iterate take 3, expected '%d', only saw '%d' objects\n",
  713. NUM_OBJS, num);
  714. res = AST_TEST_FAIL;
  715. }
  716. cleanup:
  717. if (c) {
  718. ao2_ref(c, -1);
  719. }
  720. return res;
  721. }
  722. static AO2_GLOBAL_OBJ_STATIC(astobj2_holder);
  723. AST_TEST_DEFINE(astobj2_test_3)
  724. {
  725. int res = AST_TEST_PASS;
  726. int destructor_count = 0;
  727. int num_objects = 0;
  728. struct test_obj *obj = NULL;
  729. struct test_obj *obj2 = NULL;
  730. struct test_obj *obj3 = NULL;
  731. switch (cmd) {
  732. case TEST_INIT:
  733. info->name = "astobj2_test3";
  734. info->category = "/main/astobj2/";
  735. info->summary = "Test global ao2 holder";
  736. info->description =
  737. "This test is to see if the global ao2 holder works as intended.";
  738. return AST_TEST_NOT_RUN;
  739. case TEST_EXECUTE:
  740. break;
  741. }
  742. /* Put an object in the holder */
  743. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  744. if (!obj) {
  745. ast_test_status_update(test, "ao2_alloc failed.\n");
  746. res = AST_TEST_FAIL;
  747. goto cleanup;
  748. }
  749. obj->destructor_count = &destructor_count;
  750. obj->i = ++num_objects;
  751. obj2 = ao2_t_global_obj_replace(astobj2_holder, obj, "Save object in the holder");
  752. if (obj2) {
  753. ast_test_status_update(test, "Returned object not expected.\n");
  754. res = AST_TEST_FAIL;
  755. goto cleanup;
  756. }
  757. /* Save object for next check. */
  758. obj3 = obj;
  759. /* Replace an object in the holder */
  760. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  761. if (!obj) {
  762. ast_test_status_update(test, "ao2_alloc failed.\n");
  763. res = AST_TEST_FAIL;
  764. goto cleanup;
  765. }
  766. obj->destructor_count = &destructor_count;
  767. obj->i = ++num_objects;
  768. obj2 = ao2_t_global_obj_replace(astobj2_holder, obj, "Replace object in the holder");
  769. if (!obj2) {
  770. ast_test_status_update(test, "Expected an object.\n");
  771. res = AST_TEST_FAIL;
  772. goto cleanup;
  773. }
  774. if (obj2 != obj3) {
  775. ast_test_status_update(test, "Replaced object not expected object.\n");
  776. res = AST_TEST_FAIL;
  777. goto cleanup;
  778. }
  779. ao2_ref(obj3, -1);
  780. obj3 = NULL;
  781. ao2_ref(obj2, -1);
  782. obj2 = NULL;
  783. ao2_ref(obj, -1);
  784. /* Replace with unref of an object in the holder */
  785. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  786. if (!obj) {
  787. ast_test_status_update(test, "ao2_alloc failed.\n");
  788. res = AST_TEST_FAIL;
  789. goto cleanup;
  790. }
  791. obj->destructor_count = &destructor_count;
  792. obj->i = ++num_objects;
  793. if (!ao2_t_global_obj_replace_unref(astobj2_holder, obj, "Replace w/ unref object in the holder")) {
  794. ast_test_status_update(test, "Expected an object to be replaced.\n");
  795. res = AST_TEST_FAIL;
  796. goto cleanup;
  797. }
  798. /* Save object for next check. */
  799. obj3 = obj;
  800. /* Get reference to held object. */
  801. obj = ao2_t_global_obj_ref(astobj2_holder, "Get a held object reference");
  802. if (!obj) {
  803. ast_test_status_update(test, "Expected an object.\n");
  804. res = AST_TEST_FAIL;
  805. goto cleanup;
  806. }
  807. if (obj != obj3) {
  808. ast_test_status_update(test, "Referenced object not expected object.\n");
  809. res = AST_TEST_FAIL;
  810. goto cleanup;
  811. }
  812. ao2_ref(obj3, -1);
  813. obj3 = NULL;
  814. ao2_ref(obj, -1);
  815. obj = NULL;
  816. /* Release the object in the global holder. */
  817. ao2_t_global_obj_release(astobj2_holder, "Check release all objects");
  818. destructor_count += num_objects;
  819. if (0 < destructor_count) {
  820. ast_test_status_update(test,
  821. "all destructors were not called, destructor count is %d\n",
  822. destructor_count);
  823. res = AST_TEST_FAIL;
  824. } else if (destructor_count < 0) {
  825. ast_test_status_update(test,
  826. "Destructor was called too many times, destructor count is %d\n",
  827. destructor_count);
  828. res = AST_TEST_FAIL;
  829. }
  830. cleanup:
  831. if (obj) {
  832. ao2_t_ref(obj, -1, "Test cleanup external object 1");
  833. }
  834. if (obj2) {
  835. ao2_t_ref(obj2, -1, "Test cleanup external object 2");
  836. }
  837. if (obj3) {
  838. ao2_t_ref(obj3, -1, "Test cleanup external object 3");
  839. }
  840. ao2_t_global_obj_release(astobj2_holder, "Test cleanup holder");
  841. return res;
  842. }
  843. /*!
  844. * \internal
  845. * \brief Make a nonsorted container for astobj2 testing.
  846. * \since 12.0.0
  847. *
  848. * \param type Container type to create.
  849. * \param options Container options
  850. *
  851. * \retval container on success.
  852. * \retval NULL on error.
  853. */
  854. static struct ao2_container *test_make_nonsorted(enum test_container_type type, int options)
  855. {
  856. struct ao2_container *container;
  857. container = NULL;
  858. switch (type) {
  859. case TEST_CONTAINER_LIST:
  860. container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, options,
  861. NULL, test_cmp_cb);
  862. break;
  863. case TEST_CONTAINER_HASH:
  864. container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, options, 5,
  865. test_hash_cb, NULL, test_cmp_cb);
  866. break;
  867. case TEST_CONTAINER_RBTREE:
  868. /* Container type must be sorted. */
  869. break;
  870. }
  871. return container;
  872. }
  873. /*!
  874. * \internal
  875. * \brief Make a sorted container for astobj2 testing.
  876. * \since 12.0.0
  877. *
  878. * \param type Container type to create.
  879. * \param options Container options
  880. *
  881. * \retval container on success.
  882. * \retval NULL on error.
  883. */
  884. static struct ao2_container *test_make_sorted(enum test_container_type type, int options)
  885. {
  886. struct ao2_container *container;
  887. container = NULL;
  888. switch (type) {
  889. case TEST_CONTAINER_LIST:
  890. container = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, options,
  891. test_sort_cb, test_cmp_cb, "test");
  892. break;
  893. case TEST_CONTAINER_HASH:
  894. container = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, options, 5,
  895. test_hash_cb, test_sort_cb, test_cmp_cb, "test");
  896. break;
  897. case TEST_CONTAINER_RBTREE:
  898. container = ao2_t_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX, options,
  899. test_sort_cb, test_cmp_cb, "test");
  900. break;
  901. }
  902. return container;
  903. }
  904. /*!
  905. * \internal
  906. * \brief Insert the given test vector into the given container.
  907. * \since 12.0.0
  908. *
  909. * \note The given test vector must not have any duplicates.
  910. *
  911. * \param container Container to insert the test vector.
  912. * \param destroy_counter What to increment when the object is destroyed.
  913. * \param vector Test vector to insert.
  914. * \param count Number of objects in the vector.
  915. * \param prefix Test output prefix string.
  916. * \param test Test output controller.
  917. *
  918. * \retval 0 on success.
  919. * \retval -1 on error.
  920. */
  921. static int insert_test_vector(struct ao2_container *container, int *destroy_counter, const int *vector, int count, const char *prefix, struct ast_test *test)
  922. {
  923. int idx;
  924. struct test_obj *obj;
  925. for (idx = 0; idx < count; ++idx) {
  926. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  927. if (!obj) {
  928. ast_test_status_update(test, "%s: ao2_alloc failed.\n", prefix);
  929. return -1;
  930. }
  931. if (destroy_counter) {
  932. /* This object ultimately needs to be destroyed. */
  933. ++*destroy_counter;
  934. }
  935. obj->destructor_count = destroy_counter;
  936. obj->i = vector[idx];
  937. ao2_link(container, obj);
  938. ao2_t_ref(obj, -1, "test");
  939. if (ao2_container_check(container, 0)) {
  940. ast_test_status_update(test, "%s: Container integrity check failed linking vector[%d]:%d\n",
  941. prefix, idx, vector[idx]);
  942. return -1;
  943. }
  944. if (ao2_container_count(container) != idx + 1) {
  945. ast_test_status_update(test,
  946. "%s: Unexpected container count. Expected:%d Got:%d\n",
  947. prefix, idx + 1, ao2_container_count(container));
  948. return -1;
  949. }
  950. }
  951. return 0;
  952. }
  953. /*!
  954. * \internal
  955. * \brief Insert duplicates of number into the given container.
  956. * \since 12.0.0
  957. *
  958. * \note The given container must not already have the number in it.
  959. *
  960. * \param container Container to insert the duplicates.
  961. * \param destroy_counter What to increment when the object is destroyed.
  962. * \param number Number of object to duplicate.
  963. * \param prefix Test output prefix string.
  964. * \param test Test output controller.
  965. *
  966. * \retval 0 on success.
  967. * \retval -1 on error.
  968. */
  969. static int insert_test_duplicates(struct ao2_container *container, int *destroy_counter, int number, const char *prefix, struct ast_test *test)
  970. {
  971. int count;
  972. struct test_obj *obj;
  973. struct test_obj *obj_dup;
  974. /* Check if object already exists in the container. */
  975. obj = ao2_find(container, &number, OBJ_KEY);
  976. if (obj) {
  977. ast_test_status_update(test, "%s: Object %d already exists.\n", prefix, number);
  978. ao2_t_ref(obj, -1, "test");
  979. return -1;
  980. }
  981. /* Add three duplicate keyed objects. */
  982. obj_dup = NULL;
  983. for (count = 0; count < 4; ++count) {
  984. obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  985. if (!obj) {
  986. ast_test_status_update(test, "%s: ao2_alloc failed.\n", prefix);
  987. if (obj_dup) {
  988. ao2_t_ref(obj_dup, -1, "test");
  989. }
  990. return -1;
  991. }
  992. if (destroy_counter) {
  993. /* This object ultimately needs to be destroyed. */
  994. ++*destroy_counter;
  995. }
  996. obj->destructor_count = destroy_counter;
  997. obj->i = number;
  998. obj->dup_number = count;
  999. ao2_link(container, obj);
  1000. if (count == 2) {
  1001. /* Duplicate this object. */
  1002. obj_dup = obj;
  1003. } else {
  1004. ao2_t_ref(obj, -1, "test");
  1005. }
  1006. if (ao2_container_check(container, 0)) {
  1007. ast_test_status_update(test, "%s: Container integrity check failed linking num:%d dup:%d\n",
  1008. prefix, number, count);
  1009. if (obj_dup) {
  1010. ao2_t_ref(obj_dup, -1, "test");
  1011. }
  1012. return -1;
  1013. }
  1014. }
  1015. /* Add the duplicate object. */
  1016. ao2_link(container, obj_dup);
  1017. ao2_t_ref(obj_dup, -1, "test");
  1018. if (ao2_container_check(container, 0)) {
  1019. ast_test_status_update(test, "%s: Container integrity check failed linking obj_dup\n",
  1020. prefix);
  1021. return -1;
  1022. }
  1023. return 0;
  1024. }
  1025. /*!
  1026. * \internal
  1027. * \brief Iterate over the container and compare the objects with the given vector.
  1028. * \since 12.0.0
  1029. *
  1030. * \param res Passed in enum ast_test_result_state.
  1031. * \param container Container to iterate.
  1032. * \param flags Flags controlling the iteration.
  1033. * \param vector Expected vector to find.
  1034. * \param count Number of objects in the vector.
  1035. * \param prefix Test output prefix string.
  1036. * \param test Test output controller.
  1037. *
  1038. * \return enum ast_test_result_state
  1039. */
  1040. static int test_ao2_iteration(int res, struct ao2_container *container,
  1041. enum ao2_iterator_flags flags,
  1042. const int *vector, int count, const char *prefix, struct ast_test *test)
  1043. {
  1044. struct ao2_iterator iter;
  1045. struct test_obj *obj;
  1046. int idx;
  1047. if (ao2_container_count(container) != count) {
  1048. ast_test_status_update(test, "%s: Container count doesn't match vector count.\n",
  1049. prefix);
  1050. res = AST_TEST_FAIL;
  1051. }
  1052. iter = ao2_iterator_init(container, flags);
  1053. /* Check iterated objects against the given vector. */
  1054. for (idx = 0; idx < count; ++idx) {
  1055. obj = ao2_iterator_next(&iter);
  1056. if (!obj) {
  1057. ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
  1058. res = AST_TEST_FAIL;
  1059. break;
  1060. }
  1061. if (vector[idx] != obj->i) {
  1062. ast_test_status_update(test, "%s: Object %d != vector[%d] %d.\n",
  1063. prefix, obj->i, idx, vector[idx]);
  1064. res = AST_TEST_FAIL;
  1065. }
  1066. ao2_ref(obj, -1); /* remove ref from iterator */
  1067. }
  1068. obj = ao2_iterator_next(&iter);
  1069. if (obj) {
  1070. ast_test_status_update(test, "%s: Too many objects found. Object %d\n",
  1071. prefix, obj->i);
  1072. ao2_ref(obj, -1); /* remove ref from iterator */
  1073. res = AST_TEST_FAIL;
  1074. }
  1075. ao2_iterator_destroy(&iter);
  1076. return res;
  1077. }
  1078. /*!
  1079. * \internal
  1080. * \brief Run an ao2_callback() and compare the returned vector with the given vector.
  1081. * \since 12.0.0
  1082. *
  1083. * \param res Passed in enum ast_test_result_state.
  1084. * \param container Container to traverse.
  1085. * \param flags Callback flags controlling the traversal.
  1086. * \param cmp_fn Compare function to select objects.
  1087. * \param arg Optional argument.
  1088. * \param vector Expected vector to find.
  1089. * \param count Number of objects in the vector.
  1090. * \param prefix Test output prefix string.
  1091. * \param test Test output controller.
  1092. *
  1093. * \return enum ast_test_result_state
  1094. */
  1095. static int test_ao2_callback_traversal(int res, struct ao2_container *container,
  1096. enum search_flags flags, ao2_callback_fn *cmp_fn, void *arg,
  1097. const int *vector, int count, const char *prefix, struct ast_test *test)
  1098. {
  1099. struct ao2_iterator *mult_iter;
  1100. struct test_obj *obj;
  1101. int idx;
  1102. mult_iter = ao2_callback(container, flags | OBJ_MULTIPLE, cmp_fn, arg);
  1103. if (!mult_iter) {
  1104. ast_test_status_update(test, "%s: Did not return iterator.\n", prefix);
  1105. return AST_TEST_FAIL;
  1106. }
  1107. /* Check matching objects against the given vector. */
  1108. for (idx = 0; idx < count; ++idx) {
  1109. obj = ao2_iterator_next(mult_iter);
  1110. if (!obj) {
  1111. ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
  1112. res = AST_TEST_FAIL;
  1113. break;
  1114. }
  1115. if (vector[idx] != obj->i) {
  1116. ast_test_status_update(test, "%s: Object %d != vector[%d] %d.\n",
  1117. prefix, obj->i, idx, vector[idx]);
  1118. res = AST_TEST_FAIL;
  1119. }
  1120. ao2_ref(obj, -1); /* remove ref from iterator */
  1121. }
  1122. obj = ao2_iterator_next(mult_iter);
  1123. if (obj) {
  1124. ast_test_status_update(test, "%s: Too many objects found. Object %d\n",
  1125. prefix, obj->i);
  1126. ao2_ref(obj, -1); /* remove ref from iterator */
  1127. res = AST_TEST_FAIL;
  1128. }
  1129. ao2_iterator_destroy(mult_iter);
  1130. return res;
  1131. }
  1132. /*!
  1133. * \internal
  1134. * \brief Run an ao2_find() for duplicates and compare the returned vector with the given vector.
  1135. * \since 12.0.0
  1136. *
  1137. * \param res Passed in enum ast_test_result_state.
  1138. * \param container Container to traverse.
  1139. * \param flags Callback flags controlling the traversal.
  1140. * \param number Number of object to find all duplicates.
  1141. * \param vector Expected vector to find.
  1142. * \param count Number of objects in the vector.
  1143. * \param prefix Test output prefix string.
  1144. * \param test Test output controller.
  1145. *
  1146. * \return enum ast_test_result_state
  1147. */
  1148. static int test_expected_duplicates(int res, struct ao2_container *container,
  1149. enum search_flags flags, int number,
  1150. const int *vector, int count, const char *prefix, struct ast_test *test)
  1151. {
  1152. struct ao2_iterator *mult_iter;
  1153. struct test_obj *obj;
  1154. int idx;
  1155. mult_iter = ao2_find(container, &number, flags | OBJ_MULTIPLE | OBJ_KEY);
  1156. if (!mult_iter) {
  1157. ast_test_status_update(test, "%s: Did not return iterator.\n", prefix);
  1158. return AST_TEST_FAIL;
  1159. }
  1160. /* Check matching objects against the given vector. */
  1161. for (idx = 0; idx < count; ++idx) {
  1162. obj = ao2_iterator_next(mult_iter);
  1163. if (!obj) {
  1164. ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
  1165. res = AST_TEST_FAIL;
  1166. break;
  1167. }
  1168. if (number != obj->i) {
  1169. ast_test_status_update(test, "%s: Object %d != %d.\n",
  1170. prefix, obj->i, number);
  1171. res = AST_TEST_FAIL;
  1172. }
  1173. if (vector[idx] != obj->dup_number) {
  1174. ast_test_status_update(test, "%s: Object dup id %d != vector[%d] %d.\n",
  1175. prefix, obj->dup_number, idx, vector[idx]);
  1176. res = AST_TEST_FAIL;
  1177. }
  1178. ao2_ref(obj, -1); /* remove ref from iterator */
  1179. }
  1180. obj = ao2_iterator_next(mult_iter);
  1181. if (obj) {
  1182. ast_test_status_update(test,
  1183. "%s: Too many objects found. Object %d, dup id %d\n",
  1184. prefix, obj->i, obj->dup_number);
  1185. ao2_ref(obj, -1); /* remove ref from iterator */
  1186. res = AST_TEST_FAIL;
  1187. }
  1188. ao2_iterator_destroy(mult_iter);
  1189. return res;
  1190. }
  1191. /*!
  1192. * \internal
  1193. * \brief Test nonsorted container traversal.
  1194. * \since 12.0.0
  1195. *
  1196. * \param res Passed in enum ast_test_result_state.
  1197. * \param tst_num Test number.
  1198. * \param type Container type to test.
  1199. * \param test Test output controller.
  1200. *
  1201. * \return enum ast_test_result_state
  1202. */
  1203. static int test_traversal_nonsorted(int res, int tst_num, enum test_container_type type, struct ast_test *test)
  1204. {
  1205. struct ao2_container *c1;
  1206. struct ao2_container *c2 = NULL;
  1207. int partial;
  1208. int destructor_count = 0;
  1209. /*! Container object insertion vector. */
  1210. static const int test_initial[] = {
  1211. 1, 0, 2, 6, 4, 7, 5, 3, 9, 8
  1212. };
  1213. /*! Container object insertion vector reversed. */
  1214. static const int test_reverse[] = {
  1215. 8, 9, 3, 5, 7, 4, 6, 2, 0, 1
  1216. };
  1217. static const int test_list_partial_forward[] = {
  1218. 6, 7, 5
  1219. };
  1220. static const int test_list_partial_backward[] = {
  1221. 5, 7, 6
  1222. };
  1223. /* The hash orders assume that there are 5 buckets. */
  1224. static const int test_hash_end_forward[] = {
  1225. 0, 5, 1, 6, 2, 7, 3, 8, 4, 9
  1226. };
  1227. static const int test_hash_end_backward[] = {
  1228. 9, 4, 8, 3, 7, 2, 6, 1, 5, 0
  1229. };
  1230. static const int test_hash_begin_forward[] = {
  1231. 5, 0, 6, 1, 7, 2, 8, 3, 9, 4
  1232. };
  1233. static const int test_hash_begin_backward[] = {
  1234. 4, 9, 3, 8, 2, 7, 1, 6, 0, 5
  1235. };
  1236. static const int test_hash_partial_forward[] = {
  1237. 5, 6, 7
  1238. };
  1239. static const int test_hash_partial_backward[] = {
  1240. 7, 6, 5
  1241. };
  1242. ast_test_status_update(test, "Test %d, %s containers.\n",
  1243. tst_num, test_container2str(type));
  1244. /* Create container that inserts objects at the end. */
  1245. c1 = test_make_nonsorted(type, 0);
  1246. if (!c1) {
  1247. ast_test_status_update(test, "Container c1 creation failed.\n");
  1248. res = AST_TEST_FAIL;
  1249. goto test_cleanup;
  1250. }
  1251. if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1", test)) {
  1252. res = AST_TEST_FAIL;
  1253. goto test_cleanup;
  1254. }
  1255. /* Create container that inserts objects at the beginning. */
  1256. c2 = test_make_nonsorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN);
  1257. if (!c2) {
  1258. ast_test_status_update(test, "Container c2 creation failed.\n");
  1259. res = AST_TEST_FAIL;
  1260. goto test_cleanup;
  1261. }
  1262. if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2", test)) {
  1263. res = AST_TEST_FAIL;
  1264. goto test_cleanup;
  1265. }
  1266. /* Check container iteration directions */
  1267. switch (type) {
  1268. case TEST_CONTAINER_LIST:
  1269. res = test_ao2_iteration(res, c1, 0,
  1270. test_initial, ARRAY_LEN(test_initial),
  1271. "Iteration (ascending, insert end)", test);
  1272. res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
  1273. test_reverse, ARRAY_LEN(test_reverse),
  1274. "Iteration (descending, insert end)", test);
  1275. res = test_ao2_iteration(res, c2, 0,
  1276. test_reverse, ARRAY_LEN(test_reverse),
  1277. "Iteration (ascending, insert begin)", test);
  1278. res = test_ao2_iteration(res, c2, AO2_ITERATOR_DESCENDING,
  1279. test_initial, ARRAY_LEN(test_initial),
  1280. "Iteration (descending, insert begin)", test);
  1281. break;
  1282. case TEST_CONTAINER_HASH:
  1283. res = test_ao2_iteration(res, c1, 0,
  1284. test_hash_end_forward, ARRAY_LEN(test_hash_end_forward),
  1285. "Iteration (ascending, insert end)", test);
  1286. res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
  1287. test_hash_end_backward, ARRAY_LEN(test_hash_end_backward),
  1288. "Iteration (descending, insert end)", test);
  1289. res = test_ao2_iteration(res, c2, 0,
  1290. test_hash_begin_forward, ARRAY_LEN(test_hash_begin_forward),
  1291. "Iteration (ascending, insert begin)", test);
  1292. res = test_ao2_iteration(res, c2, AO2_ITERATOR_DESCENDING,
  1293. test_hash_begin_backward, ARRAY_LEN(test_hash_begin_backward),
  1294. "Iteration (descending, insert begin)", test);
  1295. break;
  1296. case TEST_CONTAINER_RBTREE:
  1297. break;
  1298. }
  1299. /* Check container traversal directions */
  1300. switch (type) {
  1301. case TEST_CONTAINER_LIST:
  1302. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
  1303. test_initial, ARRAY_LEN(test_initial),
  1304. "Traversal (ascending, insert end)", test);
  1305. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
  1306. test_reverse, ARRAY_LEN(test_reverse),
  1307. "Traversal (descending, insert end)", test);
  1308. res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_ASCENDING, NULL, NULL,
  1309. test_reverse, ARRAY_LEN(test_reverse),
  1310. "Traversal (ascending, insert begin)", test);
  1311. res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_DESCENDING, NULL, NULL,
  1312. test_initial, ARRAY_LEN(test_initial),
  1313. "Traversal (descending, insert begin)", test);
  1314. break;
  1315. case TEST_CONTAINER_HASH:
  1316. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
  1317. test_hash_end_forward, ARRAY_LEN(test_hash_end_forward),
  1318. "Traversal (ascending, insert end)", test);
  1319. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
  1320. test_hash_end_backward, ARRAY_LEN(test_hash_end_backward),
  1321. "Traversal (descending, insert end)", test);
  1322. res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_ASCENDING, NULL, NULL,
  1323. test_hash_begin_forward, ARRAY_LEN(test_hash_begin_forward),
  1324. "Traversal (ascending, insert begin)", test);
  1325. res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_DESCENDING, NULL, NULL,
  1326. test_hash_begin_backward, ARRAY_LEN(test_hash_begin_backward),
  1327. "Traversal (descending, insert begin)", test);
  1328. break;
  1329. case TEST_CONTAINER_RBTREE:
  1330. break;
  1331. }
  1332. /* Check traversal with OBJ_PARTIAL_KEY search range. */
  1333. partial = 6;
  1334. partial_key_match_range = 1;
  1335. switch (type) {
  1336. case TEST_CONTAINER_LIST:
  1337. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
  1338. test_cmp_cb, &partial,
  1339. test_list_partial_forward, ARRAY_LEN(test_list_partial_forward),
  1340. "Traversal OBJ_PARTIAL_KEY (ascending)", test);
  1341. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
  1342. test_cmp_cb, &partial,
  1343. test_list_partial_backward, ARRAY_LEN(test_list_partial_backward),
  1344. "Traversal OBJ_PARTIAL_KEY (descending)", test);
  1345. break;
  1346. case TEST_CONTAINER_HASH:
  1347. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
  1348. test_cmp_cb, &partial,
  1349. test_hash_partial_forward, ARRAY_LEN(test_hash_partial_forward),
  1350. "Traversal OBJ_PARTIAL_KEY (ascending)", test);
  1351. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
  1352. test_cmp_cb, &partial,
  1353. test_hash_partial_backward, ARRAY_LEN(test_hash_partial_backward),
  1354. "Traversal OBJ_PARTIAL_KEY (descending)", test);
  1355. break;
  1356. case TEST_CONTAINER_RBTREE:
  1357. break;
  1358. }
  1359. test_cleanup:
  1360. /* destroy containers */
  1361. if (c1) {
  1362. ao2_t_ref(c1, -1, "bye c1");
  1363. }
  1364. if (c2) {
  1365. ao2_t_ref(c2, -1, "bye c2");
  1366. }
  1367. if (destructor_count > 0) {
  1368. ast_test_status_update(test,
  1369. "all destructors were not called, destructor count is %d\n",
  1370. destructor_count);
  1371. res = AST_TEST_FAIL;
  1372. } else if (destructor_count < 0) {
  1373. ast_test_status_update(test,
  1374. "Destructor was called too many times, destructor count is %d\n",
  1375. destructor_count);
  1376. res = AST_TEST_FAIL;
  1377. }
  1378. return res;
  1379. }
  1380. /*!
  1381. * \internal
  1382. * \brief Test sorted container traversal.
  1383. * \since 12.0.0
  1384. *
  1385. * \param res Passed in enum ast_test_result_state.
  1386. * \param tst_num Test number.
  1387. * \param type Container type to test.
  1388. * \param test Test output controller.
  1389. *
  1390. * \return enum ast_test_result_state
  1391. */
  1392. static int test_traversal_sorted(int res, int tst_num, enum test_container_type type, struct ast_test *test)
  1393. {
  1394. struct ao2_container *c1;
  1395. struct ao2_container *c2 = NULL;
  1396. int partial;
  1397. int destructor_count = 0;
  1398. int duplicate_number = 100;
  1399. /*! Container object insertion vector. */
  1400. static const int test_initial[] = {
  1401. 1, 0, 2, 6, 4, 7, 5, 3, 9, 8
  1402. };
  1403. /*! Container forward traversal/iteration. */
  1404. static const int test_forward[] = {
  1405. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  1406. };
  1407. /*! Container backward traversal/iteration. */
  1408. static const int test_backward[] = {
  1409. 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  1410. };
  1411. static const int test_partial_forward[] = {
  1412. 5, 6, 7
  1413. };
  1414. static const int test_partial_backward[] = {
  1415. 7, 6, 5
  1416. };
  1417. /* The hash orders assume that there are 5 buckets. */
  1418. static const int test_hash_forward[] = {
  1419. 0, 5, 1, 6, 2, 7, 3, 8, 4, 9
  1420. };
  1421. static const int test_hash_backward[] = {
  1422. 9, 4, 8, 3, 7, 2, 6, 1, 5, 0
  1423. };
  1424. static const int test_hash_partial_forward[] = {
  1425. 5, 6, 7
  1426. };
  1427. static const int test_hash_partial_backward[] = {
  1428. 7, 6, 5
  1429. };
  1430. /* Duplicate identifier order */
  1431. static const int test_dup_allow_forward[] = {
  1432. 0, 1, 2, 3, 2
  1433. };
  1434. static const int test_dup_allow_backward[] = {
  1435. 2, 3, 2, 1, 0
  1436. };
  1437. static const int test_dup_reject[] = {
  1438. 0
  1439. };
  1440. static const int test_dup_obj_reject_forward[] = {
  1441. 0, 1, 2, 3
  1442. };
  1443. static const int test_dup_obj_reject_backward[] = {
  1444. 3, 2, 1, 0
  1445. };
  1446. static const int test_dup_replace[] = {
  1447. 2
  1448. };
  1449. ast_test_status_update(test, "Test %d, %s containers.\n",
  1450. tst_num, test_container2str(type));
  1451. /* Create container that inserts duplicate objects after matching objects. */
  1452. c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW);
  1453. if (!c1) {
  1454. ast_test_status_update(test, "Container c1 creation failed.\n");
  1455. res = AST_TEST_FAIL;
  1456. goto test_cleanup;
  1457. }
  1458. if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_ALLOW)", test)) {
  1459. res = AST_TEST_FAIL;
  1460. goto test_cleanup;
  1461. }
  1462. /* Create container that inserts duplicate objects before matching objects. */
  1463. c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW);
  1464. if (!c2) {
  1465. ast_test_status_update(test, "Container c2 creation failed.\n");
  1466. res = AST_TEST_FAIL;
  1467. goto test_cleanup;
  1468. }
  1469. if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_ALLOW)", test)) {
  1470. res = AST_TEST_FAIL;
  1471. goto test_cleanup;
  1472. }
  1473. #if defined(TEST_CONTAINER_DEBUG_DUMP)
  1474. ao2_container_dump(c1, 0, "c1(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
  1475. ao2_container_stats(c1, 0, "c1(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug);
  1476. ao2_container_dump(c2, 0, "c2(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
  1477. ao2_container_stats(c2, 0, "c2(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug);
  1478. #endif /* defined(TEST_CONTAINER_DEBUG_DUMP) */
  1479. /* Check container iteration directions */
  1480. switch (type) {
  1481. case TEST_CONTAINER_RBTREE:
  1482. case TEST_CONTAINER_LIST:
  1483. res = test_ao2_iteration(res, c1, 0,
  1484. test_forward, ARRAY_LEN(test_forward),
  1485. "Iteration (ascending)", test);
  1486. res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
  1487. test_backward, ARRAY_LEN(test_backward),
  1488. "Iteration (descending)", test);
  1489. break;
  1490. case TEST_CONTAINER_HASH:
  1491. res = test_ao2_iteration(res, c1, 0,
  1492. test_hash_forward, ARRAY_LEN(test_hash_forward),
  1493. "Iteration (ascending)", test);
  1494. res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
  1495. test_hash_backward, ARRAY_LEN(test_hash_backward),
  1496. "Iteration (descending)", test);
  1497. break;
  1498. }
  1499. /* Check container traversal directions */
  1500. switch (type) {
  1501. case TEST_CONTAINER_RBTREE:
  1502. case TEST_CONTAINER_LIST:
  1503. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
  1504. test_forward, ARRAY_LEN(test_forward),
  1505. "Traversal (ascending)", test);
  1506. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
  1507. test_backward, ARRAY_LEN(test_backward),
  1508. "Traversal (descending)", test);
  1509. break;
  1510. case TEST_CONTAINER_HASH:
  1511. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
  1512. test_hash_forward, ARRAY_LEN(test_hash_forward),
  1513. "Traversal (ascending, insert end)", test);
  1514. res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
  1515. test_hash_backward, ARRAY_LEN(test_hash_backward),
  1516. "Traversal (descending)", test);
  1517. break;
  1518. }
  1519. /* Check traversal with OBJ_PARTIAL_KEY search range. */
  1520. partial = 6;
  1521. partial_key_match_range = 1;
  1522. switch (type) {
  1523. case TEST_CONTAINER_RBTREE:
  1524. case TEST_CONTAINER_LIST:
  1525. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
  1526. test_cmp_cb, &partial,
  1527. test_partial_forward, ARRAY_LEN(test_partial_forward),
  1528. "Traversal OBJ_PARTIAL_KEY (ascending)", test);
  1529. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
  1530. test_cmp_cb, &partial,
  1531. test_partial_backward, ARRAY_LEN(test_partial_backward),
  1532. "Traversal OBJ_PARTIAL_KEY (descending)", test);
  1533. break;
  1534. case TEST_CONTAINER_HASH:
  1535. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
  1536. test_cmp_cb, &partial,
  1537. test_hash_partial_forward, ARRAY_LEN(test_hash_partial_forward),
  1538. "Traversal OBJ_PARTIAL_KEY (ascending)", test);
  1539. res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
  1540. test_cmp_cb, &partial,
  1541. test_hash_partial_backward, ARRAY_LEN(test_hash_partial_backward),
  1542. "Traversal OBJ_PARTIAL_KEY (descending)", test);
  1543. break;
  1544. }
  1545. /* Add duplicates to initial containers that allow duplicates */
  1546. if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_ALLOW)", test)) {
  1547. res = AST_TEST_FAIL;
  1548. goto test_cleanup;
  1549. }
  1550. if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_ALLOW)", test)) {
  1551. res = AST_TEST_FAIL;
  1552. goto test_cleanup;
  1553. }
  1554. #if defined(TEST_CONTAINER_DEBUG_DUMP)
  1555. ao2_container_dump(c1, 0, "c1(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
  1556. ao2_container_stats(c1, 0, "c1(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug);
  1557. ao2_container_dump(c2, 0, "c2(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
  1558. ao2_container_stats(c2, 0, "c2(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug);
  1559. #endif /* defined(TEST_CONTAINER_DEBUG_DUMP) */
  1560. /* Check duplicates in containers that allow duplicates. */
  1561. res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
  1562. test_dup_allow_forward, ARRAY_LEN(test_dup_allow_forward),
  1563. "Duplicates (ascending, DUPS_ALLOW)", test);
  1564. res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
  1565. test_dup_allow_backward, ARRAY_LEN(test_dup_allow_backward),
  1566. "Duplicates (descending, DUPS_ALLOW)", test);
  1567. ao2_t_ref(c1, -1, "bye c1");
  1568. c1 = NULL;
  1569. ao2_t_ref(c2, -1, "bye c2");
  1570. c2 = NULL;
  1571. /* Create containers that reject duplicate keyed objects. */
  1572. c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT);
  1573. if (!c1) {
  1574. ast_test_status_update(test, "Container c1 creation failed.\n");
  1575. res = AST_TEST_FAIL;
  1576. goto test_cleanup;
  1577. }
  1578. if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_REJECT)", test)) {
  1579. res = AST_TEST_FAIL;
  1580. goto test_cleanup;
  1581. }
  1582. if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_REJECT)", test)) {
  1583. res = AST_TEST_FAIL;
  1584. goto test_cleanup;
  1585. }
  1586. c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT);
  1587. if (!c2) {
  1588. ast_test_status_update(test, "Container c2 creation failed.\n");
  1589. res = AST_TEST_FAIL;
  1590. goto test_cleanup;
  1591. }
  1592. if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_REJECT)", test)) {
  1593. res = AST_TEST_FAIL;
  1594. goto test_cleanup;
  1595. }
  1596. if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_REJECT)", test)) {
  1597. res = AST_TEST_FAIL;
  1598. goto test_cleanup;
  1599. }
  1600. /* Check duplicates in containers that reject duplicate keyed objects. */
  1601. res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
  1602. test_dup_reject, ARRAY_LEN(test_dup_reject),
  1603. "Duplicates (ascending, DUPS_REJECT)", test);
  1604. res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
  1605. test_dup_reject, ARRAY_LEN(test_dup_reject),
  1606. "Duplicates (descending, DUPS_REJECT)", test);
  1607. ao2_t_ref(c1, -1, "bye c1");
  1608. c1 = NULL;
  1609. ao2_t_ref(c2, -1, "bye c2");
  1610. c2 = NULL;
  1611. /* Create containers that reject duplicate objects. */
  1612. c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT);
  1613. if (!c1) {
  1614. ast_test_status_update(test, "Container c1 creation failed.\n");
  1615. res = AST_TEST_FAIL;
  1616. goto test_cleanup;
  1617. }
  1618. if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_OBJ_REJECT)", test)) {
  1619. res = AST_TEST_FAIL;
  1620. goto test_cleanup;
  1621. }
  1622. if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_OBJ_REJECT)", test)) {
  1623. res = AST_TEST_FAIL;
  1624. goto test_cleanup;
  1625. }
  1626. c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT);
  1627. if (!c2) {
  1628. ast_test_status_update(test, "Container c2 creation failed.\n");
  1629. res = AST_TEST_FAIL;
  1630. goto test_cleanup;
  1631. }
  1632. if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_OBJ_REJECT)", test)) {
  1633. res = AST_TEST_FAIL;
  1634. goto test_cleanup;
  1635. }
  1636. if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_OBJ_REJECT)", test)) {
  1637. res = AST_TEST_FAIL;
  1638. goto test_cleanup;
  1639. }
  1640. /* Check duplicates in containers that reject duplicate objects. */
  1641. res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
  1642. test_dup_obj_reject_forward, ARRAY_LEN(test_dup_obj_reject_forward),
  1643. "Duplicates (ascending, DUPS_OBJ_REJECT)", test);
  1644. res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
  1645. test_dup_obj_reject_backward, ARRAY_LEN(test_dup_obj_reject_backward),
  1646. "Duplicates (descending, DUPS_OBJ_REJECT)", test);
  1647. ao2_t_ref(c1, -1, "bye c1");
  1648. c1 = NULL;
  1649. ao2_t_ref(c2, -1, "bye c2");
  1650. c2 = NULL;
  1651. /* Create container that replaces duplicate keyed objects. */
  1652. c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE);
  1653. if (!c1) {
  1654. ast_test_status_update(test, "Container c1 creation failed.\n");
  1655. res = AST_TEST_FAIL;
  1656. goto test_cleanup;
  1657. }
  1658. if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_REJECT)", test)) {
  1659. res = AST_TEST_FAIL;
  1660. goto test_cleanup;
  1661. }
  1662. if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_REJECT)", test)) {
  1663. res = AST_TEST_FAIL;
  1664. goto test_cleanup;
  1665. }
  1666. c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE);
  1667. if (!c2) {
  1668. ast_test_status_update(test, "Container c2 creation failed.\n");
  1669. res = AST_TEST_FAIL;
  1670. goto test_cleanup;
  1671. }
  1672. if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_REPLACE)", test)) {
  1673. res = AST_TEST_FAIL;
  1674. goto test_cleanup;
  1675. }
  1676. if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_REPLACE)", test)) {
  1677. res = AST_TEST_FAIL;
  1678. goto test_cleanup;
  1679. }
  1680. /* Check duplicates in containers that replaces duplicate keyed objects. */
  1681. res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
  1682. test_dup_replace, ARRAY_LEN(test_dup_replace),
  1683. "Duplicates (ascending, DUPS_REPLACE)", test);
  1684. res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
  1685. test_dup_replace, ARRAY_LEN(test_dup_replace),
  1686. "Duplicates (descending, DUPS_REPLACE)", test);
  1687. test_cleanup:
  1688. /* destroy containers */
  1689. if (c1) {
  1690. ao2_t_ref(c1, -1, "bye c1");
  1691. }
  1692. if (c2) {
  1693. ao2_t_ref(c2, -1, "bye c2");
  1694. }
  1695. if (destructor_count > 0) {
  1696. ast_test_status_update(test,
  1697. "all destructors were not called, destructor count is %d\n",
  1698. destructor_count);
  1699. res = AST_TEST_FAIL;
  1700. } else if (destructor_count < 0) {
  1701. ast_test_status_update(test,
  1702. "Destructor was called too many times, destructor count is %d\n",
  1703. destructor_count);
  1704. res = AST_TEST_FAIL;
  1705. }
  1706. return res;
  1707. }
  1708. AST_TEST_DEFINE(astobj2_test_4)
  1709. {
  1710. int res = AST_TEST_PASS;
  1711. switch (cmd) {
  1712. case TEST_INIT:
  1713. info->name = "astobj2_test4";
  1714. info->category = "/main/astobj2/";
  1715. info->summary = "Test container traversal/iteration";
  1716. info->description =
  1717. "This test is to see if the container traversal/iteration works "
  1718. "as intended for each supported container type.";
  1719. return AST_TEST_NOT_RUN;
  1720. case TEST_EXECUTE:
  1721. break;
  1722. }
  1723. res = test_traversal_nonsorted(res, 1, TEST_CONTAINER_LIST, test);
  1724. res = test_traversal_nonsorted(res, 2, TEST_CONTAINER_HASH, test);
  1725. res = test_traversal_sorted(res, 3, TEST_CONTAINER_LIST, test);
  1726. res = test_traversal_sorted(res, 4, TEST_CONTAINER_HASH, test);
  1727. res = test_traversal_sorted(res, 5, TEST_CONTAINER_RBTREE, test);
  1728. return res;
  1729. }
  1730. static enum ast_test_result_state test_performance(struct ast_test *test,
  1731. enum test_container_type type, unsigned int copt)
  1732. {
  1733. /*!
  1734. * \brief The number of objects inserted and searched for in the container under test.
  1735. */
  1736. #define OBJS 73
  1737. int res = AST_TEST_PASS;
  1738. struct ao2_container *c1 = NULL;
  1739. struct test_obj *tobj[OBJS];
  1740. struct test_obj *tobj2;
  1741. int i;
  1742. switch (type) {
  1743. case TEST_CONTAINER_HASH:
  1744. c1 = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, copt, 17,
  1745. test_hash_cb, test_sort_cb, test_cmp_cb);
  1746. break;
  1747. case TEST_CONTAINER_LIST:
  1748. c1 = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, copt,
  1749. test_sort_cb, test_cmp_cb);
  1750. break;
  1751. case TEST_CONTAINER_RBTREE:
  1752. c1 = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX, copt,
  1753. test_sort_cb, test_cmp_cb);
  1754. break;
  1755. }
  1756. for (i = 0; i < OBJS; i++) {
  1757. tobj[i] = NULL;
  1758. }
  1759. if (!c1) {
  1760. ast_test_status_update(test, "Container c1 creation failed.\n");
  1761. res = AST_TEST_FAIL;
  1762. goto test_cleanup;
  1763. }
  1764. for (i = 0; i < OBJS; i++) {
  1765. tobj[i] = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
  1766. if (!tobj[i]) {
  1767. ast_test_status_update(test, "test object creation failed.\n");
  1768. res = AST_TEST_FAIL;
  1769. goto test_cleanup;
  1770. }
  1771. tobj[i]->i = i;
  1772. ao2_link(c1, tobj[i]);
  1773. }
  1774. for (i = 0; i < OBJS; i++) {
  1775. if ((!(tobj2 = ao2_find(c1, &i, OBJ_KEY)))) {
  1776. ast_test_status_update(test, "Should have found object %d in container.\n", i);
  1777. res = AST_TEST_FAIL;
  1778. goto test_cleanup;
  1779. }
  1780. ao2_ref(tobj2, -1);
  1781. tobj2 = NULL;
  1782. }
  1783. test_cleanup:
  1784. for (i = 0; i < OBJS ; i++) {
  1785. ao2_cleanup(tobj[i]);
  1786. }
  1787. ao2_cleanup(c1);
  1788. return res;
  1789. }
  1790. static enum ast_test_result_state testloop(struct ast_test *test,
  1791. enum test_container_type type, int copt, int iterations)
  1792. {
  1793. int res = AST_TEST_PASS;
  1794. int i;
  1795. int reportcount = iterations / 5;
  1796. struct timeval start;
  1797. start = ast_tvnow();
  1798. for (i = 1 ; i <= iterations && res == AST_TEST_PASS ; i++) {
  1799. if (i % reportcount == 0 && i != iterations) {
  1800. ast_test_status_update(test, "%5.2fK traversals, %9s\n",
  1801. i / 1000.0, test_container2str(type));
  1802. }
  1803. res = test_performance(test, type, copt);
  1804. }
  1805. ast_test_status_update(test, "%5.2fK traversals, %9s : %5lu ms\n",
  1806. iterations / 1000.0, test_container2str(type), (unsigned long)ast_tvdiff_ms(ast_tvnow(), start));
  1807. return res;
  1808. }
  1809. AST_TEST_DEFINE(astobj2_test_perf)
  1810. {
  1811. /*!
  1812. * \brief The number of iteration of testloop to be performed.
  1813. * \note
  1814. * In order to keep the elapsed time sane, if AO2_DEBUG is defined in menuselect,
  1815. * only 25000 iterations are performed. Otherwise 100000.
  1816. */
  1817. #ifdef AO2_DEBUG
  1818. #define ITERATIONS 25000
  1819. #else
  1820. #define ITERATIONS 100000
  1821. #endif
  1822. int res = AST_TEST_PASS;
  1823. switch (cmd) {
  1824. case TEST_INIT:
  1825. info->name = "astobj2_test_perf";
  1826. info->category = "/main/astobj2/perf/";
  1827. info->summary = "Test container performance";
  1828. info->description =
  1829. "Runs container traversal tests.";
  1830. return AST_TEST_NOT_RUN;
  1831. case TEST_EXECUTE:
  1832. break;
  1833. }
  1834. res = testloop(test, TEST_CONTAINER_LIST, 0, ITERATIONS);
  1835. if (!res) {
  1836. return res;
  1837. }
  1838. res = testloop(test, TEST_CONTAINER_HASH, 0, ITERATIONS);
  1839. if (!res) {
  1840. return res;
  1841. }
  1842. res = testloop(test, TEST_CONTAINER_RBTREE, 0, ITERATIONS);
  1843. return res;
  1844. }
  1845. static int unload_module(void)
  1846. {
  1847. AST_TEST_UNREGISTER(astobj2_test_1);
  1848. AST_TEST_UNREGISTER(astobj2_test_2);
  1849. AST_TEST_UNREGISTER(astobj2_test_3);
  1850. AST_TEST_UNREGISTER(astobj2_test_4);
  1851. AST_TEST_UNREGISTER(astobj2_test_perf);
  1852. return 0;
  1853. }
  1854. static int load_module(void)
  1855. {
  1856. AST_TEST_REGISTER(astobj2_test_1);
  1857. AST_TEST_REGISTER(astobj2_test_2);
  1858. AST_TEST_REGISTER(astobj2_test_3);
  1859. AST_TEST_REGISTER(astobj2_test_4);
  1860. AST_TEST_REGISTER(astobj2_test_perf);
  1861. return AST_MODULE_LOAD_SUCCESS;
  1862. }
  1863. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "ASTOBJ2 Unit Tests");