tst-scratch_buffer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  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 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. <https://www.gnu.org/licenses/>. */
  15. #include <array_length.h>
  16. #include <scratch_buffer.h>
  17. #include <support/check.h>
  18. #include <support/support.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. static bool
  23. unchanged_array_size (struct scratch_buffer *buf, size_t a, size_t b)
  24. {
  25. size_t old_length = buf->length;
  26. if (!scratch_buffer_set_array_size (buf, a, b))
  27. {
  28. printf ("scratch_buffer_set_array_size failed: %zu %zu\n",
  29. a, b);
  30. return false;
  31. }
  32. if (old_length != buf->length)
  33. {
  34. printf ("scratch_buffer_set_array_size did not preserve size: %zu %zu\n",
  35. a, b);
  36. return false;
  37. }
  38. return true;
  39. }
  40. static bool
  41. array_size_must_fail (size_t a, size_t b)
  42. {
  43. for (int pass = 0; pass < 2; ++pass)
  44. {
  45. struct scratch_buffer buf;
  46. scratch_buffer_init (&buf);
  47. if (pass > 0)
  48. if (!scratch_buffer_grow (&buf))
  49. {
  50. printf ("scratch_buffer_grow in array_size_must_fail failed\n");
  51. return false;
  52. }
  53. if (scratch_buffer_set_array_size (&buf, a, b))
  54. {
  55. printf ("scratch_buffer_set_array_size passed: %d %zu %zu\n",
  56. pass, a, b);
  57. return false;
  58. }
  59. if (buf.data != buf.__space.__c)
  60. {
  61. printf ("scratch_buffer_set_array_size did not free: %d %zu %zu\n",
  62. pass, a, b);
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. static int
  69. do_test (void)
  70. {
  71. {
  72. struct scratch_buffer buf;
  73. scratch_buffer_init (&buf);
  74. memset (buf.data, ' ', buf.length);
  75. scratch_buffer_free (&buf);
  76. }
  77. {
  78. struct scratch_buffer buf;
  79. scratch_buffer_init (&buf);
  80. memset (buf.data, ' ', buf.length);
  81. size_t old_length = buf.length;
  82. scratch_buffer_grow (&buf);
  83. if (buf.length <= old_length)
  84. {
  85. printf ("scratch_buffer_grow did not enlarge buffer\n");
  86. return 1;
  87. }
  88. memset (buf.data, ' ', buf.length);
  89. scratch_buffer_free (&buf);
  90. }
  91. {
  92. struct scratch_buffer buf;
  93. scratch_buffer_init (&buf);
  94. memset (buf.data, '@', buf.length);
  95. strcpy (buf.data, "prefix");
  96. size_t old_length = buf.length;
  97. scratch_buffer_grow_preserve (&buf);
  98. if (buf.length <= old_length)
  99. {
  100. printf ("scratch_buffer_grow_preserve did not enlarge buffer\n");
  101. return 1;
  102. }
  103. if (strcmp (buf.data, "prefix") != 0)
  104. {
  105. printf ("scratch_buffer_grow_preserve did not copy buffer\n");
  106. return 1;
  107. }
  108. for (unsigned i = 7; i < old_length; ++i)
  109. if (((char *)buf.data)[i] != '@')
  110. {
  111. printf ("scratch_buffer_grow_preserve did not copy buffer (%u)\n",
  112. i);
  113. return 1;
  114. }
  115. scratch_buffer_free (&buf);
  116. }
  117. {
  118. struct scratch_buffer buf;
  119. scratch_buffer_init (&buf);
  120. for (int pass = 0; pass < 4; ++pass)
  121. {
  122. if (!(unchanged_array_size (&buf, 0, 0)
  123. && unchanged_array_size (&buf, 1, 0)
  124. && unchanged_array_size (&buf, 0, 1)
  125. && unchanged_array_size (&buf, -1, 0)
  126. && unchanged_array_size (&buf, 0, -1)
  127. && unchanged_array_size (&buf, 1ULL << 16, 0)
  128. && unchanged_array_size (&buf, 0, 1ULL << 16)
  129. && unchanged_array_size (&buf, (size_t) (1ULL << 32), 0)
  130. && unchanged_array_size (&buf, 0, (size_t) (1ULL << 32))))
  131. return 1;
  132. if (!scratch_buffer_grow (&buf))
  133. {
  134. printf ("scratch_buffer_grow_failed (pass %d)\n", pass);
  135. }
  136. }
  137. scratch_buffer_free (&buf);
  138. }
  139. {
  140. if (!(array_size_must_fail (-1, 1)
  141. && array_size_must_fail (-1, -1)
  142. && array_size_must_fail (1, -1)
  143. && array_size_must_fail (((size_t)-1) / 4, 4)
  144. && array_size_must_fail (4, ((size_t)-1) / 4)))
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. #include <support/test-driver.c>