]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/input.output/iostream.format/output.streams/ostream.formatted/ostream.inserters.arithmetic/minus1.pass.cpp
Vendor import of libc++ trunk r300422:
[FreeBSD/FreeBSD.git] / test / std / input.output / iostream.format / output.streams / ostream.formatted / ostream.inserters.arithmetic / minus1.pass.cpp
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 <type_traits>
27 #include <cctype>
28 #include <cstdint>
29 #include <cassert>
30
31 template <typename T>
32 void test_octal(const char *expected)
33 {
34     std::stringstream ss;
35     ss << std::oct << static_cast<T>(-1);
36     assert(ss.str() == expected);
37 }
38
39 template <typename T>
40 void test_dec(const char *expected)
41 {
42     std::stringstream ss;
43     ss << std::dec << static_cast<T>(-1);
44     assert(ss.str() == expected);
45 }
46
47 template <typename T>
48 void test_hex(const char *expected)
49 {
50     std::stringstream ss;
51     ss << std::hex << static_cast<T>(-1);
52
53     std::string str = ss.str();
54     for (size_t i = 0; i < str.size(); ++i )
55         str[i] = static_cast<char>(std::toupper(str[i]));
56
57     assert(str == expected);
58 }
59
60 int main()
61 {
62
63     test_octal<uint16_t>(                "177777");
64     test_octal< int16_t>(                "177777");
65     test_octal<uint32_t>(           "37777777777");
66     test_octal< int32_t>(           "37777777777");
67     test_octal<uint64_t>("1777777777777777777777");
68     test_octal< int64_t>("1777777777777777777777");
69     test_octal<uint64_t>("1777777777777777777777");
70
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_64) {
75         test_octal< unsigned long>("1777777777777777777777");
76         test_octal<          long>("1777777777777777777777");
77     }
78     if (long_long_is_64) {
79         test_octal< unsigned long long>("1777777777777777777777");
80         test_octal<          long long>("1777777777777777777777");
81     }
82
83     test_dec<uint16_t>(               "65535");
84     test_dec< int16_t>(                  "-1");
85     test_dec<uint32_t>(          "4294967295");
86     test_dec< int32_t>(                  "-1");
87     test_dec<uint64_t>("18446744073709551615");
88     test_dec< int64_t>(                  "-1");
89     if (long_is_64) {
90         test_dec<unsigned long>("18446744073709551615");
91         test_dec<         long>(                  "-1");
92     }
93     if (long_long_is_64) {
94         test_dec<unsigned long long>("18446744073709551615");
95         test_dec<         long long>(                  "-1");
96     }
97
98     test_hex<uint16_t>(            "FFFF");
99     test_hex< int16_t>(            "FFFF");
100     test_hex<uint32_t>(        "FFFFFFFF");
101     test_hex< int32_t>(        "FFFFFFFF");
102     test_hex<uint64_t>("FFFFFFFFFFFFFFFF");
103     test_hex< int64_t>("FFFFFFFFFFFFFFFF");
104     if (long_is_64) {
105         test_hex<unsigned long>("FFFFFFFFFFFFFFFF");
106         test_hex<         long>("FFFFFFFFFFFFFFFF");
107     }
108     if (long_long_is_64) {
109         test_hex<unsigned long long>("FFFFFFFFFFFFFFFF");
110         test_hex<         long long>("FFFFFFFFFFFFFFFF");
111     }
112 }