cstring.pass.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <cstring>
  9. #include <cstring>
  10. #include <type_traits>
  11. #include "test_macros.h"
  12. #ifndef NULL
  13. #error NULL not defined
  14. #endif
  15. int main(int, char**)
  16. {
  17. std::size_t s = 0;
  18. void* vp = 0;
  19. const void* vpc = 0;
  20. char* cp = 0;
  21. const char* cpc = 0;
  22. ASSERT_SAME_TYPE(void*, decltype(std::memcpy(vp, vpc, s)));
  23. ASSERT_SAME_TYPE(void*, decltype(std::memmove(vp, vpc, s)));
  24. ASSERT_SAME_TYPE(char*, decltype(std::strcpy(cp, cpc)));
  25. ASSERT_SAME_TYPE(char*, decltype(std::strncpy(cp, cpc, s)));
  26. ASSERT_SAME_TYPE(char*, decltype(std::strcat(cp, cpc)));
  27. ASSERT_SAME_TYPE(char*, decltype(std::strncat(cp, cpc, s)));
  28. ASSERT_SAME_TYPE(int, decltype(std::memcmp(vpc, vpc, s)));
  29. ASSERT_SAME_TYPE(int, decltype(std::strcmp(cpc, cpc)));
  30. ASSERT_SAME_TYPE(int, decltype(std::strncmp(cpc, cpc, s)));
  31. ASSERT_SAME_TYPE(int, decltype(std::strcoll(cpc, cpc)));
  32. ASSERT_SAME_TYPE(std::size_t, decltype(std::strxfrm(cp, cpc, s)));
  33. ASSERT_SAME_TYPE(void*, decltype(std::memchr(vp, 0, s)));
  34. ASSERT_SAME_TYPE(char*, decltype(std::strchr(cp, 0)));
  35. ASSERT_SAME_TYPE(std::size_t, decltype(std::strcspn(cpc, cpc)));
  36. ASSERT_SAME_TYPE(char*, decltype(std::strpbrk(cp, cpc)));
  37. ASSERT_SAME_TYPE(char*, decltype(std::strrchr(cp, 0)));
  38. ASSERT_SAME_TYPE(std::size_t, decltype(std::strspn(cpc, cpc)));
  39. ASSERT_SAME_TYPE(char*, decltype(std::strstr(cp, cpc)));
  40. #ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
  41. ASSERT_SAME_TYPE(char*, decltype(std::strtok(cp, cpc)));
  42. #endif
  43. ASSERT_SAME_TYPE(void*, decltype(std::memset(vp, 0, s)));
  44. ASSERT_SAME_TYPE(char*, decltype(std::strerror(0)));
  45. ASSERT_SAME_TYPE(std::size_t, decltype(std::strlen(cpc)));
  46. // These tests fail on systems whose C library doesn't provide a correct overload
  47. // set for strchr, strpbrk, strrchr, strstr, and memchr, unless the compiler is
  48. // a suitably recent version of Clang.
  49. #if !defined(__APPLE__) || defined(_LIBCPP_PREFERRED_OVERLOAD)
  50. ASSERT_SAME_TYPE(const void*, decltype(std::memchr(vpc, 0, s)));
  51. ASSERT_SAME_TYPE(const char*, decltype(std::strchr(cpc, 0)));
  52. ASSERT_SAME_TYPE(const char*, decltype(std::strpbrk(cpc, cpc)));
  53. ASSERT_SAME_TYPE(const char*, decltype(std::strrchr(cpc, 0)));
  54. ASSERT_SAME_TYPE(const char*, decltype(std::strstr(cpc, cpc)));
  55. #endif
  56. return 0;
  57. }