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 | #ifdef abs |
18 | #error abs is defined |
19 | #endif |
20 | |
21 | #ifdef labs |
22 | #error labs is defined |
23 | #endif |
24 | |
25 | #ifdef llabs |
26 | #error llabs is defined |
27 | #endif |
28 | |
29 | #ifdef div |
30 | #error div is defined |
31 | #endif |
32 | |
33 | #ifdef ldiv |
34 | #error ldiv is defined |
35 | #endif |
36 | |
37 | #ifdef lldiv |
38 | #error lldiv is defined |
39 | #endif |
40 | |
41 | #ifndef EXIT_FAILURE |
42 | #error EXIT_FAILURE not defined |
43 | #endif |
44 | |
45 | #ifndef EXIT_SUCCESS |
46 | #error EXIT_SUCCESS not defined |
47 | #endif |
48 | |
49 | #ifndef MB_CUR_MAX |
50 | #error MB_CUR_MAX not defined |
51 | #endif |
52 | |
53 | #ifndef NULL |
54 | #error NULL not defined |
55 | #endif |
56 | |
57 | #ifndef RAND_MAX |
58 | #error RAND_MAX not defined |
59 | #endif |
60 | |
61 | template <class T, class = decltype(::abs(std::declval<T>()))> |
62 | std::true_type has_abs_imp(int); |
63 | template <class T> |
64 | std::false_type has_abs_imp(...); |
65 | |
66 | template <class T> |
67 | struct has_abs : decltype(has_abs_imp<T>(0)) {}; |
68 | |
69 | void test_abs() { |
70 | TEST_DIAGNOSTIC_PUSH |
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))); |
84 | |
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, "" ); |
89 | |
90 | TEST_DIAGNOSTIC_POP |
91 | |
92 | assert(abs(-1.) == 1); |
93 | } |
94 | |
95 | int main(int, char**) { |
96 | size_t s = 0; ((void)s); |
97 | div_t d; ((void)d); |
98 | ldiv_t ld; ((void)ld); |
99 | lldiv_t lld; ((void)lld); |
100 | char** endptr = 0; |
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))); |
115 | |
116 | // aligned_alloc tested in stdlib_h.aligned_alloc.compile.pass.cpp |
117 | |
118 | void* pv = 0; |
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 |
140 | wchar_t* pw = 0; |
141 | const wchar_t* pwc = 0; |
142 | char* pc = 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))); |
148 | #endif |
149 | |
150 | test_abs(); |
151 | |
152 | return 0; |
153 | } |
154 | |