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 // <cwchar>
10
11 #include <cwchar>
12 #include <ctime>
13 #include <cstdarg>
14 #include <cstdio>
15 #include <type_traits>
16
17 #include "test_macros.h"
18
19 #ifndef NULL
20 #error NULL not defined
21 #endif
22
23 #ifndef WCHAR_MAX
24 #error WCHAR_MAX not defined
25 #endif
26
27 #ifndef WCHAR_MIN
28 #error WCHAR_MIN not defined
29 #endif
30
31 #ifndef WEOF
32 #error WEOF not defined
33 #endif
34
main(int,char **)35 int main(int, char**)
36 {
37 std::mbstate_t mb = {};
38 std::size_t s = 0;
39 std::tm *tm = 0;
40 std::wint_t w = 0;
41 ::FILE* fp = 0;
42 std::va_list va;
43
44 char* ns = 0;
45 wchar_t* ws = 0;
46
47 ((void)mb); // Prevent unused warning
48 ((void)s); // Prevent unused warning
49 ((void)tm); // Prevent unused warning
50 ((void)w); // Prevent unused warning
51 ((void)fp); // Prevent unused warning
52 ((void)va); // Prevent unused warning
53 ((void)ns); // Prevent unused warning
54 ((void)ws); // Prevent unused warning
55
56 ASSERT_SAME_TYPE(int, decltype(std::fwprintf(fp, L"")));
57 ASSERT_SAME_TYPE(int, decltype(std::fwscanf(fp, L"")));
58 ASSERT_SAME_TYPE(int, decltype(std::swprintf(ws, s, L"")));
59 ASSERT_SAME_TYPE(int, decltype(std::swscanf(L"", L"")));
60 ASSERT_SAME_TYPE(int, decltype(std::vfwprintf(fp, L"", va)));
61 ASSERT_SAME_TYPE(int, decltype(std::vfwscanf(fp, L"", va)));
62 ASSERT_SAME_TYPE(int, decltype(std::vswprintf(ws, s, L"", va)));
63 ASSERT_SAME_TYPE(int, decltype(std::vswscanf(L"", L"", va)));
64 ASSERT_SAME_TYPE(std::wint_t, decltype(std::fgetwc(fp)));
65 ASSERT_SAME_TYPE(wchar_t*, decltype(std::fgetws(ws, 0, fp)));
66 ASSERT_SAME_TYPE(std::wint_t, decltype(std::fputwc(L' ', fp)));
67 ASSERT_SAME_TYPE(int, decltype(std::fputws(L"", fp)));
68 ASSERT_SAME_TYPE(int, decltype(std::fwide(fp, 0)));
69 ASSERT_SAME_TYPE(std::wint_t, decltype(std::getwc(fp)));
70 ASSERT_SAME_TYPE(std::wint_t, decltype(std::putwc(L' ', fp)));
71 ASSERT_SAME_TYPE(std::wint_t, decltype(std::ungetwc(L' ', fp)));
72 ASSERT_SAME_TYPE(double, decltype(std::wcstod(L"", (wchar_t**)0)));
73 ASSERT_SAME_TYPE(float, decltype(std::wcstof(L"", (wchar_t**)0)));
74 ASSERT_SAME_TYPE(long double, decltype(std::wcstold(L"", (wchar_t**)0)));
75 ASSERT_SAME_TYPE(long, decltype(std::wcstol(L"", (wchar_t**)0, 0)));
76 ASSERT_SAME_TYPE(long long, decltype(std::wcstoll(L"", (wchar_t**)0, 0)));
77 ASSERT_SAME_TYPE(unsigned long, decltype(std::wcstoul(L"", (wchar_t**)0, 0)));
78 ASSERT_SAME_TYPE(unsigned long long, decltype(std::wcstoull(L"", (wchar_t**)0, 0)));
79 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcscpy(ws, L"")));
80 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcsncpy(ws, L"", s)));
81 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcscat(ws, L"")));
82 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcsncat(ws, L"", s)));
83 ASSERT_SAME_TYPE(int, decltype(std::wcscmp(L"", L"")));
84 ASSERT_SAME_TYPE(int, decltype(std::wcscoll(L"", L"")));
85 ASSERT_SAME_TYPE(int, decltype(std::wcsncmp(L"", L"", s)));
86 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcsxfrm(ws, L"", s)));
87 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcschr((wchar_t*)0, L' ')));
88 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcscspn(L"", L"")));
89 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcslen(L"")));
90 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcspbrk((wchar_t*)0, L"")));
91 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcsrchr((wchar_t*)0, L' ')));
92 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcsspn(L"", L"")));
93 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcsstr((wchar_t*)0, L"")));
94 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wcstok(ws, L"", (wchar_t**)0)));
95 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wmemchr((wchar_t*)0, L' ', s)));
96 ASSERT_SAME_TYPE(int, decltype(std::wmemcmp(L"", L"", s)));
97 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wmemcpy(ws, L"", s)));
98 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wmemmove(ws, L"", s)));
99 ASSERT_SAME_TYPE(wchar_t*, decltype(std::wmemset(ws, L' ', s)));
100 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcsftime(ws, s, L"", tm)));
101 ASSERT_SAME_TYPE(wint_t, decltype(std::btowc(0)));
102 ASSERT_SAME_TYPE(int, decltype(std::wctob(w)));
103 ASSERT_SAME_TYPE(int, decltype(std::mbsinit(&mb)));
104 ASSERT_SAME_TYPE(std::size_t, decltype(std::mbrlen("", s, &mb)));
105 ASSERT_SAME_TYPE(std::size_t, decltype(std::mbrtowc(ws, "", s, &mb)));
106 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcrtomb(ns, L' ', &mb)));
107 ASSERT_SAME_TYPE(std::size_t, decltype(std::mbsrtowcs(ws, (const char**)0, s, &mb)));
108 ASSERT_SAME_TYPE(std::size_t, decltype(std::wcsrtombs(ns, (const wchar_t**)0, s, &mb)));
109
110 // These tests fail on systems whose C library doesn't provide a correct overload
111 // set for wcschr, wcspbrk, wcsrchr, wcsstr, and wmemchr, unless the compiler is
112 // a suitably recent version of Clang.
113 #if !defined(__APPLE__) || defined(_LIBCPP_PREFERRED_OVERLOAD)
114 ASSERT_SAME_TYPE(const wchar_t*, decltype(std::wcschr((const wchar_t*)0, L' ')));
115 ASSERT_SAME_TYPE(const wchar_t*, decltype(std::wcspbrk((const wchar_t*)0, L"")));
116 ASSERT_SAME_TYPE(const wchar_t*, decltype(std::wcsrchr((const wchar_t*)0, L' ')));
117 ASSERT_SAME_TYPE(const wchar_t*, decltype(std::wcsstr((const wchar_t*)0, L"")));
118 ASSERT_SAME_TYPE(const wchar_t*, decltype(std::wmemchr((const wchar_t*)0, L' ', s)));
119 #endif
120
121 #ifndef _LIBCPP_HAS_NO_STDIN
122 ASSERT_SAME_TYPE(std::wint_t, decltype(std::getwchar()));
123 ASSERT_SAME_TYPE(int, decltype(std::vwscanf(L"", va)));
124 ASSERT_SAME_TYPE(int, decltype(std::wscanf(L"")));
125 #endif
126
127 #ifndef _LIBCPP_HAS_NO_STDOUT
128 ASSERT_SAME_TYPE(std::wint_t, decltype(std::putwchar(L' ')));
129 ASSERT_SAME_TYPE(int, decltype(std::vwprintf(L"", va)));
130 ASSERT_SAME_TYPE(int, decltype(std::wprintf(L"")));
131 #endif
132
133 return 0;
134 }
135