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