tst-calloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright (C) 2000-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <error.h>
  17. #include <limits.h>
  18. #include <malloc.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. /* Number of samples per size. */
  22. #define N 50000
  23. static void
  24. fixed_test (int size)
  25. {
  26. char *ptrs[N];
  27. int i;
  28. for (i = 0; i < N; ++i)
  29. {
  30. int j;
  31. ptrs[i] = (char *) calloc (1, size);
  32. if (ptrs[i] == NULL)
  33. break;
  34. for (j = 0; j < size; ++j)
  35. {
  36. if (ptrs[i][j] != '\0')
  37. error (EXIT_FAILURE, 0,
  38. "byte not cleared (size %d, element %d, byte %d)",
  39. size, i, j);
  40. ptrs[i][j] = '\xff';
  41. }
  42. }
  43. while (i-- > 0)
  44. free (ptrs[i]);
  45. }
  46. static void
  47. random_test (void)
  48. {
  49. char *ptrs[N];
  50. int i;
  51. for (i = 0; i < N; ++i)
  52. {
  53. int j;
  54. int n = 1 + random () % 10;
  55. int elem = 1 + random () % 100;
  56. int size = n * elem;
  57. ptrs[i] = (char *) calloc (n, elem);
  58. if (ptrs[i] == NULL)
  59. break;
  60. for (j = 0; j < size; ++j)
  61. {
  62. if (ptrs[i][j] != '\0')
  63. error (EXIT_FAILURE, 0,
  64. "byte not cleared (size %d, element %d, byte %d)",
  65. size, i, j);
  66. ptrs[i][j] = '\xff';
  67. }
  68. }
  69. while (i-- > 0)
  70. free (ptrs[i]);
  71. }
  72. static void
  73. null_test (void)
  74. {
  75. /* If the size is 0 the result is implementation defined. Just make
  76. sure the program doesn't crash. */
  77. calloc (0, 0);
  78. calloc (0, UINT_MAX);
  79. calloc (UINT_MAX, 0);
  80. calloc (0, ~((size_t) 0));
  81. calloc (~((size_t) 0), 0);
  82. }
  83. static int
  84. do_test (void)
  85. {
  86. /* We are allocating blocks with `calloc' and check whether every
  87. block is completely cleared. We first try this for some fixed
  88. times and then with random size. */
  89. fixed_test (15);
  90. fixed_test (5);
  91. fixed_test (17);
  92. fixed_test (6);
  93. fixed_test (31);
  94. fixed_test (96);
  95. random_test ();
  96. null_test ();
  97. return 0;
  98. }
  99. #define TEST_FUNCTION do_test ()
  100. #include "../test-skeleton.c"