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