Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / language.support / support.runtime / cstdlib.pass.cpp
blob823417f8a418eb710103e159969cc33695951012
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 //===----------------------------------------------------------------------===//
9 // test <cstdlib>
11 #include <cstdlib>
12 #include <type_traits>
13 #include <cassert>
15 #include "test_macros.h"
17 #ifndef EXIT_FAILURE
18 #error EXIT_FAILURE not defined
19 #endif
21 #ifndef EXIT_SUCCESS
22 #error EXIT_SUCCESS not defined
23 #endif
25 #ifndef MB_CUR_MAX
26 #error MB_CUR_MAX not defined
27 #endif
29 #ifndef NULL
30 #error NULL not defined
31 #endif
33 #ifndef RAND_MAX
34 #error RAND_MAX not defined
35 #endif
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);
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(...);
51 template <class T>
52 struct has_abs : decltype(has_abs_imp<T>(0)) {};
54 void test_abs() {
55 TEST_DIAGNOSTIC_PUSH
56 TEST_CLANG_DIAGNOSTIC_IGNORED("-Wabsolute-value")
57 static_assert((std::is_same<decltype(std::abs((float)0)), float>::value), "");
58 static_assert((std::is_same<decltype(std::abs((double)0)), double>::value), "");
59 static_assert(
60 (std::is_same<decltype(std::abs((long double)0)), long double>::value), "");
61 static_assert((std::is_same<decltype(std::abs((int)0)), int>::value), "");
62 static_assert((std::is_same<decltype(std::abs((long)0)), long>::value), "");
63 static_assert((std::is_same<decltype(std::abs((long long)0)), long long>::value),
64 "");
65 static_assert((std::is_same<decltype(std::abs((unsigned char)0)), int>::value),
66 "");
67 static_assert((std::is_same<decltype(std::abs((unsigned short)0)), int>::value),
68 "");
69 static_assert((std::is_same<decltype(std::abs((signed char)0)), int>::value),
70 "");
71 static_assert((std::is_same<decltype(std::abs((short)0)), int>::value),
72 "");
73 static_assert((std::is_same<decltype(std::abs((unsigned char)0)), int>::value),
74 "");
75 static_assert((std::is_same<decltype(std::abs((char)0)), int>::value),
76 "");
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, "");
83 TEST_DIAGNOSTIC_POP
85 assert(std::abs(-1.) == 1);
88 int main(int, char**)
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("")), double>::value), "");
98 static_assert((std::is_same<decltype(std::atoi("")), int>::value), "");
99 static_assert((std::is_same<decltype(std::atol("")), long>::value), "");
100 static_assert((std::is_same<decltype(std::atoll("")), long long>::value), "");
101 static_assert((std::is_same<decltype(std::getenv("")), char*>::value), "");
102 static_assert((std::is_same<decltype(std::strtod("", endptr)), double>::value), "");
103 static_assert((std::is_same<decltype(std::strtof("", endptr)), float>::value), "");
104 static_assert((std::is_same<decltype(std::strtold("", endptr)), long double>::value), "");
105 static_assert((std::is_same<decltype(std::strtol("", endptr,0)), long>::value), "");
106 static_assert((std::is_same<decltype(std::strtoll("", endptr,0)), long long>::value), "");
107 static_assert((std::is_same<decltype(std::strtoul("", endptr,0)), unsigned long>::value), "");
108 static_assert((std::is_same<decltype(std::strtoull("", endptr,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(0)), void>::value), "");
112 // std::aligned_alloc tested in cstdlib.aligned_alloc.compile.pass.cpp
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(0,0)), void*>::value), "");
118 static_assert((std::is_same<decltype(std::free(0)), void>::value), "");
119 static_assert((std::is_same<decltype(std::malloc(0)), void*>::value), "");
120 static_assert((std::is_same<decltype(std::realloc(0,0)), void*>::value), "");
121 static_assert((std::is_same<decltype(std::abort()), void>::value), "");
122 static_assert((std::is_same<decltype(std::atexit(handler)), int>::value), "");
123 static_assert((std::is_same<decltype(std::exit(0)), void>::value), "");
124 static_assert((std::is_same<decltype(std::_Exit(0)), void>::value), "");
125 static_assert((std::is_same<decltype(std::getenv("")), char*>::value), "");
126 static_assert((std::is_same<decltype(std::system("")), int>::value), "");
127 static_assert((std::is_same<decltype(std::bsearch(pv,pv,0,0,comp)), void*>::value), "");
128 static_assert((std::is_same<decltype(std::qsort(pv,0,0,comp)), void>::value), "");
129 static_assert((std::is_same<decltype(std::abs(0)), int>::value), "");
130 static_assert((std::is_same<decltype(std::abs((long)0)), long>::value), "");
131 static_assert((std::is_same<decltype(std::abs((long long)0)), long long>::value), "");
132 static_assert((std::is_same<decltype(std::labs((long)0)), long>::value), "");
133 static_assert((std::is_same<decltype(std::llabs((long long)0)), long long>::value), "");
134 static_assert((std::is_same<decltype(std::div(0,0)), std::div_t>::value), "");
135 static_assert((std::is_same<decltype(std::div(0L,0L)), std::ldiv_t>::value), "");
136 static_assert((std::is_same<decltype(std::div(0LL,0LL)), std::lldiv_t>::value), "");
137 static_assert((std::is_same<decltype(std::ldiv(0L,0L)), std::ldiv_t>::value), "");
138 static_assert((std::is_same<decltype(std::lldiv(0LL,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("",0)), int>::value), "");
144 static_assert((std::is_same<decltype(std::mbtowc(pw,"",0)), int>::value), "");
145 static_assert((std::is_same<decltype(std::wctomb(pc,L' ')), int>::value), "");
146 static_assert((std::is_same<decltype(std::mbstowcs(pw,"",0)), std::size_t>::value), "");
147 static_assert((std::is_same<decltype(std::wcstombs(pc,pwc,0)), std::size_t>::value), "");
148 #endif
150 test_abs();
152 return 0;