tst-calloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include <malloc.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. static int errors = 0;
  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. ++errors;
  38. printf("byte not cleared (size %d, element %d, byte %d)",
  39. size, i, j);
  40. }
  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. ++errors;
  65. printf("byte not cleared (size %d, element %d, byte %d)",
  66. size, i, j);
  67. }
  68. ptrs[i][j] = '\xff';
  69. }
  70. }
  71. while (i-- > 0)
  72. free (ptrs[i]);
  73. }
  74. static void
  75. null_test (void)
  76. {
  77. /* If the size is 0 the result is implementation defined. Just make
  78. sure the program doesn't crash. */
  79. calloc (0, 0);
  80. calloc (0, UINT_MAX);
  81. calloc (UINT_MAX, 0);
  82. calloc (0, ~((size_t) 0));
  83. calloc (~((size_t) 0), 0);
  84. }
  85. int
  86. main (void)
  87. {
  88. /* We are allocating blocks with `calloc' and check whether every
  89. block is completely cleared. We first try this for some fixed
  90. times and then with random size. */
  91. fixed_test (15);
  92. fixed_test (5);
  93. fixed_test (17);
  94. fixed_test (6);
  95. fixed_test (31);
  96. fixed_test (96);
  97. random_test ();
  98. null_test ();
  99. return errors != 0;
  100. }