test_common_allocation.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001, 2002, 2003, 2005, 2006, 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. * @file util/test_common_allocation.c
  18. * @brief testcase for common_allocation.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. static int
  23. check ()
  24. {
  25. #define MAX_TESTVAL 1024
  26. char *ptrs[MAX_TESTVAL];
  27. unsigned int **a2;
  28. char ***a3;
  29. int i;
  30. int j;
  31. int k;
  32. unsigned int ui;
  33. /* GNUNET_malloc/GNUNET_free test */
  34. k = 352; /* random start value */
  35. for (i = 1; i < MAX_TESTVAL; i++)
  36. {
  37. ptrs[i] = GNUNET_malloc (i);
  38. for (j = 0; j < i; j++)
  39. ptrs[i][j] = k++;
  40. }
  41. for (i = MAX_TESTVAL - 1; i >= 1; i--)
  42. {
  43. for (j = i - 1; j >= 0; j--)
  44. if (ptrs[i][j] != (char) --k)
  45. return 1;
  46. GNUNET_free (ptrs[i]);
  47. }
  48. /* GNUNET_free_non_null test */
  49. GNUNET_free_non_null (NULL);
  50. GNUNET_free_non_null (GNUNET_malloc (4));
  51. /* GNUNET_strdup tests */
  52. ptrs[0] = GNUNET_strdup ("bar");
  53. if (0 != strcmp (ptrs[0], "bar"))
  54. return 3;
  55. /* now realloc */
  56. ptrs[0] = GNUNET_realloc (ptrs[0], 12);
  57. strcpy (ptrs[0], "Hello World");
  58. GNUNET_free (ptrs[0]);
  59. GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
  60. GNUNET_assert (strlen (ptrs[0]) == 11);
  61. GNUNET_free (ptrs[0]);
  62. /* GNUNET_array_grow tests */
  63. ptrs[0] = NULL;
  64. ui = 0;
  65. GNUNET_array_grow (ptrs[0], ui, 42);
  66. if (ui != 42)
  67. return 4;
  68. GNUNET_array_grow (ptrs[0], ui, 22);
  69. if (ui != 22)
  70. return 5;
  71. for (j = 0; j < 22; j++)
  72. ptrs[0][j] = j;
  73. GNUNET_array_grow (ptrs[0], ui, 32);
  74. for (j = 0; j < 22; j++)
  75. if (ptrs[0][j] != j)
  76. return 6;
  77. for (j = 22; j < 32; j++)
  78. if (ptrs[0][j] != 0)
  79. return 7;
  80. GNUNET_array_grow (ptrs[0], ui, 0);
  81. if (i != 0)
  82. return 8;
  83. if (ptrs[0] != NULL)
  84. return 9;
  85. /* GNUNET_new_array_2d tests */
  86. a2 = GNUNET_new_array_2d (17, 22, unsigned int);
  87. for (i = 0; i < 17; i++)
  88. {
  89. for (j = 0; j < 22; j++)
  90. {
  91. if (0 != a2[i][j])
  92. {
  93. GNUNET_free (a2);
  94. return 10;
  95. }
  96. a2[i][j] = i * 100 + j;
  97. }
  98. }
  99. GNUNET_free (a2);
  100. /* GNUNET_new_array_3d tests */
  101. a3 = GNUNET_new_array_3d (2, 3, 4, char);
  102. for (i = 0; i < 2; i++)
  103. {
  104. for (j = 0; j < 3; j++)
  105. {
  106. for (k = 0; k < 4; k++)
  107. {
  108. if (0 != a3[i][j][k])
  109. {
  110. GNUNET_free (a3);
  111. return 11;
  112. }
  113. a3[i][j][k] = i * 100 + j * 10 + k;
  114. }
  115. }
  116. }
  117. GNUNET_free (a3);
  118. return 0;
  119. }
  120. int
  121. main (int argc, char *argv[])
  122. {
  123. int ret;
  124. GNUNET_log_setup ("test-common-allocation",
  125. "WARNING",
  126. NULL);
  127. ret = check ();
  128. if (ret != 0)
  129. FPRINTF (stderr,
  130. "ERROR %d.\n",
  131. ret);
  132. return ret;
  133. }
  134. /* end of test_common_allocation.c */