tst-calloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 2000, 2002 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <limits.h>
  19. #include <malloc.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. /* Number of samples per size. */
  23. #define N 50000
  24. static void
  25. fixed_test (int size)
  26. {
  27. char *ptrs[N];
  28. int i;
  29. for (i = 0; i < N; ++i)
  30. {
  31. int j;
  32. ptrs[i] = (char *) calloc (1, size);
  33. if (ptrs[i] == NULL)
  34. break;
  35. for (j = 0; j < size; ++j)
  36. {
  37. if (ptrs[i][j] != '\0')
  38. error (EXIT_FAILURE, 0,
  39. "byte not cleared (size %d, element %d, byte %d)",
  40. size, i, j);
  41. ptrs[i][j] = '\xff';
  42. }
  43. }
  44. while (i-- > 0)
  45. free (ptrs[i]);
  46. }
  47. static void
  48. random_test (void)
  49. {
  50. char *ptrs[N];
  51. int i;
  52. for (i = 0; i < N; ++i)
  53. {
  54. int j;
  55. int n = 1 + random () % 10;
  56. int elem = 1 + random () % 100;
  57. int size = n * elem;
  58. ptrs[i] = (char *) calloc (n, elem);
  59. if (ptrs[i] == NULL)
  60. break;
  61. for (j = 0; j < size; ++j)
  62. {
  63. if (ptrs[i][j] != '\0')
  64. error (EXIT_FAILURE, 0,
  65. "byte not cleared (size %d, element %d, byte %d)",
  66. size, i, j);
  67. ptrs[i][j] = '\xff';
  68. }
  69. }
  70. while (i-- > 0)
  71. free (ptrs[i]);
  72. }
  73. static void
  74. null_test (void)
  75. {
  76. /* If the size is 0 the result is implementation defined. Just make
  77. sure the program doesn't crash. */
  78. calloc (0, 0);
  79. calloc (0, UINT_MAX);
  80. calloc (UINT_MAX, 0);
  81. calloc (0, ~((size_t) 0));
  82. calloc (~((size_t) 0), 0);
  83. }
  84. int
  85. main (void)
  86. {
  87. /* We are allocating blocks with `calloc' and check whether every
  88. block is completely cleared. We first try this for some fixed
  89. times and then with random size. */
  90. fixed_test (15);
  91. fixed_test (5);
  92. fixed_test (17);
  93. fixed_test (6);
  94. fixed_test (31);
  95. fixed_test (96);
  96. random_test ();
  97. null_test ();
  98. return 0;
  99. }