Home | History | Annotate | Download | only in support.runtime
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // test <cstdlib>
     11 
     12 #include <cstdlib>
     13 #include <type_traits>
     14 #include <cassert>
     15 
     16 #include "test_macros.h"
     17 
     18 // As of 1/10/2015 clang emits a -Wnonnull warnings even if the warning occurs
     19 // in an unevaluated context. For this reason we manually suppress the warning.
     20 #if defined(__clang__)
     21 #pragma clang diagnostic ignored "-Wnonnull"
     22 #endif
     23 
     24 #ifndef EXIT_FAILURE
     25 #error EXIT_FAILURE not defined
     26 #endif
     27 
     28 #ifndef EXIT_SUCCESS
     29 #error EXIT_SUCCESS not defined
     30 #endif
     31 
     32 #ifndef MB_CUR_MAX
     33 #error MB_CUR_MAX not defined
     34 #endif
     35 
     36 #ifndef NULL
     37 #error NULL not defined
     38 #endif
     39 
     40 #ifndef RAND_MAX
     41 #error RAND_MAX not defined
     42 #endif
     43 
     44 template <class TestType, class IntType>
     45 void test_div_struct() {
     46     TestType obj;
     47     static_assert(sizeof(obj) >= sizeof(IntType) * 2, ""); // >= to account for alignment.
     48     static_assert((std::is_same<decltype(obj.quot), IntType>::value), "");
     49     static_assert((std::is_same<decltype(obj.rem), IntType>::value), "");
     50     ((void) obj);
     51 };
     52 
     53 int main()
     54 {
     55     std::size_t s = 0;
     56     ((void)s);
     57     static_assert((std::is_same<std::size_t, decltype(sizeof(int))>::value), "");
     58     test_div_struct<std::div_t, int>();
     59     test_div_struct<std::ldiv_t, long>();
     60     test_div_struct<std::lldiv_t, long long>();
     61     char** endptr = 0;
     62     static_assert((std::is_same<decltype(std::atof("")), double>::value), "");
     63     static_assert((std::is_same<decltype(std::atoi("")), int>::value), "");
     64     static_assert((std::is_same<decltype(std::atol("")), long>::value), "");
     65     static_assert((std::is_same<decltype(std::atoll("")), long long>::value), "");
     66     static_assert((std::is_same<decltype(std::getenv("")), char*>::value), "");
     67     static_assert((std::is_same<decltype(std::strtod("", endptr)), double>::value), "");
     68     static_assert((std::is_same<decltype(std::strtof("", endptr)), float>::value), "");
     69     static_assert((std::is_same<decltype(std::strtold("", endptr)), long double>::value), "");
     70     static_assert((std::is_same<decltype(std::strtol("", endptr,0)), long>::value), "");
     71     static_assert((std::is_same<decltype(std::strtoll("", endptr,0)), long long>::value), "");
     72     static_assert((std::is_same<decltype(std::strtoul("", endptr,0)), unsigned long>::value), "");
     73     static_assert((std::is_same<decltype(std::strtoull("", endptr,0)), unsigned long long>::value), "");
     74     static_assert((std::is_same<decltype(std::rand()), int>::value), "");
     75     static_assert((std::is_same<decltype(std::srand(0)), void>::value), "");
     76 
     77 //  Microsoft does not implement aligned_alloc in their C library
     78 #if TEST_STD_VER > 14 && defined(TEST_HAS_C11_FEATURES) && !defined(_WIN32)
     79     static_assert((std::is_same<decltype(aligned_alloc(0,0)), void*>::value), "");
     80 #endif
     81 
     82     static_assert((std::is_same<decltype(std::calloc(0,0)), void*>::value), "");
     83     static_assert((std::is_same<decltype(std::free(0)), void>::value), "");
     84     static_assert((std::is_same<decltype(std::malloc(0)), void*>::value), "");
     85     static_assert((std::is_same<decltype(std::realloc(0,0)), void*>::value), "");
     86     static_assert((std::is_same<decltype(std::abort()), void>::value), "");
     87     static_assert((std::is_same<decltype(std::atexit(0)), int>::value), "");
     88     static_assert((std::is_same<decltype(std::exit(0)), void>::value), "");
     89     static_assert((std::is_same<decltype(std::_Exit(0)), void>::value), "");
     90     static_assert((std::is_same<decltype(std::getenv("")), char*>::value), "");
     91     static_assert((std::is_same<decltype(std::system("")), int>::value), "");
     92     static_assert((std::is_same<decltype(std::bsearch(0,0,0,0,0)), void*>::value), "");
     93     static_assert((std::is_same<decltype(std::qsort(0,0,0,0)), void>::value), "");
     94     static_assert((std::is_same<decltype(std::abs(0)), int>::value), "");
     95     static_assert((std::is_same<decltype(std::abs((long)0)), long>::value), "");
     96     static_assert((std::is_same<decltype(std::abs((long long)0)), long long>::value), "");
     97     static_assert((std::is_same<decltype(std::labs((long)0)), long>::value), "");
     98     static_assert((std::is_same<decltype(std::llabs((long long)0)), long long>::value), "");
     99     static_assert((std::is_same<decltype(std::div(0,0)), std::div_t>::value), "");
    100     static_assert((std::is_same<decltype(std::div(0L,0L)), std::ldiv_t>::value), "");
    101     static_assert((std::is_same<decltype(std::div(0LL,0LL)), std::lldiv_t>::value), "");
    102     static_assert((std::is_same<decltype(std::ldiv(0L,0L)), std::ldiv_t>::value), "");
    103     static_assert((std::is_same<decltype(std::lldiv(0LL,0LL)), std::lldiv_t>::value), "");
    104     wchar_t* pw = 0;
    105     const wchar_t* pwc = 0;
    106     char* pc = 0;
    107     static_assert((std::is_same<decltype(std::mblen("",0)), int>::value), "");
    108     static_assert((std::is_same<decltype(std::mbtowc(pw,"",0)), int>::value), "");
    109     static_assert((std::is_same<decltype(std::wctomb(pc,L' ')), int>::value), "");
    110     static_assert((std::is_same<decltype(std::mbstowcs(pw,"",0)), std::size_t>::value), "");
    111     static_assert((std::is_same<decltype(std::wcstombs(pc,pwc,0)), std::size_t>::value), "");
    112 }
    113