testcopy.c 2.9 KB

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