minus1.pass.cpp (3594B)
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 // XFAIL: with_system_cxx_lib=macosx10.12 11 12 // <ostream> 13 14 // template <class charT, class traits = char_traits<charT> > 15 // class basic_ostream; 16 17 // operator<<( int16_t val); 18 // operator<<(uint16_t val); 19 // operator<<( int32_t val); 20 // operator<<(uint32_t val); 21 // operator<<( int64_t val); 22 // operator<<(uint64_t val); 23 24 // Testing to make sure that the max length values are correctly inserted 25 26 #include <sstream> 27 #include <ios> 28 #include <type_traits> 29 #include <cctype> 30 #include <cstdint> 31 #include <cassert> 32 33 template <typename T> 34 void test_octal(const char *expected) 35 { 36 std::stringstream ss; 37 ss << std::oct << static_cast<T>(-1); 38 assert(ss.str() == expected); 39 } 40 41 template <typename T> 42 void test_dec(const char *expected) 43 { 44 std::stringstream ss; 45 ss << std::dec << static_cast<T>(-1); 46 assert(ss.str() == expected); 47 } 48 49 template <typename T> 50 void test_hex(const char *expected) 51 { 52 std::stringstream ss; 53 ss << std::hex << static_cast<T>(-1); 54 55 std::string str = ss.str(); 56 for (size_t i = 0; i < str.size(); ++i ) 57 str[i] = static_cast<char>(std::toupper(str[i])); 58 59 assert(str == expected); 60 } 61 62 int main() 63 { 64 65 test_octal<uint16_t>( "177777"); 66 test_octal< int16_t>( "177777"); 67 test_octal<uint32_t>( "37777777777"); 68 test_octal< int32_t>( "37777777777"); 69 test_octal<uint64_t>("1777777777777777777777"); 70 test_octal< int64_t>("1777777777777777777777"); 71 test_octal<uint64_t>("1777777777777777777777"); 72 73 const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(int64_t)>::value; // avoid compiler warnings 74 const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(int64_t)>::value; // avoid compiler warnings 75 76 if (long_is_64) { 77 test_octal< unsigned long>("1777777777777777777777"); 78 test_octal< long>("1777777777777777777777"); 79 } 80 if (long_long_is_64) { 81 test_octal< unsigned long long>("1777777777777777777777"); 82 test_octal< long long>("1777777777777777777777"); 83 } 84 85 test_dec<uint16_t>( "65535"); 86 test_dec< int16_t>( "-1"); 87 test_dec<uint32_t>( "4294967295"); 88 test_dec< int32_t>( "-1"); 89 test_dec<uint64_t>("18446744073709551615"); 90 test_dec< int64_t>( "-1"); 91 if (long_is_64) { 92 test_dec<unsigned long>("18446744073709551615"); 93 test_dec< long>( "-1"); 94 } 95 if (long_long_is_64) { 96 test_dec<unsigned long long>("18446744073709551615"); 97 test_dec< long long>( "-1"); 98 } 99 100 test_hex<uint16_t>( "FFFF"); 101 test_hex< int16_t>( "FFFF"); 102 test_hex<uint32_t>( "FFFFFFFF"); 103 test_hex< int32_t>( "FFFFFFFF"); 104 test_hex<uint64_t>("FFFFFFFFFFFFFFFF"); 105 test_hex< int64_t>("FFFFFFFFFFFFFFFF"); 106 if (long_is_64) { 107 test_hex<unsigned long>("FFFFFFFFFFFFFFFF"); 108 test_hex< long>("FFFFFFFFFFFFFFFF"); 109 } 110 if (long_long_is_64) { 111 test_hex<unsigned long long>("FFFFFFFFFFFFFFFF"); 112 test_hex< long long>("FFFFFFFFFFFFFFFF"); 113 } 114 }