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