Home | History | Annotate | Download | only in ostream.inserters.arithmetic
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <ostream>
     11 
     12 // template <class charT, class traits = char_traits<charT> >
     13 //   class basic_ostream;
     14 
     15 // operator<<(short n);
     16 // operator<<(unsigned short n);
     17 // operator<<(int n);
     18 // operator<<(unsigned int n);
     19 // operator<<(long n);
     20 // operator<<(unsigned long n);
     21 // operator<<(long long n);
     22 // operator<<(unsigned long long n);
     23 
     24 //  Testing to make sure that the max length values are correctly inserted when
     25 //  using std::showbase
     26 
     27 #include <cassert>
     28 #include <cstdint>
     29 #include <ios>
     30 #include <limits>
     31 #include <sstream>
     32 #include <type_traits>
     33 
     34 template <typename T>
     35 static void test(std::ios_base::fmtflags fmt, const char *expected)
     36 {
     37     std::stringstream ss;
     38     ss.setf(fmt, std::ios_base::basefield);
     39     ss << std::showbase << (std::is_signed<T>::value ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max());
     40     assert(ss.str() == expected);
     41 }
     42 
     43 int main(void)
     44 {
     45     const std::ios_base::fmtflags o = std::ios_base::oct;
     46     const std::ios_base::fmtflags d = std::ios_base::dec;
     47     const std::ios_base::fmtflags x = std::ios_base::hex;
     48 
     49     test<short>(o, "0100000");
     50     test<short>(d, "-32768");
     51     test<short>(x, "0x8000");
     52 
     53     test<unsigned short>(o, "0177777");
     54     test<unsigned short>(d, "65535");
     55     test<unsigned short>(x, "0xffff");
     56 
     57     test<int>(o, "020000000000");
     58     test<int>(d, "-2147483648");
     59     test<int>(x, "0x80000000");
     60 
     61     test<unsigned int>(o, "037777777777");
     62     test<unsigned int>(d, "4294967295");
     63     test<unsigned int>(x, "0xffffffff");
     64 
     65     const bool long_is_32 = std::integral_constant<bool, sizeof(long) == sizeof(int32_t)>::value; // avoid compiler warnings
     66     const bool long_is_64 = std::integral_constant<bool, sizeof(long) == sizeof(int64_t)>::value; // avoid compiler warnings
     67     const bool long_long_is_64 = std::integral_constant<bool, sizeof(long long) == sizeof(int64_t)>::value; // avoid compiler warnings
     68 
     69     if (long_is_32) {
     70         test<long>(o, "020000000000");
     71         test<long>(d, "-2147483648");
     72         test<long>(x, "0x80000000");
     73 
     74         test<unsigned long>(o, "037777777777");
     75         test<unsigned long>(d, "4294967295");
     76         test<unsigned long>(x, "0xffffffff");
     77     } else if (long_is_64) {
     78         test<long>(o, "01000000000000000000000");
     79         test<long>(d, "-9223372036854775808");
     80         test<long>(x, "0x8000000000000000");
     81 
     82         test<unsigned long>(o, "01777777777777777777777");
     83         test<unsigned long>(d, "18446744073709551615");
     84         test<unsigned long>(x, "0xffffffffffffffff");
     85     }
     86     if (long_long_is_64) {
     87         test<long long>(o, "01000000000000000000000");
     88         test<long long>(d, "-9223372036854775808");
     89         test<long long>(x, "0x8000000000000000");
     90 
     91         test<unsigned long long>(o, "01777777777777777777777");
     92         test<unsigned long long>(d, "18446744073709551615");
     93         test<unsigned long long>(x, "0xffffffffffffffff");
     94     }
     95 
     96     return 0;
     97 }
     98