test-stpncpy.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Test and measure stpncpy functions.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Jakub Jelinek <jakub@redhat.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #define STRNCPY_RESULT(dst, len, n) ((dst) + ((len) > (n) ? (n) : (len)))
  17. #define TEST_MAIN
  18. #ifndef WIDE
  19. # define TEST_NAME "stpncpy"
  20. #else
  21. # define TEST_NAME "wcpncpy"
  22. #endif /* WIDE */
  23. #include "test-string.h"
  24. #ifndef WIDE
  25. # define CHAR char
  26. # define SIMPLE_STPNCPY simple_stpncpy
  27. # define STUPID_STPNCPY stupid_stpncpy
  28. # define STPNCPY stpncpy
  29. # define STRNLEN strnlen
  30. #else
  31. # include <wchar.h>
  32. # define CHAR wchar_t
  33. # define SIMPLE_STPNCPY simple_wcpncpy
  34. # define STUPID_STPNCPY stupid_wcpncpy
  35. # define STPNCPY wcpncpy
  36. # define STRNLEN wcsnlen
  37. #endif /* WIDE */
  38. CHAR *SIMPLE_STPNCPY (CHAR *, const CHAR *, size_t);
  39. CHAR *STUPID_STPNCPY (CHAR *, const CHAR *, size_t);
  40. IMPL (STUPID_STPNCPY, 0)
  41. IMPL (SIMPLE_STPNCPY, 0)
  42. IMPL (STPNCPY, 1)
  43. CHAR *
  44. SIMPLE_STPNCPY (CHAR *dst, const CHAR *src, size_t n)
  45. {
  46. while (n--)
  47. if ((*dst++ = *src++) == '\0')
  48. {
  49. size_t i;
  50. for (i = 0; i < n; ++i)
  51. dst[i] = '\0';
  52. return dst - 1;
  53. }
  54. return dst;
  55. }
  56. CHAR *
  57. STUPID_STPNCPY (CHAR *dst, const CHAR *src, size_t n)
  58. {
  59. size_t nc = STRNLEN (src, n);
  60. size_t i;
  61. for (i = 0; i < nc; ++i)
  62. dst[i] = src[i];
  63. for (; i < n; ++i)
  64. dst[i] = '\0';
  65. return dst + nc;
  66. }
  67. #undef CHAR
  68. #include "test-strncpy.c"