dim updated this revision to Diff 97198. dim added a comment. Simplify test case a bit.
https://reviews.llvm.org/D32670 Files: include/locale test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minmax_showbase.pass.cpp
Index: test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minmax_showbase.pass.cpp =================================================================== --- /dev/null +++ test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minmax_showbase.pass.cpp @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <ostream> + +// template <class charT, class traits = char_traits<charT> > +// class basic_ostream; + +// operator<<(short n); +// operator<<(unsigned short n); +// operator<<(int n); +// operator<<(unsigned int n); +// operator<<(long n); +// operator<<(unsigned long n); +// operator<<(long long n); +// operator<<(unsigned long long n); + +// Testing to make sure that the max length values are correctly inserted when +// using std::showbase + +#include <cassert> +#include <cstdint> +#include <ios> +#include <limits> +#include <sstream> +#include <type_traits> + +template <typename T> +static void test(std::ios_base::fmtflags fmt, const char *expected) +{ + std::stringstream ss; + ss.setf(fmt, std::ios_base::basefield); + ss << std::showbase << (std::is_signed<T>::value ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max()); + assert(ss.str() == expected); +} + +int main(void) +{ + const std::ios_base::fmtflags o = std::ios_base::oct; + const std::ios_base::fmtflags d = std::ios_base::dec; + const std::ios_base::fmtflags x = std::ios_base::hex; + + test<short>(o, "0100000"); + test<short>(d, "-32768"); + test<short>(x, "0x8000"); + + test<unsigned short>(o, "0177777"); + test<unsigned short>(d, "65535"); + test<unsigned short>(x, "0xffff"); + + test<int>(o, "020000000000"); + test<int>(d, "-2147483648"); + test<int>(x, "0x80000000"); + + test<unsigned int>(o, "037777777777"); + test<unsigned int>(d, "4294967295"); + test<unsigned int>(x, "0xffffffff"); + + const bool long_is_32 = std::integral_constant<bool, sizeof(long) == sizeof(int32_t)>::value; // avoid compiler warnings + const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(int64_t)>::value; // avoid compiler warnings + const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(int64_t)>::value; // avoid compiler warnings + + if (long_is_32) { + test<long>(o, "020000000000"); + test<long>(d, "-2147483648"); + test<long>(x, "0x80000000"); + + test<unsigned long>(o, "037777777777"); + test<unsigned long>(d, "4294967295"); + test<unsigned long>(x, "0xffffffff"); + } else if (long_is_64) { + test<long>(o, "01000000000000000000000"); + test<long>(d, "-9223372036854775808"); + test<long>(x, "0x8000000000000000"); + + test<unsigned long>(o, "01777777777777777777777"); + test<unsigned long>(d, "18446744073709551615"); + test<unsigned long>(x, "0xffffffffffffffff"); + } + if (long_long_is_64) { + test<long long>(o, "01000000000000000000000"); + test<long long>(d, "-9223372036854775808"); + test<long long>(x, "0x8000000000000000"); + + test<unsigned long long>(o, "01777777777777777777777"); + test<unsigned long long>(d, "18446744073709551615"); + test<unsigned long long>(x, "0xffffffffffffffff"); + } + + return 0; +} Index: include/locale =================================================================== --- include/locale +++ include/locale @@ -1402,6 +1402,7 @@ this->__format_int(__fmt+1, __len, true, __iob.flags()); const unsigned __nbuf = (numeric_limits<long>::digits / 3) + ((numeric_limits<long>::digits % 3) != 0) + + ((__iob.flags() & ios_base::showbase) != 0) + 2; char __nar[__nbuf]; int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v); @@ -1428,6 +1429,7 @@ this->__format_int(__fmt+1, __len, true, __iob.flags()); const unsigned __nbuf = (numeric_limits<long long>::digits / 3) + ((numeric_limits<long long>::digits % 3) != 0) + + ((__iob.flags() & ios_base::showbase) != 0) + 2; char __nar[__nbuf]; int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v); @@ -1454,6 +1456,7 @@ this->__format_int(__fmt+1, __len, false, __iob.flags()); const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3) + ((numeric_limits<unsigned long>::digits % 3) != 0) + + ((__iob.flags() & ios_base::showbase) != 0) + 1; char __nar[__nbuf]; int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v); @@ -1480,6 +1483,7 @@ this->__format_int(__fmt+1, __len, false, __iob.flags()); const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3) + ((numeric_limits<unsigned long long>::digits % 3) != 0) + + ((__iob.flags() & ios_base::showbase) != 0) + 1; char __nar[__nbuf]; int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits