test_container_multihashmap32.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008 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. * @file util/test_container_multihashmap32.c
  18. * @brief Test for container_multihashmap32.c
  19. * @author Christian Grothoff
  20. * @author Sree Harsha Totakura
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (m != NULL) GNUNET_CONTAINER_multihashmap32_destroy(m); return 1; }
  25. #define CHECK(c) { if (! (c)) ABORT(); }
  26. static int
  27. testMap (int i)
  28. {
  29. struct GNUNET_CONTAINER_MultiHashMap32 *m;
  30. uint32_t k1;
  31. uint32_t k2;
  32. const char *ret;
  33. int j;
  34. CHECK (NULL != (m = GNUNET_CONTAINER_multihashmap32_create (i)));
  35. k1 = 0;
  36. k2 = UINT32_MAX;
  37. CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains (m, k1));
  38. CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains (m, k2));
  39. CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_remove (m, k1, NULL));
  40. CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_remove (m, k2, NULL));
  41. CHECK (NULL == GNUNET_CONTAINER_multihashmap32_get (m, k1));
  42. CHECK (NULL == GNUNET_CONTAINER_multihashmap32_get (m, k2));
  43. CHECK (0 == GNUNET_CONTAINER_multihashmap32_remove_all (m, k1));
  44. CHECK (0 == GNUNET_CONTAINER_multihashmap32_size (m));
  45. CHECK (0 == GNUNET_CONTAINER_multihashmap32_iterate (m, NULL, NULL));
  46. CHECK (0 == GNUNET_CONTAINER_multihashmap32_get_multiple (m, k1, NULL, NULL));
  47. CHECK (GNUNET_OK ==
  48. GNUNET_CONTAINER_multihashmap32_put (m, k1, "v1",
  49. GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
  50. CHECK (1 == GNUNET_CONTAINER_multihashmap32_size (m));
  51. ret = GNUNET_CONTAINER_multihashmap32_get (m, k1);
  52. GNUNET_assert (ret != NULL);
  53. CHECK (0 == strcmp ("v1", ret));
  54. CHECK (GNUNET_NO ==
  55. GNUNET_CONTAINER_multihashmap32_put (m, k1, "v1",
  56. GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
  57. CHECK (1 == GNUNET_CONTAINER_multihashmap32_size (m));
  58. CHECK (GNUNET_OK ==
  59. GNUNET_CONTAINER_multihashmap32_put (m, k1, "v2",
  60. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  61. CHECK (GNUNET_OK ==
  62. GNUNET_CONTAINER_multihashmap32_put (m, k1, "v3",
  63. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  64. CHECK (3 == GNUNET_CONTAINER_multihashmap32_size (m));
  65. CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap32_remove (m, k1, "v3"));
  66. CHECK (2 == GNUNET_CONTAINER_multihashmap32_size (m));
  67. CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap32_contains (m, k1));
  68. CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap32_contains (m, k2));
  69. CHECK (2 == GNUNET_CONTAINER_multihashmap32_get_multiple (m, k1, NULL, NULL));
  70. CHECK (0 == GNUNET_CONTAINER_multihashmap32_get_multiple (m, k2, NULL, NULL));
  71. CHECK (2 == GNUNET_CONTAINER_multihashmap32_iterate (m, NULL, NULL));
  72. CHECK (2 == GNUNET_CONTAINER_multihashmap32_remove_all (m, k1));
  73. for (j = 0; j < 1024; j++)
  74. CHECK (GNUNET_OK ==
  75. GNUNET_CONTAINER_multihashmap32_put (m, k1, "v2",
  76. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  77. GNUNET_CONTAINER_multihashmap32_destroy (m);
  78. return 0;
  79. }
  80. int
  81. main (int argc, char *argv[])
  82. {
  83. int failureCount = 0;
  84. int i;
  85. GNUNET_log_setup ("test-container-multihashmap", "WARNING", NULL);
  86. for (i = 1; i < 255; i++)
  87. failureCount += testMap (i);
  88. if (failureCount != 0)
  89. return 1;
  90. return 0;
  91. }
  92. /* end of test_container_multihashmap.c */