brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.2 KiB · 6623315 Raw
160 lines · cpp
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-exception6//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 is13// 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 abs24#error abs is defined25#endif26 27#ifdef labs28#error labs is defined29#endif30 31#ifdef llabs32#error llabs is defined33#endif34 35#ifdef div36#error div is defined37#endif38 39#ifdef ldiv40#error ldiv is defined41#endif42 43#ifdef lldiv44#error lldiv is defined45#endif46 47#ifndef EXIT_FAILURE48#error EXIT_FAILURE not defined49#endif50 51#ifndef EXIT_SUCCESS52#error EXIT_SUCCESS not defined53#endif54 55#ifndef MB_CUR_MAX56#error MB_CUR_MAX not defined57#endif58 59#ifndef NULL60#error NULL not defined61#endif62 63#ifndef RAND_MAX64#error RAND_MAX not defined65#endif66 67template <class T, class = decltype(::abs(std::declval<T>()))>68std::true_type has_abs_imp(int);69template <class T>70std::false_type has_abs_imp(...);71 72template <class T>73struct has_abs : decltype(has_abs_imp<T>(0)) {};74 75void test_abs() {76  TEST_DIAGNOSTIC_PUSH77  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_POP97 98  assert(abs(-1.) == 1);99}100 101int 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.cpp123 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_CHARACTERS146    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#endif155 156    test_abs();157 158    return 0;159}160