| 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 | |
| 9 | // <cstring> |
| 10 | |
| 11 | #include <cstring> |
| 12 | #include <cassert> |
| 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(int, char**) { |
| 22 | // Functions we get directly from the C library (just check the signature) |
| 23 | { |
| 24 | std::size_t s = 0; |
| 25 | void* vp = 0; |
| 26 | const void* vpc = 0; |
| 27 | char* cp = 0; |
| 28 | const char* cpc = 0; |
| 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(std::size_t, decltype(std::strcspn(cpc, cpc))); |
| 41 | ASSERT_SAME_TYPE(std::size_t, decltype(std::strspn(cpc, cpc))); |
| 42 | ASSERT_SAME_TYPE(char*, decltype(std::strtok(cp, cpc))); |
| 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 | } |
| 47 | |
| 48 | // Functions we (may) reimplement |
| 49 | { |
| 50 | // const char* strchr(const char*, int) |
| 51 | char storage[] = "hello world" ; |
| 52 | const char* s = storage; |
| 53 | ASSERT_SAME_TYPE(const char*, decltype(std::strchr(s, 'l'))); |
| 54 | const char* res = std::strchr(s: s, c: 'l'); |
| 55 | assert(res == &s[2]); |
| 56 | } |
| 57 | { |
| 58 | // char* strchr(char*, int) |
| 59 | char storage[] = "hello world" ; |
| 60 | char* s = storage; |
| 61 | ASSERT_SAME_TYPE(char*, decltype(std::strchr(s, 'l'))); |
| 62 | char* res = std::strchr(s: s, c: 'l'); |
| 63 | assert(res == &s[2]); |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | // const char* strpbrk(const char*, const char*) |
| 68 | char storage[] = "hello world" ; |
| 69 | const char* s = storage; |
| 70 | ASSERT_SAME_TYPE(const char*, decltype(std::strpbrk(s, "el" ))); |
| 71 | const char* res = std::strpbrk(s: s, accept: "el" ); |
| 72 | assert(res == &s[1]); |
| 73 | } |
| 74 | { |
| 75 | // char* strpbrk(char*, const char*) |
| 76 | char storage[] = "hello world" ; |
| 77 | char* s = storage; |
| 78 | ASSERT_SAME_TYPE(char*, decltype(std::strpbrk(s, "el" ))); |
| 79 | char* res = std::strpbrk(s: s, accept: "el" ); |
| 80 | assert(res == &s[1]); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | // const char* strrchr(const char*, int) |
| 85 | char storage[] = "hello world" ; |
| 86 | const char* s = storage; |
| 87 | ASSERT_SAME_TYPE(const char*, decltype(std::strrchr(s, 'l'))); |
| 88 | const char* res = std::strrchr(s: s, c: 'l'); |
| 89 | assert(res == &s[9]); |
| 90 | } |
| 91 | { |
| 92 | // char* strrchr(char*, int) |
| 93 | char storage[] = "hello world" ; |
| 94 | char* s = storage; |
| 95 | ASSERT_SAME_TYPE(char*, decltype(std::strrchr(s, 'l'))); |
| 96 | char* res = std::strrchr(s: s, c: 'l'); |
| 97 | assert(res == &s[9]); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | // const void* memchr(const void*, int, size_t) |
| 102 | char storage[] = "hello world" ; |
| 103 | std::size_t count = 11; |
| 104 | const void* s = storage; |
| 105 | ASSERT_SAME_TYPE(const void*, decltype(std::memchr(s, 'l', count))); |
| 106 | const void* res = std::memchr(s: s, c: 'l', n: count); |
| 107 | assert(res == &storage[2]); |
| 108 | } |
| 109 | { |
| 110 | // void* memchr(void*, int, size_t) |
| 111 | char storage[] = "hello world" ; |
| 112 | std::size_t count = 11; |
| 113 | void* s = storage; |
| 114 | ASSERT_SAME_TYPE(void*, decltype(std::memchr(s, 'l', count))); |
| 115 | void* res = std::memchr(s: s, c: 'l', n: count); |
| 116 | assert(res == &storage[2]); |
| 117 | } |
| 118 | |
| 119 | { |
| 120 | // const char* strstr(const char*, const char*) |
| 121 | char storage[] = "hello world" ; |
| 122 | const char* s = storage; |
| 123 | ASSERT_SAME_TYPE(const char*, decltype(std::strstr(s, "wor" ))); |
| 124 | const char* res = std::strstr(haystack: s, needle: "wor" ); |
| 125 | assert(res == &storage[6]); |
| 126 | } |
| 127 | { |
| 128 | // char* strstr(char*, const char*) |
| 129 | char storage[] = "hello world" ; |
| 130 | char* s = storage; |
| 131 | ASSERT_SAME_TYPE(char*, decltype(std::strstr(s, "wor" ))); |
| 132 | char* res = std::strstr(haystack: s, needle: "wor" ); |
| 133 | assert(res == &storage[6]); |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |