| 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 |
// <string.h> |
| 10 |
|
| 11 |
#include <string.h> |
| 12 |
#include <cassert> |
| 13 |
|
| 14 |
#include "test_macros.h" |
| 15 |
|
| 16 |
#ifndef NULL |
| 17 |
#error NULL not defined |
| 18 |
#endif |
| 19 |
|
| 20 |
int main(int, char**) { |
| 21 |
// Functions we get directly from the C library (just check the signature) |
| 22 |
{ |
| 23 |
size_t s = 0; |
| 24 |
void* vp = 0; |
| 25 |
const void* vpc = 0; |
| 26 |
char* cp = 0; |
| 27 |
const char* cpc = 0; |
| 28 |
ASSERT_SAME_TYPE(void*, decltype(memcpy(vp, vpc, s))); |
| 29 |
ASSERT_SAME_TYPE(void*, decltype(memmove(vp, vpc, s))); |
| 30 |
ASSERT_SAME_TYPE(char*, decltype(strcpy(cp, cpc))); |
| 31 |
ASSERT_SAME_TYPE(char*, decltype(strncpy(cp, cpc, s))); |
| 32 |
ASSERT_SAME_TYPE(char*, decltype(strcat(cp, cpc))); |
| 33 |
ASSERT_SAME_TYPE(char*, decltype(strncat(cp, cpc, s))); |
| 34 |
ASSERT_SAME_TYPE(int, decltype(memcmp(vpc, vpc, s))); |
| 35 |
ASSERT_SAME_TYPE(int, decltype(strcmp(cpc, cpc))); |
| 36 |
ASSERT_SAME_TYPE(int, decltype(strncmp(cpc, cpc, s))); |
| 37 |
ASSERT_SAME_TYPE(int, decltype(strcoll(cpc, cpc))); |
| 38 |
ASSERT_SAME_TYPE(size_t, decltype(strxfrm(cp, cpc, s))); |
| 39 |
ASSERT_SAME_TYPE(size_t, decltype(strcspn(cpc, cpc))); |
| 40 |
ASSERT_SAME_TYPE(size_t, decltype(strspn(cpc, cpc))); |
| 41 |
ASSERT_SAME_TYPE(char*, decltype(strtok(cp, cpc))); |
| 42 |
ASSERT_SAME_TYPE(void*, decltype(memset(vp, 0, s))); |
| 43 |
ASSERT_SAME_TYPE(char*, decltype(strerror(0))); |
| 44 |
ASSERT_SAME_TYPE(size_t, decltype(strlen(cpc))); |
| 45 |
} |
| 46 |
|
| 47 |
// Functions we (may) reimplement |
| 48 |
{ |
| 49 |
// const char* strchr(const char*, int) |
| 50 |
char storage[] = "hello world"; |
| 51 |
const char* s = storage; |
| 52 |
ASSERT_SAME_TYPE(const char*, decltype(strchr(s, 'l'))); |
| 53 |
const char* res = strchr(s, 'l'); |
| 54 |
assert(res == &s[2]); |
| 55 |
} |
| 56 |
{ |
| 57 |
// char* strchr(char*, int) |
| 58 |
char storage[] = "hello world"; |
| 59 |
char* s = storage; |
| 60 |
ASSERT_SAME_TYPE(char*, decltype(strchr(s, 'l'))); |
| 61 |
char* res = strchr(s, 'l'); |
| 62 |
assert(res == &s[2]); |
| 63 |
} |
| 64 |
|
| 65 |
{ |
| 66 |
// const char* strpbrk(const char*, const char*) |
| 67 |
char storage[] = "hello world"; |
| 68 |
const char* s = storage; |
| 69 |
ASSERT_SAME_TYPE(const char*, decltype(strpbrk(s, "el"))); |
| 70 |
const char* res = strpbrk(s, "el"); |
| 71 |
assert(res == &s[1]); |
| 72 |
} |
| 73 |
{ |
| 74 |
// char* strpbrk(char*, const char*) |
| 75 |
char storage[] = "hello world"; |
| 76 |
char* s = storage; |
| 77 |
ASSERT_SAME_TYPE(char*, decltype(strpbrk(s, "el"))); |
| 78 |
char* res = strpbrk(s, "el"); |
| 79 |
assert(res == &s[1]); |
| 80 |
} |
| 81 |
|
| 82 |
{ |
| 83 |
// const char* strrchr(const char*, int) |
| 84 |
char storage[] = "hello world"; |
| 85 |
const char* s = storage; |
| 86 |
ASSERT_SAME_TYPE(const char*, decltype(strrchr(s, 'l'))); |
| 87 |
const char* res = strrchr(s, 'l'); |
| 88 |
assert(res == &s[9]); |
| 89 |
} |
| 90 |
{ |
| 91 |
// char* strrchr(char*, int) |
| 92 |
char storage[] = "hello world"; |
| 93 |
char* s = storage; |
| 94 |
ASSERT_SAME_TYPE(char*, decltype(strrchr(s, 'l'))); |
| 95 |
char* res = strrchr(s, 'l'); |
| 96 |
assert(res == &s[9]); |
| 97 |
} |
| 98 |
|
| 99 |
{ |
| 100 |
// const void* memchr(const void*, int, size_t) |
| 101 |
char storage[] = "hello world"; |
| 102 |
size_t count = 11; |
| 103 |
const void* s = storage; |
| 104 |
ASSERT_SAME_TYPE(const void*, decltype(memchr(s, 'l', count))); |
| 105 |
const void* res = memchr(s, 'l', count); |
| 106 |
assert(res == &storage[2]); |
| 107 |
} |
| 108 |
{ |
| 109 |
// void* memchr(void*, int, size_t) |
| 110 |
char storage[] = "hello world"; |
| 111 |
size_t count = 11; |
| 112 |
void* s = storage; |
| 113 |
ASSERT_SAME_TYPE(void*, decltype(memchr(s, 'l', count))); |
| 114 |
void* res = memchr(s, 'l', count); |
| 115 |
assert(res == &storage[2]); |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
// const char* strstr(const char*, const char*) |
| 120 |
char storage[] = "hello world"; |
| 121 |
const char* s = storage; |
| 122 |
ASSERT_SAME_TYPE(const char*, decltype(strstr(s, "wor"))); |
| 123 |
const char* res = strstr(s, "wor"); |
| 124 |
assert(res == &storage[6]); |
| 125 |
} |
| 126 |
{ |
| 127 |
// char* strstr(char*, const char*) |
| 128 |
char storage[] = "hello world"; |
| 129 |
char* s = storage; |
| 130 |
ASSERT_SAME_TYPE(char*, decltype(strstr(s, "wor"))); |
| 131 |
char* res = strstr(s, "wor"); |
| 132 |
assert(res == &storage[6]); |
| 133 |
} |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |