test_container_dll.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2017 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. * @file util/test_container_dll.c
  19. * @brief Test of DLL operations
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. /**
  24. * Element in the DLL.
  25. */
  26. struct Element
  27. {
  28. /**
  29. * Required pointer to previous element.
  30. */
  31. struct Element *prev;
  32. /**
  33. * Required pointer to next element.
  34. */
  35. struct Element *next;
  36. /**
  37. * Used to sort.
  38. */
  39. unsigned int value;
  40. };
  41. /**
  42. * Compare two elements.
  43. *
  44. * @param cls closure, NULL
  45. * @param e1 an element of to sort
  46. * @param e2 another element to sort
  47. * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
  48. */
  49. static int
  50. cmp_elem (void *cls,
  51. struct Element *e1,
  52. struct Element *e2)
  53. {
  54. if (e1->value == e2->value)
  55. return 0;
  56. return (e1->value < e2->value) ? 1 : -1;
  57. }
  58. int
  59. main (int argc, char **argv)
  60. {
  61. unsigned int values[] = {
  62. 4, 5, 8, 6, 9, 3, 7, 2, 1, 0
  63. };
  64. struct Element *head = NULL;
  65. struct Element *tail = NULL;
  66. struct Element *e;
  67. unsigned int want;
  68. GNUNET_log_setup ("test-container-dll",
  69. "WARNING",
  70. NULL);
  71. for (unsigned int off = 0;
  72. 0 != values[off];
  73. off++)
  74. {
  75. e = GNUNET_new (struct Element);
  76. e->value = values[off];
  77. GNUNET_CONTAINER_DLL_insert_sorted (struct Element,
  78. cmp_elem,
  79. NULL,
  80. head,
  81. tail,
  82. e);
  83. }
  84. want = 1;
  85. while (NULL != (e = head))
  86. {
  87. GNUNET_assert (e->value == want);
  88. GNUNET_CONTAINER_DLL_remove (head,
  89. tail,
  90. e);
  91. GNUNET_free (e);
  92. want++;
  93. }
  94. return 0;
  95. }
  96. /* end of test_container_heap.c */