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