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 // test <stdlib.h>
10
11 #include <stdlib.h>
12 #include <type_traits>
13 #include <cassert>
14
15 #include "test_macros.h"
16
17 // As of 1/10/2015 clang emits a -Wnonnull warnings even if the warning occurs
18 // in an unevaluated context. For this reason we manually suppress the warning.
19 #if defined(__clang__)
20 #pragma clang diagnostic ignored "-Wnonnull"
21 #endif
22
23 #ifdef abs
24 #error abs is defined
25 #endif
26
27 #ifdef labs
28 #error labs is defined
29 #endif
30
31 #ifdef llabs
32 #error llabs is defined
33 #endif
34
35 #ifdef div
36 #error div is defined
37 #endif
38
39 #ifdef ldiv
40 #error ldiv is defined
41 #endif
42
43 #ifdef lldiv
44 #error lldiv is defined
45 #endif
46
47 #ifndef EXIT_FAILURE
48 #error EXIT_FAILURE not defined
49 #endif
50
51 #ifndef EXIT_SUCCESS
52 #error EXIT_SUCCESS not defined
53 #endif
54
55 #ifndef MB_CUR_MAX
56 #error MB_CUR_MAX not defined
57 #endif
58
59 #ifndef NULL
60 #error NULL not defined
61 #endif
62
63 #ifndef RAND_MAX
64 #error RAND_MAX not defined
65 #endif
66
67 template <class T, class = decltype(::abs(std::declval<T>()))>
68 std::true_type has_abs_imp(int);
69 template <class T>
70 std::false_type has_abs_imp(...);
71
72 template <class T>
73 struct has_abs : decltype(has_abs_imp<T>(0)) {};
74
test_abs()75 void test_abs() {
76 #ifdef __clang__
77 #pragma clang diagnostic push
78 #pragma clang diagnostic ignored "-Wabsolute-value"
79 #endif
80 static_assert((std::is_same<decltype(abs((float)0)), float>::value), "");
81 static_assert((std::is_same<decltype(abs((double)0)), double>::value), "");
82 static_assert(
83 (std::is_same<decltype(abs((long double)0)), long double>::value), "");
84 static_assert((std::is_same<decltype(abs((int)0)), int>::value), "");
85 static_assert((std::is_same<decltype(abs((long)0)), long>::value), "");
86 static_assert((std::is_same<decltype(abs((long long)0)), long long>::value),
87 "");
88 static_assert((std::is_same<decltype(abs((unsigned char)0)), int>::value),
89 "");
90 static_assert((std::is_same<decltype(abs((unsigned short)0)), int>::value),
91 "");
92 static_assert((std::is_same<decltype(abs((signed char)0)), int>::value),
93 "");
94 static_assert((std::is_same<decltype(abs((short)0)), int>::value),
95 "");
96 static_assert((std::is_same<decltype(abs((unsigned char)0)), int>::value),
97 "");
98 static_assert((std::is_same<decltype(abs((char)0)), int>::value),
99 "");
100
101 static_assert(!has_abs<unsigned>::value, "");
102 static_assert(!has_abs<unsigned long>::value, "");
103 static_assert(!has_abs<unsigned long long>::value, "");
104 static_assert(!has_abs<size_t>::value, "");
105
106 #ifdef __clang__
107 #pragma clang diagnostic pop
108 #endif
109
110 assert(abs(-1.) == 1);
111 }
112
main(int,char **)113 int main(int, char**)
114 {
115 size_t s = 0; ((void)s);
116 div_t d; ((void)d);
117 ldiv_t ld; ((void)ld);
118 lldiv_t lld; ((void)lld);
119 char** endptr = 0;
120 static_assert((std::is_same<decltype(atof("")), double>::value), "");
121 static_assert((std::is_same<decltype(atoi("")), int>::value), "");
122 static_assert((std::is_same<decltype(atol("")), long>::value), "");
123 static_assert((std::is_same<decltype(atoll("")), long long>::value), "");
124 static_assert((std::is_same<decltype(getenv("")), char*>::value), "");
125 static_assert((std::is_same<decltype(strtod("", endptr)), double>::value), "");
126 static_assert((std::is_same<decltype(strtof("", endptr)), float>::value), "");
127 static_assert((std::is_same<decltype(strtold("", endptr)), long double>::value), "");
128 static_assert((std::is_same<decltype(strtol("", endptr,0)), long>::value), "");
129 static_assert((std::is_same<decltype(strtoll("", endptr,0)), long long>::value), "");
130 static_assert((std::is_same<decltype(strtoul("", endptr,0)), unsigned long>::value), "");
131 static_assert((std::is_same<decltype(strtoull("", endptr,0)), unsigned long long>::value), "");
132 static_assert((std::is_same<decltype(rand()), int>::value), "");
133 static_assert((std::is_same<decltype(srand(0)), void>::value), "");
134
135 #if TEST_STD_VER > 14 && defined(TEST_HAS_ALIGNED_ALLOC)
136 static_assert((std::is_same<decltype(aligned_alloc(1, 0)), void*>::value),
137 "");
138 #endif
139
140 void* pv = 0;
141 void (*handler)() = 0;
142 int (*comp)(void const*, void const*) = 0;
143 static_assert((std::is_same<decltype(calloc(0,0)), void*>::value), "");
144 static_assert((std::is_same<decltype(free(0)), void>::value), "");
145 static_assert((std::is_same<decltype(malloc(0)), void*>::value), "");
146 static_assert((std::is_same<decltype(realloc(0,0)), void*>::value), "");
147 static_assert((std::is_same<decltype(abort()), void>::value), "");
148 static_assert((std::is_same<decltype(atexit(handler)), int>::value), "");
149 static_assert((std::is_same<decltype(exit(0)), void>::value), "");
150 static_assert((std::is_same<decltype(_Exit(0)), void>::value), "");
151 static_assert((std::is_same<decltype(getenv("")), char*>::value), "");
152 static_assert((std::is_same<decltype(system("")), int>::value), "");
153 static_assert((std::is_same<decltype(bsearch(pv,pv,0,0,comp)), void*>::value), "");
154 static_assert((std::is_same<decltype(qsort(pv,0,0,comp)), void>::value), "");
155 static_assert((std::is_same<decltype(abs(0)), int>::value), "");
156 static_assert((std::is_same<decltype(labs((long)0)), long>::value), "");
157 static_assert((std::is_same<decltype(llabs((long long)0)), long long>::value), "");
158 static_assert((std::is_same<decltype(div(0,0)), div_t>::value), "");
159 static_assert((std::is_same<decltype(ldiv(0L,0L)), ldiv_t>::value), "");
160 static_assert((std::is_same<decltype(lldiv(0LL,0LL)), lldiv_t>::value), "");
161 wchar_t* pw = 0;
162 const wchar_t* pwc = 0;
163 char* pc = 0;
164 static_assert((std::is_same<decltype(mblen("",0)), int>::value), "");
165 static_assert((std::is_same<decltype(mbtowc(pw,"",0)), int>::value), "");
166 static_assert((std::is_same<decltype(wctomb(pc,L' ')), int>::value), "");
167 static_assert((std::is_same<decltype(mbstowcs(pw,"",0)), size_t>::value), "");
168 static_assert((std::is_same<decltype(wcstombs(pc,pwc,0)), size_t>::value), "");
169
170 test_abs();
171
172 return 0;
173 }
174