Home | History | Annotate | Download | only in c.strings
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <cstring>
     11 
     12 #include <cstring>
     13 #include <type_traits>
     14 
     15 #include "test_macros.h"
     16 
     17 #ifndef NULL
     18 #error NULL not defined
     19 #endif
     20 
     21 int main()
     22 {
     23     std::size_t s = 0;
     24     void* vp = 0;
     25     const void* vpc = 0;
     26     char* cp = 0;
     27     const char* cpc = 0;
     28 
     29     ASSERT_SAME_TYPE(void*,       decltype(std::memcpy(vp, vpc, s)));
     30     ASSERT_SAME_TYPE(void*,       decltype(std::memmove(vp, vpc, s)));
     31     ASSERT_SAME_TYPE(char*,       decltype(std::strcpy(cp, cpc)));
     32     ASSERT_SAME_TYPE(char*,       decltype(std::strncpy(cp, cpc, s)));
     33     ASSERT_SAME_TYPE(char*,       decltype(std::strcat(cp, cpc)));
     34     ASSERT_SAME_TYPE(char*,       decltype(std::strncat(cp, cpc, s)));
     35     ASSERT_SAME_TYPE(int,         decltype(std::memcmp(vpc, vpc, s)));
     36     ASSERT_SAME_TYPE(int,         decltype(std::strcmp(cpc, cpc)));
     37     ASSERT_SAME_TYPE(int,         decltype(std::strncmp(cpc, cpc, s)));
     38     ASSERT_SAME_TYPE(int,         decltype(std::strcoll(cpc, cpc)));
     39     ASSERT_SAME_TYPE(std::size_t, decltype(std::strxfrm(cp, cpc, s)));
     40     ASSERT_SAME_TYPE(void*,       decltype(std::memchr(vp, 0, s)));
     41     ASSERT_SAME_TYPE(char*,       decltype(std::strchr(cp, 0)));
     42     ASSERT_SAME_TYPE(std::size_t, decltype(std::strcspn(cpc, cpc)));
     43     ASSERT_SAME_TYPE(char*,       decltype(std::strpbrk(cp, cpc)));
     44     ASSERT_SAME_TYPE(char*,       decltype(std::strrchr(cp, 0)));
     45     ASSERT_SAME_TYPE(std::size_t, decltype(std::strspn(cpc, cpc)));
     46     ASSERT_SAME_TYPE(char*,       decltype(std::strstr(cp, cpc)));
     47 #ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
     48     ASSERT_SAME_TYPE(char*,       decltype(std::strtok(cp, cpc)));
     49 #endif
     50     ASSERT_SAME_TYPE(void*,       decltype(std::memset(vp, 0, s)));
     51     ASSERT_SAME_TYPE(char*,       decltype(std::strerror(0)));
     52     ASSERT_SAME_TYPE(std::size_t, decltype(std::strlen(cpc)));
     53 
     54     // These tests fail on systems whose C library doesn't provide a correct overload
     55     // set for strchr, strpbrk, strrchr, strstr, and memchr, unless the compiler is
     56     // a suitably recent version of Clang.
     57 #if !defined(__APPLE__) || defined(_LIBCPP_PREFERRED_OVERLOAD)
     58     ASSERT_SAME_TYPE(const void*, decltype(std::memchr(vpc, 0, s)));
     59     ASSERT_SAME_TYPE(const char*, decltype(std::strchr(cpc, 0)));
     60     ASSERT_SAME_TYPE(const char*, decltype(std::strpbrk(cpc, cpc)));
     61     ASSERT_SAME_TYPE(const char*, decltype(std::strrchr(cpc, 0)));
     62     ASSERT_SAME_TYPE(const char*, decltype(std::strstr(cpc, cpc)));
     63 #endif
     64 }
     65