testcopy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (C) 1990-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <support/support.h>
  18. static int
  19. do_test (void)
  20. {
  21. char *mem, *memp;
  22. char *rand_mem;
  23. char *lo_around, *hi_around;
  24. int size, max_size;
  25. int src_off, dst_off;
  26. int i;
  27. int space_around = 10;
  28. max_size = 256;
  29. mem = xmalloc (max_size + 2 * max_size + 2 * space_around);
  30. rand_mem = xmalloc (max_size);
  31. lo_around = xmalloc (space_around);
  32. hi_around = xmalloc (space_around);
  33. memp = mem + space_around;
  34. /* Fill RAND_MEM with random bytes, each non-zero. */
  35. for (i = 0; i < max_size; i++)
  36. {
  37. int x;
  38. do
  39. x = random ();
  40. while (x == 0);
  41. rand_mem[i] = x;
  42. }
  43. for (size = 0; size < max_size; size++)
  44. {
  45. printf("phase %d\n", size);
  46. for (src_off = 0; src_off <= 16; src_off++)
  47. {
  48. for (dst_off = 0; dst_off <= 16; dst_off++)
  49. {
  50. /* Put zero around the intended destination, to check
  51. that it's not clobbered. */
  52. for (i = 1; i < space_around; i++)
  53. {
  54. memp[dst_off - i] = 0;
  55. memp[dst_off + size - 1 + i] = 0;
  56. }
  57. /* Fill the source area with known contents. */
  58. for (i = 0; i < size; i++)
  59. memp[src_off + i] = rand_mem[i];
  60. /* Remember the contents around the destination area.
  61. (It might not be what we wrote some lines above, since
  62. the src area and the dst area overlap.) */
  63. for (i = 1; i < space_around; i++)
  64. {
  65. lo_around[i] = memp[dst_off - i];
  66. hi_around[i] = memp[dst_off + size - 1 + i];
  67. }
  68. memmove (memp + dst_off, memp + src_off, size);
  69. /* Check that the destination area has the same
  70. contents we wrote to the source area. */
  71. for (i = 0; i < size; i++)
  72. {
  73. if (memp[dst_off + i] != rand_mem[i])
  74. abort ();
  75. }
  76. /* Check that the area around the destination is not
  77. clobbered. */
  78. for (i = 1; i < space_around; i++)
  79. {
  80. if (memp[dst_off - i] != lo_around[i])
  81. abort ();
  82. if (memp[dst_off + size - 1 + i] != hi_around[i])
  83. abort ();
  84. }
  85. }
  86. }
  87. }
  88. puts ("Test succeeded.");
  89. return 0;
  90. }
  91. #include <support/test-driver.c>