minmax_showbase.pass.cpp (3522B)
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 // <ostream> 11 12 // template <class charT, class traits = char_traits<charT> > 13 // class basic_ostream; 14 15 // operator<<(short n); 16 // operator<<(unsigned short n); 17 // operator<<(int n); 18 // operator<<(unsigned int n); 19 // operator<<(long n); 20 // operator<<(unsigned long n); 21 // operator<<(long long n); 22 // operator<<(unsigned long long n); 23 24 // Testing to make sure that the max length values are correctly inserted when 25 // using std::showbase 26 27 // This test exposes a regression that was not fixed yet in the libc++ 28 // shipped with macOS 10.12, 10.13 and 10.14. See D32670 for details. 29 // XFAIL: with_system_cxx_lib=macosx10.14 30 // XFAIL: with_system_cxx_lib=macosx10.13 31 // XFAIL: with_system_cxx_lib=macosx10.12 32 33 #include <cassert> 34 #include <cstdint> 35 #include <ios> 36 #include <limits> 37 #include <sstream> 38 #include <type_traits> 39 40 template <typename T> 41 static void test(std::ios_base::fmtflags fmt, const char *expected) 42 { 43 std::stringstream ss; 44 ss.setf(fmt, std::ios_base::basefield); 45 ss << std::showbase << (std::is_signed<T>::value ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max()); 46 assert(ss.str() == expected); 47 } 48 49 int main() 50 { 51 const std::ios_base::fmtflags o = std::ios_base::oct; 52 const std::ios_base::fmtflags d = std::ios_base::dec; 53 const std::ios_base::fmtflags x = std::ios_base::hex; 54 55 test<short>(o, "0100000"); 56 test<short>(d, "-32768"); 57 test<short>(x, "0x8000"); 58 59 test<unsigned short>(o, "0177777"); 60 test<unsigned short>(d, "65535"); 61 test<unsigned short>(x, "0xffff"); 62 63 test<int>(o, "020000000000"); 64 test<int>(d, "-2147483648"); 65 test<int>(x, "0x80000000"); 66 67 test<unsigned int>(o, "037777777777"); 68 test<unsigned int>(d, "4294967295"); 69 test<unsigned int>(x, "0xffffffff"); 70 71 const bool long_is_32 = std::integral_constant<bool, sizeof(long) == sizeof(int32_t)>::value; // avoid compiler warnings 72 const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(int64_t)>::value; // avoid compiler warnings 73 const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(int64_t)>::value; // avoid compiler warnings 74 75 if (long_is_32) { 76 test<long>(o, "020000000000"); 77 test<long>(d, "-2147483648"); 78 test<long>(x, "0x80000000"); 79 80 test<unsigned long>(o, "037777777777"); 81 test<unsigned long>(d, "4294967295"); 82 test<unsigned long>(x, "0xffffffff"); 83 } else if (long_is_64) { 84 test<long>(o, "01000000000000000000000"); 85 test<long>(d, "-9223372036854775808"); 86 test<long>(x, "0x8000000000000000"); 87 88 test<unsigned long>(o, "01777777777777777777777"); 89 test<unsigned long>(d, "18446744073709551615"); 90 test<unsigned long>(x, "0xffffffffffffffff"); 91 } 92 if (long_long_is_64) { 93 test<long long>(o, "01000000000000000000000"); 94 test<long long>(d, "-9223372036854775808"); 95 test<long long>(x, "0x8000000000000000"); 96 97 test<unsigned long long>(o, "01777777777777777777777"); 98 test<unsigned long long>(d, "18446744073709551615"); 99 test<unsigned long long>(x, "0xffffffffffffffff"); 100 } 101 102 return 0; 103 }