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 <cstdlib> |
10 | |
11 | #include <cstdlib> |
12 | #include <type_traits> |
13 | #include <cassert> |
14 | |
15 | #include "test_macros.h" |
16 | |
17 | #ifndef EXIT_FAILURE |
18 | #error EXIT_FAILURE not defined |
19 | #endif |
20 | |
21 | #ifndef EXIT_SUCCESS |
22 | #error EXIT_SUCCESS not defined |
23 | #endif |
24 | |
25 | #ifndef MB_CUR_MAX |
26 | #error MB_CUR_MAX not defined |
27 | #endif |
28 | |
29 | #ifndef NULL |
30 | #error NULL not defined |
31 | #endif |
32 | |
33 | #ifndef RAND_MAX |
34 | #error RAND_MAX not defined |
35 | #endif |
36 | |
37 | template <class TestType, class IntType> |
38 | void test_div_struct() { |
39 | TestType obj; |
40 | static_assert(sizeof(obj) >= sizeof(IntType) * 2, "" ); // >= to account for alignment. |
41 | static_assert((std::is_same<decltype(obj.quot), IntType>::value), "" ); |
42 | static_assert((std::is_same<decltype(obj.rem), IntType>::value), "" ); |
43 | ((void) obj); |
44 | }; |
45 | |
46 | template <class T, class = decltype(std::abs(std::declval<T>()))> |
47 | std::true_type has_abs_imp(int); |
48 | template <class T> |
49 | std::false_type has_abs_imp(...); |
50 | |
51 | template <class T> |
52 | struct has_abs : decltype(has_abs_imp<T>(0)) {}; |
53 | |
54 | void test_abs() { |
55 | TEST_DIAGNOSTIC_PUSH |
56 | TEST_CLANG_DIAGNOSTIC_IGNORED("-Wabsolute-value" ) |
57 | static_assert((std::is_same<decltype(std::abs(x: (float)0)), float>::value), "" ); |
58 | static_assert((std::is_same<decltype(std::abs(x: (double)0)), double>::value), "" ); |
59 | static_assert( |
60 | (std::is_same<decltype(std::abs(x: (long double)0)), long double>::value), "" ); |
61 | static_assert((std::is_same<decltype(std::abs(x: (int)0)), int>::value), "" ); |
62 | static_assert((std::is_same<decltype(std::abs(i: (long)0)), long>::value), "" ); |
63 | static_assert((std::is_same<decltype(std::abs(x: (long long)0)), long long>::value), |
64 | "" ); |
65 | static_assert((std::is_same<decltype(std::abs(x: (unsigned char)0)), int>::value), |
66 | "" ); |
67 | static_assert((std::is_same<decltype(std::abs(x: (unsigned short)0)), int>::value), |
68 | "" ); |
69 | static_assert((std::is_same<decltype(std::abs(x: (signed char)0)), int>::value), |
70 | "" ); |
71 | static_assert((std::is_same<decltype(std::abs(x: (short)0)), int>::value), |
72 | "" ); |
73 | static_assert((std::is_same<decltype(std::abs(x: (unsigned char)0)), int>::value), |
74 | "" ); |
75 | static_assert((std::is_same<decltype(std::abs(x: (char)0)), int>::value), |
76 | "" ); |
77 | |
78 | static_assert(!has_abs<unsigned>::value, "" ); |
79 | static_assert(!has_abs<unsigned long>::value, "" ); |
80 | static_assert(!has_abs<unsigned long long>::value, "" ); |
81 | static_assert(!has_abs<std::size_t>::value, "" ); |
82 | |
83 | TEST_DIAGNOSTIC_POP |
84 | |
85 | assert(std::abs(-1.) == 1); |
86 | } |
87 | |
88 | int main(int, char**) |
89 | { |
90 | std::size_t s = 0; |
91 | ((void)s); |
92 | static_assert((std::is_same<std::size_t, decltype(sizeof(int))>::value), "" ); |
93 | test_div_struct<std::div_t, int>(); |
94 | test_div_struct<std::ldiv_t, long>(); |
95 | test_div_struct<std::lldiv_t, long long>(); |
96 | char** endptr = 0; |
97 | static_assert((std::is_same<decltype(std::atof(nptr: "" )), double>::value), "" ); |
98 | static_assert((std::is_same<decltype(std::atoi(nptr: "" )), int>::value), "" ); |
99 | static_assert((std::is_same<decltype(std::atol(nptr: "" )), long>::value), "" ); |
100 | static_assert((std::is_same<decltype(std::atoll(nptr: "" )), long long>::value), "" ); |
101 | static_assert((std::is_same<decltype(std::getenv(name: "" )), char*>::value), "" ); |
102 | static_assert((std::is_same<decltype(std::strtod(nptr: "" , endptr: endptr)), double>::value), "" ); |
103 | static_assert((std::is_same<decltype(std::strtof(nptr: "" , endptr: endptr)), float>::value), "" ); |
104 | static_assert((std::is_same<decltype(std::strtold(nptr: "" , endptr: endptr)), long double>::value), "" ); |
105 | static_assert((std::is_same<decltype(std::strtol(nptr: "" , endptr: endptr,base: 0)), long>::value), "" ); |
106 | static_assert((std::is_same<decltype(std::strtoll(nptr: "" , endptr: endptr,base: 0)), long long>::value), "" ); |
107 | static_assert((std::is_same<decltype(std::strtoul(nptr: "" , endptr: endptr,base: 0)), unsigned long>::value), "" ); |
108 | static_assert((std::is_same<decltype(std::strtoull(nptr: "" , endptr: endptr,base: 0)), unsigned long long>::value), "" ); |
109 | static_assert((std::is_same<decltype(std::rand()), int>::value), "" ); |
110 | static_assert((std::is_same<decltype(std::srand(seed: 0)), void>::value), "" ); |
111 | |
112 | // std::aligned_alloc tested in cstdlib.aligned_alloc.compile.pass.cpp |
113 | |
114 | void* pv = 0; |
115 | void (*handler)() = 0; |
116 | int (*comp)(void const*, void const*) = 0; |
117 | static_assert((std::is_same<decltype(std::calloc(nmemb: 0,size: 0)), void*>::value), "" ); |
118 | static_assert((std::is_same<decltype(std::free(ptr: 0)), void>::value), "" ); |
119 | static_assert((std::is_same<decltype(std::malloc(size: 0)), void*>::value), "" ); |
120 | static_assert((std::is_same<decltype(std::realloc(ptr: 0,size: 0)), void*>::value), "" ); |
121 | static_assert((std::is_same<decltype(std::abort()), void>::value), "" ); |
122 | static_assert((std::is_same<decltype(std::atexit(func: handler)), int>::value), "" ); |
123 | static_assert((std::is_same<decltype(std::exit(status: 0)), void>::value), "" ); |
124 | static_assert((std::is_same<decltype(std::_Exit(status: 0)), void>::value), "" ); |
125 | static_assert((std::is_same<decltype(std::getenv(name: "" )), char*>::value), "" ); |
126 | static_assert((std::is_same<decltype(std::system(command: "" )), int>::value), "" ); |
127 | static_assert((std::is_same<decltype(std::bsearch(key: pv,base: pv,nmemb: 0,size: 0,compar: comp)), void*>::value), "" ); |
128 | static_assert((std::is_same<decltype(std::qsort(base: pv,nmemb: 0,size: 0,compar: comp)), void>::value), "" ); |
129 | static_assert((std::is_same<decltype(std::abs(x: 0)), int>::value), "" ); |
130 | static_assert((std::is_same<decltype(std::abs(i: (long)0)), long>::value), "" ); |
131 | static_assert((std::is_same<decltype(std::abs(x: (long long)0)), long long>::value), "" ); |
132 | static_assert((std::is_same<decltype(std::labs(x: (long)0)), long>::value), "" ); |
133 | static_assert((std::is_same<decltype(std::llabs(x: (long long)0)), long long>::value), "" ); |
134 | static_assert((std::is_same<decltype(std::div(numer: 0,denom: 0)), std::div_t>::value), "" ); |
135 | static_assert((std::is_same<decltype(std::div(i: 0L,j: 0L)), std::ldiv_t>::value), "" ); |
136 | static_assert((std::is_same<decltype(std::div(n: 0LL,d: 0LL)), std::lldiv_t>::value), "" ); |
137 | static_assert((std::is_same<decltype(std::ldiv(numer: 0L,denom: 0L)), std::ldiv_t>::value), "" ); |
138 | static_assert((std::is_same<decltype(std::lldiv(numer: 0LL,denom: 0LL)), std::lldiv_t>::value), "" ); |
139 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
140 | wchar_t* pw = 0; |
141 | const wchar_t* pwc = 0; |
142 | char* pc = 0; |
143 | static_assert((std::is_same<decltype(std::mblen(s: "" ,n: 0)), int>::value), "" ); |
144 | static_assert((std::is_same<decltype(std::mbtowc(pwc: pw,s: "" ,n: 0)), int>::value), "" ); |
145 | static_assert((std::is_same<decltype(std::wctomb(s: pc,wchar: L' ')), int>::value), "" ); |
146 | static_assert((std::is_same<decltype(std::mbstowcs(pwcs: pw,s: "" ,n: 0)), std::size_t>::value), "" ); |
147 | static_assert((std::is_same<decltype(std::wcstombs(s: pc,pwcs: pwc,n: 0)), std::size_t>::value), "" ); |
148 | #endif |
149 | |
150 | test_abs(); |
151 | |
152 | return 0; |
153 | } |
154 | |