tst-calloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <limits.h>
  18. #include <malloc.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. static int errors = 0;
  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. ++errors;
  39. printf("byte not cleared (size %d, element %d, byte %d)",
  40. size, i, j);
  41. }
  42. ptrs[i][j] = '\xff';
  43. }
  44. }
  45. while (i-- > 0)
  46. free (ptrs[i]);
  47. }
  48. static void
  49. random_test (void)
  50. {
  51. char *ptrs[N];
  52. int i;
  53. for (i = 0; i < N; ++i)
  54. {
  55. int j;
  56. int n = 1 + random () % 10;
  57. int elem = 1 + random () % 100;
  58. int size = n * elem;
  59. ptrs[i] = (char *) calloc (n, elem);
  60. if (ptrs[i] == NULL)
  61. break;
  62. for (j = 0; j < size; ++j)
  63. {
  64. if (ptrs[i][j] != '\0') {
  65. ++errors;
  66. printf("byte not cleared (size %d, element %d, byte %d)",
  67. size, i, j);
  68. }
  69. ptrs[i][j] = '\xff';
  70. }
  71. }
  72. while (i-- > 0)
  73. free (ptrs[i]);
  74. }
  75. static void
  76. null_test (void)
  77. {
  78. /* If the size is 0 the result is implementation defined. Just make
  79. sure the program doesn't crash. */
  80. calloc (0, 0);
  81. calloc (0, UINT_MAX);
  82. calloc (UINT_MAX, 0);
  83. calloc (0, ~((size_t) 0));
  84. calloc (~((size_t) 0), 0);
  85. }
  86. int
  87. main (void)
  88. {
  89. /* We are allocating blocks with `calloc' and check whether every
  90. block is completely cleared. We first try this for some fixed
  91. times and then with random size. */
  92. fixed_test (15);
  93. fixed_test (5);
  94. fixed_test (17);
  95. fixed_test (6);
  96. fixed_test (31);
  97. fixed_test (96);
  98. random_test ();
  99. null_test ();
  100. return errors != 0;
  101. }