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: stdlib=system && target={{.+}}-apple-macosx10.{{(13|14)(.0)?}}
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
39template <typename T>
40static void test(std::ios_base::fmtflags fmt, const char *expected)
41{
42 std::stringstream ss;
43 ss.setf(fmtfl: fmt, mask: 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
48int 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>(fmt: o, expected: "0100000");
55 test<short>(fmt: d, expected: "-32768");
56 test<short>(fmt: x, expected: "0x8000");
57
58 test<unsigned short>(fmt: o, expected: "0177777");
59 test<unsigned short>(fmt: d, expected: "65535");
60 test<unsigned short>(fmt: x, expected: "0xffff");
61
62 test<int>(fmt: o, expected: "020000000000");
63 test<int>(fmt: d, expected: "-2147483648");
64 test<int>(fmt: x, expected: "0x80000000");
65
66 test<unsigned int>(fmt: o, expected: "037777777777");
67 test<unsigned int>(fmt: d, expected: "4294967295");
68 test<unsigned int>(fmt: x, expected: "0xffffffff");
69
70 const bool long_is_32 = std::integral_constant<bool, sizeof(long) == sizeof(std::int32_t)>::value; // avoid compiler warnings
71 const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(std::int64_t)>::value; // avoid compiler warnings
72 const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(std::int64_t)>::value; // avoid compiler warnings
73
74 if (long_is_32) {
75 test<long>(fmt: o, expected: "020000000000");
76 test<long>(fmt: d, expected: "-2147483648");
77 test<long>(fmt: x, expected: "0x80000000");
78
79 test<unsigned long>(fmt: o, expected: "037777777777");
80 test<unsigned long>(fmt: d, expected: "4294967295");
81 test<unsigned long>(fmt: x, expected: "0xffffffff");
82 } else if (long_is_64) {
83 test<long>(fmt: o, expected: "01000000000000000000000");
84 test<long>(fmt: d, expected: "-9223372036854775808");
85 test<long>(fmt: x, expected: "0x8000000000000000");
86
87 test<unsigned long>(fmt: o, expected: "01777777777777777777777");
88 test<unsigned long>(fmt: d, expected: "18446744073709551615");
89 test<unsigned long>(fmt: x, expected: "0xffffffffffffffff");
90 }
91 if (long_long_is_64) {
92 test<long long>(fmt: o, expected: "01000000000000000000000");
93 test<long long>(fmt: d, expected: "-9223372036854775808");
94 test<long long>(fmt: x, expected: "0x8000000000000000");
95
96 test<unsigned long long>(fmt: o, expected: "01777777777777777777777");
97 test<unsigned long long>(fmt: d, expected: "18446744073709551615");
98 test<unsigned long long>(fmt: x, expected: "0xffffffffffffffff");
99 }
100
101 return 0;
102}
103

source code of libcxx/test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minmax_showbase.pass.cpp