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