intrusive_list-selftests.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* Tests fpr intrusive double linked list for GDB, the GNU debugger.
  2. Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbsupport/intrusive_list.h"
  16. #include "gdbsupport/selftest.h"
  17. #include <unordered_set>
  18. /* An item type using intrusive_list_node by inheriting from it and its
  19. corresponding list type. Put another base before intrusive_list_node
  20. so that a pointer to the node != a pointer to the item. */
  21. struct other_base
  22. {
  23. int n = 1;
  24. };
  25. struct item_with_base : public other_base,
  26. public intrusive_list_node<item_with_base>
  27. {
  28. explicit item_with_base (const char *name)
  29. : name (name)
  30. {}
  31. const char *const name;
  32. };
  33. using item_with_base_list = intrusive_list<item_with_base>;
  34. /* An item type using intrusive_list_node as a field and its corresponding
  35. list type. Put the other field before the node, so that a pointer to the
  36. node != a pointer to the item. */
  37. struct item_with_member
  38. {
  39. explicit item_with_member (const char *name)
  40. : name (name)
  41. {}
  42. const char *const name;
  43. intrusive_list_node<item_with_member> node;
  44. };
  45. using item_with_member_node
  46. = intrusive_member_node<item_with_member, &item_with_member::node>;
  47. using item_with_member_list
  48. = intrusive_list<item_with_member, item_with_member_node>;
  49. /* To run all tests using both the base and member methods, all tests are
  50. declared in this templated class, which is instantiated once for each
  51. list type. */
  52. template <typename ListType>
  53. struct intrusive_list_test
  54. {
  55. using item_type = typename ListType::value_type;
  56. /* Verify that LIST contains exactly the items in EXPECTED.
  57. Traverse the list forward and backwards to exercise all links. */
  58. static void
  59. verify_items (const ListType &list,
  60. gdb::array_view<const typename ListType::value_type *> expected)
  61. {
  62. int i = 0;
  63. for (typename ListType::iterator it = list.begin ();
  64. it != list.end ();
  65. ++it)
  66. {
  67. const item_type &item = *it;
  68. gdb_assert (i < expected.size ());
  69. gdb_assert (&item == expected[i]);
  70. ++i;
  71. }
  72. gdb_assert (i == expected.size ());
  73. for (typename ListType::reverse_iterator it = list.rbegin ();
  74. it != list.rend ();
  75. ++it)
  76. {
  77. const item_type &item = *it;
  78. --i;
  79. gdb_assert (i >= 0);
  80. gdb_assert (&item == expected[i]);
  81. }
  82. gdb_assert (i == 0);
  83. }
  84. static void
  85. test_move_constructor ()
  86. {
  87. {
  88. /* Other list is not empty. */
  89. item_type a ("a"), b ("b"), c ("c");
  90. ListType list1;
  91. std::vector<const item_type *> expected;
  92. list1.push_back (a);
  93. list1.push_back (b);
  94. list1.push_back (c);
  95. ListType list2 (std::move (list1));
  96. expected = {};
  97. verify_items (list1, expected);
  98. expected = {&a, &b, &c};
  99. verify_items (list2, expected);
  100. }
  101. {
  102. /* Other list contains 1 element. */
  103. item_type a ("a");
  104. ListType list1;
  105. std::vector<const item_type *> expected;
  106. list1.push_back (a);
  107. ListType list2 (std::move (list1));
  108. expected = {};
  109. verify_items (list1, expected);
  110. expected = {&a};
  111. verify_items (list2, expected);
  112. }
  113. {
  114. /* Other list is empty. */
  115. ListType list1;
  116. std::vector<const item_type *> expected;
  117. ListType list2 (std::move (list1));
  118. expected = {};
  119. verify_items (list1, expected);
  120. expected = {};
  121. verify_items (list2, expected);
  122. }
  123. }
  124. static void
  125. test_move_assignment ()
  126. {
  127. {
  128. /* Both lists are not empty. */
  129. item_type a ("a"), b ("b"), c ("c"), d ("d"), e ("e");
  130. ListType list1;
  131. ListType list2;
  132. std::vector<const item_type *> expected;
  133. list1.push_back (a);
  134. list1.push_back (b);
  135. list1.push_back (c);
  136. list2.push_back (d);
  137. list2.push_back (e);
  138. list2 = std::move (list1);
  139. expected = {};
  140. verify_items (list1, expected);
  141. expected = {&a, &b, &c};
  142. verify_items (list2, expected);
  143. }
  144. {
  145. /* rhs list is empty. */
  146. item_type a ("a"), b ("b"), c ("c");
  147. ListType list1;
  148. ListType list2;
  149. std::vector<const item_type *> expected;
  150. list2.push_back (a);
  151. list2.push_back (b);
  152. list2.push_back (c);
  153. list2 = std::move (list1);
  154. expected = {};
  155. verify_items (list1, expected);
  156. expected = {};
  157. verify_items (list2, expected);
  158. }
  159. {
  160. /* lhs list is empty. */
  161. item_type a ("a"), b ("b"), c ("c");
  162. ListType list1;
  163. ListType list2;
  164. std::vector<const item_type *> expected;
  165. list1.push_back (a);
  166. list1.push_back (b);
  167. list1.push_back (c);
  168. list2 = std::move (list1);
  169. expected = {};
  170. verify_items (list1, expected);
  171. expected = {&a, &b, &c};
  172. verify_items (list2, expected);
  173. }
  174. {
  175. /* Both lists contain 1 item. */
  176. item_type a ("a"), b ("b");
  177. ListType list1;
  178. ListType list2;
  179. std::vector<const item_type *> expected;
  180. list1.push_back (a);
  181. list2.push_back (b);
  182. list2 = std::move (list1);
  183. expected = {};
  184. verify_items (list1, expected);
  185. expected = {&a};
  186. verify_items (list2, expected);
  187. }
  188. {
  189. /* Both lists are empty. */
  190. ListType list1;
  191. ListType list2;
  192. std::vector<const item_type *> expected;
  193. list2 = std::move (list1);
  194. expected = {};
  195. verify_items (list1, expected);
  196. expected = {};
  197. verify_items (list2, expected);
  198. }
  199. }
  200. static void
  201. test_swap ()
  202. {
  203. {
  204. /* Two non-empty lists. */
  205. item_type a ("a"), b ("b"), c ("c"), d ("d"), e ("e");
  206. ListType list1;
  207. ListType list2;
  208. std::vector<const item_type *> expected;
  209. list1.push_back (a);
  210. list1.push_back (b);
  211. list1.push_back (c);
  212. list2.push_back (d);
  213. list2.push_back (e);
  214. std::swap (list1, list2);
  215. expected = {&d, &e};
  216. verify_items (list1, expected);
  217. expected = {&a, &b, &c};
  218. verify_items (list2, expected);
  219. }
  220. {
  221. /* Other is empty. */
  222. item_type a ("a"), b ("b"), c ("c");
  223. ListType list1;
  224. ListType list2;
  225. std::vector<const item_type *> expected;
  226. list1.push_back (a);
  227. list1.push_back (b);
  228. list1.push_back (c);
  229. std::swap (list1, list2);
  230. expected = {};
  231. verify_items (list1, expected);
  232. expected = {&a, &b, &c};
  233. verify_items (list2, expected);
  234. }
  235. {
  236. /* *this is empty. */
  237. item_type a ("a"), b ("b"), c ("c");
  238. ListType list1;
  239. ListType list2;
  240. std::vector<const item_type *> expected;
  241. list2.push_back (a);
  242. list2.push_back (b);
  243. list2.push_back (c);
  244. std::swap (list1, list2);
  245. expected = {&a, &b, &c};
  246. verify_items (list1, expected);
  247. expected = {};
  248. verify_items (list2, expected);
  249. }
  250. {
  251. /* Both lists empty. */
  252. ListType list1;
  253. ListType list2;
  254. std::vector<const item_type *> expected;
  255. std::swap (list1, list2);
  256. expected = {};
  257. verify_items (list1, expected);
  258. expected = {};
  259. verify_items (list2, expected);
  260. }
  261. {
  262. /* Swap one element twice. */
  263. item_type a ("a");
  264. ListType list1;
  265. ListType list2;
  266. std::vector<const item_type *> expected;
  267. list1.push_back (a);
  268. std::swap (list1, list2);
  269. expected = {};
  270. verify_items (list1, expected);
  271. expected = {&a};
  272. verify_items (list2, expected);
  273. std::swap (list1, list2);
  274. expected = {&a};
  275. verify_items (list1, expected);
  276. expected = {};
  277. verify_items (list2, expected);
  278. }
  279. }
  280. static void
  281. test_front_back ()
  282. {
  283. item_type a ("a"), b ("b"), c ("c");
  284. ListType list;
  285. const ListType &clist = list;
  286. list.push_back (a);
  287. list.push_back (b);
  288. list.push_back (c);
  289. gdb_assert (&list.front () == &a);
  290. gdb_assert (&clist.front () == &a);
  291. gdb_assert (&list.back () == &c);
  292. gdb_assert (&clist.back () == &c);
  293. }
  294. static void
  295. test_push_front ()
  296. {
  297. item_type a ("a"), b ("b"), c ("c");
  298. ListType list;
  299. std::vector<const item_type *> expected;
  300. expected = {};
  301. verify_items (list, expected);
  302. list.push_front (a);
  303. expected = {&a};
  304. verify_items (list, expected);
  305. list.push_front (b);
  306. expected = {&b, &a};
  307. verify_items (list, expected);
  308. list.push_front (c);
  309. expected = {&c, &b, &a};
  310. verify_items (list, expected);
  311. }
  312. static void
  313. test_push_back ()
  314. {
  315. item_type a ("a"), b ("b"), c ("c");
  316. ListType list;
  317. std::vector<const item_type *> expected;
  318. expected = {};
  319. verify_items (list, expected);
  320. list.push_back (a);
  321. expected = {&a};
  322. verify_items (list, expected);
  323. list.push_back (b);
  324. expected = {&a, &b};
  325. verify_items (list, expected);
  326. list.push_back (c);
  327. expected = {&a, &b, &c};
  328. verify_items (list, expected);
  329. }
  330. static void
  331. test_insert ()
  332. {
  333. std::vector<const item_type *> expected;
  334. {
  335. /* Insert at beginning. */
  336. item_type a ("a"), b ("b"), c ("c");
  337. ListType list;
  338. list.insert (list.begin (), a);
  339. expected = {&a};
  340. verify_items (list, expected);
  341. list.insert (list.begin (), b);
  342. expected = {&b, &a};
  343. verify_items (list, expected);
  344. list.insert (list.begin (), c);
  345. expected = {&c, &b, &a};
  346. verify_items (list, expected);
  347. }
  348. {
  349. /* Insert at end. */
  350. item_type a ("a"), b ("b"), c ("c");
  351. ListType list;
  352. list.insert (list.end (), a);
  353. expected = {&a};
  354. verify_items (list, expected);
  355. list.insert (list.end (), b);
  356. expected = {&a, &b};
  357. verify_items (list, expected);
  358. list.insert (list.end (), c);
  359. expected = {&a, &b, &c};
  360. verify_items (list, expected);
  361. }
  362. {
  363. /* Insert in the middle. */
  364. item_type a ("a"), b ("b"), c ("c");
  365. ListType list;
  366. list.push_back (a);
  367. list.push_back (b);
  368. list.insert (list.iterator_to (b), c);
  369. expected = {&a, &c, &b};
  370. verify_items (list, expected);
  371. }
  372. {
  373. /* Insert in empty list. */
  374. item_type a ("a");
  375. ListType list;
  376. list.insert (list.end (), a);
  377. expected = {&a};
  378. verify_items (list, expected);
  379. }
  380. }
  381. static void
  382. test_splice ()
  383. {
  384. {
  385. /* Two non-empty lists. */
  386. item_type a ("a"), b ("b"), c ("c"), d ("d"), e ("e");
  387. ListType list1;
  388. ListType list2;
  389. std::vector<const item_type *> expected;
  390. list1.push_back (a);
  391. list1.push_back (b);
  392. list1.push_back (c);
  393. list2.push_back (d);
  394. list2.push_back (e);
  395. list1.splice (std::move (list2));
  396. expected = {&a, &b, &c, &d, &e};
  397. verify_items (list1, expected);
  398. expected = {};
  399. verify_items (list2, expected);
  400. }
  401. {
  402. /* Receiving list empty. */
  403. item_type a ("a"), b ("b"), c ("c");
  404. ListType list1;
  405. ListType list2;
  406. std::vector<const item_type *> expected;
  407. list2.push_back (a);
  408. list2.push_back (b);
  409. list2.push_back (c);
  410. list1.splice (std::move (list2));
  411. expected = {&a, &b, &c};
  412. verify_items (list1, expected);
  413. expected = {};
  414. verify_items (list2, expected);
  415. }
  416. {
  417. /* Giving list empty. */
  418. item_type a ("a"), b ("b"), c ("c");
  419. ListType list1;
  420. ListType list2;
  421. std::vector<const item_type *> expected;
  422. list1.push_back (a);
  423. list1.push_back (b);
  424. list1.push_back (c);
  425. list1.splice (std::move (list2));
  426. expected = {&a, &b, &c};
  427. verify_items (list1, expected);
  428. expected = {};
  429. verify_items (list2, expected);
  430. }
  431. {
  432. /* Both lists empty. */
  433. item_type a ("a"), b ("b"), c ("c");
  434. ListType list1;
  435. ListType list2;
  436. std::vector<const item_type *> expected;
  437. list1.splice (std::move (list2));
  438. expected = {};
  439. verify_items (list1, expected);
  440. expected = {};
  441. verify_items (list2, expected);
  442. }
  443. }
  444. static void
  445. test_pop_front ()
  446. {
  447. item_type a ("a"), b ("b"), c ("c");
  448. ListType list;
  449. std::vector<const item_type *> expected;
  450. list.push_back (a);
  451. list.push_back (b);
  452. list.push_back (c);
  453. list.pop_front ();
  454. expected = {&b, &c};
  455. verify_items (list, expected);
  456. list.pop_front ();
  457. expected = {&c};
  458. verify_items (list, expected);
  459. list.pop_front ();
  460. expected = {};
  461. verify_items (list, expected);
  462. }
  463. static void
  464. test_pop_back ()
  465. {
  466. item_type a ("a"), b ("b"), c ("c");
  467. ListType list;
  468. std::vector<const item_type *> expected;
  469. list.push_back (a);
  470. list.push_back (b);
  471. list.push_back (c);
  472. list.pop_back();
  473. expected = {&a, &b};
  474. verify_items (list, expected);
  475. list.pop_back ();
  476. expected = {&a};
  477. verify_items (list, expected);
  478. list.pop_back ();
  479. expected = {};
  480. verify_items (list, expected);
  481. }
  482. static void
  483. test_erase ()
  484. {
  485. item_type a ("a"), b ("b"), c ("c");
  486. ListType list;
  487. std::vector<const item_type *> expected;
  488. list.push_back (a);
  489. list.push_back (b);
  490. list.push_back (c);
  491. list.erase (list.iterator_to (b));
  492. expected = {&a, &c};
  493. verify_items (list, expected);
  494. list.erase (list.iterator_to (c));
  495. expected = {&a};
  496. verify_items (list, expected);
  497. list.erase (list.iterator_to (a));
  498. expected = {};
  499. verify_items (list, expected);
  500. }
  501. static void
  502. test_clear ()
  503. {
  504. item_type a ("a"), b ("b"), c ("c");
  505. ListType list;
  506. std::vector<const item_type *> expected;
  507. list.push_back (a);
  508. list.push_back (b);
  509. list.push_back (c);
  510. list.clear ();
  511. expected = {};
  512. verify_items (list, expected);
  513. /* Verify idempotency. */
  514. list.clear ();
  515. expected = {};
  516. verify_items (list, expected);
  517. }
  518. static void
  519. test_clear_and_dispose ()
  520. {
  521. item_type a ("a"), b ("b"), c ("c");
  522. ListType list;
  523. std::vector<const item_type *> expected;
  524. std::unordered_set<const item_type *> disposer_seen;
  525. int disposer_calls = 0;
  526. list.push_back (a);
  527. list.push_back (b);
  528. list.push_back (c);
  529. auto disposer = [&] (const item_type *item)
  530. {
  531. disposer_seen.insert (item);
  532. disposer_calls++;
  533. };
  534. list.clear_and_dispose (disposer);
  535. expected = {};
  536. verify_items (list, expected);
  537. gdb_assert (disposer_calls == 3);
  538. gdb_assert (disposer_seen.find (&a) != disposer_seen.end ());
  539. gdb_assert (disposer_seen.find (&b) != disposer_seen.end ());
  540. gdb_assert (disposer_seen.find (&c) != disposer_seen.end ());
  541. /* Verify idempotency. */
  542. list.clear_and_dispose (disposer);
  543. gdb_assert (disposer_calls == 3);
  544. }
  545. static void
  546. test_empty ()
  547. {
  548. item_type a ("a");
  549. ListType list;
  550. gdb_assert (list.empty ());
  551. list.push_back (a);
  552. gdb_assert (!list.empty ());
  553. list.erase (list.iterator_to (a));
  554. gdb_assert (list.empty ());
  555. }
  556. static void
  557. test_begin_end ()
  558. {
  559. item_type a ("a"), b ("b"), c ("c");
  560. ListType list;
  561. const ListType &clist = list;
  562. list.push_back (a);
  563. list.push_back (b);
  564. list.push_back (c);
  565. gdb_assert (&*list.begin () == &a);
  566. gdb_assert (&*list.cbegin () == &a);
  567. gdb_assert (&*clist.begin () == &a);
  568. gdb_assert (&*list.rbegin () == &c);
  569. gdb_assert (&*list.crbegin () == &c);
  570. gdb_assert (&*clist.rbegin () == &c);
  571. /* At least check that they compile. */
  572. list.end ();
  573. list.cend ();
  574. clist.end ();
  575. list.rend ();
  576. list.crend ();
  577. clist.end ();
  578. }
  579. };
  580. template <typename ListType>
  581. static void
  582. test_intrusive_list_1 ()
  583. {
  584. intrusive_list_test<ListType> tests;
  585. tests.test_move_constructor ();
  586. tests.test_move_assignment ();
  587. tests.test_swap ();
  588. tests.test_front_back ();
  589. tests.test_push_front ();
  590. tests.test_push_back ();
  591. tests.test_insert ();
  592. tests.test_splice ();
  593. tests.test_pop_front ();
  594. tests.test_pop_back ();
  595. tests.test_erase ();
  596. tests.test_clear ();
  597. tests.test_clear_and_dispose ();
  598. tests.test_empty ();
  599. tests.test_begin_end ();
  600. }
  601. static void
  602. test_node_is_linked ()
  603. {
  604. {
  605. item_with_base a ("a");
  606. item_with_base_list list;
  607. gdb_assert (!a.is_linked ());
  608. list.push_back (a);
  609. gdb_assert (a.is_linked ());
  610. list.pop_back ();
  611. gdb_assert (!a.is_linked ());
  612. }
  613. {
  614. item_with_member a ("a");
  615. item_with_member_list list;
  616. gdb_assert (!a.node.is_linked ());
  617. list.push_back (a);
  618. gdb_assert (a.node.is_linked ());
  619. list.pop_back ();
  620. gdb_assert (!a.node.is_linked ());
  621. }
  622. }
  623. static void
  624. test_intrusive_list ()
  625. {
  626. test_intrusive_list_1<item_with_base_list> ();
  627. test_intrusive_list_1<item_with_member_list> ();
  628. test_node_is_linked ();
  629. }
  630. void _initialize_intrusive_list_selftests ();
  631. void
  632. _initialize_intrusive_list_selftests ()
  633. {
  634. selftests::register_test
  635. ("intrusive_list", test_intrusive_list);
  636. }