testcopy.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  2. Contributed by Torbjorn Granlund (tege@sics.se).
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <malloc.h>
  20. int main(int argc, char **argv)
  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 = rand ();
  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. }