test-stpncpy.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Test and measure stpncpy functions.
  2. Copyright (C) 1999-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. #define STRNCPY_RESULT(dst, len, n) ((dst) + ((len) > (n) ? (n) : (len)))
  16. #define TEST_MAIN
  17. #ifndef WIDE
  18. # define TEST_NAME "stpncpy"
  19. #else
  20. # define TEST_NAME "wcpncpy"
  21. #endif /* WIDE */
  22. #include "test-string.h"
  23. #ifndef WIDE
  24. # define CHAR char
  25. # define SIMPLE_STPNCPY simple_stpncpy
  26. # define STUPID_STPNCPY stupid_stpncpy
  27. # define STPNCPY stpncpy
  28. # define STRNLEN strnlen
  29. #else
  30. # include <wchar.h>
  31. # define CHAR wchar_t
  32. # define SIMPLE_STPNCPY simple_wcpncpy
  33. # define STUPID_STPNCPY stupid_wcpncpy
  34. # define STPNCPY wcpncpy
  35. # define STRNLEN wcsnlen
  36. #endif /* WIDE */
  37. CHAR *SIMPLE_STPNCPY (CHAR *, const CHAR *, size_t);
  38. CHAR *STUPID_STPNCPY (CHAR *, const CHAR *, size_t);
  39. IMPL (STUPID_STPNCPY, 0)
  40. IMPL (SIMPLE_STPNCPY, 0)
  41. IMPL (STPNCPY, 1)
  42. CHAR *
  43. SIMPLE_STPNCPY (CHAR *dst, const CHAR *src, size_t n)
  44. {
  45. while (n--)
  46. if ((*dst++ = *src++) == '\0')
  47. {
  48. size_t i;
  49. for (i = 0; i < n; ++i)
  50. dst[i] = '\0';
  51. return dst - 1;
  52. }
  53. return dst;
  54. }
  55. CHAR *
  56. STUPID_STPNCPY (CHAR *dst, const CHAR *src, size_t n)
  57. {
  58. size_t nc = STRNLEN (src, n);
  59. size_t i;
  60. for (i = 0; i < nc; ++i)
  61. dst[i] = src[i];
  62. for (; i < n; ++i)
  63. dst[i] = '\0';
  64. return dst + nc;
  65. }
  66. #undef CHAR
  67. #include "test-strncpy.c"