testcopy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 1990, 1991, 1992, 1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Torbjorn Granlund (tege@sics.se).
  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 <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <malloc.h>
  19. int
  20. main (void)
  21. {
  22. char *mem, *memp;
  23. char *rand_mem;
  24. char *lo_around, *hi_around;
  25. int size, max_size;
  26. int src_off, dst_off;
  27. int i;
  28. int space_around = 10;
  29. max_size = 256;
  30. mem = malloc (max_size + 2 * max_size + 2 * space_around);
  31. rand_mem = malloc (max_size);
  32. lo_around = malloc (space_around);
  33. hi_around = malloc (space_around);
  34. memp = mem + space_around;
  35. /* Fill RAND_MEM with random bytes, each non-zero. */
  36. for (i = 0; i < max_size; i++)
  37. {
  38. int x;
  39. do
  40. x = random ();
  41. while (x == 0);
  42. rand_mem[i] = x;
  43. }
  44. for (size = 0; size < max_size; size++)
  45. {
  46. printf("phase %d\n", size);
  47. for (src_off = 0; src_off <= 16; src_off++)
  48. {
  49. for (dst_off = 0; dst_off <= 16; dst_off++)
  50. {
  51. /* Put zero around the intended destination, to check
  52. that it's not clobbered. */
  53. for (i = 1; i < space_around; i++)
  54. {
  55. memp[dst_off - i] = 0;
  56. memp[dst_off + size - 1 + i] = 0;
  57. }
  58. /* Fill the source area with known contents. */
  59. for (i = 0; i < size; i++)
  60. memp[src_off + i] = rand_mem[i];
  61. /* Remember the contents around the destination area.
  62. (It might not be what we wrote some lines above, since
  63. the src area and the dst area overlap.) */
  64. for (i = 1; i < space_around; i++)
  65. {
  66. lo_around[i] = memp[dst_off - i];
  67. hi_around[i] = memp[dst_off + size - 1 + i];
  68. }
  69. memmove (memp + dst_off, memp + src_off, size);
  70. /* Check that the destination area has the same
  71. contents we wrote to the source area. */
  72. for (i = 0; i < size; i++)
  73. {
  74. if (memp[dst_off + i] != rand_mem[i])
  75. abort ();
  76. }
  77. /* Check that the area around the destination is not
  78. clobbered. */
  79. for (i = 1; i < space_around; i++)
  80. {
  81. if (memp[dst_off - i] != lo_around[i])
  82. abort ();
  83. if (memp[dst_off + size - 1 + i] != hi_around[i])
  84. abort ();
  85. }
  86. }
  87. }
  88. }
  89. puts ("Test succeeded.");
  90. return 0;
  91. }