1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
12 #include <type_traits>
15 #include "test_macros.h"
22 #error labs is defined
26 #error llabs is defined
34 #error ldiv is defined
38 #error lldiv is defined
42 #error EXIT_FAILURE not defined
46 #error EXIT_SUCCESS not defined
50 #error MB_CUR_MAX not defined
54 #error NULL not defined
58 #error RAND_MAX not defined
61 template <class T
, class = decltype(::abs(std::declval
<T
>()))>
62 std::true_type
has_abs_imp(int);
64 std::false_type
has_abs_imp(...);
67 struct has_abs
: decltype(has_abs_imp
<T
>(0)) {};
71 TEST_CLANG_DIAGNOSTIC_IGNORED("-Wabsolute-value")
72 ASSERT_SAME_TYPE(float, decltype(abs((float)0)));
73 ASSERT_SAME_TYPE(double, decltype(abs((double)0)));
74 ASSERT_SAME_TYPE(long double, decltype(abs((long double)0)));
75 ASSERT_SAME_TYPE(int, decltype(abs((int)0)));
76 ASSERT_SAME_TYPE(long, decltype(abs((long)0)));
77 ASSERT_SAME_TYPE(long long, decltype(abs((long long)0)));
78 ASSERT_SAME_TYPE(int, decltype(abs((unsigned char)0)));
79 ASSERT_SAME_TYPE(int, decltype(abs((unsigned short)0)));
80 ASSERT_SAME_TYPE(int, decltype(abs((signed char)0)));
81 ASSERT_SAME_TYPE(int, decltype(abs((short)0)));
82 ASSERT_SAME_TYPE(int, decltype(abs((unsigned char)0)));
83 ASSERT_SAME_TYPE(int, decltype(abs((char)0)));
85 static_assert(!has_abs
<unsigned>::value
, "");
86 static_assert(!has_abs
<unsigned long>::value
, "");
87 static_assert(!has_abs
<unsigned long long>::value
, "");
88 static_assert(!has_abs
<size_t>::value
, "");
92 assert(abs(-1.) == 1);
95 int main(int, char**) {
96 size_t s
= 0; ((void)s
);
98 ldiv_t ld
; ((void)ld
);
99 lldiv_t lld
; ((void)lld
);
101 ASSERT_SAME_TYPE(double, decltype(atof("")));
102 ASSERT_SAME_TYPE(int, decltype(atoi("")));
103 ASSERT_SAME_TYPE(long, decltype(atol("")));
104 ASSERT_SAME_TYPE(long long, decltype(atoll("")));
105 ASSERT_SAME_TYPE(char*, decltype(getenv("")));
106 ASSERT_SAME_TYPE(double, decltype(strtod("", endptr
)));
107 ASSERT_SAME_TYPE(float, decltype(strtof("", endptr
)));
108 ASSERT_SAME_TYPE(long double, decltype(strtold("", endptr
)));
109 ASSERT_SAME_TYPE(long, decltype(strtol("", endptr
,0)));
110 ASSERT_SAME_TYPE(long long, decltype(strtoll("", endptr
,0)));
111 ASSERT_SAME_TYPE(unsigned long, decltype(strtoul("", endptr
,0)));
112 ASSERT_SAME_TYPE(unsigned long long, decltype(strtoull("", endptr
,0)));
113 ASSERT_SAME_TYPE(int, decltype(rand()));
114 ASSERT_SAME_TYPE(void, decltype(srand(0)));
116 // aligned_alloc tested in stdlib_h.aligned_alloc.compile.pass.cpp
119 void (*handler
)() = 0;
120 int (*comp
)(void const*, void const*) = 0;
121 ASSERT_SAME_TYPE(void*, decltype(calloc(0,0)));
122 ASSERT_SAME_TYPE(void, decltype(free(0)));
123 ASSERT_SAME_TYPE(void*, decltype(malloc(0)));
124 ASSERT_SAME_TYPE(void*, decltype(realloc(0,0)));
125 ASSERT_SAME_TYPE(void, decltype(abort()));
126 ASSERT_SAME_TYPE(int, decltype(atexit(handler
)));
127 ASSERT_SAME_TYPE(void, decltype(exit(0)));
128 ASSERT_SAME_TYPE(void, decltype(_Exit(0)));
129 ASSERT_SAME_TYPE(char*, decltype(getenv("")));
130 ASSERT_SAME_TYPE(int, decltype(system("")));
131 ASSERT_SAME_TYPE(void*, decltype(bsearch(pv
,pv
,0,0,comp
)));
132 ASSERT_SAME_TYPE(void, decltype(qsort(pv
,0,0,comp
)));
133 ASSERT_SAME_TYPE(int, decltype(abs(0)));
134 ASSERT_SAME_TYPE(long, decltype(labs((long)0)));
135 ASSERT_SAME_TYPE(long long, decltype(llabs((long long)0)));
136 ASSERT_SAME_TYPE(div_t, decltype(div(0,0)));
137 ASSERT_SAME_TYPE(ldiv_t, decltype(ldiv(0L,0L)));
138 ASSERT_SAME_TYPE(lldiv_t
, decltype(lldiv(0LL,0LL)));
139 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
141 const wchar_t* pwc
= 0;
143 ASSERT_SAME_TYPE(int, decltype(mblen("",0)));
144 ASSERT_SAME_TYPE(int, decltype(mbtowc(pw
,"",0)));
145 ASSERT_SAME_TYPE(int, decltype(wctomb(pc
,L
' ')));
146 ASSERT_SAME_TYPE(size_t, decltype(mbstowcs(pw
,"",0)));
147 ASSERT_SAME_TYPE(size_t, decltype(wcstombs(pc
,pwc
,0)));