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 #ifndef NULL
     16 #error NULL not defined
     17 #endif
     18 
     19 int main()
     20 {
     21     std::size_t s = 0;
     22     void* vp = 0;
     23     const void* vpc = 0;
     24     char* cp = 0;
     25     const char* cpc = 0;
     26     static_assert((std::is_same<decltype(std::memcpy(vp, vpc, s)), void*>::value), "");
     27     static_assert((std::is_same<decltype(std::memmove(vp, vpc, s)), void*>::value), "");
     28     static_assert((std::is_same<decltype(std::strcpy(cp, cpc)), char*>::value), "");
     29     static_assert((std::is_same<decltype(std::strncpy(cp, cpc, s)), char*>::value), "");
     30     static_assert((std::is_same<decltype(std::strcat(cp, cpc)), char*>::value), "");
     31     static_assert((std::is_same<decltype(std::strncat(cp, cpc, s)), char*>::value), "");
     32     static_assert((std::is_same<decltype(std::memcmp(vpc, vpc, s)), int>::value), "");
     33     static_assert((std::is_same<decltype(std::strcmp(cpc, cpc)), int>::value), "");
     34     static_assert((std::is_same<decltype(std::strncmp(cpc, cpc, s)), int>::value), "");
     35     static_assert((std::is_same<decltype(std::strcoll(cpc, cpc)), int>::value), "");
     36     static_assert((std::is_same<decltype(std::strxfrm(cp, cpc, s)), std::size_t>::value), "");
     37     static_assert((std::is_same<decltype(std::memchr(vp, 0, s)), void*>::value), "");
     38     static_assert((std::is_same<decltype(std::strchr(cp, 0)), char*>::value), "");
     39     static_assert((std::is_same<decltype(std::strcspn(cpc, cpc)), std::size_t>::value), "");
     40     static_assert((std::is_same<decltype(std::strpbrk(cp, cpc)), char*>::value), "");
     41     static_assert((std::is_same<decltype(std::strrchr(cp, 0)), char*>::value), "");
     42     static_assert((std::is_same<decltype(std::strspn(cpc, cpc)), std::size_t>::value), "");
     43     static_assert((std::is_same<decltype(std::strstr(cp, cpc)), char*>::value), "");
     44 #ifndef _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
     45     static_assert((std::is_same<decltype(std::strtok(cp, cpc)), char*>::value), "");
     46 #endif
     47     static_assert((std::is_same<decltype(std::memset(vp, 0, s)), void*>::value), "");
     48     static_assert((std::is_same<decltype(std::strerror(0)), char*>::value), "");
     49     static_assert((std::is_same<decltype(std::strlen(cpc)), std::size_t>::value), "");
     50 
     51     // These tests fail on systems whose C library doesn't provide a correct overload
     52     // set for strchr, strpbrk, strrchr, strstr, and memchr, unless the compiler is
     53     // a suitably recent version of Clang.
     54 #if !defined(__APPLE__) || defined(_LIBCPP_PREFERRED_OVERLOAD)
     55     static_assert((std::is_same<decltype(std::memchr(vpc, 0, s)), const void*>::value), "");
     56     static_assert((std::is_same<decltype(std::strchr(cpc, 0)), const char*>::value), "");
     57     static_assert((std::is_same<decltype(std::strpbrk(cpc, cpc)), const char*>::value), "");
     58     static_assert((std::is_same<decltype(std::strrchr(cpc, 0)), const char*>::value), "");
     59     static_assert((std::is_same<decltype(std::strstr(cpc, cpc)), const char*>::value), "");
     60 #endif
     61 }
     62