xref: /llvm-project/libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp (revision c281b127ab5656eec289cf0b39bf1f473cf71757)
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 // mblen was added in Android API 26.
12 // TODO: Switch from UNSUPPORTED to XFAIL once the Android CI Docker sysroot is
13 // updated.
14 // UNSUPPORTED: LIBCXX-ANDROID-FIXME && target={{.+}}-android{{(eabi)?(21|22|23|24|25)}}
15 
16 #include <stdlib.h>
17 #include <cassert>
18 #include <type_traits>
19 #include <utility>
20 
21 #include "test_macros.h"
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 
75 void test_abs() {
76   TEST_DIAGNOSTIC_PUSH
77   TEST_CLANG_DIAGNOSTIC_IGNORED("-Wabsolute-value")
78   ASSERT_SAME_TYPE(float,       decltype(abs((float)0)));
79   ASSERT_SAME_TYPE(double,      decltype(abs((double)0)));
80   ASSERT_SAME_TYPE(long double, decltype(abs((long double)0)));
81   ASSERT_SAME_TYPE(int,         decltype(abs((int)0)));
82   ASSERT_SAME_TYPE(long,        decltype(abs((long)0)));
83   ASSERT_SAME_TYPE(long long,   decltype(abs((long long)0)));
84   ASSERT_SAME_TYPE(int,         decltype(abs((unsigned char)0)));
85   ASSERT_SAME_TYPE(int,         decltype(abs((unsigned short)0)));
86   ASSERT_SAME_TYPE(int,         decltype(abs((signed char)0)));
87   ASSERT_SAME_TYPE(int,         decltype(abs((short)0)));
88   ASSERT_SAME_TYPE(int,         decltype(abs((unsigned char)0)));
89   ASSERT_SAME_TYPE(int,         decltype(abs((char)0)));
90 
91   static_assert(!has_abs<unsigned>::value, "");
92   static_assert(!has_abs<unsigned long>::value, "");
93   static_assert(!has_abs<unsigned long long>::value, "");
94   static_assert(!has_abs<size_t>::value, "");
95 
96   TEST_DIAGNOSTIC_POP
97 
98   assert(abs(-1.) == 1);
99 }
100 
101 int main(int, char**) {
102     size_t s = 0; ((void)s);
103     div_t d; ((void)d);
104     ldiv_t ld; ((void)ld);
105     lldiv_t lld; ((void)lld);
106     char** endptr = 0;
107     ASSERT_SAME_TYPE(double,             decltype(atof("")));
108     ASSERT_SAME_TYPE(int,                decltype(atoi("")));
109     ASSERT_SAME_TYPE(long,               decltype(atol("")));
110     ASSERT_SAME_TYPE(long long,          decltype(atoll("")));
111     ASSERT_SAME_TYPE(char*,              decltype(getenv("")));
112     ASSERT_SAME_TYPE(double,             decltype(strtod("", endptr)));
113     ASSERT_SAME_TYPE(float,              decltype(strtof("", endptr)));
114     ASSERT_SAME_TYPE(long double,        decltype(strtold("", endptr)));
115     ASSERT_SAME_TYPE(long,               decltype(strtol("", endptr,0)));
116     ASSERT_SAME_TYPE(long long,          decltype(strtoll("", endptr,0)));
117     ASSERT_SAME_TYPE(unsigned long,      decltype(strtoul("", endptr,0)));
118     ASSERT_SAME_TYPE(unsigned long long, decltype(strtoull("", endptr,0)));
119     ASSERT_SAME_TYPE(int,                decltype(rand()));
120     ASSERT_SAME_TYPE(void,               decltype(srand(0)));
121 
122     // aligned_alloc tested in stdlib_h.aligned_alloc.compile.pass.cpp
123 
124     void* pv = 0;
125     void (*handler)() = 0;
126     int (*comp)(void const*, void const*) = 0;
127     ASSERT_SAME_TYPE(void*,     decltype(calloc(0,0)));
128     ASSERT_SAME_TYPE(void,      decltype(free(0)));
129     ASSERT_SAME_TYPE(void*,     decltype(malloc(0)));
130     ASSERT_SAME_TYPE(void*,     decltype(realloc(0,0)));
131     ASSERT_SAME_TYPE(void,      decltype(abort()));
132     ASSERT_SAME_TYPE(int,       decltype(atexit(handler)));
133     ASSERT_SAME_TYPE(void,      decltype(exit(0)));
134     ASSERT_SAME_TYPE(void,      decltype(_Exit(0)));
135     ASSERT_SAME_TYPE(char*,     decltype(getenv("")));
136     ASSERT_SAME_TYPE(int,       decltype(system("")));
137     ASSERT_SAME_TYPE(void*,     decltype(bsearch(pv,pv,0,0,comp)));
138     ASSERT_SAME_TYPE(void,      decltype(qsort(pv,0,0,comp)));
139     ASSERT_SAME_TYPE(int,       decltype(abs(0)));
140     ASSERT_SAME_TYPE(long,      decltype(labs((long)0)));
141     ASSERT_SAME_TYPE(long long, decltype(llabs((long long)0)));
142     ASSERT_SAME_TYPE(div_t,     decltype(div(0,0)));
143     ASSERT_SAME_TYPE(ldiv_t,    decltype(ldiv(0L,0L)));
144     ASSERT_SAME_TYPE(lldiv_t,   decltype(lldiv(0LL,0LL)));
145 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
146     wchar_t* pw = 0;
147     const wchar_t* pwc = 0;
148     char* pc = 0;
149     ASSERT_SAME_TYPE(int,    decltype(mblen("",0)));
150     ASSERT_SAME_TYPE(int,    decltype(mbtowc(pw,"",0)));
151     ASSERT_SAME_TYPE(int,    decltype(wctomb(pc,L' ')));
152     ASSERT_SAME_TYPE(size_t, decltype(mbstowcs(pw,"",0)));
153     ASSERT_SAME_TYPE(size_t, decltype(wcstombs(pc,pwc,0)));
154 #endif
155 
156     test_abs();
157 
158     return 0;
159 }
160